blob: 6ca0d9cc46f93734aa034e0349c1f714f8c9df97 [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)) {
511 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000512 if (!name)
513 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400514 rcu_string_free(device->name);
515 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500516 if (device->missing) {
517 fs_devices->missing_devices--;
518 device->missing = 0;
519 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400520 }
521
522 if (found_transid > fs_devices->latest_trans) {
523 fs_devices->latest_devid = devid;
524 fs_devices->latest_trans = found_transid;
525 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400526 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100527
528 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400529}
530
Yan Zhenge4404d62008-12-12 10:03:26 -0500531static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
532{
533 struct btrfs_fs_devices *fs_devices;
534 struct btrfs_device *device;
535 struct btrfs_device *orig_dev;
536
Ilya Dryomov2208a372013-08-12 14:33:03 +0300537 fs_devices = alloc_fs_devices(orig->fsid);
538 if (IS_ERR(fs_devices))
539 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500540
Yan Zhenge4404d62008-12-12 10:03:26 -0500541 fs_devices->latest_devid = orig->latest_devid;
542 fs_devices->latest_trans = orig->latest_trans;
Josef Bacik02db0842012-06-21 16:03:58 -0400543 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500544
Xiao Guangrong46224702011-04-20 10:08:47 +0000545 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500546 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400547 struct rcu_string *name;
548
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300549 device = btrfs_alloc_device(NULL, &orig_dev->devid,
550 orig_dev->uuid);
551 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500552 goto error;
553
Josef Bacik606686e2012-06-04 14:03:51 -0400554 /*
555 * This is ok to do without rcu read locked because we hold the
556 * uuid mutex so nothing we touch in here is going to disappear.
557 */
558 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
559 if (!name) {
Julia Lawallfd2696f2009-09-29 13:51:04 -0400560 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500561 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400562 }
Josef Bacik606686e2012-06-04 14:03:51 -0400563 rcu_assign_pointer(device->name, name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500564
Yan Zhenge4404d62008-12-12 10:03:26 -0500565 list_add(&device->dev_list, &fs_devices->devices);
566 device->fs_devices = fs_devices;
567 fs_devices->num_devices++;
568 }
569 return fs_devices;
570error:
571 free_fs_devices(fs_devices);
572 return ERR_PTR(-ENOMEM);
573}
574
Stefan Behrens8dabb742012-11-06 13:15:27 +0100575void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
576 struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400577{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500578 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400579
Chris Masona6b0d5c2012-02-20 20:53:43 -0500580 struct block_device *latest_bdev = NULL;
581 u64 latest_devid = 0;
582 u64 latest_transid = 0;
583
Chris Masondfe25022008-05-13 13:46:40 -0400584 mutex_lock(&uuid_mutex);
585again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000586 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500587 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500588 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100589 if (!device->is_tgtdev_for_dev_replace &&
590 (!latest_transid ||
591 device->generation > latest_transid)) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500592 latest_devid = device->devid;
593 latest_transid = device->generation;
594 latest_bdev = device->bdev;
595 }
Yan Zheng2b820322008-11-17 21:11:30 -0500596 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500597 }
Yan Zheng2b820322008-11-17 21:11:30 -0500598
Stefan Behrens8dabb742012-11-06 13:15:27 +0100599 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
600 /*
601 * In the first step, keep the device which has
602 * the correct fsid and the devid that is used
603 * for the dev_replace procedure.
604 * In the second step, the dev_replace state is
605 * read from the device tree and it is known
606 * whether the procedure is really active or
607 * not, which means whether this device is
608 * used or whether it should be removed.
609 */
610 if (step == 0 || device->is_tgtdev_for_dev_replace) {
611 continue;
612 }
613 }
Yan Zheng2b820322008-11-17 21:11:30 -0500614 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100615 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500616 device->bdev = NULL;
617 fs_devices->open_devices--;
618 }
619 if (device->writeable) {
620 list_del_init(&device->dev_alloc_list);
621 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100622 if (!device->is_tgtdev_for_dev_replace)
623 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500624 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500625 list_del_init(&device->dev_list);
626 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400627 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500628 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400629 }
Yan Zheng2b820322008-11-17 21:11:30 -0500630
631 if (fs_devices->seed) {
632 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500633 goto again;
634 }
635
Chris Masona6b0d5c2012-02-20 20:53:43 -0500636 fs_devices->latest_bdev = latest_bdev;
637 fs_devices->latest_devid = latest_devid;
638 fs_devices->latest_trans = latest_transid;
639
Chris Masondfe25022008-05-13 13:46:40 -0400640 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400641}
Chris Masona0af4692008-05-13 16:03:06 -0400642
Xiao Guangrong1f781602011-04-20 10:09:16 +0000643static void __free_device(struct work_struct *work)
644{
645 struct btrfs_device *device;
646
647 device = container_of(work, struct btrfs_device, rcu_work);
648
649 if (device->bdev)
650 blkdev_put(device->bdev, device->mode);
651
Josef Bacik606686e2012-06-04 14:03:51 -0400652 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000653 kfree(device);
654}
655
656static void free_device(struct rcu_head *head)
657{
658 struct btrfs_device *device;
659
660 device = container_of(head, struct btrfs_device, rcu);
661
662 INIT_WORK(&device->rcu_work, __free_device);
663 schedule_work(&device->rcu_work);
664}
665
Yan Zheng2b820322008-11-17 21:11:30 -0500666static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400667{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400668 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500669
Yan Zheng2b820322008-11-17 21:11:30 -0500670 if (--fs_devices->opened > 0)
671 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400672
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000673 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500674 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000675 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400676 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000677
678 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400679 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000680
Ilya Dryomovf747cab2013-10-10 20:37:29 +0300681 if (device->writeable &&
682 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500683 list_del_init(&device->dev_alloc_list);
684 fs_devices->rw_devices--;
685 }
686
Josef Bacikd5e20032011-08-04 14:52:27 +0000687 if (device->can_discard)
688 fs_devices->num_can_discard--;
Josef Bacik726551e2013-08-26 13:45:53 -0400689 if (device->missing)
690 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000691
Ilya Dryomova1e87802013-08-12 14:33:04 +0300692 new_device = btrfs_alloc_device(NULL, &device->devid,
693 device->uuid);
694 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400695
696 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400697 if (device->name) {
698 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300699 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400700 rcu_assign_pointer(new_device->name, name);
701 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300702
Xiao Guangrong1f781602011-04-20 10:09:16 +0000703 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300704 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000705
706 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400707 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000708 mutex_unlock(&fs_devices->device_list_mutex);
709
Yan Zhenge4404d62008-12-12 10:03:26 -0500710 WARN_ON(fs_devices->open_devices);
711 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500712 fs_devices->opened = 0;
713 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500714
Chris Mason8a4b83c2008-03-24 15:02:07 -0400715 return 0;
716}
717
Yan Zheng2b820322008-11-17 21:11:30 -0500718int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
719{
Yan Zhenge4404d62008-12-12 10:03:26 -0500720 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500721 int ret;
722
723 mutex_lock(&uuid_mutex);
724 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500725 if (!fs_devices->opened) {
726 seed_devices = fs_devices->seed;
727 fs_devices->seed = NULL;
728 }
Yan Zheng2b820322008-11-17 21:11:30 -0500729 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500730
731 while (seed_devices) {
732 fs_devices = seed_devices;
733 seed_devices = fs_devices->seed;
734 __btrfs_close_devices(fs_devices);
735 free_fs_devices(fs_devices);
736 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000737 /*
738 * Wait for rcu kworkers under __btrfs_close_devices
739 * to finish all blkdev_puts so device is really
740 * free when umount is done.
741 */
742 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500743 return ret;
744}
745
Yan Zhenge4404d62008-12-12 10:03:26 -0500746static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
747 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400748{
Josef Bacikd5e20032011-08-04 14:52:27 +0000749 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400750 struct block_device *bdev;
751 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400752 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400753 struct block_device *latest_bdev = NULL;
754 struct buffer_head *bh;
755 struct btrfs_super_block *disk_super;
756 u64 latest_devid = 0;
757 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400758 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500759 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400760 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400761
Tejun Heod4d77622010-11-13 11:55:18 +0100762 flags |= FMODE_EXCL;
763
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500764 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400765 if (device->bdev)
766 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400767 if (!device->name)
768 continue;
769
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000770 /* Just open everything we can; ignore failures here */
771 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
772 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100773 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400774
775 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000776 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400777 if (devid != device->devid)
778 goto error_brelse;
779
Yan Zheng2b820322008-11-17 21:11:30 -0500780 if (memcmp(device->uuid, disk_super->dev_item.uuid,
781 BTRFS_UUID_SIZE))
782 goto error_brelse;
783
784 device->generation = btrfs_super_generation(disk_super);
785 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400786 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500787 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400788 latest_bdev = bdev;
789 }
790
Yan Zheng2b820322008-11-17 21:11:30 -0500791 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
792 device->writeable = 0;
793 } else {
794 device->writeable = !bdev_read_only(bdev);
795 seeding = 0;
796 }
797
Josef Bacikd5e20032011-08-04 14:52:27 +0000798 q = bdev_get_queue(bdev);
799 if (blk_queue_discard(q)) {
800 device->can_discard = 1;
801 fs_devices->num_can_discard++;
802 }
803
Chris Mason8a4b83c2008-03-24 15:02:07 -0400804 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400805 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500806 device->mode = flags;
807
Chris Masonc2898112009-06-10 09:51:32 -0400808 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
809 fs_devices->rotating = 1;
810
Chris Masona0af4692008-05-13 16:03:06 -0400811 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300812 if (device->writeable &&
813 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500814 fs_devices->rw_devices++;
815 list_add(&device->dev_alloc_list,
816 &fs_devices->alloc_list);
817 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000818 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400819 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400820
Chris Masona0af4692008-05-13 16:03:06 -0400821error_brelse:
822 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100823 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400824 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400825 }
Chris Masona0af4692008-05-13 16:03:06 -0400826 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300827 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400828 goto out;
829 }
Yan Zheng2b820322008-11-17 21:11:30 -0500830 fs_devices->seeding = seeding;
831 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400832 fs_devices->latest_bdev = latest_bdev;
833 fs_devices->latest_devid = latest_devid;
834 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500835 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400836out:
Yan Zheng2b820322008-11-17 21:11:30 -0500837 return ret;
838}
839
840int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500841 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500842{
843 int ret;
844
845 mutex_lock(&uuid_mutex);
846 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500847 fs_devices->opened++;
848 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500849 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500850 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500851 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400852 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400853 return ret;
854}
855
David Sterba6f60cbd2013-02-15 11:31:02 -0700856/*
857 * Look for a btrfs signature on a device. This may be called out of the mount path
858 * and we are not allowed to call set_blocksize during the scan. The superblock
859 * is read via pagecache
860 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500861int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400862 struct btrfs_fs_devices **fs_devices_ret)
863{
864 struct btrfs_super_block *disk_super;
865 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700866 struct page *page;
867 void *p;
868 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400869 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400870 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400871 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700872 u64 bytenr;
873 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400874
David Sterba6f60cbd2013-02-15 11:31:02 -0700875 /*
876 * we would like to check all the supers, but that would make
877 * a btrfs mount succeed after a mkfs from a different FS.
878 * So, we need to add a special mount option to scan for
879 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
880 */
881 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100882 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500883 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700884
885 bdev = blkdev_get_by_path(path, flags, holder);
886
887 if (IS_ERR(bdev)) {
888 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100889 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700890 }
891
892 /* make sure our super fits in the device */
893 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
894 goto error_bdev_put;
895
896 /* make sure our super fits in the page */
897 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
898 goto error_bdev_put;
899
900 /* make sure our super doesn't straddle pages on disk */
901 index = bytenr >> PAGE_CACHE_SHIFT;
902 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
903 goto error_bdev_put;
904
905 /* pull in the page with our super */
906 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
907 index, GFP_NOFS);
908
909 if (IS_ERR_OR_NULL(page))
910 goto error_bdev_put;
911
912 p = kmap(page);
913
914 /* align our pointer to the offset of the super block */
915 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
916
917 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +0800918 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -0700919 goto error_unmap;
920
Xiao Guangronga3438322010-01-06 11:48:18 +0000921 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400922 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400923 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700924
Chris Mason8a4b83c2008-03-24 15:02:07 -0400925 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +0100926 if (ret > 0) {
927 if (disk_super->label[0]) {
928 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
929 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
930 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
931 } else {
932 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
933 }
934
935 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
936 ret = 0;
937 }
Josef Bacik02db0842012-06-21 16:03:58 -0400938 if (!ret && fs_devices_ret)
939 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700940
941error_unmap:
942 kunmap(page);
943 page_cache_release(page);
944
945error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100946 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400947error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100948 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400949 return ret;
950}
Chris Mason0b86a832008-03-24 15:01:56 -0400951
Miao Xie6d07bce2011-01-05 10:07:31 +0000952/* helper to account the used device space in the range */
953int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
954 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400955{
956 struct btrfs_key key;
957 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000958 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500959 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000960 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400961 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000962 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400963 struct extent_buffer *l;
964
Miao Xie6d07bce2011-01-05 10:07:31 +0000965 *length = 0;
966
Stefan Behrens63a212a2012-11-05 18:29:28 +0100967 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000968 return 0;
969
Yan Zheng2b820322008-11-17 21:11:30 -0500970 path = btrfs_alloc_path();
971 if (!path)
972 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400973 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400974
Chris Mason0b86a832008-03-24 15:01:56 -0400975 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000976 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400977 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000978
979 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400980 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000981 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400982 if (ret > 0) {
983 ret = btrfs_previous_item(root, path, key.objectid, key.type);
984 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000985 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400986 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000987
Chris Mason0b86a832008-03-24 15:01:56 -0400988 while (1) {
989 l = path->nodes[0];
990 slot = path->slots[0];
991 if (slot >= btrfs_header_nritems(l)) {
992 ret = btrfs_next_leaf(root, path);
993 if (ret == 0)
994 continue;
995 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000996 goto out;
997
998 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400999 }
1000 btrfs_item_key_to_cpu(l, &key, slot);
1001
1002 if (key.objectid < device->devid)
1003 goto next;
1004
1005 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001006 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001007
Chris Masond3977122009-01-05 21:25:51 -05001008 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001009 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001010
Chris Mason0b86a832008-03-24 15:01:56 -04001011 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001012 extent_end = key.offset + btrfs_dev_extent_length(l,
1013 dev_extent);
1014 if (key.offset <= start && extent_end > end) {
1015 *length = end - start + 1;
1016 break;
1017 } else if (key.offset <= start && extent_end > start)
1018 *length += extent_end - start;
1019 else if (key.offset > start && extent_end <= end)
1020 *length += extent_end - key.offset;
1021 else if (key.offset > start && key.offset <= end) {
1022 *length += end - key.offset + 1;
1023 break;
1024 } else if (key.offset > end)
1025 break;
1026
1027next:
1028 path->slots[0]++;
1029 }
1030 ret = 0;
1031out:
1032 btrfs_free_path(path);
1033 return ret;
1034}
1035
Josef Bacik6df9a952013-06-27 13:22:46 -04001036static int contains_pending_extent(struct btrfs_trans_handle *trans,
1037 struct btrfs_device *device,
1038 u64 *start, u64 len)
1039{
1040 struct extent_map *em;
1041 int ret = 0;
1042
1043 list_for_each_entry(em, &trans->transaction->pending_chunks, list) {
1044 struct map_lookup *map;
1045 int i;
1046
1047 map = (struct map_lookup *)em->bdev;
1048 for (i = 0; i < map->num_stripes; i++) {
1049 if (map->stripes[i].dev != device)
1050 continue;
1051 if (map->stripes[i].physical >= *start + len ||
1052 map->stripes[i].physical + em->orig_block_len <=
1053 *start)
1054 continue;
1055 *start = map->stripes[i].physical +
1056 em->orig_block_len;
1057 ret = 1;
1058 }
1059 }
1060
1061 return ret;
1062}
1063
1064
Chris Mason0b86a832008-03-24 15:01:56 -04001065/*
Miao Xie7bfc8372011-01-05 10:07:26 +00001066 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +00001067 * @device: the device which we search the free space in
1068 * @num_bytes: the size of the free space that we need
1069 * @start: store the start of the free space.
1070 * @len: the size of the free space. that we find, or the size of the max
1071 * free space if we don't find suitable free space
1072 *
Chris Mason0b86a832008-03-24 15:01:56 -04001073 * this uses a pretty simple search, the expectation is that it is
1074 * called very infrequently and that a given device has a small number
1075 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001076 *
1077 * @start is used to store the start of the free space if we find. But if we
1078 * don't find suitable free space, it will be used to store the start position
1079 * of the max free space.
1080 *
1081 * @len is used to store the size of the free space that we find.
1082 * But if we don't find suitable free space, it is used to store the size of
1083 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001084 */
Josef Bacik6df9a952013-06-27 13:22:46 -04001085int find_free_dev_extent(struct btrfs_trans_handle *trans,
1086 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001087 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001088{
1089 struct btrfs_key key;
1090 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001091 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001092 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001093 u64 hole_size;
1094 u64 max_hole_start;
1095 u64 max_hole_size;
1096 u64 extent_end;
1097 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001098 u64 search_end = device->total_bytes;
1099 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001100 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001101 struct extent_buffer *l;
1102
Chris Mason0b86a832008-03-24 15:01:56 -04001103 /* FIXME use last free of some kind */
1104
1105 /* we don't want to overwrite the superblock on the drive,
1106 * so we make sure to start at an offset of at least 1MB
1107 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001108 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001109
Josef Bacik6df9a952013-06-27 13:22:46 -04001110 path = btrfs_alloc_path();
1111 if (!path)
1112 return -ENOMEM;
1113again:
Miao Xie7bfc8372011-01-05 10:07:26 +00001114 max_hole_start = search_start;
1115 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +00001116 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +00001117
Stefan Behrens63a212a2012-11-05 18:29:28 +01001118 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001119 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001120 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001121 }
1122
Miao Xie7bfc8372011-01-05 10:07:26 +00001123 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001124 path->search_commit_root = 1;
1125 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001126
Chris Mason0b86a832008-03-24 15:01:56 -04001127 key.objectid = device->devid;
1128 key.offset = search_start;
1129 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001130
Li Zefan125ccb02011-12-08 15:07:24 +08001131 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001132 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001133 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001134 if (ret > 0) {
1135 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1136 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001137 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001138 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001139
Chris Mason0b86a832008-03-24 15:01:56 -04001140 while (1) {
1141 l = path->nodes[0];
1142 slot = path->slots[0];
1143 if (slot >= btrfs_header_nritems(l)) {
1144 ret = btrfs_next_leaf(root, path);
1145 if (ret == 0)
1146 continue;
1147 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001148 goto out;
1149
1150 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001151 }
1152 btrfs_item_key_to_cpu(l, &key, slot);
1153
1154 if (key.objectid < device->devid)
1155 goto next;
1156
1157 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001158 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001159
Chris Mason0b86a832008-03-24 15:01:56 -04001160 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
1161 goto next;
1162
Miao Xie7bfc8372011-01-05 10:07:26 +00001163 if (key.offset > search_start) {
1164 hole_size = key.offset - search_start;
1165
Josef Bacik6df9a952013-06-27 13:22:46 -04001166 /*
1167 * Have to check before we set max_hole_start, otherwise
1168 * we could end up sending back this offset anyway.
1169 */
1170 if (contains_pending_extent(trans, device,
1171 &search_start,
1172 hole_size))
1173 hole_size = 0;
1174
Miao Xie7bfc8372011-01-05 10:07:26 +00001175 if (hole_size > max_hole_size) {
1176 max_hole_start = search_start;
1177 max_hole_size = hole_size;
1178 }
1179
1180 /*
1181 * If this free space is greater than which we need,
1182 * it must be the max free space that we have found
1183 * until now, so max_hole_start must point to the start
1184 * of this free space and the length of this free space
1185 * is stored in max_hole_size. Thus, we return
1186 * max_hole_start and max_hole_size and go back to the
1187 * caller.
1188 */
1189 if (hole_size >= num_bytes) {
1190 ret = 0;
1191 goto out;
1192 }
1193 }
1194
Chris Mason0b86a832008-03-24 15:01:56 -04001195 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001196 extent_end = key.offset + btrfs_dev_extent_length(l,
1197 dev_extent);
1198 if (extent_end > search_start)
1199 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001200next:
1201 path->slots[0]++;
1202 cond_resched();
1203 }
Chris Mason0b86a832008-03-24 15:01:56 -04001204
liubo38c01b92011-08-02 02:39:03 +00001205 /*
1206 * At this point, search_start should be the end of
1207 * allocated dev extents, and when shrinking the device,
1208 * search_end may be smaller than search_start.
1209 */
1210 if (search_end > search_start)
1211 hole_size = search_end - search_start;
1212
Miao Xie7bfc8372011-01-05 10:07:26 +00001213 if (hole_size > max_hole_size) {
1214 max_hole_start = search_start;
1215 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001216 }
Chris Mason0b86a832008-03-24 15:01:56 -04001217
Josef Bacik6df9a952013-06-27 13:22:46 -04001218 if (contains_pending_extent(trans, device, &search_start, hole_size)) {
1219 btrfs_release_path(path);
1220 goto again;
1221 }
1222
Miao Xie7bfc8372011-01-05 10:07:26 +00001223 /* See above. */
1224 if (hole_size < num_bytes)
1225 ret = -ENOSPC;
1226 else
1227 ret = 0;
1228
1229out:
Yan Zheng2b820322008-11-17 21:11:30 -05001230 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001231 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001232 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001233 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001234 return ret;
1235}
1236
Christoph Hellwigb2950862008-12-02 09:54:17 -05001237static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001238 struct btrfs_device *device,
1239 u64 start)
1240{
1241 int ret;
1242 struct btrfs_path *path;
1243 struct btrfs_root *root = device->dev_root;
1244 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001245 struct btrfs_key found_key;
1246 struct extent_buffer *leaf = NULL;
1247 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001248
1249 path = btrfs_alloc_path();
1250 if (!path)
1251 return -ENOMEM;
1252
1253 key.objectid = device->devid;
1254 key.offset = start;
1255 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001256again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001257 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001258 if (ret > 0) {
1259 ret = btrfs_previous_item(root, path, key.objectid,
1260 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001261 if (ret)
1262 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001263 leaf = path->nodes[0];
1264 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1265 extent = btrfs_item_ptr(leaf, path->slots[0],
1266 struct btrfs_dev_extent);
1267 BUG_ON(found_key.offset > start || found_key.offset +
1268 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001269 key = found_key;
1270 btrfs_release_path(path);
1271 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001272 } else if (ret == 0) {
1273 leaf = path->nodes[0];
1274 extent = btrfs_item_ptr(leaf, path->slots[0],
1275 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001276 } else {
1277 btrfs_error(root->fs_info, ret, "Slot search failed");
1278 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001279 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001280
Josef Bacik2bf64752011-09-26 17:12:22 -04001281 if (device->bytes_used > 0) {
1282 u64 len = btrfs_dev_extent_length(leaf, extent);
1283 device->bytes_used -= len;
1284 spin_lock(&root->fs_info->free_chunk_lock);
1285 root->fs_info->free_chunk_space += len;
1286 spin_unlock(&root->fs_info->free_chunk_lock);
1287 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001288 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001289 if (ret) {
1290 btrfs_error(root->fs_info, ret,
1291 "Failed to remove dev extent item");
1292 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001293out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001294 btrfs_free_path(path);
1295 return ret;
1296}
1297
Eric Sandeen48a3b632013-04-25 20:41:01 +00001298static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1299 struct btrfs_device *device,
1300 u64 chunk_tree, u64 chunk_objectid,
1301 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001302{
1303 int ret;
1304 struct btrfs_path *path;
1305 struct btrfs_root *root = device->dev_root;
1306 struct btrfs_dev_extent *extent;
1307 struct extent_buffer *leaf;
1308 struct btrfs_key key;
1309
Chris Masondfe25022008-05-13 13:46:40 -04001310 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001311 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001312 path = btrfs_alloc_path();
1313 if (!path)
1314 return -ENOMEM;
1315
Chris Mason0b86a832008-03-24 15:01:56 -04001316 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001317 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001318 key.type = BTRFS_DEV_EXTENT_KEY;
1319 ret = btrfs_insert_empty_item(trans, root, path, &key,
1320 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001321 if (ret)
1322 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001323
1324 leaf = path->nodes[0];
1325 extent = btrfs_item_ptr(leaf, path->slots[0],
1326 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001327 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1328 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1329 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1330
1331 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001332 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001333
Chris Mason0b86a832008-03-24 15:01:56 -04001334 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1335 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001336out:
Chris Mason0b86a832008-03-24 15:01:56 -04001337 btrfs_free_path(path);
1338 return ret;
1339}
1340
Josef Bacik6df9a952013-06-27 13:22:46 -04001341static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001342{
Josef Bacik6df9a952013-06-27 13:22:46 -04001343 struct extent_map_tree *em_tree;
1344 struct extent_map *em;
1345 struct rb_node *n;
1346 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001347
Josef Bacik6df9a952013-06-27 13:22:46 -04001348 em_tree = &fs_info->mapping_tree.map_tree;
1349 read_lock(&em_tree->lock);
1350 n = rb_last(&em_tree->map);
1351 if (n) {
1352 em = rb_entry(n, struct extent_map, rb_node);
1353 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001354 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001355 read_unlock(&em_tree->lock);
1356
Chris Mason0b86a832008-03-24 15:01:56 -04001357 return ret;
1358}
1359
Ilya Dryomov53f10652013-08-12 14:33:01 +03001360static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1361 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001362{
1363 int ret;
1364 struct btrfs_key key;
1365 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001366 struct btrfs_path *path;
1367
Yan Zheng2b820322008-11-17 21:11:30 -05001368 path = btrfs_alloc_path();
1369 if (!path)
1370 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001371
1372 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1373 key.type = BTRFS_DEV_ITEM_KEY;
1374 key.offset = (u64)-1;
1375
Ilya Dryomov53f10652013-08-12 14:33:01 +03001376 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001377 if (ret < 0)
1378 goto error;
1379
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001380 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001381
Ilya Dryomov53f10652013-08-12 14:33:01 +03001382 ret = btrfs_previous_item(fs_info->chunk_root, path,
1383 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001384 BTRFS_DEV_ITEM_KEY);
1385 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001386 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001387 } else {
1388 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1389 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001390 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001391 }
1392 ret = 0;
1393error:
Yan Zheng2b820322008-11-17 21:11:30 -05001394 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001395 return ret;
1396}
1397
1398/*
1399 * the device information is stored in the chunk root
1400 * the btrfs_device struct should be fully filled in
1401 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001402static int btrfs_add_device(struct btrfs_trans_handle *trans,
1403 struct btrfs_root *root,
1404 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001405{
1406 int ret;
1407 struct btrfs_path *path;
1408 struct btrfs_dev_item *dev_item;
1409 struct extent_buffer *leaf;
1410 struct btrfs_key key;
1411 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001412
1413 root = root->fs_info->chunk_root;
1414
1415 path = btrfs_alloc_path();
1416 if (!path)
1417 return -ENOMEM;
1418
Chris Mason0b86a832008-03-24 15:01:56 -04001419 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1420 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001421 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001422
1423 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001424 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001425 if (ret)
1426 goto out;
1427
1428 leaf = path->nodes[0];
1429 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1430
1431 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001432 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001433 btrfs_set_device_type(leaf, dev_item, device->type);
1434 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1435 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1436 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001437 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1438 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001439 btrfs_set_device_group(leaf, dev_item, 0);
1440 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1441 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001442 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001443
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001444 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001445 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001446 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001447 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001448 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001449
Yan Zheng2b820322008-11-17 21:11:30 -05001450 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001451out:
1452 btrfs_free_path(path);
1453 return ret;
1454}
Chris Mason8f18cf12008-04-25 16:53:30 -04001455
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001456/*
1457 * Function to update ctime/mtime for a given device path.
1458 * Mainly used for ctime/mtime based probe like libblkid.
1459 */
1460static void update_dev_time(char *path_name)
1461{
1462 struct file *filp;
1463
1464 filp = filp_open(path_name, O_RDWR, 0);
1465 if (!filp)
1466 return;
1467 file_update_time(filp);
1468 filp_close(filp, NULL);
1469 return;
1470}
1471
Chris Masona061fc82008-05-07 11:43:44 -04001472static int btrfs_rm_dev_item(struct btrfs_root *root,
1473 struct btrfs_device *device)
1474{
1475 int ret;
1476 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001477 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001478 struct btrfs_trans_handle *trans;
1479
1480 root = root->fs_info->chunk_root;
1481
1482 path = btrfs_alloc_path();
1483 if (!path)
1484 return -ENOMEM;
1485
Yan, Zhenga22285a2010-05-16 10:48:46 -04001486 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001487 if (IS_ERR(trans)) {
1488 btrfs_free_path(path);
1489 return PTR_ERR(trans);
1490 }
Chris Masona061fc82008-05-07 11:43:44 -04001491 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1492 key.type = BTRFS_DEV_ITEM_KEY;
1493 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001494 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001495
1496 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1497 if (ret < 0)
1498 goto out;
1499
1500 if (ret > 0) {
1501 ret = -ENOENT;
1502 goto out;
1503 }
1504
1505 ret = btrfs_del_item(trans, root, path);
1506 if (ret)
1507 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001508out:
1509 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001510 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001511 btrfs_commit_transaction(trans, root);
1512 return ret;
1513}
1514
1515int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1516{
1517 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001518 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001519 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001520 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001521 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001522 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001523 u64 all_avail;
1524 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001525 u64 num_devices;
1526 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001527 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001528 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001529 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001530
Chris Masona061fc82008-05-07 11:43:44 -04001531 mutex_lock(&uuid_mutex);
1532
Miao Xiede98ced2013-01-29 10:13:12 +00001533 do {
1534 seq = read_seqbegin(&root->fs_info->profiles_lock);
1535
1536 all_avail = root->fs_info->avail_data_alloc_bits |
1537 root->fs_info->avail_system_alloc_bits |
1538 root->fs_info->avail_metadata_alloc_bits;
1539 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001540
Stefan Behrens8dabb742012-11-06 13:15:27 +01001541 num_devices = root->fs_info->fs_devices->num_devices;
1542 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1543 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1544 WARN_ON(num_devices < 1);
1545 num_devices--;
1546 }
1547 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1548
1549 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001550 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001551 goto out;
1552 }
1553
Stefan Behrens8dabb742012-11-06 13:15:27 +01001554 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001555 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001556 goto out;
1557 }
1558
David Woodhouse53b381b2013-01-29 18:40:14 -05001559 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1560 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001561 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001562 goto out;
1563 }
1564 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1565 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001566 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001567 goto out;
1568 }
1569
Chris Masondfe25022008-05-13 13:46:40 -04001570 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001571 struct list_head *devices;
1572 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001573
Chris Masondfe25022008-05-13 13:46:40 -04001574 device = NULL;
1575 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001576 /*
1577 * It is safe to read the devices since the volume_mutex
1578 * is held.
1579 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001580 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001581 if (tmp->in_fs_metadata &&
1582 !tmp->is_tgtdev_for_dev_replace &&
1583 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001584 device = tmp;
1585 break;
1586 }
1587 }
1588 bdev = NULL;
1589 bh = NULL;
1590 disk_super = NULL;
1591 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001592 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001593 goto out;
1594 }
Chris Masondfe25022008-05-13 13:46:40 -04001595 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001596 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001597 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001598 root->fs_info->bdev_holder, 0,
1599 &bdev, &bh);
1600 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001601 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001602 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001603 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001604 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001605 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001606 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001607 if (!device) {
1608 ret = -ENOENT;
1609 goto error_brelse;
1610 }
Chris Masondfe25022008-05-13 13:46:40 -04001611 }
Yan Zheng2b820322008-11-17 21:11:30 -05001612
Stefan Behrens63a212a2012-11-05 18:29:28 +01001613 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001614 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001615 goto error_brelse;
1616 }
1617
Yan Zheng2b820322008-11-17 21:11:30 -05001618 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001619 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001620 goto error_brelse;
1621 }
1622
1623 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001624 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001625 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001626 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001627 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001628 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001629 }
Chris Masona061fc82008-05-07 11:43:44 -04001630
Carey Underwoodd7901552013-03-04 16:37:06 -06001631 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001632 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001633 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001634 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001635 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001636
Stefan Behrens63a212a2012-11-05 18:29:28 +01001637 /*
1638 * TODO: the superblock still includes this device in its num_devices
1639 * counter although write_all_supers() is not locked out. This
1640 * could give a filesystem state which requires a degraded mount.
1641 */
Chris Masona061fc82008-05-07 11:43:44 -04001642 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1643 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001644 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001645
Josef Bacik2bf64752011-09-26 17:12:22 -04001646 spin_lock(&root->fs_info->free_chunk_lock);
1647 root->fs_info->free_chunk_space = device->total_bytes -
1648 device->bytes_used;
1649 spin_unlock(&root->fs_info->free_chunk_lock);
1650
Yan Zheng2b820322008-11-17 21:11:30 -05001651 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001652 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001653
1654 /*
1655 * the device list mutex makes sure that we don't change
1656 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001657 * the device supers. Whoever is writing all supers, should
1658 * lock the device list mutex before getting the number of
1659 * devices in the super block (super_copy). Conversely,
1660 * whoever updates the number of devices in the super block
1661 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001662 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001663
1664 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001665 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001666 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001667
Yan Zhenge4404d62008-12-12 10:03:26 -05001668 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001669 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001670
Chris Masoncd02dca2010-12-13 14:56:23 -05001671 if (device->missing)
1672 root->fs_info->fs_devices->missing_devices--;
1673
Yan Zheng2b820322008-11-17 21:11:30 -05001674 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1675 struct btrfs_device, dev_list);
1676 if (device->bdev == root->fs_info->sb->s_bdev)
1677 root->fs_info->sb->s_bdev = next_device->bdev;
1678 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1679 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1680
Xiao Guangrong1f781602011-04-20 10:09:16 +00001681 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001682 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001683
Anand Jain99994cd2014-06-03 11:36:00 +08001684 /* remove sysfs entry */
1685 btrfs_kobj_rm_device(root->fs_info, device);
1686
Xiao Guangrong1f781602011-04-20 10:09:16 +00001687 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001688
David Sterba6c417612011-04-13 15:41:04 +02001689 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1690 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001691 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001692
Xiao Guangrong1f781602011-04-20 10:09:16 +00001693 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001694 struct btrfs_fs_devices *fs_devices;
1695 fs_devices = root->fs_info->fs_devices;
1696 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001697 if (fs_devices->seed == cur_devices) {
1698 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001699 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001700 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001701 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001702 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001703 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001704 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001705 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001706 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001707 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001708 }
1709
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001710 root->fs_info->num_tolerated_disk_barrier_failures =
1711 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1712
Yan Zheng2b820322008-11-17 21:11:30 -05001713 /*
1714 * at this point, the device is zero sized. We want to
1715 * remove it from the devices list and zero out the old super
1716 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001717 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001718 u64 bytenr;
1719 int i;
1720
Chris Masondfe25022008-05-13 13:46:40 -04001721 /* make sure this device isn't detected as part of
1722 * the FS anymore
1723 */
1724 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1725 set_buffer_dirty(bh);
1726 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001727
1728 /* clear the mirror copies of super block on the disk
1729 * being removed, 0th copy is been taken care above and
1730 * the below would take of the rest
1731 */
1732 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1733 bytenr = btrfs_sb_offset(i);
1734 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1735 i_size_read(bdev->bd_inode))
1736 break;
1737
1738 brelse(bh);
1739 bh = __bread(bdev, bytenr / 4096,
1740 BTRFS_SUPER_INFO_SIZE);
1741 if (!bh)
1742 continue;
1743
1744 disk_super = (struct btrfs_super_block *)bh->b_data;
1745
1746 if (btrfs_super_bytenr(disk_super) != bytenr ||
1747 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1748 continue;
1749 }
1750 memset(&disk_super->magic, 0,
1751 sizeof(disk_super->magic));
1752 set_buffer_dirty(bh);
1753 sync_dirty_buffer(bh);
1754 }
Chris Masondfe25022008-05-13 13:46:40 -04001755 }
Chris Masona061fc82008-05-07 11:43:44 -04001756
Chris Masona061fc82008-05-07 11:43:44 -04001757 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001758
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001759 if (bdev) {
1760 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001761 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001762
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001763 /* Update ctime/mtime for device path for libblkid */
1764 update_dev_time(device_path);
1765 }
1766
Chris Masona061fc82008-05-07 11:43:44 -04001767error_brelse:
1768 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001769 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001770 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001771out:
1772 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001773 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001774error_undo:
1775 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001776 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001777 list_add(&device->dev_alloc_list,
1778 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001779 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001780 root->fs_info->fs_devices->rw_devices++;
1781 }
1782 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001783}
1784
Stefan Behrense93c89c2012-11-05 17:33:06 +01001785void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1786 struct btrfs_device *srcdev)
1787{
1788 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001789
Stefan Behrense93c89c2012-11-05 17:33:06 +01001790 list_del_rcu(&srcdev->dev_list);
1791 list_del_rcu(&srcdev->dev_alloc_list);
1792 fs_info->fs_devices->num_devices--;
1793 if (srcdev->missing) {
1794 fs_info->fs_devices->missing_devices--;
1795 fs_info->fs_devices->rw_devices++;
1796 }
1797 if (srcdev->can_discard)
1798 fs_info->fs_devices->num_can_discard--;
Ilya Dryomov13572722013-10-02 20:41:01 +03001799 if (srcdev->bdev) {
Stefan Behrense93c89c2012-11-05 17:33:06 +01001800 fs_info->fs_devices->open_devices--;
1801
Ilya Dryomov13572722013-10-02 20:41:01 +03001802 /* zero out the old super */
1803 btrfs_scratch_superblock(srcdev);
1804 }
1805
Stefan Behrense93c89c2012-11-05 17:33:06 +01001806 call_rcu(&srcdev->rcu, free_device);
1807}
1808
1809void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1810 struct btrfs_device *tgtdev)
1811{
1812 struct btrfs_device *next_device;
1813
1814 WARN_ON(!tgtdev);
1815 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1816 if (tgtdev->bdev) {
1817 btrfs_scratch_superblock(tgtdev);
1818 fs_info->fs_devices->open_devices--;
1819 }
1820 fs_info->fs_devices->num_devices--;
1821 if (tgtdev->can_discard)
1822 fs_info->fs_devices->num_can_discard++;
1823
1824 next_device = list_entry(fs_info->fs_devices->devices.next,
1825 struct btrfs_device, dev_list);
1826 if (tgtdev->bdev == fs_info->sb->s_bdev)
1827 fs_info->sb->s_bdev = next_device->bdev;
1828 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1829 fs_info->fs_devices->latest_bdev = next_device->bdev;
1830 list_del_rcu(&tgtdev->dev_list);
1831
1832 call_rcu(&tgtdev->rcu, free_device);
1833
1834 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1835}
1836
Eric Sandeen48a3b632013-04-25 20:41:01 +00001837static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1838 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001839{
1840 int ret = 0;
1841 struct btrfs_super_block *disk_super;
1842 u64 devid;
1843 u8 *dev_uuid;
1844 struct block_device *bdev;
1845 struct buffer_head *bh;
1846
1847 *device = NULL;
1848 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1849 root->fs_info->bdev_holder, 0, &bdev, &bh);
1850 if (ret)
1851 return ret;
1852 disk_super = (struct btrfs_super_block *)bh->b_data;
1853 devid = btrfs_stack_device_id(&disk_super->dev_item);
1854 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001855 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001856 disk_super->fsid);
1857 brelse(bh);
1858 if (!*device)
1859 ret = -ENOENT;
1860 blkdev_put(bdev, FMODE_READ);
1861 return ret;
1862}
1863
1864int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1865 char *device_path,
1866 struct btrfs_device **device)
1867{
1868 *device = NULL;
1869 if (strcmp(device_path, "missing") == 0) {
1870 struct list_head *devices;
1871 struct btrfs_device *tmp;
1872
1873 devices = &root->fs_info->fs_devices->devices;
1874 /*
1875 * It is safe to read the devices since the volume_mutex
1876 * is held by the caller.
1877 */
1878 list_for_each_entry(tmp, devices, dev_list) {
1879 if (tmp->in_fs_metadata && !tmp->bdev) {
1880 *device = tmp;
1881 break;
1882 }
1883 }
1884
1885 if (!*device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001886 btrfs_err(root->fs_info, "no missing device found");
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001887 return -ENOENT;
1888 }
1889
1890 return 0;
1891 } else {
1892 return btrfs_find_device_by_path(root, device_path, device);
1893 }
1894}
1895
Yan Zheng2b820322008-11-17 21:11:30 -05001896/*
1897 * does all the dirty work required for changing file system's UUID.
1898 */
Li Zefan125ccb02011-12-08 15:07:24 +08001899static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001900{
1901 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1902 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001903 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001904 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001905 struct btrfs_device *device;
1906 u64 super_flags;
1907
1908 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001909 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001910 return -EINVAL;
1911
Ilya Dryomov2208a372013-08-12 14:33:03 +03001912 seed_devices = __alloc_fs_devices();
1913 if (IS_ERR(seed_devices))
1914 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001915
Yan Zhenge4404d62008-12-12 10:03:26 -05001916 old_devices = clone_fs_devices(fs_devices);
1917 if (IS_ERR(old_devices)) {
1918 kfree(seed_devices);
1919 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001920 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001921
Yan Zheng2b820322008-11-17 21:11:30 -05001922 list_add(&old_devices->list, &fs_uuids);
1923
Yan Zhenge4404d62008-12-12 10:03:26 -05001924 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1925 seed_devices->opened = 1;
1926 INIT_LIST_HEAD(&seed_devices->devices);
1927 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001928 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001929
1930 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001931 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1932 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001933
Yan Zhenge4404d62008-12-12 10:03:26 -05001934 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1935 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1936 device->fs_devices = seed_devices;
1937 }
1938
Yan Zheng2b820322008-11-17 21:11:30 -05001939 fs_devices->seeding = 0;
1940 fs_devices->num_devices = 0;
1941 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001942 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001943
1944 generate_random_uuid(fs_devices->fsid);
1945 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1946 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01001947 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1948
Yan Zheng2b820322008-11-17 21:11:30 -05001949 super_flags = btrfs_super_flags(disk_super) &
1950 ~BTRFS_SUPER_FLAG_SEEDING;
1951 btrfs_set_super_flags(disk_super, super_flags);
1952
1953 return 0;
1954}
1955
1956/*
1957 * strore the expected generation for seed devices in device items.
1958 */
1959static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1960 struct btrfs_root *root)
1961{
1962 struct btrfs_path *path;
1963 struct extent_buffer *leaf;
1964 struct btrfs_dev_item *dev_item;
1965 struct btrfs_device *device;
1966 struct btrfs_key key;
1967 u8 fs_uuid[BTRFS_UUID_SIZE];
1968 u8 dev_uuid[BTRFS_UUID_SIZE];
1969 u64 devid;
1970 int ret;
1971
1972 path = btrfs_alloc_path();
1973 if (!path)
1974 return -ENOMEM;
1975
1976 root = root->fs_info->chunk_root;
1977 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1978 key.offset = 0;
1979 key.type = BTRFS_DEV_ITEM_KEY;
1980
1981 while (1) {
1982 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1983 if (ret < 0)
1984 goto error;
1985
1986 leaf = path->nodes[0];
1987next_slot:
1988 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1989 ret = btrfs_next_leaf(root, path);
1990 if (ret > 0)
1991 break;
1992 if (ret < 0)
1993 goto error;
1994 leaf = path->nodes[0];
1995 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001996 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001997 continue;
1998 }
1999
2000 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2001 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2002 key.type != BTRFS_DEV_ITEM_KEY)
2003 break;
2004
2005 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2006 struct btrfs_dev_item);
2007 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002008 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002009 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002010 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002011 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002012 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2013 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002014 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002015
2016 if (device->fs_devices->seeding) {
2017 btrfs_set_device_generation(leaf, dev_item,
2018 device->generation);
2019 btrfs_mark_buffer_dirty(leaf);
2020 }
2021
2022 path->slots[0]++;
2023 goto next_slot;
2024 }
2025 ret = 0;
2026error:
2027 btrfs_free_path(path);
2028 return ret;
2029}
2030
Chris Mason788f20e2008-04-28 15:29:42 -04002031int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2032{
Josef Bacikd5e20032011-08-04 14:52:27 +00002033 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002034 struct btrfs_trans_handle *trans;
2035 struct btrfs_device *device;
2036 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002037 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002038 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002039 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04002040 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002041 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002042 int ret = 0;
2043
Yan Zheng2b820322008-11-17 21:11:30 -05002044 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002045 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002046
Li Zefana5d163332011-12-07 20:08:40 -05002047 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002048 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002049 if (IS_ERR(bdev))
2050 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002051
Yan Zheng2b820322008-11-17 21:11:30 -05002052 if (root->fs_info->fs_devices->seeding) {
2053 seeding_dev = 1;
2054 down_write(&sb->s_umount);
2055 mutex_lock(&uuid_mutex);
2056 }
2057
Chris Mason8c8bee12008-09-29 11:19:10 -04002058 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002059
Chris Mason788f20e2008-04-28 15:29:42 -04002060 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002061
2062 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002063 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002064 if (device->bdev == bdev) {
2065 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002066 mutex_unlock(
2067 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002068 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002069 }
2070 }
Liu Bod25628b2012-11-14 14:35:30 +00002071 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002072
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002073 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2074 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002075 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002076 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002077 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002078 }
2079
Josef Bacik606686e2012-06-04 14:03:51 -04002080 name = rcu_string_strdup(device_path, GFP_NOFS);
2081 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002082 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002083 ret = -ENOMEM;
2084 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002085 }
Josef Bacik606686e2012-06-04 14:03:51 -04002086 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002087
Yan, Zhenga22285a2010-05-16 10:48:46 -04002088 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002089 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002090 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002091 kfree(device);
2092 ret = PTR_ERR(trans);
2093 goto error;
2094 }
2095
Yan Zheng2b820322008-11-17 21:11:30 -05002096 lock_chunks(root);
2097
Josef Bacikd5e20032011-08-04 14:52:27 +00002098 q = bdev_get_queue(bdev);
2099 if (blk_queue_discard(q))
2100 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002101 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002102 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002103 device->io_width = root->sectorsize;
2104 device->io_align = root->sectorsize;
2105 device->sector_size = root->sectorsize;
2106 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002107 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002108 device->dev_root = root->fs_info->dev_root;
2109 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002110 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002111 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002112 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002113 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002114 set_blocksize(device->bdev, 4096);
2115
Yan Zheng2b820322008-11-17 21:11:30 -05002116 if (seeding_dev) {
2117 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002118 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002119 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002120 }
2121
2122 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002123
Chris Masone5e9a522009-06-10 15:17:02 -04002124 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002125 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002126 list_add(&device->dev_alloc_list,
2127 &root->fs_info->fs_devices->alloc_list);
2128 root->fs_info->fs_devices->num_devices++;
2129 root->fs_info->fs_devices->open_devices++;
2130 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002131 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00002132 if (device->can_discard)
2133 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05002134 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2135
Josef Bacik2bf64752011-09-26 17:12:22 -04002136 spin_lock(&root->fs_info->free_chunk_lock);
2137 root->fs_info->free_chunk_space += device->total_bytes;
2138 spin_unlock(&root->fs_info->free_chunk_lock);
2139
Chris Masonc2898112009-06-10 09:51:32 -04002140 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2141 root->fs_info->fs_devices->rotating = 1;
2142
David Sterba6c417612011-04-13 15:41:04 +02002143 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2144 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002145 total_bytes + device->total_bytes);
2146
David Sterba6c417612011-04-13 15:41:04 +02002147 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
2148 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002149 total_bytes + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002150
2151 /* add sysfs device entry */
2152 btrfs_kobj_add_device(root->fs_info, device);
2153
Chris Masone5e9a522009-06-10 15:17:02 -04002154 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002155
Yan Zheng2b820322008-11-17 21:11:30 -05002156 if (seeding_dev) {
Anand Jainb2373f22014-06-03 11:36:03 +08002157 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002158 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002159 if (ret) {
2160 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002161 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002162 }
Yan Zheng2b820322008-11-17 21:11:30 -05002163 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002164 if (ret) {
2165 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002166 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002167 }
Anand Jainb2373f22014-06-03 11:36:03 +08002168
2169 /* Sprouting would change fsid of the mounted root,
2170 * so rename the fsid on the sysfs
2171 */
2172 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2173 root->fs_info->fsid);
2174 if (kobject_rename(&root->fs_info->super_kobj, fsid_buf))
2175 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05002176 } else {
2177 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002178 if (ret) {
2179 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002180 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002181 }
Yan Zheng2b820322008-11-17 21:11:30 -05002182 }
2183
Chris Mason913d9522009-03-10 13:17:18 -04002184 /*
2185 * we've got more storage, clear any full flags on the space
2186 * infos
2187 */
2188 btrfs_clear_space_info_full(root->fs_info);
2189
Chris Mason7d9eb122008-07-08 14:19:17 -04002190 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002191 root->fs_info->num_tolerated_disk_barrier_failures =
2192 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002193 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002194
2195 if (seeding_dev) {
2196 mutex_unlock(&uuid_mutex);
2197 up_write(&sb->s_umount);
2198
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002199 if (ret) /* transaction commit */
2200 return ret;
2201
Yan Zheng2b820322008-11-17 21:11:30 -05002202 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002203 if (ret < 0)
2204 btrfs_error(root->fs_info, ret,
2205 "Failed to relocate sys chunks after "
2206 "device initialization. This can be fixed "
2207 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002208 trans = btrfs_attach_transaction(root);
2209 if (IS_ERR(trans)) {
2210 if (PTR_ERR(trans) == -ENOENT)
2211 return 0;
2212 return PTR_ERR(trans);
2213 }
2214 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002215 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002216
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002217 /* Update ctime/mtime for libblkid */
2218 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002219 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002220
2221error_trans:
2222 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002223 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002224 rcu_string_free(device->name);
Anand Jain0d393762014-06-03 11:36:01 +08002225 btrfs_kobj_rm_device(root->fs_info, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002226 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002227error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002228 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002229 if (seeding_dev) {
2230 mutex_unlock(&uuid_mutex);
2231 up_write(&sb->s_umount);
2232 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002233 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002234}
2235
Stefan Behrense93c89c2012-11-05 17:33:06 +01002236int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2237 struct btrfs_device **device_out)
2238{
2239 struct request_queue *q;
2240 struct btrfs_device *device;
2241 struct block_device *bdev;
2242 struct btrfs_fs_info *fs_info = root->fs_info;
2243 struct list_head *devices;
2244 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002245 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002246 int ret = 0;
2247
2248 *device_out = NULL;
2249 if (fs_info->fs_devices->seeding)
2250 return -EINVAL;
2251
2252 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2253 fs_info->bdev_holder);
2254 if (IS_ERR(bdev))
2255 return PTR_ERR(bdev);
2256
2257 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2258
2259 devices = &fs_info->fs_devices->devices;
2260 list_for_each_entry(device, devices, dev_list) {
2261 if (device->bdev == bdev) {
2262 ret = -EEXIST;
2263 goto error;
2264 }
2265 }
2266
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002267 device = btrfs_alloc_device(NULL, &devid, NULL);
2268 if (IS_ERR(device)) {
2269 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002270 goto error;
2271 }
2272
2273 name = rcu_string_strdup(device_path, GFP_NOFS);
2274 if (!name) {
2275 kfree(device);
2276 ret = -ENOMEM;
2277 goto error;
2278 }
2279 rcu_assign_pointer(device->name, name);
2280
2281 q = bdev_get_queue(bdev);
2282 if (blk_queue_discard(q))
2283 device->can_discard = 1;
2284 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2285 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002286 device->generation = 0;
2287 device->io_width = root->sectorsize;
2288 device->io_align = root->sectorsize;
2289 device->sector_size = root->sectorsize;
2290 device->total_bytes = i_size_read(bdev->bd_inode);
2291 device->disk_total_bytes = device->total_bytes;
2292 device->dev_root = fs_info->dev_root;
2293 device->bdev = bdev;
2294 device->in_fs_metadata = 1;
2295 device->is_tgtdev_for_dev_replace = 1;
2296 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002297 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002298 set_blocksize(device->bdev, 4096);
2299 device->fs_devices = fs_info->fs_devices;
2300 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2301 fs_info->fs_devices->num_devices++;
2302 fs_info->fs_devices->open_devices++;
2303 if (device->can_discard)
2304 fs_info->fs_devices->num_can_discard++;
2305 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2306
2307 *device_out = device;
2308 return ret;
2309
2310error:
2311 blkdev_put(bdev, FMODE_EXCL);
2312 return ret;
2313}
2314
2315void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2316 struct btrfs_device *tgtdev)
2317{
2318 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2319 tgtdev->io_width = fs_info->dev_root->sectorsize;
2320 tgtdev->io_align = fs_info->dev_root->sectorsize;
2321 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2322 tgtdev->dev_root = fs_info->dev_root;
2323 tgtdev->in_fs_metadata = 1;
2324}
2325
Chris Masond3977122009-01-05 21:25:51 -05002326static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2327 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002328{
2329 int ret;
2330 struct btrfs_path *path;
2331 struct btrfs_root *root;
2332 struct btrfs_dev_item *dev_item;
2333 struct extent_buffer *leaf;
2334 struct btrfs_key key;
2335
2336 root = device->dev_root->fs_info->chunk_root;
2337
2338 path = btrfs_alloc_path();
2339 if (!path)
2340 return -ENOMEM;
2341
2342 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2343 key.type = BTRFS_DEV_ITEM_KEY;
2344 key.offset = device->devid;
2345
2346 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2347 if (ret < 0)
2348 goto out;
2349
2350 if (ret > 0) {
2351 ret = -ENOENT;
2352 goto out;
2353 }
2354
2355 leaf = path->nodes[0];
2356 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2357
2358 btrfs_set_device_id(leaf, dev_item, device->devid);
2359 btrfs_set_device_type(leaf, dev_item, device->type);
2360 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2361 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2362 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002363 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002364 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2365 btrfs_mark_buffer_dirty(leaf);
2366
2367out:
2368 btrfs_free_path(path);
2369 return ret;
2370}
2371
Chris Mason7d9eb122008-07-08 14:19:17 -04002372static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002373 struct btrfs_device *device, u64 new_size)
2374{
2375 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002376 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002377 u64 old_total = btrfs_super_total_bytes(super_copy);
2378 u64 diff = new_size - device->total_bytes;
2379
Yan Zheng2b820322008-11-17 21:11:30 -05002380 if (!device->writeable)
2381 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002382 if (new_size <= device->total_bytes ||
2383 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002384 return -EINVAL;
2385
Chris Mason8f18cf12008-04-25 16:53:30 -04002386 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002387 device->fs_devices->total_rw_bytes += diff;
2388
2389 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002390 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002391 btrfs_clear_space_info_full(device->dev_root->fs_info);
2392
Chris Mason8f18cf12008-04-25 16:53:30 -04002393 return btrfs_update_device(trans, device);
2394}
2395
Chris Mason7d9eb122008-07-08 14:19:17 -04002396int btrfs_grow_device(struct btrfs_trans_handle *trans,
2397 struct btrfs_device *device, u64 new_size)
2398{
2399 int ret;
2400 lock_chunks(device->dev_root);
2401 ret = __btrfs_grow_device(trans, device, new_size);
2402 unlock_chunks(device->dev_root);
2403 return ret;
2404}
2405
Chris Mason8f18cf12008-04-25 16:53:30 -04002406static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2407 struct btrfs_root *root,
2408 u64 chunk_tree, u64 chunk_objectid,
2409 u64 chunk_offset)
2410{
2411 int ret;
2412 struct btrfs_path *path;
2413 struct btrfs_key key;
2414
2415 root = root->fs_info->chunk_root;
2416 path = btrfs_alloc_path();
2417 if (!path)
2418 return -ENOMEM;
2419
2420 key.objectid = chunk_objectid;
2421 key.offset = chunk_offset;
2422 key.type = BTRFS_CHUNK_ITEM_KEY;
2423
2424 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002425 if (ret < 0)
2426 goto out;
2427 else if (ret > 0) { /* Logic error or corruption */
2428 btrfs_error(root->fs_info, -ENOENT,
2429 "Failed lookup while freeing chunk.");
2430 ret = -ENOENT;
2431 goto out;
2432 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002433
2434 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002435 if (ret < 0)
2436 btrfs_error(root->fs_info, ret,
2437 "Failed to delete chunk item.");
2438out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002439 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002440 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002441}
2442
Christoph Hellwigb2950862008-12-02 09:54:17 -05002443static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002444 chunk_offset)
2445{
David Sterba6c417612011-04-13 15:41:04 +02002446 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002447 struct btrfs_disk_key *disk_key;
2448 struct btrfs_chunk *chunk;
2449 u8 *ptr;
2450 int ret = 0;
2451 u32 num_stripes;
2452 u32 array_size;
2453 u32 len = 0;
2454 u32 cur;
2455 struct btrfs_key key;
2456
2457 array_size = btrfs_super_sys_array_size(super_copy);
2458
2459 ptr = super_copy->sys_chunk_array;
2460 cur = 0;
2461
2462 while (cur < array_size) {
2463 disk_key = (struct btrfs_disk_key *)ptr;
2464 btrfs_disk_key_to_cpu(&key, disk_key);
2465
2466 len = sizeof(*disk_key);
2467
2468 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2469 chunk = (struct btrfs_chunk *)(ptr + len);
2470 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2471 len += btrfs_chunk_item_size(num_stripes);
2472 } else {
2473 ret = -EIO;
2474 break;
2475 }
2476 if (key.objectid == chunk_objectid &&
2477 key.offset == chunk_offset) {
2478 memmove(ptr, ptr + len, array_size - (cur + len));
2479 array_size -= len;
2480 btrfs_set_super_sys_array_size(super_copy, array_size);
2481 } else {
2482 ptr += len;
2483 cur += len;
2484 }
2485 }
2486 return ret;
2487}
2488
Christoph Hellwigb2950862008-12-02 09:54:17 -05002489static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002490 u64 chunk_tree, u64 chunk_objectid,
2491 u64 chunk_offset)
2492{
2493 struct extent_map_tree *em_tree;
2494 struct btrfs_root *extent_root;
2495 struct btrfs_trans_handle *trans;
2496 struct extent_map *em;
2497 struct map_lookup *map;
2498 int ret;
2499 int i;
2500
2501 root = root->fs_info->chunk_root;
2502 extent_root = root->fs_info->extent_root;
2503 em_tree = &root->fs_info->mapping_tree.map_tree;
2504
Josef Bacikba1bf482009-09-11 16:11:19 -04002505 ret = btrfs_can_relocate(extent_root, chunk_offset);
2506 if (ret)
2507 return -ENOSPC;
2508
Chris Mason8f18cf12008-04-25 16:53:30 -04002509 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002510 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002511 if (ret)
2512 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002513
Yan, Zhenga22285a2010-05-16 10:48:46 -04002514 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002515 if (IS_ERR(trans)) {
2516 ret = PTR_ERR(trans);
2517 btrfs_std_error(root->fs_info, ret);
2518 return ret;
2519 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002520
Chris Mason7d9eb122008-07-08 14:19:17 -04002521 lock_chunks(root);
2522
Chris Mason8f18cf12008-04-25 16:53:30 -04002523 /*
2524 * step two, delete the device extents and the
2525 * chunk tree entries
2526 */
Chris Mason890871b2009-09-02 16:24:52 -04002527 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002528 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002529 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002530
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002531 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002532 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002533 map = (struct map_lookup *)em->bdev;
2534
2535 for (i = 0; i < map->num_stripes; i++) {
2536 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2537 map->stripes[i].physical);
2538 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002539
Chris Masondfe25022008-05-13 13:46:40 -04002540 if (map->stripes[i].dev) {
2541 ret = btrfs_update_device(trans, map->stripes[i].dev);
2542 BUG_ON(ret);
2543 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002544 }
2545 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2546 chunk_offset);
2547
2548 BUG_ON(ret);
2549
liubo1abe9b82011-03-24 11:18:59 +00002550 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2551
Chris Mason8f18cf12008-04-25 16:53:30 -04002552 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2553 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2554 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002555 }
2556
Zheng Yan1a40e232008-09-26 10:09:34 -04002557 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2558 BUG_ON(ret);
2559
Chris Mason890871b2009-09-02 16:24:52 -04002560 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002561 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002562 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002563
Chris Mason8f18cf12008-04-25 16:53:30 -04002564 /* once for the tree */
2565 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002566 /* once for us */
2567 free_extent_map(em);
2568
Chris Mason7d9eb122008-07-08 14:19:17 -04002569 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002570 btrfs_end_transaction(trans, root);
2571 return 0;
2572}
2573
Yan Zheng2b820322008-11-17 21:11:30 -05002574static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2575{
2576 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2577 struct btrfs_path *path;
2578 struct extent_buffer *leaf;
2579 struct btrfs_chunk *chunk;
2580 struct btrfs_key key;
2581 struct btrfs_key found_key;
2582 u64 chunk_tree = chunk_root->root_key.objectid;
2583 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002584 bool retried = false;
2585 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002586 int ret;
2587
2588 path = btrfs_alloc_path();
2589 if (!path)
2590 return -ENOMEM;
2591
Josef Bacikba1bf482009-09-11 16:11:19 -04002592again:
Yan Zheng2b820322008-11-17 21:11:30 -05002593 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2594 key.offset = (u64)-1;
2595 key.type = BTRFS_CHUNK_ITEM_KEY;
2596
2597 while (1) {
2598 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2599 if (ret < 0)
2600 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002601 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002602
2603 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2604 key.type);
2605 if (ret < 0)
2606 goto error;
2607 if (ret > 0)
2608 break;
2609
2610 leaf = path->nodes[0];
2611 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2612
2613 chunk = btrfs_item_ptr(leaf, path->slots[0],
2614 struct btrfs_chunk);
2615 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002616 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002617
2618 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2619 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2620 found_key.objectid,
2621 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002622 if (ret == -ENOSPC)
2623 failed++;
2624 else if (ret)
2625 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002626 }
2627
2628 if (found_key.offset == 0)
2629 break;
2630 key.offset = found_key.offset - 1;
2631 }
2632 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002633 if (failed && !retried) {
2634 failed = 0;
2635 retried = true;
2636 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302637 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002638 ret = -ENOSPC;
2639 }
Yan Zheng2b820322008-11-17 21:11:30 -05002640error:
2641 btrfs_free_path(path);
2642 return ret;
2643}
2644
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002645static int insert_balance_item(struct btrfs_root *root,
2646 struct btrfs_balance_control *bctl)
2647{
2648 struct btrfs_trans_handle *trans;
2649 struct btrfs_balance_item *item;
2650 struct btrfs_disk_balance_args disk_bargs;
2651 struct btrfs_path *path;
2652 struct extent_buffer *leaf;
2653 struct btrfs_key key;
2654 int ret, err;
2655
2656 path = btrfs_alloc_path();
2657 if (!path)
2658 return -ENOMEM;
2659
2660 trans = btrfs_start_transaction(root, 0);
2661 if (IS_ERR(trans)) {
2662 btrfs_free_path(path);
2663 return PTR_ERR(trans);
2664 }
2665
2666 key.objectid = BTRFS_BALANCE_OBJECTID;
2667 key.type = BTRFS_BALANCE_ITEM_KEY;
2668 key.offset = 0;
2669
2670 ret = btrfs_insert_empty_item(trans, root, path, &key,
2671 sizeof(*item));
2672 if (ret)
2673 goto out;
2674
2675 leaf = path->nodes[0];
2676 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2677
2678 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2679
2680 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2681 btrfs_set_balance_data(leaf, item, &disk_bargs);
2682 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2683 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2684 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2685 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2686
2687 btrfs_set_balance_flags(leaf, item, bctl->flags);
2688
2689 btrfs_mark_buffer_dirty(leaf);
2690out:
2691 btrfs_free_path(path);
2692 err = btrfs_commit_transaction(trans, root);
2693 if (err && !ret)
2694 ret = err;
2695 return ret;
2696}
2697
2698static int del_balance_item(struct btrfs_root *root)
2699{
2700 struct btrfs_trans_handle *trans;
2701 struct btrfs_path *path;
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_search_slot(trans, root, &key, path, -1, 1);
2720 if (ret < 0)
2721 goto out;
2722 if (ret > 0) {
2723 ret = -ENOENT;
2724 goto out;
2725 }
2726
2727 ret = btrfs_del_item(trans, root, path);
2728out:
2729 btrfs_free_path(path);
2730 err = btrfs_commit_transaction(trans, root);
2731 if (err && !ret)
2732 ret = err;
2733 return ret;
2734}
2735
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002736/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002737 * This is a heuristic used to reduce the number of chunks balanced on
2738 * resume after balance was interrupted.
2739 */
2740static void update_balance_args(struct btrfs_balance_control *bctl)
2741{
2742 /*
2743 * Turn on soft mode for chunk types that were being converted.
2744 */
2745 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2746 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2747 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2748 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2749 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2750 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2751
2752 /*
2753 * Turn on usage filter if is not already used. The idea is
2754 * that chunks that we have already balanced should be
2755 * reasonably full. Don't do it for chunks that are being
2756 * converted - that will keep us from relocating unconverted
2757 * (albeit full) chunks.
2758 */
2759 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2760 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2761 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2762 bctl->data.usage = 90;
2763 }
2764 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2765 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2766 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2767 bctl->sys.usage = 90;
2768 }
2769 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2770 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2771 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2772 bctl->meta.usage = 90;
2773 }
2774}
2775
2776/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002777 * Should be called with both balance and volume mutexes held to
2778 * serialize other volume operations (add_dev/rm_dev/resize) with
2779 * restriper. Same goes for unset_balance_control.
2780 */
2781static void set_balance_control(struct btrfs_balance_control *bctl)
2782{
2783 struct btrfs_fs_info *fs_info = bctl->fs_info;
2784
2785 BUG_ON(fs_info->balance_ctl);
2786
2787 spin_lock(&fs_info->balance_lock);
2788 fs_info->balance_ctl = bctl;
2789 spin_unlock(&fs_info->balance_lock);
2790}
2791
2792static void unset_balance_control(struct btrfs_fs_info *fs_info)
2793{
2794 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2795
2796 BUG_ON(!fs_info->balance_ctl);
2797
2798 spin_lock(&fs_info->balance_lock);
2799 fs_info->balance_ctl = NULL;
2800 spin_unlock(&fs_info->balance_lock);
2801
2802 kfree(bctl);
2803}
2804
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002805/*
2806 * Balance filters. Return 1 if chunk should be filtered out
2807 * (should not be balanced).
2808 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002809static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002810 struct btrfs_balance_args *bargs)
2811{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002812 chunk_type = chunk_to_extended(chunk_type) &
2813 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002814
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002815 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002816 return 0;
2817
2818 return 1;
2819}
2820
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002821static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2822 struct btrfs_balance_args *bargs)
2823{
2824 struct btrfs_block_group_cache *cache;
2825 u64 chunk_used, user_thresh;
2826 int ret = 1;
2827
2828 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2829 chunk_used = btrfs_block_group_used(&cache->item);
2830
Ilya Dryomova105bb82013-01-21 15:15:56 +02002831 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002832 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002833 else if (bargs->usage > 100)
2834 user_thresh = cache->key.offset;
2835 else
2836 user_thresh = div_factor_fine(cache->key.offset,
2837 bargs->usage);
2838
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002839 if (chunk_used < user_thresh)
2840 ret = 0;
2841
2842 btrfs_put_block_group(cache);
2843 return ret;
2844}
2845
Ilya Dryomov409d4042012-01-16 22:04:47 +02002846static int chunk_devid_filter(struct extent_buffer *leaf,
2847 struct btrfs_chunk *chunk,
2848 struct btrfs_balance_args *bargs)
2849{
2850 struct btrfs_stripe *stripe;
2851 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2852 int i;
2853
2854 for (i = 0; i < num_stripes; i++) {
2855 stripe = btrfs_stripe_nr(chunk, i);
2856 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2857 return 0;
2858 }
2859
2860 return 1;
2861}
2862
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002863/* [pstart, pend) */
2864static int chunk_drange_filter(struct extent_buffer *leaf,
2865 struct btrfs_chunk *chunk,
2866 u64 chunk_offset,
2867 struct btrfs_balance_args *bargs)
2868{
2869 struct btrfs_stripe *stripe;
2870 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2871 u64 stripe_offset;
2872 u64 stripe_length;
2873 int factor;
2874 int i;
2875
2876 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2877 return 0;
2878
2879 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002880 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2881 factor = num_stripes / 2;
2882 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2883 factor = num_stripes - 1;
2884 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2885 factor = num_stripes - 2;
2886 } else {
2887 factor = num_stripes;
2888 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002889
2890 for (i = 0; i < num_stripes; i++) {
2891 stripe = btrfs_stripe_nr(chunk, i);
2892 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2893 continue;
2894
2895 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2896 stripe_length = btrfs_chunk_length(leaf, chunk);
2897 do_div(stripe_length, factor);
2898
2899 if (stripe_offset < bargs->pend &&
2900 stripe_offset + stripe_length > bargs->pstart)
2901 return 0;
2902 }
2903
2904 return 1;
2905}
2906
Ilya Dryomovea671762012-01-16 22:04:48 +02002907/* [vstart, vend) */
2908static int chunk_vrange_filter(struct extent_buffer *leaf,
2909 struct btrfs_chunk *chunk,
2910 u64 chunk_offset,
2911 struct btrfs_balance_args *bargs)
2912{
2913 if (chunk_offset < bargs->vend &&
2914 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2915 /* at least part of the chunk is inside this vrange */
2916 return 0;
2917
2918 return 1;
2919}
2920
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002921static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002922 struct btrfs_balance_args *bargs)
2923{
2924 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2925 return 0;
2926
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002927 chunk_type = chunk_to_extended(chunk_type) &
2928 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002929
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002930 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002931 return 1;
2932
2933 return 0;
2934}
2935
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002936static int should_balance_chunk(struct btrfs_root *root,
2937 struct extent_buffer *leaf,
2938 struct btrfs_chunk *chunk, u64 chunk_offset)
2939{
2940 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2941 struct btrfs_balance_args *bargs = NULL;
2942 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2943
2944 /* type filter */
2945 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2946 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2947 return 0;
2948 }
2949
2950 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2951 bargs = &bctl->data;
2952 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2953 bargs = &bctl->sys;
2954 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2955 bargs = &bctl->meta;
2956
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002957 /* profiles filter */
2958 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2959 chunk_profiles_filter(chunk_type, bargs)) {
2960 return 0;
2961 }
2962
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002963 /* usage filter */
2964 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2965 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2966 return 0;
2967 }
2968
Ilya Dryomov409d4042012-01-16 22:04:47 +02002969 /* devid filter */
2970 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2971 chunk_devid_filter(leaf, chunk, bargs)) {
2972 return 0;
2973 }
2974
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002975 /* drange filter, makes sense only with devid filter */
2976 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2977 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2978 return 0;
2979 }
2980
Ilya Dryomovea671762012-01-16 22:04:48 +02002981 /* vrange filter */
2982 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2983 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2984 return 0;
2985 }
2986
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002987 /* soft profile changing mode */
2988 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2989 chunk_soft_convert_filter(chunk_type, bargs)) {
2990 return 0;
2991 }
2992
David Sterba7d824b62014-05-07 17:37:51 +02002993 /*
2994 * limited by count, must be the last filter
2995 */
2996 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
2997 if (bargs->limit == 0)
2998 return 0;
2999 else
3000 bargs->limit--;
3001 }
3002
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003003 return 1;
3004}
3005
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003006static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003007{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003008 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003009 struct btrfs_root *chunk_root = fs_info->chunk_root;
3010 struct btrfs_root *dev_root = fs_info->dev_root;
3011 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003012 struct btrfs_device *device;
3013 u64 old_size;
3014 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003015 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003016 struct btrfs_path *path;
3017 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003018 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003019 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003020 struct extent_buffer *leaf;
3021 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003022 int ret;
3023 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003024 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003025 u64 limit_data = bctl->data.limit;
3026 u64 limit_meta = bctl->meta.limit;
3027 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003028
Chris Masonec44a352008-04-28 15:29:52 -04003029 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003030 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003031 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04003032 old_size = device->total_bytes;
3033 size_to_free = div_factor(old_size, 1);
3034 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003035 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003036 device->total_bytes - device->bytes_used > size_to_free ||
3037 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003038 continue;
3039
3040 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003041 if (ret == -ENOSPC)
3042 break;
Chris Masonec44a352008-04-28 15:29:52 -04003043 BUG_ON(ret);
3044
Yan, Zhenga22285a2010-05-16 10:48:46 -04003045 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003046 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003047
3048 ret = btrfs_grow_device(trans, device, old_size);
3049 BUG_ON(ret);
3050
3051 btrfs_end_transaction(trans, dev_root);
3052 }
3053
3054 /* step two, relocate all the chunks */
3055 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003056 if (!path) {
3057 ret = -ENOMEM;
3058 goto error;
3059 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003060
3061 /* zero out stat counters */
3062 spin_lock(&fs_info->balance_lock);
3063 memset(&bctl->stat, 0, sizeof(bctl->stat));
3064 spin_unlock(&fs_info->balance_lock);
3065again:
David Sterba7d824b62014-05-07 17:37:51 +02003066 if (!counting) {
3067 bctl->data.limit = limit_data;
3068 bctl->meta.limit = limit_meta;
3069 bctl->sys.limit = limit_sys;
3070 }
Chris Masonec44a352008-04-28 15:29:52 -04003071 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3072 key.offset = (u64)-1;
3073 key.type = BTRFS_CHUNK_ITEM_KEY;
3074
Chris Masond3977122009-01-05 21:25:51 -05003075 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003076 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003077 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003078 ret = -ECANCELED;
3079 goto error;
3080 }
3081
Chris Masonec44a352008-04-28 15:29:52 -04003082 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3083 if (ret < 0)
3084 goto error;
3085
3086 /*
3087 * this shouldn't happen, it means the last relocate
3088 * failed
3089 */
3090 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003091 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003092
3093 ret = btrfs_previous_item(chunk_root, path, 0,
3094 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003095 if (ret) {
3096 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003097 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003098 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003099
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003100 leaf = path->nodes[0];
3101 slot = path->slots[0];
3102 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3103
Chris Masonec44a352008-04-28 15:29:52 -04003104 if (found_key.objectid != key.objectid)
3105 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04003106
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003107 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3108
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003109 if (!counting) {
3110 spin_lock(&fs_info->balance_lock);
3111 bctl->stat.considered++;
3112 spin_unlock(&fs_info->balance_lock);
3113 }
3114
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003115 ret = should_balance_chunk(chunk_root, leaf, chunk,
3116 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003117 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003118 if (!ret)
3119 goto loop;
3120
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003121 if (counting) {
3122 spin_lock(&fs_info->balance_lock);
3123 bctl->stat.expected++;
3124 spin_unlock(&fs_info->balance_lock);
3125 goto loop;
3126 }
3127
Chris Masonec44a352008-04-28 15:29:52 -04003128 ret = btrfs_relocate_chunk(chunk_root,
3129 chunk_root->root_key.objectid,
3130 found_key.objectid,
3131 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003132 if (ret && ret != -ENOSPC)
3133 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003134 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003135 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003136 } else {
3137 spin_lock(&fs_info->balance_lock);
3138 bctl->stat.completed++;
3139 spin_unlock(&fs_info->balance_lock);
3140 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003141loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003142 if (found_key.offset == 0)
3143 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003144 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003145 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003146
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003147 if (counting) {
3148 btrfs_release_path(path);
3149 counting = false;
3150 goto again;
3151 }
Chris Masonec44a352008-04-28 15:29:52 -04003152error:
3153 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003154 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003155 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003156 enospc_errors);
3157 if (!ret)
3158 ret = -ENOSPC;
3159 }
3160
Chris Masonec44a352008-04-28 15:29:52 -04003161 return ret;
3162}
3163
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003164/**
3165 * alloc_profile_is_valid - see if a given profile is valid and reduced
3166 * @flags: profile to validate
3167 * @extended: if true @flags is treated as an extended profile
3168 */
3169static int alloc_profile_is_valid(u64 flags, int extended)
3170{
3171 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3172 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3173
3174 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3175
3176 /* 1) check that all other bits are zeroed */
3177 if (flags & ~mask)
3178 return 0;
3179
3180 /* 2) see if profile is reduced */
3181 if (flags == 0)
3182 return !extended; /* "0" is valid for usual profiles */
3183
3184 /* true if exactly one bit set */
3185 return (flags & (flags - 1)) == 0;
3186}
3187
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003188static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3189{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003190 /* cancel requested || normal exit path */
3191 return atomic_read(&fs_info->balance_cancel_req) ||
3192 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3193 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003194}
3195
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003196static void __cancel_balance(struct btrfs_fs_info *fs_info)
3197{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003198 int ret;
3199
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003200 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003201 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003202 if (ret)
3203 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003204
3205 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003206}
3207
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003208/*
3209 * Should be called with both balance and volume mutexes held
3210 */
3211int btrfs_balance(struct btrfs_balance_control *bctl,
3212 struct btrfs_ioctl_balance_args *bargs)
3213{
3214 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003215 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003216 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003217 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003218 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003219 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003220
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003221 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003222 atomic_read(&fs_info->balance_pause_req) ||
3223 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003224 ret = -EINVAL;
3225 goto out;
3226 }
3227
Ilya Dryomove4837f82012-03-27 17:09:17 +03003228 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3229 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3230 mixed = 1;
3231
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003232 /*
3233 * In case of mixed groups both data and meta should be picked,
3234 * and identical options should be given for both of them.
3235 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003236 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3237 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003238 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3239 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3240 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003241 btrfs_err(fs_info, "with mixed groups data and "
3242 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003243 ret = -EINVAL;
3244 goto out;
3245 }
3246 }
3247
Stefan Behrens8dabb742012-11-06 13:15:27 +01003248 num_devices = fs_info->fs_devices->num_devices;
3249 btrfs_dev_replace_lock(&fs_info->dev_replace);
3250 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3251 BUG_ON(num_devices < 1);
3252 num_devices--;
3253 }
3254 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003255 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003256 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003257 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003258 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003259 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003260 if (num_devices > 2)
3261 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3262 if (num_devices > 3)
3263 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3264 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003265 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3266 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3267 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003268 btrfs_err(fs_info, "unable to start balance with target "
3269 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003270 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003271 ret = -EINVAL;
3272 goto out;
3273 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003274 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3275 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3276 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003277 btrfs_err(fs_info,
3278 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003279 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003280 ret = -EINVAL;
3281 goto out;
3282 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003283 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3284 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3285 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003286 btrfs_err(fs_info,
3287 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003288 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003289 ret = -EINVAL;
3290 goto out;
3291 }
3292
Ilya Dryomove4837f82012-03-27 17:09:17 +03003293 /* allow dup'ed data chunks only in mixed mode */
3294 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003295 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003296 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003297 ret = -EINVAL;
3298 goto out;
3299 }
3300
3301 /* allow to reduce meta or sys integrity only if force set */
3302 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003303 BTRFS_BLOCK_GROUP_RAID10 |
3304 BTRFS_BLOCK_GROUP_RAID5 |
3305 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003306 do {
3307 seq = read_seqbegin(&fs_info->profiles_lock);
3308
3309 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3310 (fs_info->avail_system_alloc_bits & allowed) &&
3311 !(bctl->sys.target & allowed)) ||
3312 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3313 (fs_info->avail_metadata_alloc_bits & allowed) &&
3314 !(bctl->meta.target & allowed))) {
3315 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003316 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003317 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003318 btrfs_err(fs_info, "balance will reduce metadata "
3319 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003320 ret = -EINVAL;
3321 goto out;
3322 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003323 }
Miao Xiede98ced2013-01-29 10:13:12 +00003324 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003325
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003326 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3327 int num_tolerated_disk_barrier_failures;
3328 u64 target = bctl->sys.target;
3329
3330 num_tolerated_disk_barrier_failures =
3331 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3332 if (num_tolerated_disk_barrier_failures > 0 &&
3333 (target &
3334 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3335 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3336 num_tolerated_disk_barrier_failures = 0;
3337 else if (num_tolerated_disk_barrier_failures > 1 &&
3338 (target &
3339 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3340 num_tolerated_disk_barrier_failures = 1;
3341
3342 fs_info->num_tolerated_disk_barrier_failures =
3343 num_tolerated_disk_barrier_failures;
3344 }
3345
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003346 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003347 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003348 goto out;
3349
Ilya Dryomov59641012012-01-16 22:04:48 +02003350 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3351 BUG_ON(ret == -EEXIST);
3352 set_balance_control(bctl);
3353 } else {
3354 BUG_ON(ret != -EEXIST);
3355 spin_lock(&fs_info->balance_lock);
3356 update_balance_args(bctl);
3357 spin_unlock(&fs_info->balance_lock);
3358 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003359
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003360 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003361 mutex_unlock(&fs_info->balance_mutex);
3362
3363 ret = __btrfs_balance(fs_info);
3364
3365 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003366 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003367
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003368 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3369 fs_info->num_tolerated_disk_barrier_failures =
3370 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3371 }
3372
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003373 if (bargs) {
3374 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003375 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003376 }
3377
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003378 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3379 balance_need_close(fs_info)) {
3380 __cancel_balance(fs_info);
3381 }
3382
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003383 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003384
3385 return ret;
3386out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003387 if (bctl->flags & BTRFS_BALANCE_RESUME)
3388 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003389 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003390 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003391 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3392 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003393 return ret;
3394}
3395
3396static int balance_kthread(void *data)
3397{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003398 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003399 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003400
3401 mutex_lock(&fs_info->volume_mutex);
3402 mutex_lock(&fs_info->balance_mutex);
3403
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003404 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003405 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003406 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003407 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003408
3409 mutex_unlock(&fs_info->balance_mutex);
3410 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003411
Ilya Dryomov59641012012-01-16 22:04:48 +02003412 return ret;
3413}
3414
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003415int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3416{
3417 struct task_struct *tsk;
3418
3419 spin_lock(&fs_info->balance_lock);
3420 if (!fs_info->balance_ctl) {
3421 spin_unlock(&fs_info->balance_lock);
3422 return 0;
3423 }
3424 spin_unlock(&fs_info->balance_lock);
3425
3426 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003427 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003428 return 0;
3429 }
3430
3431 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303432 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003433}
3434
Ilya Dryomov68310a52012-06-22 12:24:12 -06003435int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003436{
Ilya Dryomov59641012012-01-16 22:04:48 +02003437 struct btrfs_balance_control *bctl;
3438 struct btrfs_balance_item *item;
3439 struct btrfs_disk_balance_args disk_bargs;
3440 struct btrfs_path *path;
3441 struct extent_buffer *leaf;
3442 struct btrfs_key key;
3443 int ret;
3444
3445 path = btrfs_alloc_path();
3446 if (!path)
3447 return -ENOMEM;
3448
Ilya Dryomov68310a52012-06-22 12:24:12 -06003449 key.objectid = BTRFS_BALANCE_OBJECTID;
3450 key.type = BTRFS_BALANCE_ITEM_KEY;
3451 key.offset = 0;
3452
3453 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3454 if (ret < 0)
3455 goto out;
3456 if (ret > 0) { /* ret = -ENOENT; */
3457 ret = 0;
3458 goto out;
3459 }
3460
Ilya Dryomov59641012012-01-16 22:04:48 +02003461 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3462 if (!bctl) {
3463 ret = -ENOMEM;
3464 goto out;
3465 }
3466
Ilya Dryomov59641012012-01-16 22:04:48 +02003467 leaf = path->nodes[0];
3468 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3469
Ilya Dryomov68310a52012-06-22 12:24:12 -06003470 bctl->fs_info = fs_info;
3471 bctl->flags = btrfs_balance_flags(leaf, item);
3472 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003473
3474 btrfs_balance_data(leaf, item, &disk_bargs);
3475 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3476 btrfs_balance_meta(leaf, item, &disk_bargs);
3477 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3478 btrfs_balance_sys(leaf, item, &disk_bargs);
3479 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3480
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003481 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3482
Ilya Dryomov68310a52012-06-22 12:24:12 -06003483 mutex_lock(&fs_info->volume_mutex);
3484 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003485
Ilya Dryomov68310a52012-06-22 12:24:12 -06003486 set_balance_control(bctl);
3487
3488 mutex_unlock(&fs_info->balance_mutex);
3489 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003490out:
3491 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003492 return ret;
3493}
3494
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003495int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3496{
3497 int ret = 0;
3498
3499 mutex_lock(&fs_info->balance_mutex);
3500 if (!fs_info->balance_ctl) {
3501 mutex_unlock(&fs_info->balance_mutex);
3502 return -ENOTCONN;
3503 }
3504
3505 if (atomic_read(&fs_info->balance_running)) {
3506 atomic_inc(&fs_info->balance_pause_req);
3507 mutex_unlock(&fs_info->balance_mutex);
3508
3509 wait_event(fs_info->balance_wait_q,
3510 atomic_read(&fs_info->balance_running) == 0);
3511
3512 mutex_lock(&fs_info->balance_mutex);
3513 /* we are good with balance_ctl ripped off from under us */
3514 BUG_ON(atomic_read(&fs_info->balance_running));
3515 atomic_dec(&fs_info->balance_pause_req);
3516 } else {
3517 ret = -ENOTCONN;
3518 }
3519
3520 mutex_unlock(&fs_info->balance_mutex);
3521 return ret;
3522}
3523
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003524int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3525{
Ilya Dryomove649e582013-10-10 20:40:21 +03003526 if (fs_info->sb->s_flags & MS_RDONLY)
3527 return -EROFS;
3528
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003529 mutex_lock(&fs_info->balance_mutex);
3530 if (!fs_info->balance_ctl) {
3531 mutex_unlock(&fs_info->balance_mutex);
3532 return -ENOTCONN;
3533 }
3534
3535 atomic_inc(&fs_info->balance_cancel_req);
3536 /*
3537 * if we are running just wait and return, balance item is
3538 * deleted in btrfs_balance in this case
3539 */
3540 if (atomic_read(&fs_info->balance_running)) {
3541 mutex_unlock(&fs_info->balance_mutex);
3542 wait_event(fs_info->balance_wait_q,
3543 atomic_read(&fs_info->balance_running) == 0);
3544 mutex_lock(&fs_info->balance_mutex);
3545 } else {
3546 /* __cancel_balance needs volume_mutex */
3547 mutex_unlock(&fs_info->balance_mutex);
3548 mutex_lock(&fs_info->volume_mutex);
3549 mutex_lock(&fs_info->balance_mutex);
3550
3551 if (fs_info->balance_ctl)
3552 __cancel_balance(fs_info);
3553
3554 mutex_unlock(&fs_info->volume_mutex);
3555 }
3556
3557 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3558 atomic_dec(&fs_info->balance_cancel_req);
3559 mutex_unlock(&fs_info->balance_mutex);
3560 return 0;
3561}
3562
Stefan Behrens803b2f52013-08-15 17:11:21 +02003563static int btrfs_uuid_scan_kthread(void *data)
3564{
3565 struct btrfs_fs_info *fs_info = data;
3566 struct btrfs_root *root = fs_info->tree_root;
3567 struct btrfs_key key;
3568 struct btrfs_key max_key;
3569 struct btrfs_path *path = NULL;
3570 int ret = 0;
3571 struct extent_buffer *eb;
3572 int slot;
3573 struct btrfs_root_item root_item;
3574 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003575 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003576
3577 path = btrfs_alloc_path();
3578 if (!path) {
3579 ret = -ENOMEM;
3580 goto out;
3581 }
3582
3583 key.objectid = 0;
3584 key.type = BTRFS_ROOT_ITEM_KEY;
3585 key.offset = 0;
3586
3587 max_key.objectid = (u64)-1;
3588 max_key.type = BTRFS_ROOT_ITEM_KEY;
3589 max_key.offset = (u64)-1;
3590
3591 path->keep_locks = 1;
3592
3593 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003594 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003595 if (ret) {
3596 if (ret > 0)
3597 ret = 0;
3598 break;
3599 }
3600
3601 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3602 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3603 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3604 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3605 goto skip;
3606
3607 eb = path->nodes[0];
3608 slot = path->slots[0];
3609 item_size = btrfs_item_size_nr(eb, slot);
3610 if (item_size < sizeof(root_item))
3611 goto skip;
3612
Stefan Behrens803b2f52013-08-15 17:11:21 +02003613 read_extent_buffer(eb, &root_item,
3614 btrfs_item_ptr_offset(eb, slot),
3615 (int)sizeof(root_item));
3616 if (btrfs_root_refs(&root_item) == 0)
3617 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003618
3619 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3620 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3621 if (trans)
3622 goto update_tree;
3623
3624 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003625 /*
3626 * 1 - subvol uuid item
3627 * 1 - received_subvol uuid item
3628 */
3629 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3630 if (IS_ERR(trans)) {
3631 ret = PTR_ERR(trans);
3632 break;
3633 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003634 continue;
3635 } else {
3636 goto skip;
3637 }
3638update_tree:
3639 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003640 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3641 root_item.uuid,
3642 BTRFS_UUID_KEY_SUBVOL,
3643 key.objectid);
3644 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003645 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003646 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003647 break;
3648 }
3649 }
3650
3651 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003652 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3653 root_item.received_uuid,
3654 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3655 key.objectid);
3656 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003657 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003658 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003659 break;
3660 }
3661 }
3662
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003663skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003664 if (trans) {
3665 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003666 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003667 if (ret)
3668 break;
3669 }
3670
Stefan Behrens803b2f52013-08-15 17:11:21 +02003671 btrfs_release_path(path);
3672 if (key.offset < (u64)-1) {
3673 key.offset++;
3674 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3675 key.offset = 0;
3676 key.type = BTRFS_ROOT_ITEM_KEY;
3677 } else if (key.objectid < (u64)-1) {
3678 key.offset = 0;
3679 key.type = BTRFS_ROOT_ITEM_KEY;
3680 key.objectid++;
3681 } else {
3682 break;
3683 }
3684 cond_resched();
3685 }
3686
3687out:
3688 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003689 if (trans && !IS_ERR(trans))
3690 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003691 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05003692 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003693 else
3694 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003695 up(&fs_info->uuid_tree_rescan_sem);
3696 return 0;
3697}
3698
Stefan Behrens70f80172013-08-15 17:11:23 +02003699/*
3700 * Callback for btrfs_uuid_tree_iterate().
3701 * returns:
3702 * 0 check succeeded, the entry is not outdated.
3703 * < 0 if an error occured.
3704 * > 0 if the check failed, which means the caller shall remove the entry.
3705 */
3706static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3707 u8 *uuid, u8 type, u64 subid)
3708{
3709 struct btrfs_key key;
3710 int ret = 0;
3711 struct btrfs_root *subvol_root;
3712
3713 if (type != BTRFS_UUID_KEY_SUBVOL &&
3714 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3715 goto out;
3716
3717 key.objectid = subid;
3718 key.type = BTRFS_ROOT_ITEM_KEY;
3719 key.offset = (u64)-1;
3720 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3721 if (IS_ERR(subvol_root)) {
3722 ret = PTR_ERR(subvol_root);
3723 if (ret == -ENOENT)
3724 ret = 1;
3725 goto out;
3726 }
3727
3728 switch (type) {
3729 case BTRFS_UUID_KEY_SUBVOL:
3730 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3731 ret = 1;
3732 break;
3733 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3734 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3735 BTRFS_UUID_SIZE))
3736 ret = 1;
3737 break;
3738 }
3739
3740out:
3741 return ret;
3742}
3743
3744static int btrfs_uuid_rescan_kthread(void *data)
3745{
3746 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3747 int ret;
3748
3749 /*
3750 * 1st step is to iterate through the existing UUID tree and
3751 * to delete all entries that contain outdated data.
3752 * 2nd step is to add all missing entries to the UUID tree.
3753 */
3754 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3755 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003756 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003757 up(&fs_info->uuid_tree_rescan_sem);
3758 return ret;
3759 }
3760 return btrfs_uuid_scan_kthread(data);
3761}
3762
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003763int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3764{
3765 struct btrfs_trans_handle *trans;
3766 struct btrfs_root *tree_root = fs_info->tree_root;
3767 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003768 struct task_struct *task;
3769 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003770
3771 /*
3772 * 1 - root node
3773 * 1 - root item
3774 */
3775 trans = btrfs_start_transaction(tree_root, 2);
3776 if (IS_ERR(trans))
3777 return PTR_ERR(trans);
3778
3779 uuid_root = btrfs_create_tree(trans, fs_info,
3780 BTRFS_UUID_TREE_OBJECTID);
3781 if (IS_ERR(uuid_root)) {
3782 btrfs_abort_transaction(trans, tree_root,
3783 PTR_ERR(uuid_root));
3784 return PTR_ERR(uuid_root);
3785 }
3786
3787 fs_info->uuid_root = uuid_root;
3788
Stefan Behrens803b2f52013-08-15 17:11:21 +02003789 ret = btrfs_commit_transaction(trans, tree_root);
3790 if (ret)
3791 return ret;
3792
3793 down(&fs_info->uuid_tree_rescan_sem);
3794 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3795 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02003796 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003797 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02003798 up(&fs_info->uuid_tree_rescan_sem);
3799 return PTR_ERR(task);
3800 }
3801
3802 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003803}
Stefan Behrens803b2f52013-08-15 17:11:21 +02003804
Stefan Behrens70f80172013-08-15 17:11:23 +02003805int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3806{
3807 struct task_struct *task;
3808
3809 down(&fs_info->uuid_tree_rescan_sem);
3810 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3811 if (IS_ERR(task)) {
3812 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003813 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02003814 up(&fs_info->uuid_tree_rescan_sem);
3815 return PTR_ERR(task);
3816 }
3817
3818 return 0;
3819}
3820
Chris Mason8f18cf12008-04-25 16:53:30 -04003821/*
3822 * shrinking a device means finding all of the device extents past
3823 * the new size, and then following the back refs to the chunks.
3824 * The chunk relocation code actually frees the device extent
3825 */
3826int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3827{
3828 struct btrfs_trans_handle *trans;
3829 struct btrfs_root *root = device->dev_root;
3830 struct btrfs_dev_extent *dev_extent = NULL;
3831 struct btrfs_path *path;
3832 u64 length;
3833 u64 chunk_tree;
3834 u64 chunk_objectid;
3835 u64 chunk_offset;
3836 int ret;
3837 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003838 int failed = 0;
3839 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003840 struct extent_buffer *l;
3841 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003842 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003843 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003844 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003845 u64 diff = device->total_bytes - new_size;
3846
Stefan Behrens63a212a2012-11-05 18:29:28 +01003847 if (device->is_tgtdev_for_dev_replace)
3848 return -EINVAL;
3849
Chris Mason8f18cf12008-04-25 16:53:30 -04003850 path = btrfs_alloc_path();
3851 if (!path)
3852 return -ENOMEM;
3853
Chris Mason8f18cf12008-04-25 16:53:30 -04003854 path->reada = 2;
3855
Chris Mason7d9eb122008-07-08 14:19:17 -04003856 lock_chunks(root);
3857
Chris Mason8f18cf12008-04-25 16:53:30 -04003858 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003859 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003860 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003861 spin_lock(&root->fs_info->free_chunk_lock);
3862 root->fs_info->free_chunk_space -= diff;
3863 spin_unlock(&root->fs_info->free_chunk_lock);
3864 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003865 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003866
Josef Bacikba1bf482009-09-11 16:11:19 -04003867again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003868 key.objectid = device->devid;
3869 key.offset = (u64)-1;
3870 key.type = BTRFS_DEV_EXTENT_KEY;
3871
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003872 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003873 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3874 if (ret < 0)
3875 goto done;
3876
3877 ret = btrfs_previous_item(root, path, 0, key.type);
3878 if (ret < 0)
3879 goto done;
3880 if (ret) {
3881 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003882 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003883 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003884 }
3885
3886 l = path->nodes[0];
3887 slot = path->slots[0];
3888 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3889
Josef Bacikba1bf482009-09-11 16:11:19 -04003890 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003891 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003892 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003893 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003894
3895 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3896 length = btrfs_dev_extent_length(l, dev_extent);
3897
Josef Bacikba1bf482009-09-11 16:11:19 -04003898 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003899 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003900 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003901 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003902
3903 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3904 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3905 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003906 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003907
3908 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3909 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003910 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003911 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003912 if (ret == -ENOSPC)
3913 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003914 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003915
3916 if (failed && !retried) {
3917 failed = 0;
3918 retried = true;
3919 goto again;
3920 } else if (failed && retried) {
3921 ret = -ENOSPC;
3922 lock_chunks(root);
3923
3924 device->total_bytes = old_size;
3925 if (device->writeable)
3926 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003927 spin_lock(&root->fs_info->free_chunk_lock);
3928 root->fs_info->free_chunk_space += diff;
3929 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003930 unlock_chunks(root);
3931 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003932 }
3933
Chris Balld6397ba2009-04-27 07:29:03 -04003934 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003935 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003936 if (IS_ERR(trans)) {
3937 ret = PTR_ERR(trans);
3938 goto done;
3939 }
3940
Chris Balld6397ba2009-04-27 07:29:03 -04003941 lock_chunks(root);
3942
3943 device->disk_total_bytes = new_size;
3944 /* Now btrfs_update_device() will change the on-disk size. */
3945 ret = btrfs_update_device(trans, device);
3946 if (ret) {
3947 unlock_chunks(root);
3948 btrfs_end_transaction(trans, root);
3949 goto done;
3950 }
3951 WARN_ON(diff > old_total);
3952 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3953 unlock_chunks(root);
3954 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003955done:
3956 btrfs_free_path(path);
3957 return ret;
3958}
3959
Li Zefan125ccb02011-12-08 15:07:24 +08003960static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003961 struct btrfs_key *key,
3962 struct btrfs_chunk *chunk, int item_size)
3963{
David Sterba6c417612011-04-13 15:41:04 +02003964 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003965 struct btrfs_disk_key disk_key;
3966 u32 array_size;
3967 u8 *ptr;
3968
3969 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08003970 if (array_size + item_size + sizeof(disk_key)
3971 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
Chris Mason0b86a832008-03-24 15:01:56 -04003972 return -EFBIG;
3973
3974 ptr = super_copy->sys_chunk_array + array_size;
3975 btrfs_cpu_key_to_disk(&disk_key, key);
3976 memcpy(ptr, &disk_key, sizeof(disk_key));
3977 ptr += sizeof(disk_key);
3978 memcpy(ptr, chunk, item_size);
3979 item_size += sizeof(disk_key);
3980 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3981 return 0;
3982}
3983
Miao Xieb2117a32011-01-05 10:07:28 +00003984/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003985 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003986 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003987static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003988{
Arne Jansen73c5de02011-04-12 12:07:57 +02003989 const struct btrfs_device_info *di_a = a;
3990 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003991
Arne Jansen73c5de02011-04-12 12:07:57 +02003992 if (di_a->max_avail > di_b->max_avail)
3993 return -1;
3994 if (di_a->max_avail < di_b->max_avail)
3995 return 1;
3996 if (di_a->total_avail > di_b->total_avail)
3997 return -1;
3998 if (di_a->total_avail < di_b->total_avail)
3999 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004000 return 0;
4001}
4002
Eric Sandeen48a3b632013-04-25 20:41:01 +00004003static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00004004 [BTRFS_RAID_RAID10] = {
4005 .sub_stripes = 2,
4006 .dev_stripes = 1,
4007 .devs_max = 0, /* 0 == as many as possible */
4008 .devs_min = 4,
4009 .devs_increment = 2,
4010 .ncopies = 2,
4011 },
4012 [BTRFS_RAID_RAID1] = {
4013 .sub_stripes = 1,
4014 .dev_stripes = 1,
4015 .devs_max = 2,
4016 .devs_min = 2,
4017 .devs_increment = 2,
4018 .ncopies = 2,
4019 },
4020 [BTRFS_RAID_DUP] = {
4021 .sub_stripes = 1,
4022 .dev_stripes = 2,
4023 .devs_max = 1,
4024 .devs_min = 1,
4025 .devs_increment = 1,
4026 .ncopies = 2,
4027 },
4028 [BTRFS_RAID_RAID0] = {
4029 .sub_stripes = 1,
4030 .dev_stripes = 1,
4031 .devs_max = 0,
4032 .devs_min = 2,
4033 .devs_increment = 1,
4034 .ncopies = 1,
4035 },
4036 [BTRFS_RAID_SINGLE] = {
4037 .sub_stripes = 1,
4038 .dev_stripes = 1,
4039 .devs_max = 1,
4040 .devs_min = 1,
4041 .devs_increment = 1,
4042 .ncopies = 1,
4043 },
Chris Masone942f882013-02-20 14:06:05 -05004044 [BTRFS_RAID_RAID5] = {
4045 .sub_stripes = 1,
4046 .dev_stripes = 1,
4047 .devs_max = 0,
4048 .devs_min = 2,
4049 .devs_increment = 1,
4050 .ncopies = 2,
4051 },
4052 [BTRFS_RAID_RAID6] = {
4053 .sub_stripes = 1,
4054 .dev_stripes = 1,
4055 .devs_max = 0,
4056 .devs_min = 3,
4057 .devs_increment = 1,
4058 .ncopies = 3,
4059 },
Liu Bo31e50222012-11-21 14:18:10 +00004060};
4061
David Woodhouse53b381b2013-01-29 18:40:14 -05004062static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4063{
4064 /* TODO allow them to set a preferred stripe size */
4065 return 64 * 1024;
4066}
4067
4068static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4069{
David Woodhouse53b381b2013-01-29 18:40:14 -05004070 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
4071 return;
4072
Miao Xieceda0862013-04-11 10:30:16 +00004073 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004074}
4075
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004076#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4077 - sizeof(struct btrfs_item) \
4078 - sizeof(struct btrfs_chunk)) \
4079 / sizeof(struct btrfs_stripe) + 1)
4080
4081#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4082 - 2 * sizeof(struct btrfs_disk_key) \
4083 - 2 * sizeof(struct btrfs_chunk)) \
4084 / sizeof(struct btrfs_stripe) + 1)
4085
Miao Xieb2117a32011-01-05 10:07:28 +00004086static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004087 struct btrfs_root *extent_root, u64 start,
4088 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004089{
4090 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004091 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4092 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004093 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004094 struct extent_map_tree *em_tree;
4095 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004096 struct btrfs_device_info *devices_info = NULL;
4097 u64 total_avail;
4098 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004099 int data_stripes; /* number of stripes that count for
4100 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004101 int sub_stripes; /* sub_stripes info for map */
4102 int dev_stripes; /* stripes per dev */
4103 int devs_max; /* max devs to use */
4104 int devs_min; /* min devs needed */
4105 int devs_increment; /* ndevs has to be a multiple of this */
4106 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004107 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004108 u64 max_stripe_size;
4109 u64 max_chunk_size;
4110 u64 stripe_size;
4111 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004112 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004113 int ndevs;
4114 int i;
4115 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004116 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004117
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004118 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004119
Miao Xieb2117a32011-01-05 10:07:28 +00004120 if (list_empty(&fs_devices->alloc_list))
4121 return -ENOSPC;
4122
Liu Bo31e50222012-11-21 14:18:10 +00004123 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004124
Liu Bo31e50222012-11-21 14:18:10 +00004125 sub_stripes = btrfs_raid_array[index].sub_stripes;
4126 dev_stripes = btrfs_raid_array[index].dev_stripes;
4127 devs_max = btrfs_raid_array[index].devs_max;
4128 devs_min = btrfs_raid_array[index].devs_min;
4129 devs_increment = btrfs_raid_array[index].devs_increment;
4130 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004131
4132 if (type & BTRFS_BLOCK_GROUP_DATA) {
4133 max_stripe_size = 1024 * 1024 * 1024;
4134 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004135 if (!devs_max)
4136 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004137 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004138 /* for larger filesystems, use larger metadata chunks */
4139 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4140 max_stripe_size = 1024 * 1024 * 1024;
4141 else
4142 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004143 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004144 if (!devs_max)
4145 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004146 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004147 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004148 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004149 if (!devs_max)
4150 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004151 } else {
David Sterba351fd352014-05-15 16:48:20 +02004152 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004153 type);
4154 BUG_ON(1);
4155 }
4156
4157 /* we don't want a chunk larger than 10% of writeable space */
4158 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4159 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004160
4161 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
4162 GFP_NOFS);
4163 if (!devices_info)
4164 return -ENOMEM;
4165
Arne Jansen73c5de02011-04-12 12:07:57 +02004166 cur = fs_devices->alloc_list.next;
4167
4168 /*
4169 * in the first pass through the devices list, we gather information
4170 * about the available holes on each device.
4171 */
4172 ndevs = 0;
4173 while (cur != &fs_devices->alloc_list) {
4174 struct btrfs_device *device;
4175 u64 max_avail;
4176 u64 dev_offset;
4177
4178 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4179
4180 cur = cur->next;
4181
4182 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004183 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004184 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004185 continue;
4186 }
4187
Stefan Behrens63a212a2012-11-05 18:29:28 +01004188 if (!device->in_fs_metadata ||
4189 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004190 continue;
4191
4192 if (device->total_bytes > device->bytes_used)
4193 total_avail = device->total_bytes - device->bytes_used;
4194 else
4195 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004196
4197 /* If there is no space on this device, skip it. */
4198 if (total_avail == 0)
4199 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004200
Josef Bacik6df9a952013-06-27 13:22:46 -04004201 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004202 max_stripe_size * dev_stripes,
4203 &dev_offset, &max_avail);
4204 if (ret && ret != -ENOSPC)
4205 goto error;
4206
4207 if (ret == 0)
4208 max_avail = max_stripe_size * dev_stripes;
4209
4210 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4211 continue;
4212
Eric Sandeen063d0062013-01-31 00:55:01 +00004213 if (ndevs == fs_devices->rw_devices) {
4214 WARN(1, "%s: found more than %llu devices\n",
4215 __func__, fs_devices->rw_devices);
4216 break;
4217 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004218 devices_info[ndevs].dev_offset = dev_offset;
4219 devices_info[ndevs].max_avail = max_avail;
4220 devices_info[ndevs].total_avail = total_avail;
4221 devices_info[ndevs].dev = device;
4222 ++ndevs;
4223 }
4224
4225 /*
4226 * now sort the devices by hole size / available space
4227 */
4228 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4229 btrfs_cmp_device_info, NULL);
4230
4231 /* round down to number of usable stripes */
4232 ndevs -= ndevs % devs_increment;
4233
4234 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4235 ret = -ENOSPC;
4236 goto error;
4237 }
4238
4239 if (devs_max && ndevs > devs_max)
4240 ndevs = devs_max;
4241 /*
4242 * the primary goal is to maximize the number of stripes, so use as many
4243 * devices as possible, even if the stripes are not maximum sized.
4244 */
4245 stripe_size = devices_info[ndevs-1].max_avail;
4246 num_stripes = ndevs * dev_stripes;
4247
David Woodhouse53b381b2013-01-29 18:40:14 -05004248 /*
4249 * this will have to be fixed for RAID1 and RAID10 over
4250 * more drives
4251 */
4252 data_stripes = num_stripes / ncopies;
4253
David Woodhouse53b381b2013-01-29 18:40:14 -05004254 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4255 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4256 btrfs_super_stripesize(info->super_copy));
4257 data_stripes = num_stripes - 1;
4258 }
4259 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4260 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4261 btrfs_super_stripesize(info->super_copy));
4262 data_stripes = num_stripes - 2;
4263 }
Chris Mason86db2572013-02-20 16:23:40 -05004264
4265 /*
4266 * Use the number of data stripes to figure out how big this chunk
4267 * is really going to be in terms of logical address space,
4268 * and compare that answer with the max chunk size
4269 */
4270 if (stripe_size * data_stripes > max_chunk_size) {
4271 u64 mask = (1ULL << 24) - 1;
4272 stripe_size = max_chunk_size;
4273 do_div(stripe_size, data_stripes);
4274
4275 /* bump the answer up to a 16MB boundary */
4276 stripe_size = (stripe_size + mask) & ~mask;
4277
4278 /* but don't go higher than the limits we found
4279 * while searching for free extents
4280 */
4281 if (stripe_size > devices_info[ndevs-1].max_avail)
4282 stripe_size = devices_info[ndevs-1].max_avail;
4283 }
4284
Arne Jansen73c5de02011-04-12 12:07:57 +02004285 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004286
4287 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05004288 do_div(stripe_size, raid_stripe_len);
4289 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004290
Miao Xieb2117a32011-01-05 10:07:28 +00004291 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4292 if (!map) {
4293 ret = -ENOMEM;
4294 goto error;
4295 }
4296 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004297
Arne Jansen73c5de02011-04-12 12:07:57 +02004298 for (i = 0; i < ndevs; ++i) {
4299 for (j = 0; j < dev_stripes; ++j) {
4300 int s = i * dev_stripes + j;
4301 map->stripes[s].dev = devices_info[i].dev;
4302 map->stripes[s].physical = devices_info[i].dev_offset +
4303 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004304 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004305 }
Chris Mason593060d2008-03-25 16:50:33 -04004306 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004307 map->stripe_len = raid_stripe_len;
4308 map->io_align = raid_stripe_len;
4309 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004310 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004311 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004312
David Woodhouse53b381b2013-01-29 18:40:14 -05004313 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004314
Arne Jansen73c5de02011-04-12 12:07:57 +02004315 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004316
David Sterba172ddd62011-04-21 00:48:27 +02004317 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004318 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004319 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004320 ret = -ENOMEM;
4321 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004322 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004323 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004324 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004325 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004326 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004327 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004328 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004329 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004330
Chris Mason0b86a832008-03-24 15:01:56 -04004331 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004332 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004333 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004334 if (!ret) {
4335 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4336 atomic_inc(&em->refs);
4337 }
Chris Mason890871b2009-09-02 16:24:52 -04004338 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004339 if (ret) {
4340 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004341 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004342 }
Yan Zheng2b820322008-11-17 21:11:30 -05004343
Josef Bacik04487482013-01-29 15:03:37 -05004344 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4345 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4346 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004347 if (ret)
4348 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004349
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004350 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004351 check_raid56_incompat_flag(extent_root->fs_info, type);
4352
Miao Xieb2117a32011-01-05 10:07:28 +00004353 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004354 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004355
Josef Bacik6df9a952013-06-27 13:22:46 -04004356error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004357 write_lock(&em_tree->lock);
4358 remove_extent_mapping(em_tree, em);
4359 write_unlock(&em_tree->lock);
4360
4361 /* One for our allocation */
4362 free_extent_map(em);
4363 /* One for the tree reference */
4364 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004365error:
Miao Xieb2117a32011-01-05 10:07:28 +00004366 kfree(devices_info);
4367 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004368}
4369
Josef Bacik6df9a952013-06-27 13:22:46 -04004370int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004371 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004372 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004373{
Yan Zheng2b820322008-11-17 21:11:30 -05004374 struct btrfs_key key;
4375 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4376 struct btrfs_device *device;
4377 struct btrfs_chunk *chunk;
4378 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004379 struct extent_map_tree *em_tree;
4380 struct extent_map *em;
4381 struct map_lookup *map;
4382 size_t item_size;
4383 u64 dev_offset;
4384 u64 stripe_size;
4385 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004386 int ret;
4387
Josef Bacik6df9a952013-06-27 13:22:46 -04004388 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4389 read_lock(&em_tree->lock);
4390 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4391 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004392
Josef Bacik6df9a952013-06-27 13:22:46 -04004393 if (!em) {
4394 btrfs_crit(extent_root->fs_info, "unable to find logical "
4395 "%Lu len %Lu", chunk_offset, chunk_size);
4396 return -EINVAL;
4397 }
4398
4399 if (em->start != chunk_offset || em->len != chunk_size) {
4400 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004401 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004402 chunk_size, em->start, em->len);
4403 free_extent_map(em);
4404 return -EINVAL;
4405 }
4406
4407 map = (struct map_lookup *)em->bdev;
4408 item_size = btrfs_chunk_item_size(map->num_stripes);
4409 stripe_size = em->orig_block_len;
4410
4411 chunk = kzalloc(item_size, GFP_NOFS);
4412 if (!chunk) {
4413 ret = -ENOMEM;
4414 goto out;
4415 }
4416
4417 for (i = 0; i < map->num_stripes; i++) {
4418 device = map->stripes[i].dev;
4419 dev_offset = map->stripes[i].physical;
4420
Yan Zheng2b820322008-11-17 21:11:30 -05004421 device->bytes_used += stripe_size;
4422 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004423 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004424 goto out;
4425 ret = btrfs_alloc_dev_extent(trans, device,
4426 chunk_root->root_key.objectid,
4427 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4428 chunk_offset, dev_offset,
4429 stripe_size);
4430 if (ret)
4431 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004432 }
4433
Josef Bacik2bf64752011-09-26 17:12:22 -04004434 spin_lock(&extent_root->fs_info->free_chunk_lock);
4435 extent_root->fs_info->free_chunk_space -= (stripe_size *
4436 map->num_stripes);
4437 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4438
Yan Zheng2b820322008-11-17 21:11:30 -05004439 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004440 for (i = 0; i < map->num_stripes; i++) {
4441 device = map->stripes[i].dev;
4442 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004443
4444 btrfs_set_stack_stripe_devid(stripe, device->devid);
4445 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4446 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4447 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004448 }
4449
4450 btrfs_set_stack_chunk_length(chunk, chunk_size);
4451 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4452 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4453 btrfs_set_stack_chunk_type(chunk, map->type);
4454 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4455 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4456 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4457 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4458 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4459
4460 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4461 key.type = BTRFS_CHUNK_ITEM_KEY;
4462 key.offset = chunk_offset;
4463
4464 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004465 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4466 /*
4467 * TODO: Cleanup of inserted chunk root in case of
4468 * failure.
4469 */
Li Zefan125ccb02011-12-08 15:07:24 +08004470 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004471 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004472 }
liubo1abe9b82011-03-24 11:18:59 +00004473
Josef Bacik6df9a952013-06-27 13:22:46 -04004474out:
Yan Zheng2b820322008-11-17 21:11:30 -05004475 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004476 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004477 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004478}
4479
4480/*
4481 * Chunk allocation falls into two parts. The first part does works
4482 * that make the new allocated chunk useable, but not do any operation
4483 * that modifies the chunk tree. The second part does the works that
4484 * require modifying the chunk tree. This division is important for the
4485 * bootstrap process of adding storage to a seed btrfs.
4486 */
4487int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4488 struct btrfs_root *extent_root, u64 type)
4489{
4490 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004491
Josef Bacik6df9a952013-06-27 13:22:46 -04004492 chunk_offset = find_next_chunk(extent_root->fs_info);
4493 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004494}
4495
Chris Masond3977122009-01-05 21:25:51 -05004496static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004497 struct btrfs_root *root,
4498 struct btrfs_device *device)
4499{
4500 u64 chunk_offset;
4501 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004502 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004503 struct btrfs_fs_info *fs_info = root->fs_info;
4504 struct btrfs_root *extent_root = fs_info->extent_root;
4505 int ret;
4506
Josef Bacik6df9a952013-06-27 13:22:46 -04004507 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004508 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004509 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4510 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004511 if (ret)
4512 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004513
Josef Bacik6df9a952013-06-27 13:22:46 -04004514 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004515 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004516 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4517 alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06004518 if (ret) {
4519 btrfs_abort_transaction(trans, root, ret);
4520 goto out;
4521 }
Yan Zheng2b820322008-11-17 21:11:30 -05004522
4523 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004524 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004525 btrfs_abort_transaction(trans, root, ret);
David Sterba005d6422012-09-18 07:52:32 -06004526out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004527 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004528}
4529
4530int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4531{
4532 struct extent_map *em;
4533 struct map_lookup *map;
4534 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4535 int readonly = 0;
4536 int i;
4537
Chris Mason890871b2009-09-02 16:24:52 -04004538 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004539 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004540 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004541 if (!em)
4542 return 1;
4543
Josef Bacikf48b9072010-01-27 02:07:59 +00004544 if (btrfs_test_opt(root, DEGRADED)) {
4545 free_extent_map(em);
4546 return 0;
4547 }
4548
Yan Zheng2b820322008-11-17 21:11:30 -05004549 map = (struct map_lookup *)em->bdev;
4550 for (i = 0; i < map->num_stripes; i++) {
4551 if (!map->stripes[i].dev->writeable) {
4552 readonly = 1;
4553 break;
4554 }
4555 }
4556 free_extent_map(em);
4557 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004558}
4559
4560void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4561{
David Sterbaa8067e02011-04-21 00:34:43 +02004562 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004563}
4564
4565void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4566{
4567 struct extent_map *em;
4568
Chris Masond3977122009-01-05 21:25:51 -05004569 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004570 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004571 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4572 if (em)
4573 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004574 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004575 if (!em)
4576 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004577 /* once for us */
4578 free_extent_map(em);
4579 /* once for the tree */
4580 free_extent_map(em);
4581 }
4582}
4583
Stefan Behrens5d964052012-11-05 14:59:07 +01004584int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004585{
Stefan Behrens5d964052012-11-05 14:59:07 +01004586 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004587 struct extent_map *em;
4588 struct map_lookup *map;
4589 struct extent_map_tree *em_tree = &map_tree->map_tree;
4590 int ret;
4591
Chris Mason890871b2009-09-02 16:24:52 -04004592 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004593 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004594 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004595
Josef Bacikfb7669b2013-04-23 10:53:18 -04004596 /*
4597 * We could return errors for these cases, but that could get ugly and
4598 * we'd probably do the same thing which is just not do anything else
4599 * and exit, so return 1 so the callers don't try to use other copies.
4600 */
4601 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004602 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004603 logical+len);
4604 return 1;
4605 }
4606
4607 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004608 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004609 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004610 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004611 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004612 return 1;
4613 }
4614
Chris Masonf1885912008-04-09 16:28:12 -04004615 map = (struct map_lookup *)em->bdev;
4616 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4617 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004618 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4619 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004620 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4621 ret = 2;
4622 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4623 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004624 else
4625 ret = 1;
4626 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004627
4628 btrfs_dev_replace_lock(&fs_info->dev_replace);
4629 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4630 ret++;
4631 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4632
Chris Masonf1885912008-04-09 16:28:12 -04004633 return ret;
4634}
4635
David Woodhouse53b381b2013-01-29 18:40:14 -05004636unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4637 struct btrfs_mapping_tree *map_tree,
4638 u64 logical)
4639{
4640 struct extent_map *em;
4641 struct map_lookup *map;
4642 struct extent_map_tree *em_tree = &map_tree->map_tree;
4643 unsigned long len = root->sectorsize;
4644
4645 read_lock(&em_tree->lock);
4646 em = lookup_extent_mapping(em_tree, logical, len);
4647 read_unlock(&em_tree->lock);
4648 BUG_ON(!em);
4649
4650 BUG_ON(em->start > logical || em->start + em->len < logical);
4651 map = (struct map_lookup *)em->bdev;
4652 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4653 BTRFS_BLOCK_GROUP_RAID6)) {
4654 len = map->stripe_len * nr_data_stripes(map);
4655 }
4656 free_extent_map(em);
4657 return len;
4658}
4659
4660int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4661 u64 logical, u64 len, int mirror_num)
4662{
4663 struct extent_map *em;
4664 struct map_lookup *map;
4665 struct extent_map_tree *em_tree = &map_tree->map_tree;
4666 int ret = 0;
4667
4668 read_lock(&em_tree->lock);
4669 em = lookup_extent_mapping(em_tree, logical, len);
4670 read_unlock(&em_tree->lock);
4671 BUG_ON(!em);
4672
4673 BUG_ON(em->start > logical || em->start + em->len < logical);
4674 map = (struct map_lookup *)em->bdev;
4675 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4676 BTRFS_BLOCK_GROUP_RAID6))
4677 ret = 1;
4678 free_extent_map(em);
4679 return ret;
4680}
4681
Stefan Behrens30d98612012-11-06 14:52:18 +01004682static int find_live_mirror(struct btrfs_fs_info *fs_info,
4683 struct map_lookup *map, int first, int num,
4684 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004685{
4686 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004687 int tolerance;
4688 struct btrfs_device *srcdev;
4689
4690 if (dev_replace_is_ongoing &&
4691 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4692 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4693 srcdev = fs_info->dev_replace.srcdev;
4694 else
4695 srcdev = NULL;
4696
4697 /*
4698 * try to avoid the drive that is the source drive for a
4699 * dev-replace procedure, only choose it if no other non-missing
4700 * mirror is available
4701 */
4702 for (tolerance = 0; tolerance < 2; tolerance++) {
4703 if (map->stripes[optimal].dev->bdev &&
4704 (tolerance || map->stripes[optimal].dev != srcdev))
4705 return optimal;
4706 for (i = first; i < first + num; i++) {
4707 if (map->stripes[i].dev->bdev &&
4708 (tolerance || map->stripes[i].dev != srcdev))
4709 return i;
4710 }
Chris Masondfe25022008-05-13 13:46:40 -04004711 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004712
Chris Masondfe25022008-05-13 13:46:40 -04004713 /* we couldn't find one that doesn't fail. Just return something
4714 * and the io error handling code will clean up eventually
4715 */
4716 return optimal;
4717}
4718
David Woodhouse53b381b2013-01-29 18:40:14 -05004719static inline int parity_smaller(u64 a, u64 b)
4720{
4721 return a > b;
4722}
4723
4724/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4725static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4726{
4727 struct btrfs_bio_stripe s;
4728 int i;
4729 u64 l;
4730 int again = 1;
4731
4732 while (again) {
4733 again = 0;
4734 for (i = 0; i < bbio->num_stripes - 1; i++) {
4735 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4736 s = bbio->stripes[i];
4737 l = raid_map[i];
4738 bbio->stripes[i] = bbio->stripes[i+1];
4739 raid_map[i] = raid_map[i+1];
4740 bbio->stripes[i+1] = s;
4741 raid_map[i+1] = l;
4742 again = 1;
4743 }
4744 }
4745 }
4746}
4747
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004748static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004749 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004750 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004751 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004752{
4753 struct extent_map *em;
4754 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004755 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004756 struct extent_map_tree *em_tree = &map_tree->map_tree;
4757 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004758 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004759 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004760 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004761 u64 stripe_nr_orig;
4762 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004763 u64 stripe_len;
4764 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004765 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004766 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004767 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004768 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004769 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004770 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004771 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4772 int dev_replace_is_ongoing = 0;
4773 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004774 int patch_the_first_stripe_for_dev_replace = 0;
4775 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004776 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004777
Chris Mason890871b2009-09-02 16:24:52 -04004778 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004779 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004780 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004781
Chris Mason3b951512008-04-17 11:29:12 -04004782 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004783 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004784 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004785 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004786 }
Chris Mason0b86a832008-03-24 15:01:56 -04004787
Josef Bacik9bb91872013-04-19 23:45:33 -04004788 if (em->start > logical || em->start + em->len < logical) {
4789 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02004790 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04004791 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004792 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04004793 return -EINVAL;
4794 }
4795
Chris Mason0b86a832008-03-24 15:01:56 -04004796 map = (struct map_lookup *)em->bdev;
4797 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004798
David Woodhouse53b381b2013-01-29 18:40:14 -05004799 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004800 stripe_nr = offset;
4801 /*
4802 * stripe_nr counts the total number of stripes we have to stride
4803 * to get to this block
4804 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004805 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004806
David Woodhouse53b381b2013-01-29 18:40:14 -05004807 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004808 BUG_ON(offset < stripe_offset);
4809
4810 /* stripe_offset is the offset of this block in its stripe*/
4811 stripe_offset = offset - stripe_offset;
4812
David Woodhouse53b381b2013-01-29 18:40:14 -05004813 /* if we're here for raid56, we need to know the stripe aligned start */
4814 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4815 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4816 raid56_full_stripe_start = offset;
4817
4818 /* allow a write of a full stripe, but make sure we don't
4819 * allow straddling of stripes
4820 */
4821 do_div(raid56_full_stripe_start, full_stripe_len);
4822 raid56_full_stripe_start *= full_stripe_len;
4823 }
4824
4825 if (rw & REQ_DISCARD) {
4826 /* we don't discard raid56 yet */
4827 if (map->type &
4828 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4829 ret = -EOPNOTSUPP;
4830 goto out;
4831 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004832 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004833 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4834 u64 max_len;
4835 /* For writes to RAID[56], allow a full stripeset across all disks.
4836 For other RAID types and for RAID[56] reads, just allow a single
4837 stripe (on a single disk). */
4838 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4839 (rw & REQ_WRITE)) {
4840 max_len = stripe_len * nr_data_stripes(map) -
4841 (offset - raid56_full_stripe_start);
4842 } else {
4843 /* we limit the length of each bio to what fits in a stripe */
4844 max_len = stripe_len - stripe_offset;
4845 }
4846 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004847 } else {
4848 *length = em->len - offset;
4849 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004850
David Woodhouse53b381b2013-01-29 18:40:14 -05004851 /* This is for when we're called from btrfs_merge_bio_hook() and all
4852 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004853 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004854 goto out;
4855
Stefan Behrens472262f2012-11-06 14:43:46 +01004856 btrfs_dev_replace_lock(dev_replace);
4857 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4858 if (!dev_replace_is_ongoing)
4859 btrfs_dev_replace_unlock(dev_replace);
4860
Stefan Behrensad6d6202012-11-06 15:06:47 +01004861 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4862 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4863 dev_replace->tgtdev != NULL) {
4864 /*
4865 * in dev-replace case, for repair case (that's the only
4866 * case where the mirror is selected explicitly when
4867 * calling btrfs_map_block), blocks left of the left cursor
4868 * can also be read from the target drive.
4869 * For REQ_GET_READ_MIRRORS, the target drive is added as
4870 * the last one to the array of stripes. For READ, it also
4871 * needs to be supported using the same mirror number.
4872 * If the requested block is not left of the left cursor,
4873 * EIO is returned. This can happen because btrfs_num_copies()
4874 * returns one more in the dev-replace case.
4875 */
4876 u64 tmp_length = *length;
4877 struct btrfs_bio *tmp_bbio = NULL;
4878 int tmp_num_stripes;
4879 u64 srcdev_devid = dev_replace->srcdev->devid;
4880 int index_srcdev = 0;
4881 int found = 0;
4882 u64 physical_of_found = 0;
4883
4884 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05004885 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004886 if (ret) {
4887 WARN_ON(tmp_bbio != NULL);
4888 goto out;
4889 }
4890
4891 tmp_num_stripes = tmp_bbio->num_stripes;
4892 if (mirror_num > tmp_num_stripes) {
4893 /*
4894 * REQ_GET_READ_MIRRORS does not contain this
4895 * mirror, that means that the requested area
4896 * is not left of the left cursor
4897 */
4898 ret = -EIO;
4899 kfree(tmp_bbio);
4900 goto out;
4901 }
4902
4903 /*
4904 * process the rest of the function using the mirror_num
4905 * of the source drive. Therefore look it up first.
4906 * At the end, patch the device pointer to the one of the
4907 * target drive.
4908 */
4909 for (i = 0; i < tmp_num_stripes; i++) {
4910 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4911 /*
4912 * In case of DUP, in order to keep it
4913 * simple, only add the mirror with the
4914 * lowest physical address
4915 */
4916 if (found &&
4917 physical_of_found <=
4918 tmp_bbio->stripes[i].physical)
4919 continue;
4920 index_srcdev = i;
4921 found = 1;
4922 physical_of_found =
4923 tmp_bbio->stripes[i].physical;
4924 }
4925 }
4926
4927 if (found) {
4928 mirror_num = index_srcdev + 1;
4929 patch_the_first_stripe_for_dev_replace = 1;
4930 physical_to_patch_in_first_stripe = physical_of_found;
4931 } else {
4932 WARN_ON(1);
4933 ret = -EIO;
4934 kfree(tmp_bbio);
4935 goto out;
4936 }
4937
4938 kfree(tmp_bbio);
4939 } else if (mirror_num > map->num_stripes) {
4940 mirror_num = 0;
4941 }
4942
Chris Masonf2d8d742008-04-21 10:03:05 -04004943 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04004944 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004945 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00004946 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00004947 do_div(stripe_nr_end, map->stripe_len);
4948 stripe_end_offset = stripe_nr_end * map->stripe_len -
4949 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004950
Li Dongyangfce3bb92011-03-24 10:24:26 +00004951 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4952 if (rw & REQ_DISCARD)
4953 num_stripes = min_t(u64, map->num_stripes,
4954 stripe_nr_end - stripe_nr_orig);
4955 stripe_index = do_div(stripe_nr, map->num_stripes);
4956 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004957 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004958 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04004959 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04004960 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004961 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01004962 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04004963 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01004964 current->pid % map->num_stripes,
4965 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004966 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004967 }
Chris Mason2fff7342008-04-29 14:12:09 -04004968
Chris Mason611f0e02008-04-03 16:29:03 -04004969 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004970 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04004971 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004972 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04004973 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004974 } else {
4975 mirror_num = 1;
4976 }
Chris Mason2fff7342008-04-29 14:12:09 -04004977
Chris Mason321aecc2008-04-16 10:49:51 -04004978 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4979 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004980
4981 stripe_index = do_div(stripe_nr, factor);
4982 stripe_index *= map->sub_stripes;
4983
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004984 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004985 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004986 else if (rw & REQ_DISCARD)
4987 num_stripes = min_t(u64, map->sub_stripes *
4988 (stripe_nr_end - stripe_nr_orig),
4989 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04004990 else if (mirror_num)
4991 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004992 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04004993 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01004994 stripe_index = find_live_mirror(fs_info, map,
4995 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04004996 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01004997 current->pid % map->sub_stripes,
4998 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04004999 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005000 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005001
5002 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5003 BTRFS_BLOCK_GROUP_RAID6)) {
5004 u64 tmp;
5005
5006 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
5007 && raid_map_ret) {
5008 int i, rot;
5009
5010 /* push stripe_nr back to the start of the full stripe */
5011 stripe_nr = raid56_full_stripe_start;
5012 do_div(stripe_nr, stripe_len);
5013
5014 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5015
5016 /* RAID[56] write or recovery. Return all stripes */
5017 num_stripes = map->num_stripes;
5018 max_errors = nr_parity_stripes(map);
5019
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05305020 raid_map = kmalloc_array(num_stripes, sizeof(u64),
David Woodhouse53b381b2013-01-29 18:40:14 -05005021 GFP_NOFS);
5022 if (!raid_map) {
5023 ret = -ENOMEM;
5024 goto out;
5025 }
5026
5027 /* Work out the disk rotation on this stripe-set */
5028 tmp = stripe_nr;
5029 rot = do_div(tmp, num_stripes);
5030
5031 /* Fill in the logical address of each stripe */
5032 tmp = stripe_nr * nr_data_stripes(map);
5033 for (i = 0; i < nr_data_stripes(map); i++)
5034 raid_map[(i+rot) % num_stripes] =
5035 em->start + (tmp + i) * map->stripe_len;
5036
5037 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5038 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5039 raid_map[(i+rot+1) % num_stripes] =
5040 RAID6_Q_STRIPE;
5041
5042 *length = map->stripe_len;
5043 stripe_index = 0;
5044 stripe_offset = 0;
5045 } else {
5046 /*
5047 * Mirror #0 or #1 means the original data block.
5048 * Mirror #2 is RAID5 parity block.
5049 * Mirror #3 is RAID6 Q block.
5050 */
5051 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5052 if (mirror_num > 1)
5053 stripe_index = nr_data_stripes(map) +
5054 mirror_num - 2;
5055
5056 /* We distribute the parity blocks across stripes */
5057 tmp = stripe_nr + stripe_index;
5058 stripe_index = do_div(tmp, map->num_stripes);
5059 }
Chris Mason8790d502008-04-03 16:29:03 -04005060 } else {
5061 /*
5062 * after this do_div call, stripe_nr is the number of stripes
5063 * on this device we have to walk to find the data, and
5064 * stripe_index is the number of our device in the stripe array
5065 */
5066 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005067 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005068 }
Chris Mason593060d2008-03-25 16:50:33 -04005069 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005070
Stefan Behrens472262f2012-11-06 14:43:46 +01005071 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005072 if (dev_replace_is_ongoing) {
5073 if (rw & (REQ_WRITE | REQ_DISCARD))
5074 num_alloc_stripes <<= 1;
5075 if (rw & REQ_GET_READ_MIRRORS)
5076 num_alloc_stripes++;
5077 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005078 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08005079 if (!bbio) {
Dave Joneseb2067f2013-07-30 13:42:17 -04005080 kfree(raid_map);
Li Zefande11cc12011-12-01 12:55:47 +08005081 ret = -ENOMEM;
5082 goto out;
5083 }
5084 atomic_set(&bbio->error, 0);
5085
Li Dongyangfce3bb92011-03-24 10:24:26 +00005086 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08005087 int factor = 0;
5088 int sub_stripes = 0;
5089 u64 stripes_per_dev = 0;
5090 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005091 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005092
5093 if (map->type &
5094 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5095 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5096 sub_stripes = 1;
5097 else
5098 sub_stripes = map->sub_stripes;
5099
5100 factor = map->num_stripes / sub_stripes;
5101 stripes_per_dev = div_u64_rem(stripe_nr_end -
5102 stripe_nr_orig,
5103 factor,
5104 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005105 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5106 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005107 }
5108
Li Dongyangfce3bb92011-03-24 10:24:26 +00005109 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005110 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005111 map->stripes[stripe_index].physical +
5112 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005113 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005114
Li Zefanec9ef7a2011-12-01 14:06:42 +08005115 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5116 BTRFS_BLOCK_GROUP_RAID10)) {
5117 bbio->stripes[i].length = stripes_per_dev *
5118 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005119
Li Zefanec9ef7a2011-12-01 14:06:42 +08005120 if (i / sub_stripes < remaining_stripes)
5121 bbio->stripes[i].length +=
5122 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005123
5124 /*
5125 * Special for the first stripe and
5126 * the last stripe:
5127 *
5128 * |-------|...|-------|
5129 * |----------|
5130 * off end_off
5131 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005132 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005133 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005134 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005135
5136 if (stripe_index >= last_stripe &&
5137 stripe_index <= (last_stripe +
5138 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005139 bbio->stripes[i].length -=
5140 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005141
Li Zefanec9ef7a2011-12-01 14:06:42 +08005142 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005143 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005144 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005145 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005146
5147 stripe_index++;
5148 if (stripe_index == map->num_stripes) {
5149 /* This could only happen for RAID0/10 */
5150 stripe_index = 0;
5151 stripe_nr++;
5152 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005153 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005154 } else {
5155 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005156 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005157 map->stripes[stripe_index].physical +
5158 stripe_offset +
5159 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005160 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005161 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005162 stripe_index++;
5163 }
Chris Mason593060d2008-03-25 16:50:33 -04005164 }
Li Zefande11cc12011-12-01 12:55:47 +08005165
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005166 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
Li Zefande11cc12011-12-01 12:55:47 +08005167 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5168 BTRFS_BLOCK_GROUP_RAID10 |
David Woodhouse53b381b2013-01-29 18:40:14 -05005169 BTRFS_BLOCK_GROUP_RAID5 |
Li Zefande11cc12011-12-01 12:55:47 +08005170 BTRFS_BLOCK_GROUP_DUP)) {
5171 max_errors = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005172 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5173 max_errors = 2;
Li Zefande11cc12011-12-01 12:55:47 +08005174 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005175 }
Li Zefande11cc12011-12-01 12:55:47 +08005176
Stefan Behrens472262f2012-11-06 14:43:46 +01005177 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5178 dev_replace->tgtdev != NULL) {
5179 int index_where_to_add;
5180 u64 srcdev_devid = dev_replace->srcdev->devid;
5181
5182 /*
5183 * duplicate the write operations while the dev replace
5184 * procedure is running. Since the copying of the old disk
5185 * to the new disk takes place at run time while the
5186 * filesystem is mounted writable, the regular write
5187 * operations to the old disk have to be duplicated to go
5188 * to the new disk as well.
5189 * Note that device->missing is handled by the caller, and
5190 * that the write to the old disk is already set up in the
5191 * stripes array.
5192 */
5193 index_where_to_add = num_stripes;
5194 for (i = 0; i < num_stripes; i++) {
5195 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5196 /* write to new disk, too */
5197 struct btrfs_bio_stripe *new =
5198 bbio->stripes + index_where_to_add;
5199 struct btrfs_bio_stripe *old =
5200 bbio->stripes + i;
5201
5202 new->physical = old->physical;
5203 new->length = old->length;
5204 new->dev = dev_replace->tgtdev;
5205 index_where_to_add++;
5206 max_errors++;
5207 }
5208 }
5209 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005210 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5211 dev_replace->tgtdev != NULL) {
5212 u64 srcdev_devid = dev_replace->srcdev->devid;
5213 int index_srcdev = 0;
5214 int found = 0;
5215 u64 physical_of_found = 0;
5216
5217 /*
5218 * During the dev-replace procedure, the target drive can
5219 * also be used to read data in case it is needed to repair
5220 * a corrupt block elsewhere. This is possible if the
5221 * requested area is left of the left cursor. In this area,
5222 * the target drive is a full copy of the source drive.
5223 */
5224 for (i = 0; i < num_stripes; i++) {
5225 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5226 /*
5227 * In case of DUP, in order to keep it
5228 * simple, only add the mirror with the
5229 * lowest physical address
5230 */
5231 if (found &&
5232 physical_of_found <=
5233 bbio->stripes[i].physical)
5234 continue;
5235 index_srcdev = i;
5236 found = 1;
5237 physical_of_found = bbio->stripes[i].physical;
5238 }
5239 }
5240 if (found) {
5241 u64 length = map->stripe_len;
5242
5243 if (physical_of_found + length <=
5244 dev_replace->cursor_left) {
5245 struct btrfs_bio_stripe *tgtdev_stripe =
5246 bbio->stripes + num_stripes;
5247
5248 tgtdev_stripe->physical = physical_of_found;
5249 tgtdev_stripe->length =
5250 bbio->stripes[index_srcdev].length;
5251 tgtdev_stripe->dev = dev_replace->tgtdev;
5252
5253 num_stripes++;
5254 }
5255 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005256 }
5257
Li Zefande11cc12011-12-01 12:55:47 +08005258 *bbio_ret = bbio;
5259 bbio->num_stripes = num_stripes;
5260 bbio->max_errors = max_errors;
5261 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005262
5263 /*
5264 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5265 * mirror_num == num_stripes + 1 && dev_replace target drive is
5266 * available as a mirror
5267 */
5268 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5269 WARN_ON(num_stripes > 1);
5270 bbio->stripes[0].dev = dev_replace->tgtdev;
5271 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5272 bbio->mirror_num = map->num_stripes + 1;
5273 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005274 if (raid_map) {
5275 sort_parity_stripes(bbio, raid_map);
5276 *raid_map_ret = raid_map;
5277 }
Chris Masoncea9e442008-04-09 16:28:12 -04005278out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005279 if (dev_replace_is_ongoing)
5280 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005281 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005282 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005283}
5284
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005285int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005286 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005287 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005288{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005289 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05005290 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04005291}
5292
Yan Zhenga512bbf2008-12-08 16:46:26 -05005293int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5294 u64 chunk_start, u64 physical, u64 devid,
5295 u64 **logical, int *naddrs, int *stripe_len)
5296{
5297 struct extent_map_tree *em_tree = &map_tree->map_tree;
5298 struct extent_map *em;
5299 struct map_lookup *map;
5300 u64 *buf;
5301 u64 bytenr;
5302 u64 length;
5303 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005304 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005305 int i, j, nr = 0;
5306
Chris Mason890871b2009-09-02 16:24:52 -04005307 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005308 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005309 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005310
Josef Bacik835d9742013-03-19 12:13:25 -04005311 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005312 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005313 chunk_start);
5314 return -EIO;
5315 }
5316
5317 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005318 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005319 em->start, chunk_start);
5320 free_extent_map(em);
5321 return -EIO;
5322 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005323 map = (struct map_lookup *)em->bdev;
5324
5325 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005326 rmap_len = map->stripe_len;
5327
Yan Zhenga512bbf2008-12-08 16:46:26 -05005328 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5329 do_div(length, map->num_stripes / map->sub_stripes);
5330 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5331 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05005332 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5333 BTRFS_BLOCK_GROUP_RAID6)) {
5334 do_div(length, nr_data_stripes(map));
5335 rmap_len = map->stripe_len * nr_data_stripes(map);
5336 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005337
5338 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005339 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005340
5341 for (i = 0; i < map->num_stripes; i++) {
5342 if (devid && map->stripes[i].dev->devid != devid)
5343 continue;
5344 if (map->stripes[i].physical > physical ||
5345 map->stripes[i].physical + length <= physical)
5346 continue;
5347
5348 stripe_nr = physical - map->stripes[i].physical;
5349 do_div(stripe_nr, map->stripe_len);
5350
5351 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5352 stripe_nr = stripe_nr * map->num_stripes + i;
5353 do_div(stripe_nr, map->sub_stripes);
5354 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5355 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005356 } /* else if RAID[56], multiply by nr_data_stripes().
5357 * Alternatively, just use rmap_len below instead of
5358 * map->stripe_len */
5359
5360 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005361 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005362 for (j = 0; j < nr; j++) {
5363 if (buf[j] == bytenr)
5364 break;
5365 }
Chris Mason934d3752008-12-08 16:43:10 -05005366 if (j == nr) {
5367 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005368 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005369 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005370 }
5371
Yan Zhenga512bbf2008-12-08 16:46:26 -05005372 *logical = buf;
5373 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005374 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005375
5376 free_extent_map(em);
5377 return 0;
5378}
5379
Miao Xie8408c712014-06-19 10:42:55 +08005380static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5381{
5382 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5383 bio_endio_nodec(bio, err);
5384 else
5385 bio_endio(bio, err);
5386 kfree(bbio);
5387}
5388
Jan Schmidta1d3c472011-08-04 17:15:33 +02005389static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005390{
Chris Mason9be33952013-05-17 18:30:14 -04005391 struct btrfs_bio *bbio = bio->bi_private;
Miao Xiec404e0d2014-01-30 16:46:55 +08005392 struct btrfs_device *dev = bbio->stripes[0].dev;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005393 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005394
Stefan Behrens442a4f62012-05-25 16:06:08 +02005395 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005396 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005397 if (err == -EIO || err == -EREMOTEIO) {
5398 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005399 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005400
5401 BUG_ON(stripe_index >= bbio->num_stripes);
5402 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005403 if (dev->bdev) {
5404 if (bio->bi_rw & WRITE)
5405 btrfs_dev_stat_inc(dev,
5406 BTRFS_DEV_STAT_WRITE_ERRS);
5407 else
5408 btrfs_dev_stat_inc(dev,
5409 BTRFS_DEV_STAT_READ_ERRS);
5410 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5411 btrfs_dev_stat_inc(dev,
5412 BTRFS_DEV_STAT_FLUSH_ERRS);
5413 btrfs_dev_stat_print_on_error(dev);
5414 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005415 }
5416 }
Chris Mason8790d502008-04-03 16:29:03 -04005417
Jan Schmidta1d3c472011-08-04 17:15:33 +02005418 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005419 is_orig_bio = 1;
5420
Miao Xiec404e0d2014-01-30 16:46:55 +08005421 btrfs_bio_counter_dec(bbio->fs_info);
5422
Jan Schmidta1d3c472011-08-04 17:15:33 +02005423 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005424 if (!is_orig_bio) {
5425 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005426 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005427 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005428
Jan Schmidta1d3c472011-08-04 17:15:33 +02005429 bio->bi_private = bbio->private;
5430 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005431 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005432 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005433 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005434 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005435 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005436 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005437 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005438 /*
5439 * this bio is actually up to date, we didn't
5440 * go over the max number of errors
5441 */
5442 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005443 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005444 }
Miao Xiec55f1392014-06-19 10:42:54 +08005445
Miao Xie8408c712014-06-19 10:42:55 +08005446 btrfs_end_bbio(bbio, bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005447 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005448 bio_put(bio);
5449 }
Chris Mason8790d502008-04-03 16:29:03 -04005450}
5451
Chris Mason8b712842008-06-11 16:50:36 -04005452/*
5453 * see run_scheduled_bios for a description of why bios are collected for
5454 * async submit.
5455 *
5456 * This will add one bio to the pending list for a device and make sure
5457 * the work struct is scheduled.
5458 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005459static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5460 struct btrfs_device *device,
5461 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005462{
5463 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005464 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005465
David Woodhouse53b381b2013-01-29 18:40:14 -05005466 if (device->missing || !device->bdev) {
5467 bio_endio(bio, -EIO);
5468 return;
5469 }
5470
Chris Mason8b712842008-06-11 16:50:36 -04005471 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005472 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005473 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005474 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005475 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005476 return;
Chris Mason8b712842008-06-11 16:50:36 -04005477 }
5478
5479 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005480 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005481 * higher layers. Otherwise, the async bio makes it appear we have
5482 * made progress against dirty pages when we've really just put it
5483 * on a queue for later
5484 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005485 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005486 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005487 bio->bi_next = NULL;
5488 bio->bi_rw |= rw;
5489
5490 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005491 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005492 pending_bios = &device->pending_sync_bios;
5493 else
5494 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005495
Chris Masonffbd5172009-04-20 15:50:09 -04005496 if (pending_bios->tail)
5497 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005498
Chris Masonffbd5172009-04-20 15:50:09 -04005499 pending_bios->tail = bio;
5500 if (!pending_bios->head)
5501 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005502 if (device->running_pending)
5503 should_queue = 0;
5504
5505 spin_unlock(&device->io_lock);
5506
5507 if (should_queue)
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +08005508 btrfs_queue_work(root->fs_info->submit_workers,
5509 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005510}
5511
Josef Bacikde1ee922012-10-19 16:50:56 -04005512static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5513 sector_t sector)
5514{
5515 struct bio_vec *prev;
5516 struct request_queue *q = bdev_get_queue(bdev);
Akinobu Mita475bf362013-11-18 22:13:18 +09005517 unsigned int max_sectors = queue_max_sectors(q);
Josef Bacikde1ee922012-10-19 16:50:56 -04005518 struct bvec_merge_data bvm = {
5519 .bi_bdev = bdev,
5520 .bi_sector = sector,
5521 .bi_rw = bio->bi_rw,
5522 };
5523
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305524 if (WARN_ON(bio->bi_vcnt == 0))
Josef Bacikde1ee922012-10-19 16:50:56 -04005525 return 1;
Josef Bacikde1ee922012-10-19 16:50:56 -04005526
5527 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005528 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005529 return 0;
5530
5531 if (!q->merge_bvec_fn)
5532 return 1;
5533
Kent Overstreet4f024f32013-10-11 15:44:27 -07005534 bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len;
Josef Bacikde1ee922012-10-19 16:50:56 -04005535 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5536 return 0;
5537 return 1;
5538}
5539
5540static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5541 struct bio *bio, u64 physical, int dev_nr,
5542 int rw, int async)
5543{
5544 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5545
5546 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005547 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005548 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005549 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005550#ifdef DEBUG
5551 {
5552 struct rcu_string *name;
5553
5554 rcu_read_lock();
5555 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005556 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005557 "(%s id %llu), size=%u\n", rw,
5558 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5559 name->str, dev->devid, bio->bi_size);
5560 rcu_read_unlock();
5561 }
5562#endif
5563 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005564
5565 btrfs_bio_counter_inc_noblocked(root->fs_info);
5566
Josef Bacikde1ee922012-10-19 16:50:56 -04005567 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005568 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005569 else
5570 btrfsic_submit_bio(rw, bio);
5571}
5572
5573static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5574 struct bio *first_bio, struct btrfs_device *dev,
5575 int dev_nr, int rw, int async)
5576{
5577 struct bio_vec *bvec = first_bio->bi_io_vec;
5578 struct bio *bio;
5579 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5580 u64 physical = bbio->stripes[dev_nr].physical;
5581
5582again:
5583 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5584 if (!bio)
5585 return -ENOMEM;
5586
5587 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5588 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5589 bvec->bv_offset) < bvec->bv_len) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07005590 u64 len = bio->bi_iter.bi_size;
Josef Bacikde1ee922012-10-19 16:50:56 -04005591
5592 atomic_inc(&bbio->stripes_pending);
5593 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5594 rw, async);
5595 physical += len;
5596 goto again;
5597 }
5598 bvec++;
5599 }
5600
5601 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5602 return 0;
5603}
5604
5605static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5606{
5607 atomic_inc(&bbio->error);
5608 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005609 /* Shoud be the original bio. */
5610 WARN_ON(bio != bbio->orig_bio);
5611
Josef Bacikde1ee922012-10-19 16:50:56 -04005612 bio->bi_private = bbio->private;
5613 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005614 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005615 bio->bi_iter.bi_sector = logical >> 9;
Miao Xie8408c712014-06-19 10:42:55 +08005616
5617 btrfs_end_bbio(bbio, bio, -EIO);
Josef Bacikde1ee922012-10-19 16:50:56 -04005618 }
5619}
5620
Chris Masonf1885912008-04-09 16:28:12 -04005621int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005622 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005623{
Chris Mason0b86a832008-03-24 15:01:56 -04005624 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005625 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005626 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005627 u64 length = 0;
5628 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005629 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005630 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005631 int dev_nr = 0;
5632 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005633 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005634
Kent Overstreet4f024f32013-10-11 15:44:27 -07005635 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005636 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005637
Miao Xiec404e0d2014-01-30 16:46:55 +08005638 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005639 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5640 mirror_num, &raid_map);
Miao Xiec404e0d2014-01-30 16:46:55 +08005641 if (ret) {
5642 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005643 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005644 }
Chris Masoncea9e442008-04-09 16:28:12 -04005645
Jan Schmidta1d3c472011-08-04 17:15:33 +02005646 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005647 bbio->orig_bio = first_bio;
5648 bbio->private = first_bio->bi_private;
5649 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005650 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005651 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5652
5653 if (raid_map) {
5654 /* In this case, map_length has been set to the length of
5655 a single stripe; not the whole write */
5656 if (rw & WRITE) {
Miao Xiec404e0d2014-01-30 16:46:55 +08005657 ret = raid56_parity_write(root, bio, bbio,
5658 raid_map, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005659 } else {
Miao Xiec404e0d2014-01-30 16:46:55 +08005660 ret = raid56_parity_recover(root, bio, bbio,
5661 raid_map, map_length,
5662 mirror_num);
David Woodhouse53b381b2013-01-29 18:40:14 -05005663 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005664 /*
5665 * FIXME, replace dosen't support raid56 yet, please fix
5666 * it in the future.
5667 */
5668 btrfs_bio_counter_dec(root->fs_info);
5669 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005670 }
5671
Chris Masoncea9e442008-04-09 16:28:12 -04005672 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005673 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005674 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005675 BUG();
5676 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005677
Chris Masond3977122009-01-05 21:25:51 -05005678 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005679 dev = bbio->stripes[dev_nr].dev;
5680 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5681 bbio_error(bbio, first_bio, logical);
5682 dev_nr++;
5683 continue;
5684 }
5685
5686 /*
5687 * Check and see if we're ok with this bio based on it's size
5688 * and offset with the given device.
5689 */
5690 if (!bio_size_ok(dev->bdev, first_bio,
5691 bbio->stripes[dev_nr].physical >> 9)) {
5692 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5693 dev_nr, rw, async_submit);
5694 BUG_ON(ret);
5695 dev_nr++;
5696 continue;
5697 }
5698
Jan Schmidta1d3c472011-08-04 17:15:33 +02005699 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005700 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005701 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005702 } else {
5703 bio = first_bio;
Miao Xiec55f1392014-06-19 10:42:54 +08005704 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
Chris Mason8790d502008-04-03 16:29:03 -04005705 }
Josef Bacik606686e2012-06-04 14:03:51 -04005706
Josef Bacikde1ee922012-10-19 16:50:56 -04005707 submit_stripe_bio(root, bbio, bio,
5708 bbio->stripes[dev_nr].physical, dev_nr, rw,
5709 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005710 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005711 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005712 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04005713 return 0;
5714}
5715
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005716struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005717 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005718{
Yan Zheng2b820322008-11-17 21:11:30 -05005719 struct btrfs_device *device;
5720 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005721
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005722 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005723 while (cur_devices) {
5724 if (!fsid ||
5725 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5726 device = __find_device(&cur_devices->devices,
5727 devid, uuid);
5728 if (device)
5729 return device;
5730 }
5731 cur_devices = cur_devices->seed;
5732 }
5733 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005734}
5735
Chris Masondfe25022008-05-13 13:46:40 -04005736static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5737 u64 devid, u8 *dev_uuid)
5738{
5739 struct btrfs_device *device;
5740 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5741
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005742 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5743 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005744 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005745
5746 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005747 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04005748 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005749
5750 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05005751 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005752
Chris Masondfe25022008-05-13 13:46:40 -04005753 return device;
5754}
5755
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005756/**
5757 * btrfs_alloc_device - allocate struct btrfs_device
5758 * @fs_info: used only for generating a new devid, can be NULL if
5759 * devid is provided (i.e. @devid != NULL).
5760 * @devid: a pointer to devid for this device. If NULL a new devid
5761 * is generated.
5762 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5763 * is generated.
5764 *
5765 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5766 * on error. Returned struct is not linked onto any lists and can be
5767 * destroyed with kfree() right away.
5768 */
5769struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5770 const u64 *devid,
5771 const u8 *uuid)
5772{
5773 struct btrfs_device *dev;
5774 u64 tmp;
5775
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305776 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005777 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005778
5779 dev = __alloc_device();
5780 if (IS_ERR(dev))
5781 return dev;
5782
5783 if (devid)
5784 tmp = *devid;
5785 else {
5786 int ret;
5787
5788 ret = find_next_devid(fs_info, &tmp);
5789 if (ret) {
5790 kfree(dev);
5791 return ERR_PTR(ret);
5792 }
5793 }
5794 dev->devid = tmp;
5795
5796 if (uuid)
5797 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5798 else
5799 generate_random_uuid(dev->uuid);
5800
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +08005801 btrfs_init_work(&dev->work, pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005802
5803 return dev;
5804}
5805
Chris Mason0b86a832008-03-24 15:01:56 -04005806static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5807 struct extent_buffer *leaf,
5808 struct btrfs_chunk *chunk)
5809{
5810 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5811 struct map_lookup *map;
5812 struct extent_map *em;
5813 u64 logical;
5814 u64 length;
5815 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005816 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005817 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005818 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005819 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005820
Chris Masone17cade2008-04-15 15:41:47 -04005821 logical = key->offset;
5822 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005823
Chris Mason890871b2009-09-02 16:24:52 -04005824 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005825 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005826 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005827
5828 /* already mapped? */
5829 if (em && em->start <= logical && em->start + em->len > logical) {
5830 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005831 return 0;
5832 } else if (em) {
5833 free_extent_map(em);
5834 }
Chris Mason0b86a832008-03-24 15:01:56 -04005835
David Sterba172ddd62011-04-21 00:48:27 +02005836 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005837 if (!em)
5838 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005839 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5840 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005841 if (!map) {
5842 free_extent_map(em);
5843 return -ENOMEM;
5844 }
5845
Wang Shilong298a8f92014-06-19 10:42:52 +08005846 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04005847 em->bdev = (struct block_device *)map;
5848 em->start = logical;
5849 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005850 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005851 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005852 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005853
Chris Mason593060d2008-03-25 16:50:33 -04005854 map->num_stripes = num_stripes;
5855 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5856 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5857 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5858 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5859 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005860 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005861 for (i = 0; i < num_stripes; i++) {
5862 map->stripes[i].physical =
5863 btrfs_stripe_offset_nr(leaf, chunk, i);
5864 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005865 read_extent_buffer(leaf, uuid, (unsigned long)
5866 btrfs_stripe_dev_uuid_nr(chunk, i),
5867 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005868 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5869 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005870 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005871 free_extent_map(em);
5872 return -EIO;
5873 }
Chris Masondfe25022008-05-13 13:46:40 -04005874 if (!map->stripes[i].dev) {
5875 map->stripes[i].dev =
5876 add_missing_dev(root, devid, uuid);
5877 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04005878 free_extent_map(em);
5879 return -EIO;
5880 }
5881 }
5882 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005883 }
5884
Chris Mason890871b2009-09-02 16:24:52 -04005885 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005886 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04005887 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005888 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005889 free_extent_map(em);
5890
5891 return 0;
5892}
5893
Jeff Mahoney143bede2012-03-01 14:56:26 +01005894static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005895 struct btrfs_dev_item *dev_item,
5896 struct btrfs_device *device)
5897{
5898 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005899
5900 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005901 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5902 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005903 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5904 device->type = btrfs_device_type(leaf, dev_item);
5905 device->io_align = btrfs_device_io_align(leaf, dev_item);
5906 device->io_width = btrfs_device_io_width(leaf, dev_item);
5907 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005908 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005909 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005910
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02005911 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04005912 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005913}
5914
Yan Zheng2b820322008-11-17 21:11:30 -05005915static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5916{
5917 struct btrfs_fs_devices *fs_devices;
5918 int ret;
5919
Li Zefanb367e472011-12-07 11:38:24 +08005920 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05005921
5922 fs_devices = root->fs_info->fs_devices->seed;
5923 while (fs_devices) {
5924 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5925 ret = 0;
5926 goto out;
5927 }
5928 fs_devices = fs_devices->seed;
5929 }
5930
5931 fs_devices = find_fsid(fsid);
5932 if (!fs_devices) {
5933 ret = -ENOENT;
5934 goto out;
5935 }
Yan Zhenge4404d62008-12-12 10:03:26 -05005936
5937 fs_devices = clone_fs_devices(fs_devices);
5938 if (IS_ERR(fs_devices)) {
5939 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005940 goto out;
5941 }
5942
Christoph Hellwig97288f22008-12-02 06:36:09 -05005943 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05005944 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02005945 if (ret) {
5946 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005947 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02005948 }
Yan Zheng2b820322008-11-17 21:11:30 -05005949
5950 if (!fs_devices->seeding) {
5951 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005952 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005953 ret = -EINVAL;
5954 goto out;
5955 }
5956
5957 fs_devices->seed = root->fs_info->fs_devices->seed;
5958 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005959out:
Yan Zheng2b820322008-11-17 21:11:30 -05005960 return ret;
5961}
5962
Chris Mason0d81ba52008-03-24 15:02:07 -04005963static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04005964 struct extent_buffer *leaf,
5965 struct btrfs_dev_item *dev_item)
5966{
5967 struct btrfs_device *device;
5968 u64 devid;
5969 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005970 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04005971 u8 dev_uuid[BTRFS_UUID_SIZE];
5972
Chris Mason0b86a832008-03-24 15:01:56 -04005973 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02005974 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04005975 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02005976 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05005977 BTRFS_UUID_SIZE);
5978
5979 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
5980 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05005981 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005982 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005983 }
5984
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005985 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05005986 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05005987 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005988 return -EIO;
5989
5990 if (!device) {
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005991 btrfs_warn(root->fs_info, "devid %llu missing", devid);
Yan Zheng2b820322008-11-17 21:11:30 -05005992 device = add_missing_dev(root, devid, dev_uuid);
5993 if (!device)
5994 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05005995 } else if (!device->missing) {
5996 /*
5997 * this happens when a device that was properly setup
5998 * in the device info lists suddenly goes bad.
5999 * device->bdev is NULL, and so we have to set
6000 * device->missing to one here
6001 */
6002 root->fs_info->fs_devices->missing_devices++;
6003 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006004 }
6005 }
6006
6007 if (device->fs_devices != root->fs_info->fs_devices) {
6008 BUG_ON(device->writeable);
6009 if (device->generation !=
6010 btrfs_device_generation(leaf, dev_item))
6011 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006012 }
Chris Mason0b86a832008-03-24 15:01:56 -04006013
6014 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006015 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006016 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006017 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006018 spin_lock(&root->fs_info->free_chunk_lock);
6019 root->fs_info->free_chunk_space += device->total_bytes -
6020 device->bytes_used;
6021 spin_unlock(&root->fs_info->free_chunk_lock);
6022 }
Chris Mason0b86a832008-03-24 15:01:56 -04006023 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006024 return ret;
6025}
6026
Yan Zhenge4404d62008-12-12 10:03:26 -05006027int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006028{
David Sterba6c417612011-04-13 15:41:04 +02006029 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006030 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006031 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006032 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04006033 u8 *ptr;
6034 unsigned long sb_ptr;
6035 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006036 u32 num_stripes;
6037 u32 array_size;
6038 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006039 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04006040 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006041
Yan Zhenge4404d62008-12-12 10:03:26 -05006042 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04006043 BTRFS_SUPER_INFO_SIZE);
6044 if (!sb)
6045 return -ENOMEM;
6046 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006047 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006048 /*
6049 * The sb extent buffer is artifical and just used to read the system array.
6050 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6051 * pages up-to-date when the page is larger: extent does not cover the
6052 * whole page and consequently check_page_uptodate does not find all
6053 * the page's extents up-to-date (the hole beyond sb),
6054 * write_extent_buffer then triggers a WARN_ON.
6055 *
6056 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6057 * but sb spans only this function. Add an explicit SetPageUptodate call
6058 * to silence the warning eg. on PowerPC 64.
6059 */
6060 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006061 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006062
Chris Masona061fc82008-05-07 11:43:44 -04006063 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006064 array_size = btrfs_super_sys_array_size(super_copy);
6065
Chris Mason0b86a832008-03-24 15:01:56 -04006066 ptr = super_copy->sys_chunk_array;
6067 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
6068 cur = 0;
6069
6070 while (cur < array_size) {
6071 disk_key = (struct btrfs_disk_key *)ptr;
6072 btrfs_disk_key_to_cpu(&key, disk_key);
6073
Chris Masona061fc82008-05-07 11:43:44 -04006074 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006075 sb_ptr += len;
6076 cur += len;
6077
Chris Mason0d81ba52008-03-24 15:02:07 -04006078 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04006079 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04006080 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006081 if (ret)
6082 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006083 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6084 len = btrfs_chunk_item_size(num_stripes);
6085 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006086 ret = -EIO;
6087 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006088 }
6089 ptr += len;
6090 sb_ptr += len;
6091 cur += len;
6092 }
Chris Masona061fc82008-05-07 11:43:44 -04006093 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006094 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006095}
6096
6097int btrfs_read_chunk_tree(struct btrfs_root *root)
6098{
6099 struct btrfs_path *path;
6100 struct extent_buffer *leaf;
6101 struct btrfs_key key;
6102 struct btrfs_key found_key;
6103 int ret;
6104 int slot;
6105
6106 root = root->fs_info->chunk_root;
6107
6108 path = btrfs_alloc_path();
6109 if (!path)
6110 return -ENOMEM;
6111
Li Zefanb367e472011-12-07 11:38:24 +08006112 mutex_lock(&uuid_mutex);
6113 lock_chunks(root);
6114
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006115 /*
6116 * Read all device items, and then all the chunk items. All
6117 * device items are found before any chunk item (their object id
6118 * is smaller than the lowest possible object id for a chunk
6119 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006120 */
6121 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6122 key.offset = 0;
6123 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006124 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006125 if (ret < 0)
6126 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006127 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006128 leaf = path->nodes[0];
6129 slot = path->slots[0];
6130 if (slot >= btrfs_header_nritems(leaf)) {
6131 ret = btrfs_next_leaf(root, path);
6132 if (ret == 0)
6133 continue;
6134 if (ret < 0)
6135 goto error;
6136 break;
6137 }
6138 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006139 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6140 struct btrfs_dev_item *dev_item;
6141 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006142 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006143 ret = read_one_dev(root, leaf, dev_item);
6144 if (ret)
6145 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006146 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6147 struct btrfs_chunk *chunk;
6148 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6149 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006150 if (ret)
6151 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006152 }
6153 path->slots[0]++;
6154 }
Chris Mason0b86a832008-03-24 15:01:56 -04006155 ret = 0;
6156error:
Li Zefanb367e472011-12-07 11:38:24 +08006157 unlock_chunks(root);
6158 mutex_unlock(&uuid_mutex);
6159
Yan Zheng2b820322008-11-17 21:11:30 -05006160 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006161 return ret;
6162}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006163
Miao Xiecb517ea2013-05-15 07:48:19 +00006164void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6165{
6166 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6167 struct btrfs_device *device;
6168
Liu Bo29cc83f2014-05-11 23:14:59 +08006169 while (fs_devices) {
6170 mutex_lock(&fs_devices->device_list_mutex);
6171 list_for_each_entry(device, &fs_devices->devices, dev_list)
6172 device->dev_root = fs_info->dev_root;
6173 mutex_unlock(&fs_devices->device_list_mutex);
6174
6175 fs_devices = fs_devices->seed;
6176 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006177}
6178
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006179static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6180{
6181 int i;
6182
6183 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6184 btrfs_dev_stat_reset(dev, i);
6185}
6186
6187int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6188{
6189 struct btrfs_key key;
6190 struct btrfs_key found_key;
6191 struct btrfs_root *dev_root = fs_info->dev_root;
6192 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6193 struct extent_buffer *eb;
6194 int slot;
6195 int ret = 0;
6196 struct btrfs_device *device;
6197 struct btrfs_path *path = NULL;
6198 int i;
6199
6200 path = btrfs_alloc_path();
6201 if (!path) {
6202 ret = -ENOMEM;
6203 goto out;
6204 }
6205
6206 mutex_lock(&fs_devices->device_list_mutex);
6207 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6208 int item_size;
6209 struct btrfs_dev_stats_item *ptr;
6210
6211 key.objectid = 0;
6212 key.type = BTRFS_DEV_STATS_KEY;
6213 key.offset = device->devid;
6214 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6215 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006216 __btrfs_reset_dev_stats(device);
6217 device->dev_stats_valid = 1;
6218 btrfs_release_path(path);
6219 continue;
6220 }
6221 slot = path->slots[0];
6222 eb = path->nodes[0];
6223 btrfs_item_key_to_cpu(eb, &found_key, slot);
6224 item_size = btrfs_item_size_nr(eb, slot);
6225
6226 ptr = btrfs_item_ptr(eb, slot,
6227 struct btrfs_dev_stats_item);
6228
6229 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6230 if (item_size >= (1 + i) * sizeof(__le64))
6231 btrfs_dev_stat_set(device, i,
6232 btrfs_dev_stats_value(eb, ptr, i));
6233 else
6234 btrfs_dev_stat_reset(device, i);
6235 }
6236
6237 device->dev_stats_valid = 1;
6238 btrfs_dev_stat_print_on_load(device);
6239 btrfs_release_path(path);
6240 }
6241 mutex_unlock(&fs_devices->device_list_mutex);
6242
6243out:
6244 btrfs_free_path(path);
6245 return ret < 0 ? ret : 0;
6246}
6247
6248static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6249 struct btrfs_root *dev_root,
6250 struct btrfs_device *device)
6251{
6252 struct btrfs_path *path;
6253 struct btrfs_key key;
6254 struct extent_buffer *eb;
6255 struct btrfs_dev_stats_item *ptr;
6256 int ret;
6257 int i;
6258
6259 key.objectid = 0;
6260 key.type = BTRFS_DEV_STATS_KEY;
6261 key.offset = device->devid;
6262
6263 path = btrfs_alloc_path();
6264 BUG_ON(!path);
6265 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6266 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006267 printk_in_rcu(KERN_WARNING "BTRFS: "
6268 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006269 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006270 goto out;
6271 }
6272
6273 if (ret == 0 &&
6274 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6275 /* need to delete old one and insert a new one */
6276 ret = btrfs_del_item(trans, dev_root, path);
6277 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006278 printk_in_rcu(KERN_WARNING "BTRFS: "
6279 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006280 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006281 goto out;
6282 }
6283 ret = 1;
6284 }
6285
6286 if (ret == 1) {
6287 /* need to insert a new item */
6288 btrfs_release_path(path);
6289 ret = btrfs_insert_empty_item(trans, dev_root, path,
6290 &key, sizeof(*ptr));
6291 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006292 printk_in_rcu(KERN_WARNING "BTRFS: "
6293 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006294 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006295 goto out;
6296 }
6297 }
6298
6299 eb = path->nodes[0];
6300 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6301 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6302 btrfs_set_dev_stats_value(eb, ptr, i,
6303 btrfs_dev_stat_read(device, i));
6304 btrfs_mark_buffer_dirty(eb);
6305
6306out:
6307 btrfs_free_path(path);
6308 return ret;
6309}
6310
6311/*
6312 * called from commit_transaction. Writes all changed device stats to disk.
6313 */
6314int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6315 struct btrfs_fs_info *fs_info)
6316{
6317 struct btrfs_root *dev_root = fs_info->dev_root;
6318 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6319 struct btrfs_device *device;
6320 int ret = 0;
6321
6322 mutex_lock(&fs_devices->device_list_mutex);
6323 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6324 if (!device->dev_stats_valid || !device->dev_stats_dirty)
6325 continue;
6326
6327 ret = update_dev_stat_item(trans, dev_root, device);
6328 if (!ret)
6329 device->dev_stats_dirty = 0;
6330 }
6331 mutex_unlock(&fs_devices->device_list_mutex);
6332
6333 return ret;
6334}
6335
Stefan Behrens442a4f62012-05-25 16:06:08 +02006336void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6337{
6338 btrfs_dev_stat_inc(dev, index);
6339 btrfs_dev_stat_print_on_error(dev);
6340}
6341
Eric Sandeen48a3b632013-04-25 20:41:01 +00006342static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006343{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006344 if (!dev->dev_stats_valid)
6345 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006346 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6347 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006348 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006349 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6350 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6351 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006352 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6353 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006354}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006355
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006356static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6357{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006358 int i;
6359
6360 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6361 if (btrfs_dev_stat_read(dev, i) != 0)
6362 break;
6363 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6364 return; /* all values == 0, suppress message */
6365
Frank Holtonefe120a2013-12-20 11:37:06 -05006366 printk_in_rcu(KERN_INFO "BTRFS: "
6367 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006368 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006369 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6370 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6371 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6372 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6373 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6374}
6375
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006376int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006377 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006378{
6379 struct btrfs_device *dev;
6380 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6381 int i;
6382
6383 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006384 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006385 mutex_unlock(&fs_devices->device_list_mutex);
6386
6387 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006388 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006389 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006390 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006391 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006392 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006393 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006394 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6395 if (stats->nr_items > i)
6396 stats->values[i] =
6397 btrfs_dev_stat_read_and_reset(dev, i);
6398 else
6399 btrfs_dev_stat_reset(dev, i);
6400 }
6401 } else {
6402 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6403 if (stats->nr_items > i)
6404 stats->values[i] = btrfs_dev_stat_read(dev, i);
6405 }
6406 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6407 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6408 return 0;
6409}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006410
6411int btrfs_scratch_superblock(struct btrfs_device *device)
6412{
6413 struct buffer_head *bh;
6414 struct btrfs_super_block *disk_super;
6415
6416 bh = btrfs_read_dev_super(device->bdev);
6417 if (!bh)
6418 return -EINVAL;
6419 disk_super = (struct btrfs_super_block *)bh->b_data;
6420
6421 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6422 set_buffer_dirty(bh);
6423 sync_dirty_buffer(bh);
6424 brelse(bh);
6425
6426 return 0;
6427}