blob: da0e632a21fc6b17c9d74913145f152aa91579e1 [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 Wenruoa8c93d42014-02-28 10:46:08 +0800419 btrfs_queue_work(fs_info->submit_workers,
420 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400421 goto done;
422 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500423 /* unplug every 64 requests just for good measure */
424 if (batch_run % 64 == 0) {
425 blk_finish_plug(&plug);
426 blk_start_plug(&plug);
427 sync_pending = 0;
428 }
Chris Mason8b712842008-06-11 16:50:36 -0400429 }
Chris Masonffbd5172009-04-20 15:50:09 -0400430
Chris Mason51684082010-03-10 15:33:32 -0500431 cond_resched();
432 if (again)
433 goto loop;
434
435 spin_lock(&device->io_lock);
436 if (device->pending_bios.head || device->pending_sync_bios.head)
437 goto loop_lock;
438 spin_unlock(&device->io_lock);
439
Chris Mason8b712842008-06-11 16:50:36 -0400440done:
Chris Mason211588a2011-04-19 20:12:40 -0400441 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400442}
443
Christoph Hellwigb2950862008-12-02 09:54:17 -0500444static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400445{
446 struct btrfs_device *device;
447
448 device = container_of(work, struct btrfs_device, work);
449 run_scheduled_bios(device);
450}
451
David Sterba60999ca2014-03-26 18:26:36 +0100452/*
453 * Add new device to list of registered devices
454 *
455 * Returns:
456 * 1 - first time device is seen
457 * 0 - device already known
458 * < 0 - error
459 */
Chris Masona1b32a52008-09-05 16:09:51 -0400460static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400461 struct btrfs_super_block *disk_super,
462 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
463{
464 struct btrfs_device *device;
465 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400466 struct rcu_string *name;
David Sterba60999ca2014-03-26 18:26:36 +0100467 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400468 u64 found_transid = btrfs_super_generation(disk_super);
469
470 fs_devices = find_fsid(disk_super->fsid);
471 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300472 fs_devices = alloc_fs_devices(disk_super->fsid);
473 if (IS_ERR(fs_devices))
474 return PTR_ERR(fs_devices);
475
Chris Mason8a4b83c2008-03-24 15:02:07 -0400476 list_add(&fs_devices->list, &fs_uuids);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400477 fs_devices->latest_devid = devid;
478 fs_devices->latest_trans = found_transid;
Ilya Dryomov2208a372013-08-12 14:33:03 +0300479
Chris Mason8a4b83c2008-03-24 15:02:07 -0400480 device = NULL;
481 } else {
Chris Masona4437552008-04-18 10:29:38 -0400482 device = __find_device(&fs_devices->devices, devid,
483 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400484 }
485 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500486 if (fs_devices->opened)
487 return -EBUSY;
488
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300489 device = btrfs_alloc_device(NULL, &devid,
490 disk_super->dev_item.uuid);
491 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400492 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300493 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400494 }
Josef Bacik606686e2012-06-04 14:03:51 -0400495
496 name = rcu_string_strdup(path, GFP_NOFS);
497 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400498 kfree(device);
499 return -ENOMEM;
500 }
Josef Bacik606686e2012-06-04 14:03:51 -0400501 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200502
Chris Masone5e9a522009-06-10 15:17:02 -0400503 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000504 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100505 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400506 mutex_unlock(&fs_devices->device_list_mutex);
507
David Sterba60999ca2014-03-26 18:26:36 +0100508 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500509 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400510 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800511 /*
512 * When FS is already mounted.
513 * 1. If you are here and if the device->name is NULL that
514 * means this device was missing at time of FS mount.
515 * 2. If you are here and if the device->name is different
516 * from 'path' that means either
517 * a. The same device disappeared and reappeared with
518 * different name. or
519 * b. The missing-disk-which-was-replaced, has
520 * reappeared now.
521 *
522 * We must allow 1 and 2a above. But 2b would be a spurious
523 * and unintentional.
524 *
525 * Further in case of 1 and 2a above, the disk at 'path'
526 * would have missed some transaction when it was away and
527 * in case of 2a the stale bdev has to be updated as well.
528 * 2b must not be allowed at all time.
529 */
530
531 /*
532 * As of now don't allow update to btrfs_fs_device through
533 * the btrfs dev scan cli, after FS has been mounted.
534 */
Anand Jain77bdae42014-07-03 18:22:06 +0800535 if (fs_devices->opened) {
Anand Jainb96de002014-07-03 18:22:05 +0800536 return -EBUSY;
Anand Jain77bdae42014-07-03 18:22:06 +0800537 } else {
538 /*
539 * That is if the FS is _not_ mounted and if you
540 * are here, that means there is more than one
541 * disk with same uuid and devid.We keep the one
542 * with larger generation number or the last-in if
543 * generation are equal.
544 */
545 if (found_transid < device->generation)
546 return -EEXIST;
547 }
Anand Jainb96de002014-07-03 18:22:05 +0800548
Josef Bacik606686e2012-06-04 14:03:51 -0400549 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000550 if (!name)
551 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400552 rcu_string_free(device->name);
553 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500554 if (device->missing) {
555 fs_devices->missing_devices--;
556 device->missing = 0;
557 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400558 }
559
Anand Jain77bdae42014-07-03 18:22:06 +0800560 /*
561 * Unmount does not free the btrfs_device struct but would zero
562 * generation along with most of the other members. So just update
563 * it back. We need it to pick the disk with largest generation
564 * (as above).
565 */
566 if (!fs_devices->opened)
567 device->generation = found_transid;
568
Chris Mason8a4b83c2008-03-24 15:02:07 -0400569 if (found_transid > fs_devices->latest_trans) {
570 fs_devices->latest_devid = devid;
571 fs_devices->latest_trans = found_transid;
572 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400573 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100574
575 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400576}
577
Yan Zhenge4404d62008-12-12 10:03:26 -0500578static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
579{
580 struct btrfs_fs_devices *fs_devices;
581 struct btrfs_device *device;
582 struct btrfs_device *orig_dev;
583
Ilya Dryomov2208a372013-08-12 14:33:03 +0300584 fs_devices = alloc_fs_devices(orig->fsid);
585 if (IS_ERR(fs_devices))
586 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500587
Yan Zhenge4404d62008-12-12 10:03:26 -0500588 fs_devices->latest_devid = orig->latest_devid;
589 fs_devices->latest_trans = orig->latest_trans;
Josef Bacik02db0842012-06-21 16:03:58 -0400590 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500591
Xiao Guangrong46224702011-04-20 10:08:47 +0000592 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500593 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400594 struct rcu_string *name;
595
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300596 device = btrfs_alloc_device(NULL, &orig_dev->devid,
597 orig_dev->uuid);
598 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500599 goto error;
600
Josef Bacik606686e2012-06-04 14:03:51 -0400601 /*
602 * This is ok to do without rcu read locked because we hold the
603 * uuid mutex so nothing we touch in here is going to disappear.
604 */
Anand Jaine755f782014-06-30 17:12:47 +0800605 if (orig_dev->name) {
606 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
607 if (!name) {
608 kfree(device);
609 goto error;
610 }
611 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400612 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500613
Yan Zhenge4404d62008-12-12 10:03:26 -0500614 list_add(&device->dev_list, &fs_devices->devices);
615 device->fs_devices = fs_devices;
616 fs_devices->num_devices++;
617 }
618 return fs_devices;
619error:
620 free_fs_devices(fs_devices);
621 return ERR_PTR(-ENOMEM);
622}
623
Stefan Behrens8dabb742012-11-06 13:15:27 +0100624void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
625 struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400626{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500627 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400628
Chris Masona6b0d5c2012-02-20 20:53:43 -0500629 struct block_device *latest_bdev = NULL;
630 u64 latest_devid = 0;
631 u64 latest_transid = 0;
632
Chris Masondfe25022008-05-13 13:46:40 -0400633 mutex_lock(&uuid_mutex);
634again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000635 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500636 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500637 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100638 if (!device->is_tgtdev_for_dev_replace &&
639 (!latest_transid ||
640 device->generation > latest_transid)) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500641 latest_devid = device->devid;
642 latest_transid = device->generation;
643 latest_bdev = device->bdev;
644 }
Yan Zheng2b820322008-11-17 21:11:30 -0500645 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500646 }
Yan Zheng2b820322008-11-17 21:11:30 -0500647
Stefan Behrens8dabb742012-11-06 13:15:27 +0100648 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
649 /*
650 * In the first step, keep the device which has
651 * the correct fsid and the devid that is used
652 * for the dev_replace procedure.
653 * In the second step, the dev_replace state is
654 * read from the device tree and it is known
655 * whether the procedure is really active or
656 * not, which means whether this device is
657 * used or whether it should be removed.
658 */
659 if (step == 0 || device->is_tgtdev_for_dev_replace) {
660 continue;
661 }
662 }
Yan Zheng2b820322008-11-17 21:11:30 -0500663 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100664 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500665 device->bdev = NULL;
666 fs_devices->open_devices--;
667 }
668 if (device->writeable) {
669 list_del_init(&device->dev_alloc_list);
670 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100671 if (!device->is_tgtdev_for_dev_replace)
672 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500673 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500674 list_del_init(&device->dev_list);
675 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400676 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500677 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400678 }
Yan Zheng2b820322008-11-17 21:11:30 -0500679
680 if (fs_devices->seed) {
681 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500682 goto again;
683 }
684
Chris Masona6b0d5c2012-02-20 20:53:43 -0500685 fs_devices->latest_bdev = latest_bdev;
686 fs_devices->latest_devid = latest_devid;
687 fs_devices->latest_trans = latest_transid;
688
Chris Masondfe25022008-05-13 13:46:40 -0400689 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400690}
Chris Masona0af4692008-05-13 16:03:06 -0400691
Xiao Guangrong1f781602011-04-20 10:09:16 +0000692static void __free_device(struct work_struct *work)
693{
694 struct btrfs_device *device;
695
696 device = container_of(work, struct btrfs_device, rcu_work);
697
698 if (device->bdev)
699 blkdev_put(device->bdev, device->mode);
700
Josef Bacik606686e2012-06-04 14:03:51 -0400701 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000702 kfree(device);
703}
704
705static void free_device(struct rcu_head *head)
706{
707 struct btrfs_device *device;
708
709 device = container_of(head, struct btrfs_device, rcu);
710
711 INIT_WORK(&device->rcu_work, __free_device);
712 schedule_work(&device->rcu_work);
713}
714
Yan Zheng2b820322008-11-17 21:11:30 -0500715static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400716{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400717 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500718
Yan Zheng2b820322008-11-17 21:11:30 -0500719 if (--fs_devices->opened > 0)
720 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400721
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000722 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500723 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000724 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400725 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000726
727 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400728 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000729
Ilya Dryomovf747cab2013-10-10 20:37:29 +0300730 if (device->writeable &&
731 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500732 list_del_init(&device->dev_alloc_list);
733 fs_devices->rw_devices--;
734 }
735
Josef Bacikd5e20032011-08-04 14:52:27 +0000736 if (device->can_discard)
737 fs_devices->num_can_discard--;
Josef Bacik726551e2013-08-26 13:45:53 -0400738 if (device->missing)
739 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000740
Ilya Dryomova1e87802013-08-12 14:33:04 +0300741 new_device = btrfs_alloc_device(NULL, &device->devid,
742 device->uuid);
743 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400744
745 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400746 if (device->name) {
747 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300748 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400749 rcu_assign_pointer(new_device->name, name);
750 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300751
Xiao Guangrong1f781602011-04-20 10:09:16 +0000752 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300753 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000754
755 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400756 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000757 mutex_unlock(&fs_devices->device_list_mutex);
758
Yan Zhenge4404d62008-12-12 10:03:26 -0500759 WARN_ON(fs_devices->open_devices);
760 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500761 fs_devices->opened = 0;
762 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500763
Chris Mason8a4b83c2008-03-24 15:02:07 -0400764 return 0;
765}
766
Yan Zheng2b820322008-11-17 21:11:30 -0500767int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
768{
Yan Zhenge4404d62008-12-12 10:03:26 -0500769 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500770 int ret;
771
772 mutex_lock(&uuid_mutex);
773 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500774 if (!fs_devices->opened) {
775 seed_devices = fs_devices->seed;
776 fs_devices->seed = NULL;
777 }
Yan Zheng2b820322008-11-17 21:11:30 -0500778 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500779
780 while (seed_devices) {
781 fs_devices = seed_devices;
782 seed_devices = fs_devices->seed;
783 __btrfs_close_devices(fs_devices);
784 free_fs_devices(fs_devices);
785 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000786 /*
787 * Wait for rcu kworkers under __btrfs_close_devices
788 * to finish all blkdev_puts so device is really
789 * free when umount is done.
790 */
791 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500792 return ret;
793}
794
Yan Zhenge4404d62008-12-12 10:03:26 -0500795static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
796 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400797{
Josef Bacikd5e20032011-08-04 14:52:27 +0000798 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400799 struct block_device *bdev;
800 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400801 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400802 struct block_device *latest_bdev = NULL;
803 struct buffer_head *bh;
804 struct btrfs_super_block *disk_super;
805 u64 latest_devid = 0;
806 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400807 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500808 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400809 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400810
Tejun Heod4d77622010-11-13 11:55:18 +0100811 flags |= FMODE_EXCL;
812
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500813 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400814 if (device->bdev)
815 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400816 if (!device->name)
817 continue;
818
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000819 /* Just open everything we can; ignore failures here */
820 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
821 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100822 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400823
824 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000825 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400826 if (devid != device->devid)
827 goto error_brelse;
828
Yan Zheng2b820322008-11-17 21:11:30 -0500829 if (memcmp(device->uuid, disk_super->dev_item.uuid,
830 BTRFS_UUID_SIZE))
831 goto error_brelse;
832
833 device->generation = btrfs_super_generation(disk_super);
834 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400835 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500836 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400837 latest_bdev = bdev;
838 }
839
Yan Zheng2b820322008-11-17 21:11:30 -0500840 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
841 device->writeable = 0;
842 } else {
843 device->writeable = !bdev_read_only(bdev);
844 seeding = 0;
845 }
846
Josef Bacikd5e20032011-08-04 14:52:27 +0000847 q = bdev_get_queue(bdev);
848 if (blk_queue_discard(q)) {
849 device->can_discard = 1;
850 fs_devices->num_can_discard++;
851 }
852
Chris Mason8a4b83c2008-03-24 15:02:07 -0400853 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400854 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500855 device->mode = flags;
856
Chris Masonc2898112009-06-10 09:51:32 -0400857 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
858 fs_devices->rotating = 1;
859
Chris Masona0af4692008-05-13 16:03:06 -0400860 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300861 if (device->writeable &&
862 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500863 fs_devices->rw_devices++;
864 list_add(&device->dev_alloc_list,
865 &fs_devices->alloc_list);
866 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000867 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400868 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400869
Chris Masona0af4692008-05-13 16:03:06 -0400870error_brelse:
871 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100872 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400873 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400874 }
Chris Masona0af4692008-05-13 16:03:06 -0400875 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300876 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400877 goto out;
878 }
Yan Zheng2b820322008-11-17 21:11:30 -0500879 fs_devices->seeding = seeding;
880 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400881 fs_devices->latest_bdev = latest_bdev;
882 fs_devices->latest_devid = latest_devid;
883 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500884 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400885out:
Yan Zheng2b820322008-11-17 21:11:30 -0500886 return ret;
887}
888
889int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500890 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500891{
892 int ret;
893
894 mutex_lock(&uuid_mutex);
895 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500896 fs_devices->opened++;
897 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500898 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500899 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500900 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400901 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400902 return ret;
903}
904
David Sterba6f60cbd2013-02-15 11:31:02 -0700905/*
906 * Look for a btrfs signature on a device. This may be called out of the mount path
907 * and we are not allowed to call set_blocksize during the scan. The superblock
908 * is read via pagecache
909 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500910int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400911 struct btrfs_fs_devices **fs_devices_ret)
912{
913 struct btrfs_super_block *disk_super;
914 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700915 struct page *page;
916 void *p;
917 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400918 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400919 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400920 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700921 u64 bytenr;
922 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400923
David Sterba6f60cbd2013-02-15 11:31:02 -0700924 /*
925 * we would like to check all the supers, but that would make
926 * a btrfs mount succeed after a mkfs from a different FS.
927 * So, we need to add a special mount option to scan for
928 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
929 */
930 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100931 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500932 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700933
934 bdev = blkdev_get_by_path(path, flags, holder);
935
936 if (IS_ERR(bdev)) {
937 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100938 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700939 }
940
941 /* make sure our super fits in the device */
942 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
943 goto error_bdev_put;
944
945 /* make sure our super fits in the page */
946 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
947 goto error_bdev_put;
948
949 /* make sure our super doesn't straddle pages on disk */
950 index = bytenr >> PAGE_CACHE_SHIFT;
951 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
952 goto error_bdev_put;
953
954 /* pull in the page with our super */
955 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
956 index, GFP_NOFS);
957
958 if (IS_ERR_OR_NULL(page))
959 goto error_bdev_put;
960
961 p = kmap(page);
962
963 /* align our pointer to the offset of the super block */
964 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
965
966 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +0800967 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -0700968 goto error_unmap;
969
Xiao Guangronga3438322010-01-06 11:48:18 +0000970 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400971 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400972 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700973
Chris Mason8a4b83c2008-03-24 15:02:07 -0400974 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +0100975 if (ret > 0) {
976 if (disk_super->label[0]) {
977 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
978 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
979 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
980 } else {
981 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
982 }
983
984 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
985 ret = 0;
986 }
Josef Bacik02db0842012-06-21 16:03:58 -0400987 if (!ret && fs_devices_ret)
988 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700989
990error_unmap:
991 kunmap(page);
992 page_cache_release(page);
993
994error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100995 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400996error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100997 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400998 return ret;
999}
Chris Mason0b86a832008-03-24 15:01:56 -04001000
Miao Xie6d07bce2011-01-05 10:07:31 +00001001/* helper to account the used device space in the range */
1002int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1003 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -04001004{
1005 struct btrfs_key key;
1006 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +00001007 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001008 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +00001009 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001010 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +00001011 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001012 struct extent_buffer *l;
1013
Miao Xie6d07bce2011-01-05 10:07:31 +00001014 *length = 0;
1015
Stefan Behrens63a212a2012-11-05 18:29:28 +01001016 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +00001017 return 0;
1018
Yan Zheng2b820322008-11-17 21:11:30 -05001019 path = btrfs_alloc_path();
1020 if (!path)
1021 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001022 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -04001023
Chris Mason0b86a832008-03-24 15:01:56 -04001024 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001025 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001026 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001027
1028 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001029 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001030 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001031 if (ret > 0) {
1032 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1033 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001034 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001035 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001036
Chris Mason0b86a832008-03-24 15:01:56 -04001037 while (1) {
1038 l = path->nodes[0];
1039 slot = path->slots[0];
1040 if (slot >= btrfs_header_nritems(l)) {
1041 ret = btrfs_next_leaf(root, path);
1042 if (ret == 0)
1043 continue;
1044 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001045 goto out;
1046
1047 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001048 }
1049 btrfs_item_key_to_cpu(l, &key, slot);
1050
1051 if (key.objectid < device->devid)
1052 goto next;
1053
1054 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001055 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001056
Chris Masond3977122009-01-05 21:25:51 -05001057 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001058 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001059
Chris Mason0b86a832008-03-24 15:01:56 -04001060 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001061 extent_end = key.offset + btrfs_dev_extent_length(l,
1062 dev_extent);
1063 if (key.offset <= start && extent_end > end) {
1064 *length = end - start + 1;
1065 break;
1066 } else if (key.offset <= start && extent_end > start)
1067 *length += extent_end - start;
1068 else if (key.offset > start && extent_end <= end)
1069 *length += extent_end - key.offset;
1070 else if (key.offset > start && key.offset <= end) {
1071 *length += end - key.offset + 1;
1072 break;
1073 } else if (key.offset > end)
1074 break;
1075
1076next:
1077 path->slots[0]++;
1078 }
1079 ret = 0;
1080out:
1081 btrfs_free_path(path);
1082 return ret;
1083}
1084
Josef Bacik6df9a952013-06-27 13:22:46 -04001085static int contains_pending_extent(struct btrfs_trans_handle *trans,
1086 struct btrfs_device *device,
1087 u64 *start, u64 len)
1088{
1089 struct extent_map *em;
1090 int ret = 0;
1091
1092 list_for_each_entry(em, &trans->transaction->pending_chunks, list) {
1093 struct map_lookup *map;
1094 int i;
1095
1096 map = (struct map_lookup *)em->bdev;
1097 for (i = 0; i < map->num_stripes; i++) {
1098 if (map->stripes[i].dev != device)
1099 continue;
1100 if (map->stripes[i].physical >= *start + len ||
1101 map->stripes[i].physical + em->orig_block_len <=
1102 *start)
1103 continue;
1104 *start = map->stripes[i].physical +
1105 em->orig_block_len;
1106 ret = 1;
1107 }
1108 }
1109
1110 return ret;
1111}
1112
1113
Chris Mason0b86a832008-03-24 15:01:56 -04001114/*
Miao Xie7bfc8372011-01-05 10:07:26 +00001115 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +00001116 * @device: the device which we search the free space in
1117 * @num_bytes: the size of the free space that we need
1118 * @start: store the start of the free space.
1119 * @len: the size of the free space. that we find, or the size of the max
1120 * free space if we don't find suitable free space
1121 *
Chris Mason0b86a832008-03-24 15:01:56 -04001122 * this uses a pretty simple search, the expectation is that it is
1123 * called very infrequently and that a given device has a small number
1124 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001125 *
1126 * @start is used to store the start of the free space if we find. But if we
1127 * don't find suitable free space, it will be used to store the start position
1128 * of the max free space.
1129 *
1130 * @len is used to store the size of the free space that we find.
1131 * But if we don't find suitable free space, it is used to store the size of
1132 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001133 */
Josef Bacik6df9a952013-06-27 13:22:46 -04001134int find_free_dev_extent(struct btrfs_trans_handle *trans,
1135 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001136 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001137{
1138 struct btrfs_key key;
1139 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001140 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001141 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001142 u64 hole_size;
1143 u64 max_hole_start;
1144 u64 max_hole_size;
1145 u64 extent_end;
1146 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001147 u64 search_end = device->total_bytes;
1148 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001149 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001150 struct extent_buffer *l;
1151
Chris Mason0b86a832008-03-24 15:01:56 -04001152 /* FIXME use last free of some kind */
1153
1154 /* we don't want to overwrite the superblock on the drive,
1155 * so we make sure to start at an offset of at least 1MB
1156 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001157 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001158
Josef Bacik6df9a952013-06-27 13:22:46 -04001159 path = btrfs_alloc_path();
1160 if (!path)
1161 return -ENOMEM;
1162again:
Miao Xie7bfc8372011-01-05 10:07:26 +00001163 max_hole_start = search_start;
1164 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +00001165 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +00001166
Stefan Behrens63a212a2012-11-05 18:29:28 +01001167 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001168 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001169 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001170 }
1171
Miao Xie7bfc8372011-01-05 10:07:26 +00001172 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001173 path->search_commit_root = 1;
1174 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001175
Chris Mason0b86a832008-03-24 15:01:56 -04001176 key.objectid = device->devid;
1177 key.offset = search_start;
1178 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001179
Li Zefan125ccb02011-12-08 15:07:24 +08001180 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001181 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001182 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001183 if (ret > 0) {
1184 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1185 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001186 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001187 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001188
Chris Mason0b86a832008-03-24 15:01:56 -04001189 while (1) {
1190 l = path->nodes[0];
1191 slot = path->slots[0];
1192 if (slot >= btrfs_header_nritems(l)) {
1193 ret = btrfs_next_leaf(root, path);
1194 if (ret == 0)
1195 continue;
1196 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001197 goto out;
1198
1199 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001200 }
1201 btrfs_item_key_to_cpu(l, &key, slot);
1202
1203 if (key.objectid < device->devid)
1204 goto next;
1205
1206 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001207 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001208
Chris Mason0b86a832008-03-24 15:01:56 -04001209 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
1210 goto next;
1211
Miao Xie7bfc8372011-01-05 10:07:26 +00001212 if (key.offset > search_start) {
1213 hole_size = key.offset - search_start;
1214
Josef Bacik6df9a952013-06-27 13:22:46 -04001215 /*
1216 * Have to check before we set max_hole_start, otherwise
1217 * we could end up sending back this offset anyway.
1218 */
1219 if (contains_pending_extent(trans, device,
1220 &search_start,
1221 hole_size))
1222 hole_size = 0;
1223
Miao Xie7bfc8372011-01-05 10:07:26 +00001224 if (hole_size > max_hole_size) {
1225 max_hole_start = search_start;
1226 max_hole_size = hole_size;
1227 }
1228
1229 /*
1230 * If this free space is greater than which we need,
1231 * it must be the max free space that we have found
1232 * until now, so max_hole_start must point to the start
1233 * of this free space and the length of this free space
1234 * is stored in max_hole_size. Thus, we return
1235 * max_hole_start and max_hole_size and go back to the
1236 * caller.
1237 */
1238 if (hole_size >= num_bytes) {
1239 ret = 0;
1240 goto out;
1241 }
1242 }
1243
Chris Mason0b86a832008-03-24 15:01:56 -04001244 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001245 extent_end = key.offset + btrfs_dev_extent_length(l,
1246 dev_extent);
1247 if (extent_end > search_start)
1248 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001249next:
1250 path->slots[0]++;
1251 cond_resched();
1252 }
Chris Mason0b86a832008-03-24 15:01:56 -04001253
liubo38c01b92011-08-02 02:39:03 +00001254 /*
1255 * At this point, search_start should be the end of
1256 * allocated dev extents, and when shrinking the device,
1257 * search_end may be smaller than search_start.
1258 */
1259 if (search_end > search_start)
1260 hole_size = search_end - search_start;
1261
Miao Xie7bfc8372011-01-05 10:07:26 +00001262 if (hole_size > max_hole_size) {
1263 max_hole_start = search_start;
1264 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001265 }
Chris Mason0b86a832008-03-24 15:01:56 -04001266
Josef Bacik6df9a952013-06-27 13:22:46 -04001267 if (contains_pending_extent(trans, device, &search_start, hole_size)) {
1268 btrfs_release_path(path);
1269 goto again;
1270 }
1271
Miao Xie7bfc8372011-01-05 10:07:26 +00001272 /* See above. */
1273 if (hole_size < num_bytes)
1274 ret = -ENOSPC;
1275 else
1276 ret = 0;
1277
1278out:
Yan Zheng2b820322008-11-17 21:11:30 -05001279 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001280 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001281 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001282 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001283 return ret;
1284}
1285
Christoph Hellwigb2950862008-12-02 09:54:17 -05001286static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001287 struct btrfs_device *device,
1288 u64 start)
1289{
1290 int ret;
1291 struct btrfs_path *path;
1292 struct btrfs_root *root = device->dev_root;
1293 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001294 struct btrfs_key found_key;
1295 struct extent_buffer *leaf = NULL;
1296 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001297
1298 path = btrfs_alloc_path();
1299 if (!path)
1300 return -ENOMEM;
1301
1302 key.objectid = device->devid;
1303 key.offset = start;
1304 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001305again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001306 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001307 if (ret > 0) {
1308 ret = btrfs_previous_item(root, path, key.objectid,
1309 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001310 if (ret)
1311 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001312 leaf = path->nodes[0];
1313 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1314 extent = btrfs_item_ptr(leaf, path->slots[0],
1315 struct btrfs_dev_extent);
1316 BUG_ON(found_key.offset > start || found_key.offset +
1317 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001318 key = found_key;
1319 btrfs_release_path(path);
1320 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001321 } else if (ret == 0) {
1322 leaf = path->nodes[0];
1323 extent = btrfs_item_ptr(leaf, path->slots[0],
1324 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001325 } else {
1326 btrfs_error(root->fs_info, ret, "Slot search failed");
1327 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001328 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001329
Josef Bacik2bf64752011-09-26 17:12:22 -04001330 if (device->bytes_used > 0) {
1331 u64 len = btrfs_dev_extent_length(leaf, extent);
1332 device->bytes_used -= len;
1333 spin_lock(&root->fs_info->free_chunk_lock);
1334 root->fs_info->free_chunk_space += len;
1335 spin_unlock(&root->fs_info->free_chunk_lock);
1336 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001337 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001338 if (ret) {
1339 btrfs_error(root->fs_info, ret,
1340 "Failed to remove dev extent item");
1341 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001342out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001343 btrfs_free_path(path);
1344 return ret;
1345}
1346
Eric Sandeen48a3b632013-04-25 20:41:01 +00001347static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1348 struct btrfs_device *device,
1349 u64 chunk_tree, u64 chunk_objectid,
1350 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001351{
1352 int ret;
1353 struct btrfs_path *path;
1354 struct btrfs_root *root = device->dev_root;
1355 struct btrfs_dev_extent *extent;
1356 struct extent_buffer *leaf;
1357 struct btrfs_key key;
1358
Chris Masondfe25022008-05-13 13:46:40 -04001359 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001360 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001361 path = btrfs_alloc_path();
1362 if (!path)
1363 return -ENOMEM;
1364
Chris Mason0b86a832008-03-24 15:01:56 -04001365 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001366 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001367 key.type = BTRFS_DEV_EXTENT_KEY;
1368 ret = btrfs_insert_empty_item(trans, root, path, &key,
1369 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001370 if (ret)
1371 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001372
1373 leaf = path->nodes[0];
1374 extent = btrfs_item_ptr(leaf, path->slots[0],
1375 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001376 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1377 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1378 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1379
1380 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001381 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001382
Chris Mason0b86a832008-03-24 15:01:56 -04001383 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1384 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001385out:
Chris Mason0b86a832008-03-24 15:01:56 -04001386 btrfs_free_path(path);
1387 return ret;
1388}
1389
Josef Bacik6df9a952013-06-27 13:22:46 -04001390static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001391{
Josef Bacik6df9a952013-06-27 13:22:46 -04001392 struct extent_map_tree *em_tree;
1393 struct extent_map *em;
1394 struct rb_node *n;
1395 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001396
Josef Bacik6df9a952013-06-27 13:22:46 -04001397 em_tree = &fs_info->mapping_tree.map_tree;
1398 read_lock(&em_tree->lock);
1399 n = rb_last(&em_tree->map);
1400 if (n) {
1401 em = rb_entry(n, struct extent_map, rb_node);
1402 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001403 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001404 read_unlock(&em_tree->lock);
1405
Chris Mason0b86a832008-03-24 15:01:56 -04001406 return ret;
1407}
1408
Ilya Dryomov53f10652013-08-12 14:33:01 +03001409static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1410 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001411{
1412 int ret;
1413 struct btrfs_key key;
1414 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001415 struct btrfs_path *path;
1416
Yan Zheng2b820322008-11-17 21:11:30 -05001417 path = btrfs_alloc_path();
1418 if (!path)
1419 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001420
1421 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1422 key.type = BTRFS_DEV_ITEM_KEY;
1423 key.offset = (u64)-1;
1424
Ilya Dryomov53f10652013-08-12 14:33:01 +03001425 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001426 if (ret < 0)
1427 goto error;
1428
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001429 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001430
Ilya Dryomov53f10652013-08-12 14:33:01 +03001431 ret = btrfs_previous_item(fs_info->chunk_root, path,
1432 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001433 BTRFS_DEV_ITEM_KEY);
1434 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001435 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001436 } else {
1437 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1438 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001439 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001440 }
1441 ret = 0;
1442error:
Yan Zheng2b820322008-11-17 21:11:30 -05001443 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001444 return ret;
1445}
1446
1447/*
1448 * the device information is stored in the chunk root
1449 * the btrfs_device struct should be fully filled in
1450 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001451static int btrfs_add_device(struct btrfs_trans_handle *trans,
1452 struct btrfs_root *root,
1453 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001454{
1455 int ret;
1456 struct btrfs_path *path;
1457 struct btrfs_dev_item *dev_item;
1458 struct extent_buffer *leaf;
1459 struct btrfs_key key;
1460 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001461
1462 root = root->fs_info->chunk_root;
1463
1464 path = btrfs_alloc_path();
1465 if (!path)
1466 return -ENOMEM;
1467
Chris Mason0b86a832008-03-24 15:01:56 -04001468 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1469 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001470 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001471
1472 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001473 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001474 if (ret)
1475 goto out;
1476
1477 leaf = path->nodes[0];
1478 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1479
1480 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001481 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001482 btrfs_set_device_type(leaf, dev_item, device->type);
1483 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1484 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1485 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001486 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1487 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001488 btrfs_set_device_group(leaf, dev_item, 0);
1489 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1490 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001491 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001492
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001493 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001494 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001495 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001496 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001497 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001498
Yan Zheng2b820322008-11-17 21:11:30 -05001499 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001500out:
1501 btrfs_free_path(path);
1502 return ret;
1503}
Chris Mason8f18cf12008-04-25 16:53:30 -04001504
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001505/*
1506 * Function to update ctime/mtime for a given device path.
1507 * Mainly used for ctime/mtime based probe like libblkid.
1508 */
1509static void update_dev_time(char *path_name)
1510{
1511 struct file *filp;
1512
1513 filp = filp_open(path_name, O_RDWR, 0);
1514 if (!filp)
1515 return;
1516 file_update_time(filp);
1517 filp_close(filp, NULL);
1518 return;
1519}
1520
Chris Masona061fc82008-05-07 11:43:44 -04001521static int btrfs_rm_dev_item(struct btrfs_root *root,
1522 struct btrfs_device *device)
1523{
1524 int ret;
1525 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001526 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001527 struct btrfs_trans_handle *trans;
1528
1529 root = root->fs_info->chunk_root;
1530
1531 path = btrfs_alloc_path();
1532 if (!path)
1533 return -ENOMEM;
1534
Yan, Zhenga22285a2010-05-16 10:48:46 -04001535 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001536 if (IS_ERR(trans)) {
1537 btrfs_free_path(path);
1538 return PTR_ERR(trans);
1539 }
Chris Masona061fc82008-05-07 11:43:44 -04001540 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1541 key.type = BTRFS_DEV_ITEM_KEY;
1542 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001543 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001544
1545 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1546 if (ret < 0)
1547 goto out;
1548
1549 if (ret > 0) {
1550 ret = -ENOENT;
1551 goto out;
1552 }
1553
1554 ret = btrfs_del_item(trans, root, path);
1555 if (ret)
1556 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001557out:
1558 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001559 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001560 btrfs_commit_transaction(trans, root);
1561 return ret;
1562}
1563
1564int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1565{
1566 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001567 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001568 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001569 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001570 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001571 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001572 u64 all_avail;
1573 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001574 u64 num_devices;
1575 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001576 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001577 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001578 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001579
Chris Masona061fc82008-05-07 11:43:44 -04001580 mutex_lock(&uuid_mutex);
1581
Miao Xiede98ced2013-01-29 10:13:12 +00001582 do {
1583 seq = read_seqbegin(&root->fs_info->profiles_lock);
1584
1585 all_avail = root->fs_info->avail_data_alloc_bits |
1586 root->fs_info->avail_system_alloc_bits |
1587 root->fs_info->avail_metadata_alloc_bits;
1588 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001589
Stefan Behrens8dabb742012-11-06 13:15:27 +01001590 num_devices = root->fs_info->fs_devices->num_devices;
1591 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1592 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1593 WARN_ON(num_devices < 1);
1594 num_devices--;
1595 }
1596 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1597
1598 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001599 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001600 goto out;
1601 }
1602
Stefan Behrens8dabb742012-11-06 13:15:27 +01001603 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001604 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001605 goto out;
1606 }
1607
David Woodhouse53b381b2013-01-29 18:40:14 -05001608 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1609 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001610 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001611 goto out;
1612 }
1613 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1614 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001615 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001616 goto out;
1617 }
1618
Chris Masondfe25022008-05-13 13:46:40 -04001619 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001620 struct list_head *devices;
1621 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001622
Chris Masondfe25022008-05-13 13:46:40 -04001623 device = NULL;
1624 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001625 /*
1626 * It is safe to read the devices since the volume_mutex
1627 * is held.
1628 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001629 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001630 if (tmp->in_fs_metadata &&
1631 !tmp->is_tgtdev_for_dev_replace &&
1632 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001633 device = tmp;
1634 break;
1635 }
1636 }
1637 bdev = NULL;
1638 bh = NULL;
1639 disk_super = NULL;
1640 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001641 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001642 goto out;
1643 }
Chris Masondfe25022008-05-13 13:46:40 -04001644 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001645 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001646 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001647 root->fs_info->bdev_holder, 0,
1648 &bdev, &bh);
1649 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001650 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001651 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001652 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001653 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001654 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001655 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001656 if (!device) {
1657 ret = -ENOENT;
1658 goto error_brelse;
1659 }
Chris Masondfe25022008-05-13 13:46:40 -04001660 }
Yan Zheng2b820322008-11-17 21:11:30 -05001661
Stefan Behrens63a212a2012-11-05 18:29:28 +01001662 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001663 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001664 goto error_brelse;
1665 }
1666
Yan Zheng2b820322008-11-17 21:11:30 -05001667 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001668 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001669 goto error_brelse;
1670 }
1671
1672 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001673 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001674 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001675 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001676 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001677 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001678 }
Chris Masona061fc82008-05-07 11:43:44 -04001679
Carey Underwoodd7901552013-03-04 16:37:06 -06001680 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001681 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001682 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001683 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001684 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001685
Stefan Behrens63a212a2012-11-05 18:29:28 +01001686 /*
1687 * TODO: the superblock still includes this device in its num_devices
1688 * counter although write_all_supers() is not locked out. This
1689 * could give a filesystem state which requires a degraded mount.
1690 */
Chris Masona061fc82008-05-07 11:43:44 -04001691 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1692 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001693 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001694
Josef Bacik2bf64752011-09-26 17:12:22 -04001695 spin_lock(&root->fs_info->free_chunk_lock);
1696 root->fs_info->free_chunk_space = device->total_bytes -
1697 device->bytes_used;
1698 spin_unlock(&root->fs_info->free_chunk_lock);
1699
Yan Zheng2b820322008-11-17 21:11:30 -05001700 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001701 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001702
1703 /*
1704 * the device list mutex makes sure that we don't change
1705 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001706 * the device supers. Whoever is writing all supers, should
1707 * lock the device list mutex before getting the number of
1708 * devices in the super block (super_copy). Conversely,
1709 * whoever updates the number of devices in the super block
1710 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001711 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001712
1713 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001714 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001715 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001716
Yan Zhenge4404d62008-12-12 10:03:26 -05001717 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001718 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001719
Chris Masoncd02dca2010-12-13 14:56:23 -05001720 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001721 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001722
Yan Zheng2b820322008-11-17 21:11:30 -05001723 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1724 struct btrfs_device, dev_list);
1725 if (device->bdev == root->fs_info->sb->s_bdev)
1726 root->fs_info->sb->s_bdev = next_device->bdev;
1727 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1728 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1729
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001730 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001731 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001732 /* remove sysfs entry */
1733 btrfs_kobj_rm_device(root->fs_info, device);
1734 }
Anand Jain99994cd2014-06-03 11:36:00 +08001735
Xiao Guangrong1f781602011-04-20 10:09:16 +00001736 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001737
David Sterba6c417612011-04-13 15:41:04 +02001738 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1739 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001740 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001741
Xiao Guangrong1f781602011-04-20 10:09:16 +00001742 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001743 struct btrfs_fs_devices *fs_devices;
1744 fs_devices = root->fs_info->fs_devices;
1745 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001746 if (fs_devices->seed == cur_devices) {
1747 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001748 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001749 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001750 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001751 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001752 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001753 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001754 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001755 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001756 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001757 }
1758
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001759 root->fs_info->num_tolerated_disk_barrier_failures =
1760 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1761
Yan Zheng2b820322008-11-17 21:11:30 -05001762 /*
1763 * at this point, the device is zero sized. We want to
1764 * remove it from the devices list and zero out the old super
1765 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001766 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001767 u64 bytenr;
1768 int i;
1769
Chris Masondfe25022008-05-13 13:46:40 -04001770 /* make sure this device isn't detected as part of
1771 * the FS anymore
1772 */
1773 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1774 set_buffer_dirty(bh);
1775 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001776
1777 /* clear the mirror copies of super block on the disk
1778 * being removed, 0th copy is been taken care above and
1779 * the below would take of the rest
1780 */
1781 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1782 bytenr = btrfs_sb_offset(i);
1783 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1784 i_size_read(bdev->bd_inode))
1785 break;
1786
1787 brelse(bh);
1788 bh = __bread(bdev, bytenr / 4096,
1789 BTRFS_SUPER_INFO_SIZE);
1790 if (!bh)
1791 continue;
1792
1793 disk_super = (struct btrfs_super_block *)bh->b_data;
1794
1795 if (btrfs_super_bytenr(disk_super) != bytenr ||
1796 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1797 continue;
1798 }
1799 memset(&disk_super->magic, 0,
1800 sizeof(disk_super->magic));
1801 set_buffer_dirty(bh);
1802 sync_dirty_buffer(bh);
1803 }
Chris Masondfe25022008-05-13 13:46:40 -04001804 }
Chris Masona061fc82008-05-07 11:43:44 -04001805
Chris Masona061fc82008-05-07 11:43:44 -04001806 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001807
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001808 if (bdev) {
1809 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001810 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001811
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001812 /* Update ctime/mtime for device path for libblkid */
1813 update_dev_time(device_path);
1814 }
1815
Chris Masona061fc82008-05-07 11:43:44 -04001816error_brelse:
1817 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001818 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001819 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001820out:
1821 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001822 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001823error_undo:
1824 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001825 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001826 list_add(&device->dev_alloc_list,
1827 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001828 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001829 root->fs_info->fs_devices->rw_devices++;
1830 }
1831 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001832}
1833
Stefan Behrense93c89c2012-11-05 17:33:06 +01001834void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1835 struct btrfs_device *srcdev)
1836{
1837 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001838
Stefan Behrense93c89c2012-11-05 17:33:06 +01001839 list_del_rcu(&srcdev->dev_list);
1840 list_del_rcu(&srcdev->dev_alloc_list);
1841 fs_info->fs_devices->num_devices--;
1842 if (srcdev->missing) {
1843 fs_info->fs_devices->missing_devices--;
1844 fs_info->fs_devices->rw_devices++;
1845 }
1846 if (srcdev->can_discard)
1847 fs_info->fs_devices->num_can_discard--;
Ilya Dryomov13572722013-10-02 20:41:01 +03001848 if (srcdev->bdev) {
Stefan Behrense93c89c2012-11-05 17:33:06 +01001849 fs_info->fs_devices->open_devices--;
1850
Ilya Dryomov13572722013-10-02 20:41:01 +03001851 /* zero out the old super */
1852 btrfs_scratch_superblock(srcdev);
1853 }
1854
Stefan Behrense93c89c2012-11-05 17:33:06 +01001855 call_rcu(&srcdev->rcu, free_device);
1856}
1857
1858void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1859 struct btrfs_device *tgtdev)
1860{
1861 struct btrfs_device *next_device;
1862
1863 WARN_ON(!tgtdev);
1864 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1865 if (tgtdev->bdev) {
1866 btrfs_scratch_superblock(tgtdev);
1867 fs_info->fs_devices->open_devices--;
1868 }
1869 fs_info->fs_devices->num_devices--;
1870 if (tgtdev->can_discard)
1871 fs_info->fs_devices->num_can_discard++;
1872
1873 next_device = list_entry(fs_info->fs_devices->devices.next,
1874 struct btrfs_device, dev_list);
1875 if (tgtdev->bdev == fs_info->sb->s_bdev)
1876 fs_info->sb->s_bdev = next_device->bdev;
1877 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1878 fs_info->fs_devices->latest_bdev = next_device->bdev;
1879 list_del_rcu(&tgtdev->dev_list);
1880
1881 call_rcu(&tgtdev->rcu, free_device);
1882
1883 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1884}
1885
Eric Sandeen48a3b632013-04-25 20:41:01 +00001886static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1887 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001888{
1889 int ret = 0;
1890 struct btrfs_super_block *disk_super;
1891 u64 devid;
1892 u8 *dev_uuid;
1893 struct block_device *bdev;
1894 struct buffer_head *bh;
1895
1896 *device = NULL;
1897 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1898 root->fs_info->bdev_holder, 0, &bdev, &bh);
1899 if (ret)
1900 return ret;
1901 disk_super = (struct btrfs_super_block *)bh->b_data;
1902 devid = btrfs_stack_device_id(&disk_super->dev_item);
1903 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001904 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001905 disk_super->fsid);
1906 brelse(bh);
1907 if (!*device)
1908 ret = -ENOENT;
1909 blkdev_put(bdev, FMODE_READ);
1910 return ret;
1911}
1912
1913int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1914 char *device_path,
1915 struct btrfs_device **device)
1916{
1917 *device = NULL;
1918 if (strcmp(device_path, "missing") == 0) {
1919 struct list_head *devices;
1920 struct btrfs_device *tmp;
1921
1922 devices = &root->fs_info->fs_devices->devices;
1923 /*
1924 * It is safe to read the devices since the volume_mutex
1925 * is held by the caller.
1926 */
1927 list_for_each_entry(tmp, devices, dev_list) {
1928 if (tmp->in_fs_metadata && !tmp->bdev) {
1929 *device = tmp;
1930 break;
1931 }
1932 }
1933
1934 if (!*device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001935 btrfs_err(root->fs_info, "no missing device found");
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001936 return -ENOENT;
1937 }
1938
1939 return 0;
1940 } else {
1941 return btrfs_find_device_by_path(root, device_path, device);
1942 }
1943}
1944
Yan Zheng2b820322008-11-17 21:11:30 -05001945/*
1946 * does all the dirty work required for changing file system's UUID.
1947 */
Li Zefan125ccb02011-12-08 15:07:24 +08001948static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001949{
1950 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1951 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001952 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001953 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001954 struct btrfs_device *device;
1955 u64 super_flags;
1956
1957 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001958 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001959 return -EINVAL;
1960
Ilya Dryomov2208a372013-08-12 14:33:03 +03001961 seed_devices = __alloc_fs_devices();
1962 if (IS_ERR(seed_devices))
1963 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001964
Yan Zhenge4404d62008-12-12 10:03:26 -05001965 old_devices = clone_fs_devices(fs_devices);
1966 if (IS_ERR(old_devices)) {
1967 kfree(seed_devices);
1968 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001969 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001970
Yan Zheng2b820322008-11-17 21:11:30 -05001971 list_add(&old_devices->list, &fs_uuids);
1972
Yan Zhenge4404d62008-12-12 10:03:26 -05001973 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1974 seed_devices->opened = 1;
1975 INIT_LIST_HEAD(&seed_devices->devices);
1976 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001977 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001978
1979 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001980 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1981 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001982
Yan Zhenge4404d62008-12-12 10:03:26 -05001983 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1984 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1985 device->fs_devices = seed_devices;
1986 }
1987
Yan Zheng2b820322008-11-17 21:11:30 -05001988 fs_devices->seeding = 0;
1989 fs_devices->num_devices = 0;
1990 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08001991 fs_devices->missing_devices = 0;
1992 fs_devices->num_can_discard = 0;
1993 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001994 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001995
1996 generate_random_uuid(fs_devices->fsid);
1997 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1998 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01001999 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2000
Yan Zheng2b820322008-11-17 21:11:30 -05002001 super_flags = btrfs_super_flags(disk_super) &
2002 ~BTRFS_SUPER_FLAG_SEEDING;
2003 btrfs_set_super_flags(disk_super, super_flags);
2004
2005 return 0;
2006}
2007
2008/*
2009 * strore the expected generation for seed devices in device items.
2010 */
2011static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2012 struct btrfs_root *root)
2013{
2014 struct btrfs_path *path;
2015 struct extent_buffer *leaf;
2016 struct btrfs_dev_item *dev_item;
2017 struct btrfs_device *device;
2018 struct btrfs_key key;
2019 u8 fs_uuid[BTRFS_UUID_SIZE];
2020 u8 dev_uuid[BTRFS_UUID_SIZE];
2021 u64 devid;
2022 int ret;
2023
2024 path = btrfs_alloc_path();
2025 if (!path)
2026 return -ENOMEM;
2027
2028 root = root->fs_info->chunk_root;
2029 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2030 key.offset = 0;
2031 key.type = BTRFS_DEV_ITEM_KEY;
2032
2033 while (1) {
2034 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2035 if (ret < 0)
2036 goto error;
2037
2038 leaf = path->nodes[0];
2039next_slot:
2040 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2041 ret = btrfs_next_leaf(root, path);
2042 if (ret > 0)
2043 break;
2044 if (ret < 0)
2045 goto error;
2046 leaf = path->nodes[0];
2047 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002048 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002049 continue;
2050 }
2051
2052 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2053 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2054 key.type != BTRFS_DEV_ITEM_KEY)
2055 break;
2056
2057 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2058 struct btrfs_dev_item);
2059 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002060 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002061 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002062 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002063 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002064 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2065 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002066 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002067
2068 if (device->fs_devices->seeding) {
2069 btrfs_set_device_generation(leaf, dev_item,
2070 device->generation);
2071 btrfs_mark_buffer_dirty(leaf);
2072 }
2073
2074 path->slots[0]++;
2075 goto next_slot;
2076 }
2077 ret = 0;
2078error:
2079 btrfs_free_path(path);
2080 return ret;
2081}
2082
Chris Mason788f20e2008-04-28 15:29:42 -04002083int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2084{
Josef Bacikd5e20032011-08-04 14:52:27 +00002085 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002086 struct btrfs_trans_handle *trans;
2087 struct btrfs_device *device;
2088 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002089 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002090 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002091 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04002092 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002093 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002094 int ret = 0;
2095
Yan Zheng2b820322008-11-17 21:11:30 -05002096 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002097 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002098
Li Zefana5d16332011-12-07 20:08:40 -05002099 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002100 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002101 if (IS_ERR(bdev))
2102 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002103
Yan Zheng2b820322008-11-17 21:11:30 -05002104 if (root->fs_info->fs_devices->seeding) {
2105 seeding_dev = 1;
2106 down_write(&sb->s_umount);
2107 mutex_lock(&uuid_mutex);
2108 }
2109
Chris Mason8c8bee12008-09-29 11:19:10 -04002110 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002111
Chris Mason788f20e2008-04-28 15:29:42 -04002112 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002113
2114 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002115 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002116 if (device->bdev == bdev) {
2117 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002118 mutex_unlock(
2119 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002120 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002121 }
2122 }
Liu Bod25628b2012-11-14 14:35:30 +00002123 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002124
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002125 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2126 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002127 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002128 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002129 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002130 }
2131
Josef Bacik606686e2012-06-04 14:03:51 -04002132 name = rcu_string_strdup(device_path, GFP_NOFS);
2133 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002134 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002135 ret = -ENOMEM;
2136 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002137 }
Josef Bacik606686e2012-06-04 14:03:51 -04002138 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002139
Yan, Zhenga22285a2010-05-16 10:48:46 -04002140 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002141 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002142 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002143 kfree(device);
2144 ret = PTR_ERR(trans);
2145 goto error;
2146 }
2147
Yan Zheng2b820322008-11-17 21:11:30 -05002148 lock_chunks(root);
2149
Josef Bacikd5e20032011-08-04 14:52:27 +00002150 q = bdev_get_queue(bdev);
2151 if (blk_queue_discard(q))
2152 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002153 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002154 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002155 device->io_width = root->sectorsize;
2156 device->io_align = root->sectorsize;
2157 device->sector_size = root->sectorsize;
2158 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002159 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002160 device->dev_root = root->fs_info->dev_root;
2161 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002162 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002163 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002164 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002165 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002166 set_blocksize(device->bdev, 4096);
2167
Yan Zheng2b820322008-11-17 21:11:30 -05002168 if (seeding_dev) {
2169 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002170 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002171 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002172 }
2173
2174 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002175
Chris Masone5e9a522009-06-10 15:17:02 -04002176 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002177 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002178 list_add(&device->dev_alloc_list,
2179 &root->fs_info->fs_devices->alloc_list);
2180 root->fs_info->fs_devices->num_devices++;
2181 root->fs_info->fs_devices->open_devices++;
2182 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002183 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00002184 if (device->can_discard)
2185 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05002186 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2187
Josef Bacik2bf64752011-09-26 17:12:22 -04002188 spin_lock(&root->fs_info->free_chunk_lock);
2189 root->fs_info->free_chunk_space += device->total_bytes;
2190 spin_unlock(&root->fs_info->free_chunk_lock);
2191
Chris Masonc2898112009-06-10 09:51:32 -04002192 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2193 root->fs_info->fs_devices->rotating = 1;
2194
David Sterba6c417612011-04-13 15:41:04 +02002195 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2196 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002197 total_bytes + device->total_bytes);
2198
David Sterba6c417612011-04-13 15:41:04 +02002199 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
2200 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002201 total_bytes + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002202
2203 /* add sysfs device entry */
2204 btrfs_kobj_add_device(root->fs_info, device);
2205
Chris Masone5e9a522009-06-10 15:17:02 -04002206 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002207
Yan Zheng2b820322008-11-17 21:11:30 -05002208 if (seeding_dev) {
Anand Jainb2373f22014-06-03 11:36:03 +08002209 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002210 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002211 if (ret) {
2212 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002213 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002214 }
Yan Zheng2b820322008-11-17 21:11:30 -05002215 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002216 if (ret) {
2217 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002218 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002219 }
Anand Jainb2373f22014-06-03 11:36:03 +08002220
2221 /* Sprouting would change fsid of the mounted root,
2222 * so rename the fsid on the sysfs
2223 */
2224 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2225 root->fs_info->fsid);
2226 if (kobject_rename(&root->fs_info->super_kobj, fsid_buf))
2227 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05002228 } else {
2229 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002230 if (ret) {
2231 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002232 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002233 }
Yan Zheng2b820322008-11-17 21:11:30 -05002234 }
2235
Chris Mason913d9522009-03-10 13:17:18 -04002236 /*
2237 * we've got more storage, clear any full flags on the space
2238 * infos
2239 */
2240 btrfs_clear_space_info_full(root->fs_info);
2241
Chris Mason7d9eb122008-07-08 14:19:17 -04002242 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002243 root->fs_info->num_tolerated_disk_barrier_failures =
2244 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002245 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002246
2247 if (seeding_dev) {
2248 mutex_unlock(&uuid_mutex);
2249 up_write(&sb->s_umount);
2250
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002251 if (ret) /* transaction commit */
2252 return ret;
2253
Yan Zheng2b820322008-11-17 21:11:30 -05002254 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002255 if (ret < 0)
2256 btrfs_error(root->fs_info, ret,
2257 "Failed to relocate sys chunks after "
2258 "device initialization. This can be fixed "
2259 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002260 trans = btrfs_attach_transaction(root);
2261 if (IS_ERR(trans)) {
2262 if (PTR_ERR(trans) == -ENOENT)
2263 return 0;
2264 return PTR_ERR(trans);
2265 }
2266 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002267 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002268
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002269 /* Update ctime/mtime for libblkid */
2270 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002271 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002272
2273error_trans:
2274 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002275 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002276 rcu_string_free(device->name);
Anand Jain0d393762014-06-03 11:36:01 +08002277 btrfs_kobj_rm_device(root->fs_info, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002278 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002279error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002280 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002281 if (seeding_dev) {
2282 mutex_unlock(&uuid_mutex);
2283 up_write(&sb->s_umount);
2284 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002285 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002286}
2287
Stefan Behrense93c89c2012-11-05 17:33:06 +01002288int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2289 struct btrfs_device **device_out)
2290{
2291 struct request_queue *q;
2292 struct btrfs_device *device;
2293 struct block_device *bdev;
2294 struct btrfs_fs_info *fs_info = root->fs_info;
2295 struct list_head *devices;
2296 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002297 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002298 int ret = 0;
2299
2300 *device_out = NULL;
2301 if (fs_info->fs_devices->seeding)
2302 return -EINVAL;
2303
2304 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2305 fs_info->bdev_holder);
2306 if (IS_ERR(bdev))
2307 return PTR_ERR(bdev);
2308
2309 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2310
2311 devices = &fs_info->fs_devices->devices;
2312 list_for_each_entry(device, devices, dev_list) {
2313 if (device->bdev == bdev) {
2314 ret = -EEXIST;
2315 goto error;
2316 }
2317 }
2318
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002319 device = btrfs_alloc_device(NULL, &devid, NULL);
2320 if (IS_ERR(device)) {
2321 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002322 goto error;
2323 }
2324
2325 name = rcu_string_strdup(device_path, GFP_NOFS);
2326 if (!name) {
2327 kfree(device);
2328 ret = -ENOMEM;
2329 goto error;
2330 }
2331 rcu_assign_pointer(device->name, name);
2332
2333 q = bdev_get_queue(bdev);
2334 if (blk_queue_discard(q))
2335 device->can_discard = 1;
2336 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2337 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002338 device->generation = 0;
2339 device->io_width = root->sectorsize;
2340 device->io_align = root->sectorsize;
2341 device->sector_size = root->sectorsize;
2342 device->total_bytes = i_size_read(bdev->bd_inode);
2343 device->disk_total_bytes = device->total_bytes;
2344 device->dev_root = fs_info->dev_root;
2345 device->bdev = bdev;
2346 device->in_fs_metadata = 1;
2347 device->is_tgtdev_for_dev_replace = 1;
2348 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002349 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002350 set_blocksize(device->bdev, 4096);
2351 device->fs_devices = fs_info->fs_devices;
2352 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2353 fs_info->fs_devices->num_devices++;
2354 fs_info->fs_devices->open_devices++;
2355 if (device->can_discard)
2356 fs_info->fs_devices->num_can_discard++;
2357 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2358
2359 *device_out = device;
2360 return ret;
2361
2362error:
2363 blkdev_put(bdev, FMODE_EXCL);
2364 return ret;
2365}
2366
2367void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2368 struct btrfs_device *tgtdev)
2369{
2370 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2371 tgtdev->io_width = fs_info->dev_root->sectorsize;
2372 tgtdev->io_align = fs_info->dev_root->sectorsize;
2373 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2374 tgtdev->dev_root = fs_info->dev_root;
2375 tgtdev->in_fs_metadata = 1;
2376}
2377
Chris Masond3977122009-01-05 21:25:51 -05002378static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2379 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002380{
2381 int ret;
2382 struct btrfs_path *path;
2383 struct btrfs_root *root;
2384 struct btrfs_dev_item *dev_item;
2385 struct extent_buffer *leaf;
2386 struct btrfs_key key;
2387
2388 root = device->dev_root->fs_info->chunk_root;
2389
2390 path = btrfs_alloc_path();
2391 if (!path)
2392 return -ENOMEM;
2393
2394 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2395 key.type = BTRFS_DEV_ITEM_KEY;
2396 key.offset = device->devid;
2397
2398 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2399 if (ret < 0)
2400 goto out;
2401
2402 if (ret > 0) {
2403 ret = -ENOENT;
2404 goto out;
2405 }
2406
2407 leaf = path->nodes[0];
2408 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2409
2410 btrfs_set_device_id(leaf, dev_item, device->devid);
2411 btrfs_set_device_type(leaf, dev_item, device->type);
2412 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2413 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2414 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002415 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002416 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2417 btrfs_mark_buffer_dirty(leaf);
2418
2419out:
2420 btrfs_free_path(path);
2421 return ret;
2422}
2423
Chris Mason7d9eb122008-07-08 14:19:17 -04002424static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002425 struct btrfs_device *device, u64 new_size)
2426{
2427 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002428 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002429 u64 old_total = btrfs_super_total_bytes(super_copy);
2430 u64 diff = new_size - device->total_bytes;
2431
Yan Zheng2b820322008-11-17 21:11:30 -05002432 if (!device->writeable)
2433 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002434 if (new_size <= device->total_bytes ||
2435 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002436 return -EINVAL;
2437
Chris Mason8f18cf12008-04-25 16:53:30 -04002438 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002439 device->fs_devices->total_rw_bytes += diff;
2440
2441 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002442 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002443 btrfs_clear_space_info_full(device->dev_root->fs_info);
2444
Chris Mason8f18cf12008-04-25 16:53:30 -04002445 return btrfs_update_device(trans, device);
2446}
2447
Chris Mason7d9eb122008-07-08 14:19:17 -04002448int btrfs_grow_device(struct btrfs_trans_handle *trans,
2449 struct btrfs_device *device, u64 new_size)
2450{
2451 int ret;
2452 lock_chunks(device->dev_root);
2453 ret = __btrfs_grow_device(trans, device, new_size);
2454 unlock_chunks(device->dev_root);
2455 return ret;
2456}
2457
Chris Mason8f18cf12008-04-25 16:53:30 -04002458static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2459 struct btrfs_root *root,
2460 u64 chunk_tree, u64 chunk_objectid,
2461 u64 chunk_offset)
2462{
2463 int ret;
2464 struct btrfs_path *path;
2465 struct btrfs_key key;
2466
2467 root = root->fs_info->chunk_root;
2468 path = btrfs_alloc_path();
2469 if (!path)
2470 return -ENOMEM;
2471
2472 key.objectid = chunk_objectid;
2473 key.offset = chunk_offset;
2474 key.type = BTRFS_CHUNK_ITEM_KEY;
2475
2476 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002477 if (ret < 0)
2478 goto out;
2479 else if (ret > 0) { /* Logic error or corruption */
2480 btrfs_error(root->fs_info, -ENOENT,
2481 "Failed lookup while freeing chunk.");
2482 ret = -ENOENT;
2483 goto out;
2484 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002485
2486 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002487 if (ret < 0)
2488 btrfs_error(root->fs_info, ret,
2489 "Failed to delete chunk item.");
2490out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002491 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002492 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002493}
2494
Christoph Hellwigb2950862008-12-02 09:54:17 -05002495static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002496 chunk_offset)
2497{
David Sterba6c417612011-04-13 15:41:04 +02002498 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002499 struct btrfs_disk_key *disk_key;
2500 struct btrfs_chunk *chunk;
2501 u8 *ptr;
2502 int ret = 0;
2503 u32 num_stripes;
2504 u32 array_size;
2505 u32 len = 0;
2506 u32 cur;
2507 struct btrfs_key key;
2508
2509 array_size = btrfs_super_sys_array_size(super_copy);
2510
2511 ptr = super_copy->sys_chunk_array;
2512 cur = 0;
2513
2514 while (cur < array_size) {
2515 disk_key = (struct btrfs_disk_key *)ptr;
2516 btrfs_disk_key_to_cpu(&key, disk_key);
2517
2518 len = sizeof(*disk_key);
2519
2520 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2521 chunk = (struct btrfs_chunk *)(ptr + len);
2522 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2523 len += btrfs_chunk_item_size(num_stripes);
2524 } else {
2525 ret = -EIO;
2526 break;
2527 }
2528 if (key.objectid == chunk_objectid &&
2529 key.offset == chunk_offset) {
2530 memmove(ptr, ptr + len, array_size - (cur + len));
2531 array_size -= len;
2532 btrfs_set_super_sys_array_size(super_copy, array_size);
2533 } else {
2534 ptr += len;
2535 cur += len;
2536 }
2537 }
2538 return ret;
2539}
2540
Christoph Hellwigb2950862008-12-02 09:54:17 -05002541static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002542 u64 chunk_tree, u64 chunk_objectid,
2543 u64 chunk_offset)
2544{
2545 struct extent_map_tree *em_tree;
2546 struct btrfs_root *extent_root;
2547 struct btrfs_trans_handle *trans;
2548 struct extent_map *em;
2549 struct map_lookup *map;
2550 int ret;
2551 int i;
2552
2553 root = root->fs_info->chunk_root;
2554 extent_root = root->fs_info->extent_root;
2555 em_tree = &root->fs_info->mapping_tree.map_tree;
2556
Josef Bacikba1bf482009-09-11 16:11:19 -04002557 ret = btrfs_can_relocate(extent_root, chunk_offset);
2558 if (ret)
2559 return -ENOSPC;
2560
Chris Mason8f18cf12008-04-25 16:53:30 -04002561 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002562 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002563 if (ret)
2564 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002565
Yan, Zhenga22285a2010-05-16 10:48:46 -04002566 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002567 if (IS_ERR(trans)) {
2568 ret = PTR_ERR(trans);
2569 btrfs_std_error(root->fs_info, ret);
2570 return ret;
2571 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002572
Chris Mason7d9eb122008-07-08 14:19:17 -04002573 lock_chunks(root);
2574
Chris Mason8f18cf12008-04-25 16:53:30 -04002575 /*
2576 * step two, delete the device extents and the
2577 * chunk tree entries
2578 */
Chris Mason890871b2009-09-02 16:24:52 -04002579 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002580 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002581 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002582
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002583 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002584 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002585 map = (struct map_lookup *)em->bdev;
2586
2587 for (i = 0; i < map->num_stripes; i++) {
2588 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2589 map->stripes[i].physical);
2590 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002591
Chris Masondfe25022008-05-13 13:46:40 -04002592 if (map->stripes[i].dev) {
2593 ret = btrfs_update_device(trans, map->stripes[i].dev);
2594 BUG_ON(ret);
2595 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002596 }
2597 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2598 chunk_offset);
2599
2600 BUG_ON(ret);
2601
liubo1abe9b82011-03-24 11:18:59 +00002602 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2603
Chris Mason8f18cf12008-04-25 16:53:30 -04002604 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2605 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2606 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002607 }
2608
Zheng Yan1a40e232008-09-26 10:09:34 -04002609 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2610 BUG_ON(ret);
2611
Chris Mason890871b2009-09-02 16:24:52 -04002612 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002613 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002614 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002615
Chris Mason8f18cf12008-04-25 16:53:30 -04002616 /* once for the tree */
2617 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002618 /* once for us */
2619 free_extent_map(em);
2620
Chris Mason7d9eb122008-07-08 14:19:17 -04002621 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002622 btrfs_end_transaction(trans, root);
2623 return 0;
2624}
2625
Yan Zheng2b820322008-11-17 21:11:30 -05002626static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2627{
2628 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2629 struct btrfs_path *path;
2630 struct extent_buffer *leaf;
2631 struct btrfs_chunk *chunk;
2632 struct btrfs_key key;
2633 struct btrfs_key found_key;
2634 u64 chunk_tree = chunk_root->root_key.objectid;
2635 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002636 bool retried = false;
2637 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002638 int ret;
2639
2640 path = btrfs_alloc_path();
2641 if (!path)
2642 return -ENOMEM;
2643
Josef Bacikba1bf482009-09-11 16:11:19 -04002644again:
Yan Zheng2b820322008-11-17 21:11:30 -05002645 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2646 key.offset = (u64)-1;
2647 key.type = BTRFS_CHUNK_ITEM_KEY;
2648
2649 while (1) {
2650 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2651 if (ret < 0)
2652 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002653 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002654
2655 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2656 key.type);
2657 if (ret < 0)
2658 goto error;
2659 if (ret > 0)
2660 break;
2661
2662 leaf = path->nodes[0];
2663 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2664
2665 chunk = btrfs_item_ptr(leaf, path->slots[0],
2666 struct btrfs_chunk);
2667 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002668 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002669
2670 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2671 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2672 found_key.objectid,
2673 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002674 if (ret == -ENOSPC)
2675 failed++;
2676 else if (ret)
2677 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002678 }
2679
2680 if (found_key.offset == 0)
2681 break;
2682 key.offset = found_key.offset - 1;
2683 }
2684 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002685 if (failed && !retried) {
2686 failed = 0;
2687 retried = true;
2688 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302689 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002690 ret = -ENOSPC;
2691 }
Yan Zheng2b820322008-11-17 21:11:30 -05002692error:
2693 btrfs_free_path(path);
2694 return ret;
2695}
2696
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002697static int insert_balance_item(struct btrfs_root *root,
2698 struct btrfs_balance_control *bctl)
2699{
2700 struct btrfs_trans_handle *trans;
2701 struct btrfs_balance_item *item;
2702 struct btrfs_disk_balance_args disk_bargs;
2703 struct btrfs_path *path;
2704 struct extent_buffer *leaf;
2705 struct btrfs_key key;
2706 int ret, err;
2707
2708 path = btrfs_alloc_path();
2709 if (!path)
2710 return -ENOMEM;
2711
2712 trans = btrfs_start_transaction(root, 0);
2713 if (IS_ERR(trans)) {
2714 btrfs_free_path(path);
2715 return PTR_ERR(trans);
2716 }
2717
2718 key.objectid = BTRFS_BALANCE_OBJECTID;
2719 key.type = BTRFS_BALANCE_ITEM_KEY;
2720 key.offset = 0;
2721
2722 ret = btrfs_insert_empty_item(trans, root, path, &key,
2723 sizeof(*item));
2724 if (ret)
2725 goto out;
2726
2727 leaf = path->nodes[0];
2728 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2729
2730 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2731
2732 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2733 btrfs_set_balance_data(leaf, item, &disk_bargs);
2734 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2735 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2736 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2737 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2738
2739 btrfs_set_balance_flags(leaf, item, bctl->flags);
2740
2741 btrfs_mark_buffer_dirty(leaf);
2742out:
2743 btrfs_free_path(path);
2744 err = btrfs_commit_transaction(trans, root);
2745 if (err && !ret)
2746 ret = err;
2747 return ret;
2748}
2749
2750static int del_balance_item(struct btrfs_root *root)
2751{
2752 struct btrfs_trans_handle *trans;
2753 struct btrfs_path *path;
2754 struct btrfs_key key;
2755 int ret, err;
2756
2757 path = btrfs_alloc_path();
2758 if (!path)
2759 return -ENOMEM;
2760
2761 trans = btrfs_start_transaction(root, 0);
2762 if (IS_ERR(trans)) {
2763 btrfs_free_path(path);
2764 return PTR_ERR(trans);
2765 }
2766
2767 key.objectid = BTRFS_BALANCE_OBJECTID;
2768 key.type = BTRFS_BALANCE_ITEM_KEY;
2769 key.offset = 0;
2770
2771 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2772 if (ret < 0)
2773 goto out;
2774 if (ret > 0) {
2775 ret = -ENOENT;
2776 goto out;
2777 }
2778
2779 ret = btrfs_del_item(trans, root, path);
2780out:
2781 btrfs_free_path(path);
2782 err = btrfs_commit_transaction(trans, root);
2783 if (err && !ret)
2784 ret = err;
2785 return ret;
2786}
2787
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002788/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002789 * This is a heuristic used to reduce the number of chunks balanced on
2790 * resume after balance was interrupted.
2791 */
2792static void update_balance_args(struct btrfs_balance_control *bctl)
2793{
2794 /*
2795 * Turn on soft mode for chunk types that were being converted.
2796 */
2797 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2798 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2799 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2800 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2801 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2802 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2803
2804 /*
2805 * Turn on usage filter if is not already used. The idea is
2806 * that chunks that we have already balanced should be
2807 * reasonably full. Don't do it for chunks that are being
2808 * converted - that will keep us from relocating unconverted
2809 * (albeit full) chunks.
2810 */
2811 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2812 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2813 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2814 bctl->data.usage = 90;
2815 }
2816 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2817 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2818 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2819 bctl->sys.usage = 90;
2820 }
2821 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2822 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2823 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2824 bctl->meta.usage = 90;
2825 }
2826}
2827
2828/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002829 * Should be called with both balance and volume mutexes held to
2830 * serialize other volume operations (add_dev/rm_dev/resize) with
2831 * restriper. Same goes for unset_balance_control.
2832 */
2833static void set_balance_control(struct btrfs_balance_control *bctl)
2834{
2835 struct btrfs_fs_info *fs_info = bctl->fs_info;
2836
2837 BUG_ON(fs_info->balance_ctl);
2838
2839 spin_lock(&fs_info->balance_lock);
2840 fs_info->balance_ctl = bctl;
2841 spin_unlock(&fs_info->balance_lock);
2842}
2843
2844static void unset_balance_control(struct btrfs_fs_info *fs_info)
2845{
2846 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2847
2848 BUG_ON(!fs_info->balance_ctl);
2849
2850 spin_lock(&fs_info->balance_lock);
2851 fs_info->balance_ctl = NULL;
2852 spin_unlock(&fs_info->balance_lock);
2853
2854 kfree(bctl);
2855}
2856
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002857/*
2858 * Balance filters. Return 1 if chunk should be filtered out
2859 * (should not be balanced).
2860 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002861static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002862 struct btrfs_balance_args *bargs)
2863{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002864 chunk_type = chunk_to_extended(chunk_type) &
2865 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002866
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002867 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002868 return 0;
2869
2870 return 1;
2871}
2872
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002873static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2874 struct btrfs_balance_args *bargs)
2875{
2876 struct btrfs_block_group_cache *cache;
2877 u64 chunk_used, user_thresh;
2878 int ret = 1;
2879
2880 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2881 chunk_used = btrfs_block_group_used(&cache->item);
2882
Ilya Dryomova105bb82013-01-21 15:15:56 +02002883 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002884 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002885 else if (bargs->usage > 100)
2886 user_thresh = cache->key.offset;
2887 else
2888 user_thresh = div_factor_fine(cache->key.offset,
2889 bargs->usage);
2890
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002891 if (chunk_used < user_thresh)
2892 ret = 0;
2893
2894 btrfs_put_block_group(cache);
2895 return ret;
2896}
2897
Ilya Dryomov409d4042012-01-16 22:04:47 +02002898static int chunk_devid_filter(struct extent_buffer *leaf,
2899 struct btrfs_chunk *chunk,
2900 struct btrfs_balance_args *bargs)
2901{
2902 struct btrfs_stripe *stripe;
2903 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2904 int i;
2905
2906 for (i = 0; i < num_stripes; i++) {
2907 stripe = btrfs_stripe_nr(chunk, i);
2908 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2909 return 0;
2910 }
2911
2912 return 1;
2913}
2914
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002915/* [pstart, pend) */
2916static int chunk_drange_filter(struct extent_buffer *leaf,
2917 struct btrfs_chunk *chunk,
2918 u64 chunk_offset,
2919 struct btrfs_balance_args *bargs)
2920{
2921 struct btrfs_stripe *stripe;
2922 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2923 u64 stripe_offset;
2924 u64 stripe_length;
2925 int factor;
2926 int i;
2927
2928 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2929 return 0;
2930
2931 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002932 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2933 factor = num_stripes / 2;
2934 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2935 factor = num_stripes - 1;
2936 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2937 factor = num_stripes - 2;
2938 } else {
2939 factor = num_stripes;
2940 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002941
2942 for (i = 0; i < num_stripes; i++) {
2943 stripe = btrfs_stripe_nr(chunk, i);
2944 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2945 continue;
2946
2947 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2948 stripe_length = btrfs_chunk_length(leaf, chunk);
2949 do_div(stripe_length, factor);
2950
2951 if (stripe_offset < bargs->pend &&
2952 stripe_offset + stripe_length > bargs->pstart)
2953 return 0;
2954 }
2955
2956 return 1;
2957}
2958
Ilya Dryomovea671762012-01-16 22:04:48 +02002959/* [vstart, vend) */
2960static int chunk_vrange_filter(struct extent_buffer *leaf,
2961 struct btrfs_chunk *chunk,
2962 u64 chunk_offset,
2963 struct btrfs_balance_args *bargs)
2964{
2965 if (chunk_offset < bargs->vend &&
2966 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2967 /* at least part of the chunk is inside this vrange */
2968 return 0;
2969
2970 return 1;
2971}
2972
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002973static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002974 struct btrfs_balance_args *bargs)
2975{
2976 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2977 return 0;
2978
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002979 chunk_type = chunk_to_extended(chunk_type) &
2980 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002981
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002982 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002983 return 1;
2984
2985 return 0;
2986}
2987
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002988static int should_balance_chunk(struct btrfs_root *root,
2989 struct extent_buffer *leaf,
2990 struct btrfs_chunk *chunk, u64 chunk_offset)
2991{
2992 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2993 struct btrfs_balance_args *bargs = NULL;
2994 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2995
2996 /* type filter */
2997 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2998 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2999 return 0;
3000 }
3001
3002 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3003 bargs = &bctl->data;
3004 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3005 bargs = &bctl->sys;
3006 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3007 bargs = &bctl->meta;
3008
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003009 /* profiles filter */
3010 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3011 chunk_profiles_filter(chunk_type, bargs)) {
3012 return 0;
3013 }
3014
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003015 /* usage filter */
3016 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3017 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3018 return 0;
3019 }
3020
Ilya Dryomov409d4042012-01-16 22:04:47 +02003021 /* devid filter */
3022 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3023 chunk_devid_filter(leaf, chunk, bargs)) {
3024 return 0;
3025 }
3026
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003027 /* drange filter, makes sense only with devid filter */
3028 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3029 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3030 return 0;
3031 }
3032
Ilya Dryomovea671762012-01-16 22:04:48 +02003033 /* vrange filter */
3034 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3035 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3036 return 0;
3037 }
3038
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003039 /* soft profile changing mode */
3040 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3041 chunk_soft_convert_filter(chunk_type, bargs)) {
3042 return 0;
3043 }
3044
David Sterba7d824b62014-05-07 17:37:51 +02003045 /*
3046 * limited by count, must be the last filter
3047 */
3048 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3049 if (bargs->limit == 0)
3050 return 0;
3051 else
3052 bargs->limit--;
3053 }
3054
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003055 return 1;
3056}
3057
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003058static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003059{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003060 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003061 struct btrfs_root *chunk_root = fs_info->chunk_root;
3062 struct btrfs_root *dev_root = fs_info->dev_root;
3063 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003064 struct btrfs_device *device;
3065 u64 old_size;
3066 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003067 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003068 struct btrfs_path *path;
3069 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003070 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003071 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003072 struct extent_buffer *leaf;
3073 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003074 int ret;
3075 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003076 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003077 u64 limit_data = bctl->data.limit;
3078 u64 limit_meta = bctl->meta.limit;
3079 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003080
Chris Masonec44a352008-04-28 15:29:52 -04003081 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003082 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003083 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04003084 old_size = device->total_bytes;
3085 size_to_free = div_factor(old_size, 1);
3086 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003087 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003088 device->total_bytes - device->bytes_used > size_to_free ||
3089 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003090 continue;
3091
3092 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003093 if (ret == -ENOSPC)
3094 break;
Chris Masonec44a352008-04-28 15:29:52 -04003095 BUG_ON(ret);
3096
Yan, Zhenga22285a2010-05-16 10:48:46 -04003097 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003098 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003099
3100 ret = btrfs_grow_device(trans, device, old_size);
3101 BUG_ON(ret);
3102
3103 btrfs_end_transaction(trans, dev_root);
3104 }
3105
3106 /* step two, relocate all the chunks */
3107 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003108 if (!path) {
3109 ret = -ENOMEM;
3110 goto error;
3111 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003112
3113 /* zero out stat counters */
3114 spin_lock(&fs_info->balance_lock);
3115 memset(&bctl->stat, 0, sizeof(bctl->stat));
3116 spin_unlock(&fs_info->balance_lock);
3117again:
David Sterba7d824b62014-05-07 17:37:51 +02003118 if (!counting) {
3119 bctl->data.limit = limit_data;
3120 bctl->meta.limit = limit_meta;
3121 bctl->sys.limit = limit_sys;
3122 }
Chris Masonec44a352008-04-28 15:29:52 -04003123 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3124 key.offset = (u64)-1;
3125 key.type = BTRFS_CHUNK_ITEM_KEY;
3126
Chris Masond3977122009-01-05 21:25:51 -05003127 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003128 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003129 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003130 ret = -ECANCELED;
3131 goto error;
3132 }
3133
Chris Masonec44a352008-04-28 15:29:52 -04003134 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3135 if (ret < 0)
3136 goto error;
3137
3138 /*
3139 * this shouldn't happen, it means the last relocate
3140 * failed
3141 */
3142 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003143 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003144
3145 ret = btrfs_previous_item(chunk_root, path, 0,
3146 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003147 if (ret) {
3148 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003149 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003150 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003151
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003152 leaf = path->nodes[0];
3153 slot = path->slots[0];
3154 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3155
Chris Masonec44a352008-04-28 15:29:52 -04003156 if (found_key.objectid != key.objectid)
3157 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04003158
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003159 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3160
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003161 if (!counting) {
3162 spin_lock(&fs_info->balance_lock);
3163 bctl->stat.considered++;
3164 spin_unlock(&fs_info->balance_lock);
3165 }
3166
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003167 ret = should_balance_chunk(chunk_root, leaf, chunk,
3168 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003169 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003170 if (!ret)
3171 goto loop;
3172
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003173 if (counting) {
3174 spin_lock(&fs_info->balance_lock);
3175 bctl->stat.expected++;
3176 spin_unlock(&fs_info->balance_lock);
3177 goto loop;
3178 }
3179
Chris Masonec44a352008-04-28 15:29:52 -04003180 ret = btrfs_relocate_chunk(chunk_root,
3181 chunk_root->root_key.objectid,
3182 found_key.objectid,
3183 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003184 if (ret && ret != -ENOSPC)
3185 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003186 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003187 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003188 } else {
3189 spin_lock(&fs_info->balance_lock);
3190 bctl->stat.completed++;
3191 spin_unlock(&fs_info->balance_lock);
3192 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003193loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003194 if (found_key.offset == 0)
3195 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003196 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003197 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003198
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003199 if (counting) {
3200 btrfs_release_path(path);
3201 counting = false;
3202 goto again;
3203 }
Chris Masonec44a352008-04-28 15:29:52 -04003204error:
3205 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003206 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003207 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003208 enospc_errors);
3209 if (!ret)
3210 ret = -ENOSPC;
3211 }
3212
Chris Masonec44a352008-04-28 15:29:52 -04003213 return ret;
3214}
3215
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003216/**
3217 * alloc_profile_is_valid - see if a given profile is valid and reduced
3218 * @flags: profile to validate
3219 * @extended: if true @flags is treated as an extended profile
3220 */
3221static int alloc_profile_is_valid(u64 flags, int extended)
3222{
3223 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3224 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3225
3226 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3227
3228 /* 1) check that all other bits are zeroed */
3229 if (flags & ~mask)
3230 return 0;
3231
3232 /* 2) see if profile is reduced */
3233 if (flags == 0)
3234 return !extended; /* "0" is valid for usual profiles */
3235
3236 /* true if exactly one bit set */
3237 return (flags & (flags - 1)) == 0;
3238}
3239
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003240static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3241{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003242 /* cancel requested || normal exit path */
3243 return atomic_read(&fs_info->balance_cancel_req) ||
3244 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3245 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003246}
3247
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003248static void __cancel_balance(struct btrfs_fs_info *fs_info)
3249{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003250 int ret;
3251
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003252 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003253 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003254 if (ret)
3255 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003256
3257 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003258}
3259
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003260/*
3261 * Should be called with both balance and volume mutexes held
3262 */
3263int btrfs_balance(struct btrfs_balance_control *bctl,
3264 struct btrfs_ioctl_balance_args *bargs)
3265{
3266 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003267 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003268 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003269 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003270 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003271 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003272
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003273 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003274 atomic_read(&fs_info->balance_pause_req) ||
3275 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003276 ret = -EINVAL;
3277 goto out;
3278 }
3279
Ilya Dryomove4837f82012-03-27 17:09:17 +03003280 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3281 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3282 mixed = 1;
3283
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003284 /*
3285 * In case of mixed groups both data and meta should be picked,
3286 * and identical options should be given for both of them.
3287 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003288 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3289 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003290 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3291 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3292 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003293 btrfs_err(fs_info, "with mixed groups data and "
3294 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003295 ret = -EINVAL;
3296 goto out;
3297 }
3298 }
3299
Stefan Behrens8dabb742012-11-06 13:15:27 +01003300 num_devices = fs_info->fs_devices->num_devices;
3301 btrfs_dev_replace_lock(&fs_info->dev_replace);
3302 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3303 BUG_ON(num_devices < 1);
3304 num_devices--;
3305 }
3306 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003307 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003308 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003309 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003310 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003311 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003312 if (num_devices > 2)
3313 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3314 if (num_devices > 3)
3315 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3316 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003317 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3318 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3319 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003320 btrfs_err(fs_info, "unable to start balance with target "
3321 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003322 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003323 ret = -EINVAL;
3324 goto out;
3325 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003326 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3327 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3328 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003329 btrfs_err(fs_info,
3330 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003331 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003332 ret = -EINVAL;
3333 goto out;
3334 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003335 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3336 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3337 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003338 btrfs_err(fs_info,
3339 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003340 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003341 ret = -EINVAL;
3342 goto out;
3343 }
3344
Ilya Dryomove4837f82012-03-27 17:09:17 +03003345 /* allow dup'ed data chunks only in mixed mode */
3346 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003347 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003348 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003349 ret = -EINVAL;
3350 goto out;
3351 }
3352
3353 /* allow to reduce meta or sys integrity only if force set */
3354 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003355 BTRFS_BLOCK_GROUP_RAID10 |
3356 BTRFS_BLOCK_GROUP_RAID5 |
3357 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003358 do {
3359 seq = read_seqbegin(&fs_info->profiles_lock);
3360
3361 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3362 (fs_info->avail_system_alloc_bits & allowed) &&
3363 !(bctl->sys.target & allowed)) ||
3364 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3365 (fs_info->avail_metadata_alloc_bits & allowed) &&
3366 !(bctl->meta.target & allowed))) {
3367 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003368 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003369 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003370 btrfs_err(fs_info, "balance will reduce metadata "
3371 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003372 ret = -EINVAL;
3373 goto out;
3374 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003375 }
Miao Xiede98ced2013-01-29 10:13:12 +00003376 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003377
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003378 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3379 int num_tolerated_disk_barrier_failures;
3380 u64 target = bctl->sys.target;
3381
3382 num_tolerated_disk_barrier_failures =
3383 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3384 if (num_tolerated_disk_barrier_failures > 0 &&
3385 (target &
3386 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3387 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3388 num_tolerated_disk_barrier_failures = 0;
3389 else if (num_tolerated_disk_barrier_failures > 1 &&
3390 (target &
3391 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3392 num_tolerated_disk_barrier_failures = 1;
3393
3394 fs_info->num_tolerated_disk_barrier_failures =
3395 num_tolerated_disk_barrier_failures;
3396 }
3397
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003398 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003399 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003400 goto out;
3401
Ilya Dryomov59641012012-01-16 22:04:48 +02003402 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3403 BUG_ON(ret == -EEXIST);
3404 set_balance_control(bctl);
3405 } else {
3406 BUG_ON(ret != -EEXIST);
3407 spin_lock(&fs_info->balance_lock);
3408 update_balance_args(bctl);
3409 spin_unlock(&fs_info->balance_lock);
3410 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003411
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003412 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003413 mutex_unlock(&fs_info->balance_mutex);
3414
3415 ret = __btrfs_balance(fs_info);
3416
3417 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003418 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003419
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003420 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3421 fs_info->num_tolerated_disk_barrier_failures =
3422 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3423 }
3424
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003425 if (bargs) {
3426 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003427 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003428 }
3429
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003430 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3431 balance_need_close(fs_info)) {
3432 __cancel_balance(fs_info);
3433 }
3434
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003435 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003436
3437 return ret;
3438out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003439 if (bctl->flags & BTRFS_BALANCE_RESUME)
3440 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003441 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003442 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003443 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3444 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003445 return ret;
3446}
3447
3448static int balance_kthread(void *data)
3449{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003450 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003451 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003452
3453 mutex_lock(&fs_info->volume_mutex);
3454 mutex_lock(&fs_info->balance_mutex);
3455
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003456 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003457 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003458 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003459 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003460
3461 mutex_unlock(&fs_info->balance_mutex);
3462 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003463
Ilya Dryomov59641012012-01-16 22:04:48 +02003464 return ret;
3465}
3466
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003467int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3468{
3469 struct task_struct *tsk;
3470
3471 spin_lock(&fs_info->balance_lock);
3472 if (!fs_info->balance_ctl) {
3473 spin_unlock(&fs_info->balance_lock);
3474 return 0;
3475 }
3476 spin_unlock(&fs_info->balance_lock);
3477
3478 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003479 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003480 return 0;
3481 }
3482
3483 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303484 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003485}
3486
Ilya Dryomov68310a52012-06-22 12:24:12 -06003487int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003488{
Ilya Dryomov59641012012-01-16 22:04:48 +02003489 struct btrfs_balance_control *bctl;
3490 struct btrfs_balance_item *item;
3491 struct btrfs_disk_balance_args disk_bargs;
3492 struct btrfs_path *path;
3493 struct extent_buffer *leaf;
3494 struct btrfs_key key;
3495 int ret;
3496
3497 path = btrfs_alloc_path();
3498 if (!path)
3499 return -ENOMEM;
3500
Ilya Dryomov68310a52012-06-22 12:24:12 -06003501 key.objectid = BTRFS_BALANCE_OBJECTID;
3502 key.type = BTRFS_BALANCE_ITEM_KEY;
3503 key.offset = 0;
3504
3505 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3506 if (ret < 0)
3507 goto out;
3508 if (ret > 0) { /* ret = -ENOENT; */
3509 ret = 0;
3510 goto out;
3511 }
3512
Ilya Dryomov59641012012-01-16 22:04:48 +02003513 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3514 if (!bctl) {
3515 ret = -ENOMEM;
3516 goto out;
3517 }
3518
Ilya Dryomov59641012012-01-16 22:04:48 +02003519 leaf = path->nodes[0];
3520 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3521
Ilya Dryomov68310a52012-06-22 12:24:12 -06003522 bctl->fs_info = fs_info;
3523 bctl->flags = btrfs_balance_flags(leaf, item);
3524 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003525
3526 btrfs_balance_data(leaf, item, &disk_bargs);
3527 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3528 btrfs_balance_meta(leaf, item, &disk_bargs);
3529 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3530 btrfs_balance_sys(leaf, item, &disk_bargs);
3531 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3532
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003533 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3534
Ilya Dryomov68310a52012-06-22 12:24:12 -06003535 mutex_lock(&fs_info->volume_mutex);
3536 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003537
Ilya Dryomov68310a52012-06-22 12:24:12 -06003538 set_balance_control(bctl);
3539
3540 mutex_unlock(&fs_info->balance_mutex);
3541 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003542out:
3543 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003544 return ret;
3545}
3546
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003547int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3548{
3549 int ret = 0;
3550
3551 mutex_lock(&fs_info->balance_mutex);
3552 if (!fs_info->balance_ctl) {
3553 mutex_unlock(&fs_info->balance_mutex);
3554 return -ENOTCONN;
3555 }
3556
3557 if (atomic_read(&fs_info->balance_running)) {
3558 atomic_inc(&fs_info->balance_pause_req);
3559 mutex_unlock(&fs_info->balance_mutex);
3560
3561 wait_event(fs_info->balance_wait_q,
3562 atomic_read(&fs_info->balance_running) == 0);
3563
3564 mutex_lock(&fs_info->balance_mutex);
3565 /* we are good with balance_ctl ripped off from under us */
3566 BUG_ON(atomic_read(&fs_info->balance_running));
3567 atomic_dec(&fs_info->balance_pause_req);
3568 } else {
3569 ret = -ENOTCONN;
3570 }
3571
3572 mutex_unlock(&fs_info->balance_mutex);
3573 return ret;
3574}
3575
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003576int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3577{
Ilya Dryomove649e582013-10-10 20:40:21 +03003578 if (fs_info->sb->s_flags & MS_RDONLY)
3579 return -EROFS;
3580
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003581 mutex_lock(&fs_info->balance_mutex);
3582 if (!fs_info->balance_ctl) {
3583 mutex_unlock(&fs_info->balance_mutex);
3584 return -ENOTCONN;
3585 }
3586
3587 atomic_inc(&fs_info->balance_cancel_req);
3588 /*
3589 * if we are running just wait and return, balance item is
3590 * deleted in btrfs_balance in this case
3591 */
3592 if (atomic_read(&fs_info->balance_running)) {
3593 mutex_unlock(&fs_info->balance_mutex);
3594 wait_event(fs_info->balance_wait_q,
3595 atomic_read(&fs_info->balance_running) == 0);
3596 mutex_lock(&fs_info->balance_mutex);
3597 } else {
3598 /* __cancel_balance needs volume_mutex */
3599 mutex_unlock(&fs_info->balance_mutex);
3600 mutex_lock(&fs_info->volume_mutex);
3601 mutex_lock(&fs_info->balance_mutex);
3602
3603 if (fs_info->balance_ctl)
3604 __cancel_balance(fs_info);
3605
3606 mutex_unlock(&fs_info->volume_mutex);
3607 }
3608
3609 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3610 atomic_dec(&fs_info->balance_cancel_req);
3611 mutex_unlock(&fs_info->balance_mutex);
3612 return 0;
3613}
3614
Stefan Behrens803b2f52013-08-15 17:11:21 +02003615static int btrfs_uuid_scan_kthread(void *data)
3616{
3617 struct btrfs_fs_info *fs_info = data;
3618 struct btrfs_root *root = fs_info->tree_root;
3619 struct btrfs_key key;
3620 struct btrfs_key max_key;
3621 struct btrfs_path *path = NULL;
3622 int ret = 0;
3623 struct extent_buffer *eb;
3624 int slot;
3625 struct btrfs_root_item root_item;
3626 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003627 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003628
3629 path = btrfs_alloc_path();
3630 if (!path) {
3631 ret = -ENOMEM;
3632 goto out;
3633 }
3634
3635 key.objectid = 0;
3636 key.type = BTRFS_ROOT_ITEM_KEY;
3637 key.offset = 0;
3638
3639 max_key.objectid = (u64)-1;
3640 max_key.type = BTRFS_ROOT_ITEM_KEY;
3641 max_key.offset = (u64)-1;
3642
3643 path->keep_locks = 1;
3644
3645 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003646 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003647 if (ret) {
3648 if (ret > 0)
3649 ret = 0;
3650 break;
3651 }
3652
3653 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3654 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3655 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3656 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3657 goto skip;
3658
3659 eb = path->nodes[0];
3660 slot = path->slots[0];
3661 item_size = btrfs_item_size_nr(eb, slot);
3662 if (item_size < sizeof(root_item))
3663 goto skip;
3664
Stefan Behrens803b2f52013-08-15 17:11:21 +02003665 read_extent_buffer(eb, &root_item,
3666 btrfs_item_ptr_offset(eb, slot),
3667 (int)sizeof(root_item));
3668 if (btrfs_root_refs(&root_item) == 0)
3669 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003670
3671 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3672 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3673 if (trans)
3674 goto update_tree;
3675
3676 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003677 /*
3678 * 1 - subvol uuid item
3679 * 1 - received_subvol uuid item
3680 */
3681 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3682 if (IS_ERR(trans)) {
3683 ret = PTR_ERR(trans);
3684 break;
3685 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003686 continue;
3687 } else {
3688 goto skip;
3689 }
3690update_tree:
3691 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003692 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3693 root_item.uuid,
3694 BTRFS_UUID_KEY_SUBVOL,
3695 key.objectid);
3696 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003697 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003698 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003699 break;
3700 }
3701 }
3702
3703 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003704 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3705 root_item.received_uuid,
3706 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3707 key.objectid);
3708 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003709 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003710 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003711 break;
3712 }
3713 }
3714
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003715skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003716 if (trans) {
3717 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003718 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003719 if (ret)
3720 break;
3721 }
3722
Stefan Behrens803b2f52013-08-15 17:11:21 +02003723 btrfs_release_path(path);
3724 if (key.offset < (u64)-1) {
3725 key.offset++;
3726 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3727 key.offset = 0;
3728 key.type = BTRFS_ROOT_ITEM_KEY;
3729 } else if (key.objectid < (u64)-1) {
3730 key.offset = 0;
3731 key.type = BTRFS_ROOT_ITEM_KEY;
3732 key.objectid++;
3733 } else {
3734 break;
3735 }
3736 cond_resched();
3737 }
3738
3739out:
3740 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003741 if (trans && !IS_ERR(trans))
3742 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003743 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05003744 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003745 else
3746 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003747 up(&fs_info->uuid_tree_rescan_sem);
3748 return 0;
3749}
3750
Stefan Behrens70f80172013-08-15 17:11:23 +02003751/*
3752 * Callback for btrfs_uuid_tree_iterate().
3753 * returns:
3754 * 0 check succeeded, the entry is not outdated.
3755 * < 0 if an error occured.
3756 * > 0 if the check failed, which means the caller shall remove the entry.
3757 */
3758static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3759 u8 *uuid, u8 type, u64 subid)
3760{
3761 struct btrfs_key key;
3762 int ret = 0;
3763 struct btrfs_root *subvol_root;
3764
3765 if (type != BTRFS_UUID_KEY_SUBVOL &&
3766 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3767 goto out;
3768
3769 key.objectid = subid;
3770 key.type = BTRFS_ROOT_ITEM_KEY;
3771 key.offset = (u64)-1;
3772 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3773 if (IS_ERR(subvol_root)) {
3774 ret = PTR_ERR(subvol_root);
3775 if (ret == -ENOENT)
3776 ret = 1;
3777 goto out;
3778 }
3779
3780 switch (type) {
3781 case BTRFS_UUID_KEY_SUBVOL:
3782 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3783 ret = 1;
3784 break;
3785 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3786 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3787 BTRFS_UUID_SIZE))
3788 ret = 1;
3789 break;
3790 }
3791
3792out:
3793 return ret;
3794}
3795
3796static int btrfs_uuid_rescan_kthread(void *data)
3797{
3798 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3799 int ret;
3800
3801 /*
3802 * 1st step is to iterate through the existing UUID tree and
3803 * to delete all entries that contain outdated data.
3804 * 2nd step is to add all missing entries to the UUID tree.
3805 */
3806 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3807 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003808 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003809 up(&fs_info->uuid_tree_rescan_sem);
3810 return ret;
3811 }
3812 return btrfs_uuid_scan_kthread(data);
3813}
3814
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003815int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3816{
3817 struct btrfs_trans_handle *trans;
3818 struct btrfs_root *tree_root = fs_info->tree_root;
3819 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003820 struct task_struct *task;
3821 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003822
3823 /*
3824 * 1 - root node
3825 * 1 - root item
3826 */
3827 trans = btrfs_start_transaction(tree_root, 2);
3828 if (IS_ERR(trans))
3829 return PTR_ERR(trans);
3830
3831 uuid_root = btrfs_create_tree(trans, fs_info,
3832 BTRFS_UUID_TREE_OBJECTID);
3833 if (IS_ERR(uuid_root)) {
3834 btrfs_abort_transaction(trans, tree_root,
3835 PTR_ERR(uuid_root));
3836 return PTR_ERR(uuid_root);
3837 }
3838
3839 fs_info->uuid_root = uuid_root;
3840
Stefan Behrens803b2f52013-08-15 17:11:21 +02003841 ret = btrfs_commit_transaction(trans, tree_root);
3842 if (ret)
3843 return ret;
3844
3845 down(&fs_info->uuid_tree_rescan_sem);
3846 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3847 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02003848 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003849 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02003850 up(&fs_info->uuid_tree_rescan_sem);
3851 return PTR_ERR(task);
3852 }
3853
3854 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003855}
Stefan Behrens803b2f52013-08-15 17:11:21 +02003856
Stefan Behrens70f80172013-08-15 17:11:23 +02003857int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3858{
3859 struct task_struct *task;
3860
3861 down(&fs_info->uuid_tree_rescan_sem);
3862 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3863 if (IS_ERR(task)) {
3864 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003865 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02003866 up(&fs_info->uuid_tree_rescan_sem);
3867 return PTR_ERR(task);
3868 }
3869
3870 return 0;
3871}
3872
Chris Mason8f18cf12008-04-25 16:53:30 -04003873/*
3874 * shrinking a device means finding all of the device extents past
3875 * the new size, and then following the back refs to the chunks.
3876 * The chunk relocation code actually frees the device extent
3877 */
3878int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3879{
3880 struct btrfs_trans_handle *trans;
3881 struct btrfs_root *root = device->dev_root;
3882 struct btrfs_dev_extent *dev_extent = NULL;
3883 struct btrfs_path *path;
3884 u64 length;
3885 u64 chunk_tree;
3886 u64 chunk_objectid;
3887 u64 chunk_offset;
3888 int ret;
3889 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003890 int failed = 0;
3891 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003892 struct extent_buffer *l;
3893 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003894 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003895 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003896 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003897 u64 diff = device->total_bytes - new_size;
3898
Stefan Behrens63a212a2012-11-05 18:29:28 +01003899 if (device->is_tgtdev_for_dev_replace)
3900 return -EINVAL;
3901
Chris Mason8f18cf12008-04-25 16:53:30 -04003902 path = btrfs_alloc_path();
3903 if (!path)
3904 return -ENOMEM;
3905
Chris Mason8f18cf12008-04-25 16:53:30 -04003906 path->reada = 2;
3907
Chris Mason7d9eb122008-07-08 14:19:17 -04003908 lock_chunks(root);
3909
Chris Mason8f18cf12008-04-25 16:53:30 -04003910 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003911 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003912 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003913 spin_lock(&root->fs_info->free_chunk_lock);
3914 root->fs_info->free_chunk_space -= diff;
3915 spin_unlock(&root->fs_info->free_chunk_lock);
3916 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003917 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003918
Josef Bacikba1bf482009-09-11 16:11:19 -04003919again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003920 key.objectid = device->devid;
3921 key.offset = (u64)-1;
3922 key.type = BTRFS_DEV_EXTENT_KEY;
3923
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003924 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003925 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3926 if (ret < 0)
3927 goto done;
3928
3929 ret = btrfs_previous_item(root, path, 0, key.type);
3930 if (ret < 0)
3931 goto done;
3932 if (ret) {
3933 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003934 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003935 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003936 }
3937
3938 l = path->nodes[0];
3939 slot = path->slots[0];
3940 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3941
Josef Bacikba1bf482009-09-11 16:11:19 -04003942 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003943 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003944 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003945 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003946
3947 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3948 length = btrfs_dev_extent_length(l, dev_extent);
3949
Josef Bacikba1bf482009-09-11 16:11:19 -04003950 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003951 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003952 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003953 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003954
3955 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3956 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3957 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003958 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003959
3960 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3961 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003962 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003963 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003964 if (ret == -ENOSPC)
3965 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003966 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003967
3968 if (failed && !retried) {
3969 failed = 0;
3970 retried = true;
3971 goto again;
3972 } else if (failed && retried) {
3973 ret = -ENOSPC;
3974 lock_chunks(root);
3975
3976 device->total_bytes = old_size;
3977 if (device->writeable)
3978 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003979 spin_lock(&root->fs_info->free_chunk_lock);
3980 root->fs_info->free_chunk_space += diff;
3981 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003982 unlock_chunks(root);
3983 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003984 }
3985
Chris Balld6397ba2009-04-27 07:29:03 -04003986 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003987 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003988 if (IS_ERR(trans)) {
3989 ret = PTR_ERR(trans);
3990 goto done;
3991 }
3992
Chris Balld6397ba2009-04-27 07:29:03 -04003993 lock_chunks(root);
3994
3995 device->disk_total_bytes = new_size;
3996 /* Now btrfs_update_device() will change the on-disk size. */
3997 ret = btrfs_update_device(trans, device);
3998 if (ret) {
3999 unlock_chunks(root);
4000 btrfs_end_transaction(trans, root);
4001 goto done;
4002 }
4003 WARN_ON(diff > old_total);
4004 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4005 unlock_chunks(root);
4006 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004007done:
4008 btrfs_free_path(path);
4009 return ret;
4010}
4011
Li Zefan125ccb02011-12-08 15:07:24 +08004012static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004013 struct btrfs_key *key,
4014 struct btrfs_chunk *chunk, int item_size)
4015{
David Sterba6c417612011-04-13 15:41:04 +02004016 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004017 struct btrfs_disk_key disk_key;
4018 u32 array_size;
4019 u8 *ptr;
4020
4021 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004022 if (array_size + item_size + sizeof(disk_key)
4023 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
Chris Mason0b86a832008-03-24 15:01:56 -04004024 return -EFBIG;
4025
4026 ptr = super_copy->sys_chunk_array + array_size;
4027 btrfs_cpu_key_to_disk(&disk_key, key);
4028 memcpy(ptr, &disk_key, sizeof(disk_key));
4029 ptr += sizeof(disk_key);
4030 memcpy(ptr, chunk, item_size);
4031 item_size += sizeof(disk_key);
4032 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
4033 return 0;
4034}
4035
Miao Xieb2117a32011-01-05 10:07:28 +00004036/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004037 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004038 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004039static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004040{
Arne Jansen73c5de02011-04-12 12:07:57 +02004041 const struct btrfs_device_info *di_a = a;
4042 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004043
Arne Jansen73c5de02011-04-12 12:07:57 +02004044 if (di_a->max_avail > di_b->max_avail)
4045 return -1;
4046 if (di_a->max_avail < di_b->max_avail)
4047 return 1;
4048 if (di_a->total_avail > di_b->total_avail)
4049 return -1;
4050 if (di_a->total_avail < di_b->total_avail)
4051 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004052 return 0;
4053}
4054
Eric Sandeen48a3b632013-04-25 20:41:01 +00004055static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00004056 [BTRFS_RAID_RAID10] = {
4057 .sub_stripes = 2,
4058 .dev_stripes = 1,
4059 .devs_max = 0, /* 0 == as many as possible */
4060 .devs_min = 4,
4061 .devs_increment = 2,
4062 .ncopies = 2,
4063 },
4064 [BTRFS_RAID_RAID1] = {
4065 .sub_stripes = 1,
4066 .dev_stripes = 1,
4067 .devs_max = 2,
4068 .devs_min = 2,
4069 .devs_increment = 2,
4070 .ncopies = 2,
4071 },
4072 [BTRFS_RAID_DUP] = {
4073 .sub_stripes = 1,
4074 .dev_stripes = 2,
4075 .devs_max = 1,
4076 .devs_min = 1,
4077 .devs_increment = 1,
4078 .ncopies = 2,
4079 },
4080 [BTRFS_RAID_RAID0] = {
4081 .sub_stripes = 1,
4082 .dev_stripes = 1,
4083 .devs_max = 0,
4084 .devs_min = 2,
4085 .devs_increment = 1,
4086 .ncopies = 1,
4087 },
4088 [BTRFS_RAID_SINGLE] = {
4089 .sub_stripes = 1,
4090 .dev_stripes = 1,
4091 .devs_max = 1,
4092 .devs_min = 1,
4093 .devs_increment = 1,
4094 .ncopies = 1,
4095 },
Chris Masone942f882013-02-20 14:06:05 -05004096 [BTRFS_RAID_RAID5] = {
4097 .sub_stripes = 1,
4098 .dev_stripes = 1,
4099 .devs_max = 0,
4100 .devs_min = 2,
4101 .devs_increment = 1,
4102 .ncopies = 2,
4103 },
4104 [BTRFS_RAID_RAID6] = {
4105 .sub_stripes = 1,
4106 .dev_stripes = 1,
4107 .devs_max = 0,
4108 .devs_min = 3,
4109 .devs_increment = 1,
4110 .ncopies = 3,
4111 },
Liu Bo31e50222012-11-21 14:18:10 +00004112};
4113
David Woodhouse53b381b2013-01-29 18:40:14 -05004114static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4115{
4116 /* TODO allow them to set a preferred stripe size */
4117 return 64 * 1024;
4118}
4119
4120static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4121{
David Woodhouse53b381b2013-01-29 18:40:14 -05004122 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
4123 return;
4124
Miao Xieceda0862013-04-11 10:30:16 +00004125 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004126}
4127
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004128#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4129 - sizeof(struct btrfs_item) \
4130 - sizeof(struct btrfs_chunk)) \
4131 / sizeof(struct btrfs_stripe) + 1)
4132
4133#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4134 - 2 * sizeof(struct btrfs_disk_key) \
4135 - 2 * sizeof(struct btrfs_chunk)) \
4136 / sizeof(struct btrfs_stripe) + 1)
4137
Miao Xieb2117a32011-01-05 10:07:28 +00004138static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004139 struct btrfs_root *extent_root, u64 start,
4140 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004141{
4142 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004143 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4144 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004145 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004146 struct extent_map_tree *em_tree;
4147 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004148 struct btrfs_device_info *devices_info = NULL;
4149 u64 total_avail;
4150 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004151 int data_stripes; /* number of stripes that count for
4152 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004153 int sub_stripes; /* sub_stripes info for map */
4154 int dev_stripes; /* stripes per dev */
4155 int devs_max; /* max devs to use */
4156 int devs_min; /* min devs needed */
4157 int devs_increment; /* ndevs has to be a multiple of this */
4158 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004159 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004160 u64 max_stripe_size;
4161 u64 max_chunk_size;
4162 u64 stripe_size;
4163 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004164 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004165 int ndevs;
4166 int i;
4167 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004168 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004169
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004170 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004171
Miao Xieb2117a32011-01-05 10:07:28 +00004172 if (list_empty(&fs_devices->alloc_list))
4173 return -ENOSPC;
4174
Liu Bo31e50222012-11-21 14:18:10 +00004175 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004176
Liu Bo31e50222012-11-21 14:18:10 +00004177 sub_stripes = btrfs_raid_array[index].sub_stripes;
4178 dev_stripes = btrfs_raid_array[index].dev_stripes;
4179 devs_max = btrfs_raid_array[index].devs_max;
4180 devs_min = btrfs_raid_array[index].devs_min;
4181 devs_increment = btrfs_raid_array[index].devs_increment;
4182 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004183
4184 if (type & BTRFS_BLOCK_GROUP_DATA) {
4185 max_stripe_size = 1024 * 1024 * 1024;
4186 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004187 if (!devs_max)
4188 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004189 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004190 /* for larger filesystems, use larger metadata chunks */
4191 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4192 max_stripe_size = 1024 * 1024 * 1024;
4193 else
4194 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004195 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004196 if (!devs_max)
4197 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004198 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004199 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004200 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004201 if (!devs_max)
4202 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004203 } else {
David Sterba351fd352014-05-15 16:48:20 +02004204 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004205 type);
4206 BUG_ON(1);
4207 }
4208
4209 /* we don't want a chunk larger than 10% of writeable space */
4210 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4211 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004212
4213 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
4214 GFP_NOFS);
4215 if (!devices_info)
4216 return -ENOMEM;
4217
Arne Jansen73c5de02011-04-12 12:07:57 +02004218 cur = fs_devices->alloc_list.next;
4219
4220 /*
4221 * in the first pass through the devices list, we gather information
4222 * about the available holes on each device.
4223 */
4224 ndevs = 0;
4225 while (cur != &fs_devices->alloc_list) {
4226 struct btrfs_device *device;
4227 u64 max_avail;
4228 u64 dev_offset;
4229
4230 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4231
4232 cur = cur->next;
4233
4234 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004235 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004236 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004237 continue;
4238 }
4239
Stefan Behrens63a212a2012-11-05 18:29:28 +01004240 if (!device->in_fs_metadata ||
4241 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004242 continue;
4243
4244 if (device->total_bytes > device->bytes_used)
4245 total_avail = device->total_bytes - device->bytes_used;
4246 else
4247 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004248
4249 /* If there is no space on this device, skip it. */
4250 if (total_avail == 0)
4251 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004252
Josef Bacik6df9a952013-06-27 13:22:46 -04004253 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004254 max_stripe_size * dev_stripes,
4255 &dev_offset, &max_avail);
4256 if (ret && ret != -ENOSPC)
4257 goto error;
4258
4259 if (ret == 0)
4260 max_avail = max_stripe_size * dev_stripes;
4261
4262 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4263 continue;
4264
Eric Sandeen063d0062013-01-31 00:55:01 +00004265 if (ndevs == fs_devices->rw_devices) {
4266 WARN(1, "%s: found more than %llu devices\n",
4267 __func__, fs_devices->rw_devices);
4268 break;
4269 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004270 devices_info[ndevs].dev_offset = dev_offset;
4271 devices_info[ndevs].max_avail = max_avail;
4272 devices_info[ndevs].total_avail = total_avail;
4273 devices_info[ndevs].dev = device;
4274 ++ndevs;
4275 }
4276
4277 /*
4278 * now sort the devices by hole size / available space
4279 */
4280 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4281 btrfs_cmp_device_info, NULL);
4282
4283 /* round down to number of usable stripes */
4284 ndevs -= ndevs % devs_increment;
4285
4286 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4287 ret = -ENOSPC;
4288 goto error;
4289 }
4290
4291 if (devs_max && ndevs > devs_max)
4292 ndevs = devs_max;
4293 /*
4294 * the primary goal is to maximize the number of stripes, so use as many
4295 * devices as possible, even if the stripes are not maximum sized.
4296 */
4297 stripe_size = devices_info[ndevs-1].max_avail;
4298 num_stripes = ndevs * dev_stripes;
4299
David Woodhouse53b381b2013-01-29 18:40:14 -05004300 /*
4301 * this will have to be fixed for RAID1 and RAID10 over
4302 * more drives
4303 */
4304 data_stripes = num_stripes / ncopies;
4305
David Woodhouse53b381b2013-01-29 18:40:14 -05004306 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4307 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4308 btrfs_super_stripesize(info->super_copy));
4309 data_stripes = num_stripes - 1;
4310 }
4311 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4312 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4313 btrfs_super_stripesize(info->super_copy));
4314 data_stripes = num_stripes - 2;
4315 }
Chris Mason86db2572013-02-20 16:23:40 -05004316
4317 /*
4318 * Use the number of data stripes to figure out how big this chunk
4319 * is really going to be in terms of logical address space,
4320 * and compare that answer with the max chunk size
4321 */
4322 if (stripe_size * data_stripes > max_chunk_size) {
4323 u64 mask = (1ULL << 24) - 1;
4324 stripe_size = max_chunk_size;
4325 do_div(stripe_size, data_stripes);
4326
4327 /* bump the answer up to a 16MB boundary */
4328 stripe_size = (stripe_size + mask) & ~mask;
4329
4330 /* but don't go higher than the limits we found
4331 * while searching for free extents
4332 */
4333 if (stripe_size > devices_info[ndevs-1].max_avail)
4334 stripe_size = devices_info[ndevs-1].max_avail;
4335 }
4336
Arne Jansen73c5de02011-04-12 12:07:57 +02004337 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004338
4339 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05004340 do_div(stripe_size, raid_stripe_len);
4341 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004342
Miao Xieb2117a32011-01-05 10:07:28 +00004343 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4344 if (!map) {
4345 ret = -ENOMEM;
4346 goto error;
4347 }
4348 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004349
Arne Jansen73c5de02011-04-12 12:07:57 +02004350 for (i = 0; i < ndevs; ++i) {
4351 for (j = 0; j < dev_stripes; ++j) {
4352 int s = i * dev_stripes + j;
4353 map->stripes[s].dev = devices_info[i].dev;
4354 map->stripes[s].physical = devices_info[i].dev_offset +
4355 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004356 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004357 }
Chris Mason593060d2008-03-25 16:50:33 -04004358 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004359 map->stripe_len = raid_stripe_len;
4360 map->io_align = raid_stripe_len;
4361 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004362 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004363 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004364
David Woodhouse53b381b2013-01-29 18:40:14 -05004365 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004366
Arne Jansen73c5de02011-04-12 12:07:57 +02004367 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004368
David Sterba172ddd62011-04-21 00:48:27 +02004369 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004370 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004371 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004372 ret = -ENOMEM;
4373 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004374 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004375 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004376 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004377 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004378 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004379 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004380 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004381 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004382
Chris Mason0b86a832008-03-24 15:01:56 -04004383 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004384 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004385 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004386 if (!ret) {
4387 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4388 atomic_inc(&em->refs);
4389 }
Chris Mason890871b2009-09-02 16:24:52 -04004390 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004391 if (ret) {
4392 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004393 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004394 }
Yan Zheng2b820322008-11-17 21:11:30 -05004395
Josef Bacik04487482013-01-29 15:03:37 -05004396 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4397 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4398 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004399 if (ret)
4400 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004401
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004402 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004403 check_raid56_incompat_flag(extent_root->fs_info, type);
4404
Miao Xieb2117a32011-01-05 10:07:28 +00004405 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004406 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004407
Josef Bacik6df9a952013-06-27 13:22:46 -04004408error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004409 write_lock(&em_tree->lock);
4410 remove_extent_mapping(em_tree, em);
4411 write_unlock(&em_tree->lock);
4412
4413 /* One for our allocation */
4414 free_extent_map(em);
4415 /* One for the tree reference */
4416 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004417error:
Miao Xieb2117a32011-01-05 10:07:28 +00004418 kfree(devices_info);
4419 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004420}
4421
Josef Bacik6df9a952013-06-27 13:22:46 -04004422int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004423 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004424 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004425{
Yan Zheng2b820322008-11-17 21:11:30 -05004426 struct btrfs_key key;
4427 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4428 struct btrfs_device *device;
4429 struct btrfs_chunk *chunk;
4430 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004431 struct extent_map_tree *em_tree;
4432 struct extent_map *em;
4433 struct map_lookup *map;
4434 size_t item_size;
4435 u64 dev_offset;
4436 u64 stripe_size;
4437 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004438 int ret;
4439
Josef Bacik6df9a952013-06-27 13:22:46 -04004440 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4441 read_lock(&em_tree->lock);
4442 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4443 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004444
Josef Bacik6df9a952013-06-27 13:22:46 -04004445 if (!em) {
4446 btrfs_crit(extent_root->fs_info, "unable to find logical "
4447 "%Lu len %Lu", chunk_offset, chunk_size);
4448 return -EINVAL;
4449 }
4450
4451 if (em->start != chunk_offset || em->len != chunk_size) {
4452 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004453 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004454 chunk_size, em->start, em->len);
4455 free_extent_map(em);
4456 return -EINVAL;
4457 }
4458
4459 map = (struct map_lookup *)em->bdev;
4460 item_size = btrfs_chunk_item_size(map->num_stripes);
4461 stripe_size = em->orig_block_len;
4462
4463 chunk = kzalloc(item_size, GFP_NOFS);
4464 if (!chunk) {
4465 ret = -ENOMEM;
4466 goto out;
4467 }
4468
4469 for (i = 0; i < map->num_stripes; i++) {
4470 device = map->stripes[i].dev;
4471 dev_offset = map->stripes[i].physical;
4472
Yan Zheng2b820322008-11-17 21:11:30 -05004473 device->bytes_used += stripe_size;
4474 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004475 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004476 goto out;
4477 ret = btrfs_alloc_dev_extent(trans, device,
4478 chunk_root->root_key.objectid,
4479 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4480 chunk_offset, dev_offset,
4481 stripe_size);
4482 if (ret)
4483 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004484 }
4485
Josef Bacik2bf64752011-09-26 17:12:22 -04004486 spin_lock(&extent_root->fs_info->free_chunk_lock);
4487 extent_root->fs_info->free_chunk_space -= (stripe_size *
4488 map->num_stripes);
4489 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4490
Yan Zheng2b820322008-11-17 21:11:30 -05004491 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004492 for (i = 0; i < map->num_stripes; i++) {
4493 device = map->stripes[i].dev;
4494 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004495
4496 btrfs_set_stack_stripe_devid(stripe, device->devid);
4497 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4498 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4499 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004500 }
4501
4502 btrfs_set_stack_chunk_length(chunk, chunk_size);
4503 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4504 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4505 btrfs_set_stack_chunk_type(chunk, map->type);
4506 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4507 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4508 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4509 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4510 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4511
4512 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4513 key.type = BTRFS_CHUNK_ITEM_KEY;
4514 key.offset = chunk_offset;
4515
4516 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004517 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4518 /*
4519 * TODO: Cleanup of inserted chunk root in case of
4520 * failure.
4521 */
Li Zefan125ccb02011-12-08 15:07:24 +08004522 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004523 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004524 }
liubo1abe9b82011-03-24 11:18:59 +00004525
Josef Bacik6df9a952013-06-27 13:22:46 -04004526out:
Yan Zheng2b820322008-11-17 21:11:30 -05004527 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004528 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004529 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004530}
4531
4532/*
4533 * Chunk allocation falls into two parts. The first part does works
4534 * that make the new allocated chunk useable, but not do any operation
4535 * that modifies the chunk tree. The second part does the works that
4536 * require modifying the chunk tree. This division is important for the
4537 * bootstrap process of adding storage to a seed btrfs.
4538 */
4539int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4540 struct btrfs_root *extent_root, u64 type)
4541{
4542 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004543
Josef Bacik6df9a952013-06-27 13:22:46 -04004544 chunk_offset = find_next_chunk(extent_root->fs_info);
4545 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004546}
4547
Chris Masond3977122009-01-05 21:25:51 -05004548static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004549 struct btrfs_root *root,
4550 struct btrfs_device *device)
4551{
4552 u64 chunk_offset;
4553 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004554 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004555 struct btrfs_fs_info *fs_info = root->fs_info;
4556 struct btrfs_root *extent_root = fs_info->extent_root;
4557 int ret;
4558
Josef Bacik6df9a952013-06-27 13:22:46 -04004559 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004560 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004561 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4562 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004563 if (ret)
4564 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004565
Josef Bacik6df9a952013-06-27 13:22:46 -04004566 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004567 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004568 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4569 alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06004570 if (ret) {
4571 btrfs_abort_transaction(trans, root, ret);
4572 goto out;
4573 }
Yan Zheng2b820322008-11-17 21:11:30 -05004574
4575 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004576 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004577 btrfs_abort_transaction(trans, root, ret);
David Sterba005d6422012-09-18 07:52:32 -06004578out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004579 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004580}
4581
4582int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4583{
4584 struct extent_map *em;
4585 struct map_lookup *map;
4586 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4587 int readonly = 0;
4588 int i;
4589
Chris Mason890871b2009-09-02 16:24:52 -04004590 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004591 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004592 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004593 if (!em)
4594 return 1;
4595
Josef Bacikf48b9072010-01-27 02:07:59 +00004596 if (btrfs_test_opt(root, DEGRADED)) {
4597 free_extent_map(em);
4598 return 0;
4599 }
4600
Yan Zheng2b820322008-11-17 21:11:30 -05004601 map = (struct map_lookup *)em->bdev;
4602 for (i = 0; i < map->num_stripes; i++) {
4603 if (!map->stripes[i].dev->writeable) {
4604 readonly = 1;
4605 break;
4606 }
4607 }
4608 free_extent_map(em);
4609 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004610}
4611
4612void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4613{
David Sterbaa8067e02011-04-21 00:34:43 +02004614 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004615}
4616
4617void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4618{
4619 struct extent_map *em;
4620
Chris Masond3977122009-01-05 21:25:51 -05004621 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004622 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004623 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4624 if (em)
4625 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004626 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004627 if (!em)
4628 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004629 /* once for us */
4630 free_extent_map(em);
4631 /* once for the tree */
4632 free_extent_map(em);
4633 }
4634}
4635
Stefan Behrens5d964052012-11-05 14:59:07 +01004636int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004637{
Stefan Behrens5d964052012-11-05 14:59:07 +01004638 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004639 struct extent_map *em;
4640 struct map_lookup *map;
4641 struct extent_map_tree *em_tree = &map_tree->map_tree;
4642 int ret;
4643
Chris Mason890871b2009-09-02 16:24:52 -04004644 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004645 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004646 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004647
Josef Bacikfb7669b2013-04-23 10:53:18 -04004648 /*
4649 * We could return errors for these cases, but that could get ugly and
4650 * we'd probably do the same thing which is just not do anything else
4651 * and exit, so return 1 so the callers don't try to use other copies.
4652 */
4653 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004654 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004655 logical+len);
4656 return 1;
4657 }
4658
4659 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004660 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004661 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004662 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004663 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004664 return 1;
4665 }
4666
Chris Masonf1885912008-04-09 16:28:12 -04004667 map = (struct map_lookup *)em->bdev;
4668 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4669 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004670 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4671 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004672 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4673 ret = 2;
4674 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4675 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004676 else
4677 ret = 1;
4678 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004679
4680 btrfs_dev_replace_lock(&fs_info->dev_replace);
4681 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4682 ret++;
4683 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4684
Chris Masonf1885912008-04-09 16:28:12 -04004685 return ret;
4686}
4687
David Woodhouse53b381b2013-01-29 18:40:14 -05004688unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4689 struct btrfs_mapping_tree *map_tree,
4690 u64 logical)
4691{
4692 struct extent_map *em;
4693 struct map_lookup *map;
4694 struct extent_map_tree *em_tree = &map_tree->map_tree;
4695 unsigned long len = root->sectorsize;
4696
4697 read_lock(&em_tree->lock);
4698 em = lookup_extent_mapping(em_tree, logical, len);
4699 read_unlock(&em_tree->lock);
4700 BUG_ON(!em);
4701
4702 BUG_ON(em->start > logical || em->start + em->len < logical);
4703 map = (struct map_lookup *)em->bdev;
4704 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4705 BTRFS_BLOCK_GROUP_RAID6)) {
4706 len = map->stripe_len * nr_data_stripes(map);
4707 }
4708 free_extent_map(em);
4709 return len;
4710}
4711
4712int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4713 u64 logical, u64 len, int mirror_num)
4714{
4715 struct extent_map *em;
4716 struct map_lookup *map;
4717 struct extent_map_tree *em_tree = &map_tree->map_tree;
4718 int ret = 0;
4719
4720 read_lock(&em_tree->lock);
4721 em = lookup_extent_mapping(em_tree, logical, len);
4722 read_unlock(&em_tree->lock);
4723 BUG_ON(!em);
4724
4725 BUG_ON(em->start > logical || em->start + em->len < logical);
4726 map = (struct map_lookup *)em->bdev;
4727 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4728 BTRFS_BLOCK_GROUP_RAID6))
4729 ret = 1;
4730 free_extent_map(em);
4731 return ret;
4732}
4733
Stefan Behrens30d98612012-11-06 14:52:18 +01004734static int find_live_mirror(struct btrfs_fs_info *fs_info,
4735 struct map_lookup *map, int first, int num,
4736 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004737{
4738 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004739 int tolerance;
4740 struct btrfs_device *srcdev;
4741
4742 if (dev_replace_is_ongoing &&
4743 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4744 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4745 srcdev = fs_info->dev_replace.srcdev;
4746 else
4747 srcdev = NULL;
4748
4749 /*
4750 * try to avoid the drive that is the source drive for a
4751 * dev-replace procedure, only choose it if no other non-missing
4752 * mirror is available
4753 */
4754 for (tolerance = 0; tolerance < 2; tolerance++) {
4755 if (map->stripes[optimal].dev->bdev &&
4756 (tolerance || map->stripes[optimal].dev != srcdev))
4757 return optimal;
4758 for (i = first; i < first + num; i++) {
4759 if (map->stripes[i].dev->bdev &&
4760 (tolerance || map->stripes[i].dev != srcdev))
4761 return i;
4762 }
Chris Masondfe25022008-05-13 13:46:40 -04004763 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004764
Chris Masondfe25022008-05-13 13:46:40 -04004765 /* we couldn't find one that doesn't fail. Just return something
4766 * and the io error handling code will clean up eventually
4767 */
4768 return optimal;
4769}
4770
David Woodhouse53b381b2013-01-29 18:40:14 -05004771static inline int parity_smaller(u64 a, u64 b)
4772{
4773 return a > b;
4774}
4775
4776/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4777static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4778{
4779 struct btrfs_bio_stripe s;
4780 int i;
4781 u64 l;
4782 int again = 1;
4783
4784 while (again) {
4785 again = 0;
4786 for (i = 0; i < bbio->num_stripes - 1; i++) {
4787 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4788 s = bbio->stripes[i];
4789 l = raid_map[i];
4790 bbio->stripes[i] = bbio->stripes[i+1];
4791 raid_map[i] = raid_map[i+1];
4792 bbio->stripes[i+1] = s;
4793 raid_map[i+1] = l;
4794 again = 1;
4795 }
4796 }
4797 }
4798}
4799
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004800static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004801 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004802 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004803 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004804{
4805 struct extent_map *em;
4806 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004807 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004808 struct extent_map_tree *em_tree = &map_tree->map_tree;
4809 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004810 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004811 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004812 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004813 u64 stripe_nr_orig;
4814 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004815 u64 stripe_len;
4816 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004817 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004818 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004819 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004820 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004821 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004822 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004823 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4824 int dev_replace_is_ongoing = 0;
4825 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004826 int patch_the_first_stripe_for_dev_replace = 0;
4827 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004828 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004829
Chris Mason890871b2009-09-02 16:24:52 -04004830 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004831 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004832 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004833
Chris Mason3b951512008-04-17 11:29:12 -04004834 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004835 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004836 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004837 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004838 }
Chris Mason0b86a832008-03-24 15:01:56 -04004839
Josef Bacik9bb91872013-04-19 23:45:33 -04004840 if (em->start > logical || em->start + em->len < logical) {
4841 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02004842 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04004843 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004844 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04004845 return -EINVAL;
4846 }
4847
Chris Mason0b86a832008-03-24 15:01:56 -04004848 map = (struct map_lookup *)em->bdev;
4849 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004850
David Woodhouse53b381b2013-01-29 18:40:14 -05004851 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004852 stripe_nr = offset;
4853 /*
4854 * stripe_nr counts the total number of stripes we have to stride
4855 * to get to this block
4856 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004857 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004858
David Woodhouse53b381b2013-01-29 18:40:14 -05004859 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004860 BUG_ON(offset < stripe_offset);
4861
4862 /* stripe_offset is the offset of this block in its stripe*/
4863 stripe_offset = offset - stripe_offset;
4864
David Woodhouse53b381b2013-01-29 18:40:14 -05004865 /* if we're here for raid56, we need to know the stripe aligned start */
4866 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4867 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4868 raid56_full_stripe_start = offset;
4869
4870 /* allow a write of a full stripe, but make sure we don't
4871 * allow straddling of stripes
4872 */
4873 do_div(raid56_full_stripe_start, full_stripe_len);
4874 raid56_full_stripe_start *= full_stripe_len;
4875 }
4876
4877 if (rw & REQ_DISCARD) {
4878 /* we don't discard raid56 yet */
4879 if (map->type &
4880 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4881 ret = -EOPNOTSUPP;
4882 goto out;
4883 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004884 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004885 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4886 u64 max_len;
4887 /* For writes to RAID[56], allow a full stripeset across all disks.
4888 For other RAID types and for RAID[56] reads, just allow a single
4889 stripe (on a single disk). */
4890 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4891 (rw & REQ_WRITE)) {
4892 max_len = stripe_len * nr_data_stripes(map) -
4893 (offset - raid56_full_stripe_start);
4894 } else {
4895 /* we limit the length of each bio to what fits in a stripe */
4896 max_len = stripe_len - stripe_offset;
4897 }
4898 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004899 } else {
4900 *length = em->len - offset;
4901 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004902
David Woodhouse53b381b2013-01-29 18:40:14 -05004903 /* This is for when we're called from btrfs_merge_bio_hook() and all
4904 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004905 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004906 goto out;
4907
Stefan Behrens472262f2012-11-06 14:43:46 +01004908 btrfs_dev_replace_lock(dev_replace);
4909 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4910 if (!dev_replace_is_ongoing)
4911 btrfs_dev_replace_unlock(dev_replace);
4912
Stefan Behrensad6d6202012-11-06 15:06:47 +01004913 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4914 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4915 dev_replace->tgtdev != NULL) {
4916 /*
4917 * in dev-replace case, for repair case (that's the only
4918 * case where the mirror is selected explicitly when
4919 * calling btrfs_map_block), blocks left of the left cursor
4920 * can also be read from the target drive.
4921 * For REQ_GET_READ_MIRRORS, the target drive is added as
4922 * the last one to the array of stripes. For READ, it also
4923 * needs to be supported using the same mirror number.
4924 * If the requested block is not left of the left cursor,
4925 * EIO is returned. This can happen because btrfs_num_copies()
4926 * returns one more in the dev-replace case.
4927 */
4928 u64 tmp_length = *length;
4929 struct btrfs_bio *tmp_bbio = NULL;
4930 int tmp_num_stripes;
4931 u64 srcdev_devid = dev_replace->srcdev->devid;
4932 int index_srcdev = 0;
4933 int found = 0;
4934 u64 physical_of_found = 0;
4935
4936 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05004937 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004938 if (ret) {
4939 WARN_ON(tmp_bbio != NULL);
4940 goto out;
4941 }
4942
4943 tmp_num_stripes = tmp_bbio->num_stripes;
4944 if (mirror_num > tmp_num_stripes) {
4945 /*
4946 * REQ_GET_READ_MIRRORS does not contain this
4947 * mirror, that means that the requested area
4948 * is not left of the left cursor
4949 */
4950 ret = -EIO;
4951 kfree(tmp_bbio);
4952 goto out;
4953 }
4954
4955 /*
4956 * process the rest of the function using the mirror_num
4957 * of the source drive. Therefore look it up first.
4958 * At the end, patch the device pointer to the one of the
4959 * target drive.
4960 */
4961 for (i = 0; i < tmp_num_stripes; i++) {
4962 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4963 /*
4964 * In case of DUP, in order to keep it
4965 * simple, only add the mirror with the
4966 * lowest physical address
4967 */
4968 if (found &&
4969 physical_of_found <=
4970 tmp_bbio->stripes[i].physical)
4971 continue;
4972 index_srcdev = i;
4973 found = 1;
4974 physical_of_found =
4975 tmp_bbio->stripes[i].physical;
4976 }
4977 }
4978
4979 if (found) {
4980 mirror_num = index_srcdev + 1;
4981 patch_the_first_stripe_for_dev_replace = 1;
4982 physical_to_patch_in_first_stripe = physical_of_found;
4983 } else {
4984 WARN_ON(1);
4985 ret = -EIO;
4986 kfree(tmp_bbio);
4987 goto out;
4988 }
4989
4990 kfree(tmp_bbio);
4991 } else if (mirror_num > map->num_stripes) {
4992 mirror_num = 0;
4993 }
4994
Chris Masonf2d8d742008-04-21 10:03:05 -04004995 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04004996 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004997 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00004998 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00004999 do_div(stripe_nr_end, map->stripe_len);
5000 stripe_end_offset = stripe_nr_end * map->stripe_len -
5001 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005002
Li Dongyangfce3bb92011-03-24 10:24:26 +00005003 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5004 if (rw & REQ_DISCARD)
5005 num_stripes = min_t(u64, map->num_stripes,
5006 stripe_nr_end - stripe_nr_orig);
5007 stripe_index = do_div(stripe_nr, map->num_stripes);
5008 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005009 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005010 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005011 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005012 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005013 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005014 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005015 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005016 current->pid % map->num_stripes,
5017 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005018 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005019 }
Chris Mason2fff7342008-04-29 14:12:09 -04005020
Chris Mason611f0e02008-04-03 16:29:03 -04005021 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005022 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005023 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005024 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005025 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005026 } else {
5027 mirror_num = 1;
5028 }
Chris Mason2fff7342008-04-29 14:12:09 -04005029
Chris Mason321aecc2008-04-16 10:49:51 -04005030 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5031 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005032
5033 stripe_index = do_div(stripe_nr, factor);
5034 stripe_index *= map->sub_stripes;
5035
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005036 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005037 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005038 else if (rw & REQ_DISCARD)
5039 num_stripes = min_t(u64, map->sub_stripes *
5040 (stripe_nr_end - stripe_nr_orig),
5041 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005042 else if (mirror_num)
5043 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005044 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005045 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005046 stripe_index = find_live_mirror(fs_info, map,
5047 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005048 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005049 current->pid % map->sub_stripes,
5050 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005051 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005052 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005053
5054 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5055 BTRFS_BLOCK_GROUP_RAID6)) {
5056 u64 tmp;
5057
5058 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
5059 && raid_map_ret) {
5060 int i, rot;
5061
5062 /* push stripe_nr back to the start of the full stripe */
5063 stripe_nr = raid56_full_stripe_start;
5064 do_div(stripe_nr, stripe_len);
5065
5066 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5067
5068 /* RAID[56] write or recovery. Return all stripes */
5069 num_stripes = map->num_stripes;
5070 max_errors = nr_parity_stripes(map);
5071
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05305072 raid_map = kmalloc_array(num_stripes, sizeof(u64),
David Woodhouse53b381b2013-01-29 18:40:14 -05005073 GFP_NOFS);
5074 if (!raid_map) {
5075 ret = -ENOMEM;
5076 goto out;
5077 }
5078
5079 /* Work out the disk rotation on this stripe-set */
5080 tmp = stripe_nr;
5081 rot = do_div(tmp, num_stripes);
5082
5083 /* Fill in the logical address of each stripe */
5084 tmp = stripe_nr * nr_data_stripes(map);
5085 for (i = 0; i < nr_data_stripes(map); i++)
5086 raid_map[(i+rot) % num_stripes] =
5087 em->start + (tmp + i) * map->stripe_len;
5088
5089 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5090 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5091 raid_map[(i+rot+1) % num_stripes] =
5092 RAID6_Q_STRIPE;
5093
5094 *length = map->stripe_len;
5095 stripe_index = 0;
5096 stripe_offset = 0;
5097 } else {
5098 /*
5099 * Mirror #0 or #1 means the original data block.
5100 * Mirror #2 is RAID5 parity block.
5101 * Mirror #3 is RAID6 Q block.
5102 */
5103 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5104 if (mirror_num > 1)
5105 stripe_index = nr_data_stripes(map) +
5106 mirror_num - 2;
5107
5108 /* We distribute the parity blocks across stripes */
5109 tmp = stripe_nr + stripe_index;
5110 stripe_index = do_div(tmp, map->num_stripes);
5111 }
Chris Mason8790d502008-04-03 16:29:03 -04005112 } else {
5113 /*
5114 * after this do_div call, stripe_nr is the number of stripes
5115 * on this device we have to walk to find the data, and
5116 * stripe_index is the number of our device in the stripe array
5117 */
5118 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005119 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005120 }
Chris Mason593060d2008-03-25 16:50:33 -04005121 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005122
Stefan Behrens472262f2012-11-06 14:43:46 +01005123 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005124 if (dev_replace_is_ongoing) {
5125 if (rw & (REQ_WRITE | REQ_DISCARD))
5126 num_alloc_stripes <<= 1;
5127 if (rw & REQ_GET_READ_MIRRORS)
5128 num_alloc_stripes++;
5129 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005130 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08005131 if (!bbio) {
Dave Joneseb2067f2013-07-30 13:42:17 -04005132 kfree(raid_map);
Li Zefande11cc12011-12-01 12:55:47 +08005133 ret = -ENOMEM;
5134 goto out;
5135 }
5136 atomic_set(&bbio->error, 0);
5137
Li Dongyangfce3bb92011-03-24 10:24:26 +00005138 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08005139 int factor = 0;
5140 int sub_stripes = 0;
5141 u64 stripes_per_dev = 0;
5142 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005143 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005144
5145 if (map->type &
5146 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5147 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5148 sub_stripes = 1;
5149 else
5150 sub_stripes = map->sub_stripes;
5151
5152 factor = map->num_stripes / sub_stripes;
5153 stripes_per_dev = div_u64_rem(stripe_nr_end -
5154 stripe_nr_orig,
5155 factor,
5156 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005157 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5158 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005159 }
5160
Li Dongyangfce3bb92011-03-24 10:24:26 +00005161 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005162 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005163 map->stripes[stripe_index].physical +
5164 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005165 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005166
Li Zefanec9ef7a2011-12-01 14:06:42 +08005167 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5168 BTRFS_BLOCK_GROUP_RAID10)) {
5169 bbio->stripes[i].length = stripes_per_dev *
5170 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005171
Li Zefanec9ef7a2011-12-01 14:06:42 +08005172 if (i / sub_stripes < remaining_stripes)
5173 bbio->stripes[i].length +=
5174 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005175
5176 /*
5177 * Special for the first stripe and
5178 * the last stripe:
5179 *
5180 * |-------|...|-------|
5181 * |----------|
5182 * off end_off
5183 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005184 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005185 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005186 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005187
5188 if (stripe_index >= last_stripe &&
5189 stripe_index <= (last_stripe +
5190 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005191 bbio->stripes[i].length -=
5192 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005193
Li Zefanec9ef7a2011-12-01 14:06:42 +08005194 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005195 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005196 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005197 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005198
5199 stripe_index++;
5200 if (stripe_index == map->num_stripes) {
5201 /* This could only happen for RAID0/10 */
5202 stripe_index = 0;
5203 stripe_nr++;
5204 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005205 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005206 } else {
5207 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005208 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005209 map->stripes[stripe_index].physical +
5210 stripe_offset +
5211 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005212 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005213 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005214 stripe_index++;
5215 }
Chris Mason593060d2008-03-25 16:50:33 -04005216 }
Li Zefande11cc12011-12-01 12:55:47 +08005217
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005218 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
Li Zefande11cc12011-12-01 12:55:47 +08005219 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5220 BTRFS_BLOCK_GROUP_RAID10 |
David Woodhouse53b381b2013-01-29 18:40:14 -05005221 BTRFS_BLOCK_GROUP_RAID5 |
Li Zefande11cc12011-12-01 12:55:47 +08005222 BTRFS_BLOCK_GROUP_DUP)) {
5223 max_errors = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005224 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5225 max_errors = 2;
Li Zefande11cc12011-12-01 12:55:47 +08005226 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005227 }
Li Zefande11cc12011-12-01 12:55:47 +08005228
Stefan Behrens472262f2012-11-06 14:43:46 +01005229 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5230 dev_replace->tgtdev != NULL) {
5231 int index_where_to_add;
5232 u64 srcdev_devid = dev_replace->srcdev->devid;
5233
5234 /*
5235 * duplicate the write operations while the dev replace
5236 * procedure is running. Since the copying of the old disk
5237 * to the new disk takes place at run time while the
5238 * filesystem is mounted writable, the regular write
5239 * operations to the old disk have to be duplicated to go
5240 * to the new disk as well.
5241 * Note that device->missing is handled by the caller, and
5242 * that the write to the old disk is already set up in the
5243 * stripes array.
5244 */
5245 index_where_to_add = num_stripes;
5246 for (i = 0; i < num_stripes; i++) {
5247 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5248 /* write to new disk, too */
5249 struct btrfs_bio_stripe *new =
5250 bbio->stripes + index_where_to_add;
5251 struct btrfs_bio_stripe *old =
5252 bbio->stripes + i;
5253
5254 new->physical = old->physical;
5255 new->length = old->length;
5256 new->dev = dev_replace->tgtdev;
5257 index_where_to_add++;
5258 max_errors++;
5259 }
5260 }
5261 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005262 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5263 dev_replace->tgtdev != NULL) {
5264 u64 srcdev_devid = dev_replace->srcdev->devid;
5265 int index_srcdev = 0;
5266 int found = 0;
5267 u64 physical_of_found = 0;
5268
5269 /*
5270 * During the dev-replace procedure, the target drive can
5271 * also be used to read data in case it is needed to repair
5272 * a corrupt block elsewhere. This is possible if the
5273 * requested area is left of the left cursor. In this area,
5274 * the target drive is a full copy of the source drive.
5275 */
5276 for (i = 0; i < num_stripes; i++) {
5277 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5278 /*
5279 * In case of DUP, in order to keep it
5280 * simple, only add the mirror with the
5281 * lowest physical address
5282 */
5283 if (found &&
5284 physical_of_found <=
5285 bbio->stripes[i].physical)
5286 continue;
5287 index_srcdev = i;
5288 found = 1;
5289 physical_of_found = bbio->stripes[i].physical;
5290 }
5291 }
5292 if (found) {
5293 u64 length = map->stripe_len;
5294
5295 if (physical_of_found + length <=
5296 dev_replace->cursor_left) {
5297 struct btrfs_bio_stripe *tgtdev_stripe =
5298 bbio->stripes + num_stripes;
5299
5300 tgtdev_stripe->physical = physical_of_found;
5301 tgtdev_stripe->length =
5302 bbio->stripes[index_srcdev].length;
5303 tgtdev_stripe->dev = dev_replace->tgtdev;
5304
5305 num_stripes++;
5306 }
5307 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005308 }
5309
Li Zefande11cc12011-12-01 12:55:47 +08005310 *bbio_ret = bbio;
5311 bbio->num_stripes = num_stripes;
5312 bbio->max_errors = max_errors;
5313 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005314
5315 /*
5316 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5317 * mirror_num == num_stripes + 1 && dev_replace target drive is
5318 * available as a mirror
5319 */
5320 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5321 WARN_ON(num_stripes > 1);
5322 bbio->stripes[0].dev = dev_replace->tgtdev;
5323 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5324 bbio->mirror_num = map->num_stripes + 1;
5325 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005326 if (raid_map) {
5327 sort_parity_stripes(bbio, raid_map);
5328 *raid_map_ret = raid_map;
5329 }
Chris Masoncea9e442008-04-09 16:28:12 -04005330out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005331 if (dev_replace_is_ongoing)
5332 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005333 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005334 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005335}
5336
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005337int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005338 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005339 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005340{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005341 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05005342 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04005343}
5344
Yan Zhenga512bbf2008-12-08 16:46:26 -05005345int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5346 u64 chunk_start, u64 physical, u64 devid,
5347 u64 **logical, int *naddrs, int *stripe_len)
5348{
5349 struct extent_map_tree *em_tree = &map_tree->map_tree;
5350 struct extent_map *em;
5351 struct map_lookup *map;
5352 u64 *buf;
5353 u64 bytenr;
5354 u64 length;
5355 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005356 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005357 int i, j, nr = 0;
5358
Chris Mason890871b2009-09-02 16:24:52 -04005359 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005360 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005361 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005362
Josef Bacik835d9742013-03-19 12:13:25 -04005363 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005364 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005365 chunk_start);
5366 return -EIO;
5367 }
5368
5369 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005370 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005371 em->start, chunk_start);
5372 free_extent_map(em);
5373 return -EIO;
5374 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005375 map = (struct map_lookup *)em->bdev;
5376
5377 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005378 rmap_len = map->stripe_len;
5379
Yan Zhenga512bbf2008-12-08 16:46:26 -05005380 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5381 do_div(length, map->num_stripes / map->sub_stripes);
5382 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5383 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05005384 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5385 BTRFS_BLOCK_GROUP_RAID6)) {
5386 do_div(length, nr_data_stripes(map));
5387 rmap_len = map->stripe_len * nr_data_stripes(map);
5388 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005389
5390 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005391 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005392
5393 for (i = 0; i < map->num_stripes; i++) {
5394 if (devid && map->stripes[i].dev->devid != devid)
5395 continue;
5396 if (map->stripes[i].physical > physical ||
5397 map->stripes[i].physical + length <= physical)
5398 continue;
5399
5400 stripe_nr = physical - map->stripes[i].physical;
5401 do_div(stripe_nr, map->stripe_len);
5402
5403 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5404 stripe_nr = stripe_nr * map->num_stripes + i;
5405 do_div(stripe_nr, map->sub_stripes);
5406 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5407 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005408 } /* else if RAID[56], multiply by nr_data_stripes().
5409 * Alternatively, just use rmap_len below instead of
5410 * map->stripe_len */
5411
5412 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005413 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005414 for (j = 0; j < nr; j++) {
5415 if (buf[j] == bytenr)
5416 break;
5417 }
Chris Mason934d3752008-12-08 16:43:10 -05005418 if (j == nr) {
5419 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005420 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005421 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005422 }
5423
Yan Zhenga512bbf2008-12-08 16:46:26 -05005424 *logical = buf;
5425 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005426 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005427
5428 free_extent_map(em);
5429 return 0;
5430}
5431
Miao Xie8408c712014-06-19 10:42:55 +08005432static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5433{
5434 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5435 bio_endio_nodec(bio, err);
5436 else
5437 bio_endio(bio, err);
5438 kfree(bbio);
5439}
5440
Jan Schmidta1d3c472011-08-04 17:15:33 +02005441static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005442{
Chris Mason9be33952013-05-17 18:30:14 -04005443 struct btrfs_bio *bbio = bio->bi_private;
Miao Xiec404e0d2014-01-30 16:46:55 +08005444 struct btrfs_device *dev = bbio->stripes[0].dev;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005445 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005446
Stefan Behrens442a4f62012-05-25 16:06:08 +02005447 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005448 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005449 if (err == -EIO || err == -EREMOTEIO) {
5450 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005451 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005452
5453 BUG_ON(stripe_index >= bbio->num_stripes);
5454 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005455 if (dev->bdev) {
5456 if (bio->bi_rw & WRITE)
5457 btrfs_dev_stat_inc(dev,
5458 BTRFS_DEV_STAT_WRITE_ERRS);
5459 else
5460 btrfs_dev_stat_inc(dev,
5461 BTRFS_DEV_STAT_READ_ERRS);
5462 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5463 btrfs_dev_stat_inc(dev,
5464 BTRFS_DEV_STAT_FLUSH_ERRS);
5465 btrfs_dev_stat_print_on_error(dev);
5466 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005467 }
5468 }
Chris Mason8790d502008-04-03 16:29:03 -04005469
Jan Schmidta1d3c472011-08-04 17:15:33 +02005470 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005471 is_orig_bio = 1;
5472
Miao Xiec404e0d2014-01-30 16:46:55 +08005473 btrfs_bio_counter_dec(bbio->fs_info);
5474
Jan Schmidta1d3c472011-08-04 17:15:33 +02005475 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005476 if (!is_orig_bio) {
5477 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005478 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005479 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005480
Jan Schmidta1d3c472011-08-04 17:15:33 +02005481 bio->bi_private = bbio->private;
5482 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005483 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005484 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005485 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005486 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005487 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005488 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005489 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005490 /*
5491 * this bio is actually up to date, we didn't
5492 * go over the max number of errors
5493 */
5494 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005495 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005496 }
Miao Xiec55f1392014-06-19 10:42:54 +08005497
Miao Xie8408c712014-06-19 10:42:55 +08005498 btrfs_end_bbio(bbio, bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005499 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005500 bio_put(bio);
5501 }
Chris Mason8790d502008-04-03 16:29:03 -04005502}
5503
Chris Mason8b712842008-06-11 16:50:36 -04005504/*
5505 * see run_scheduled_bios for a description of why bios are collected for
5506 * async submit.
5507 *
5508 * This will add one bio to the pending list for a device and make sure
5509 * the work struct is scheduled.
5510 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005511static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5512 struct btrfs_device *device,
5513 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005514{
5515 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005516 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005517
David Woodhouse53b381b2013-01-29 18:40:14 -05005518 if (device->missing || !device->bdev) {
5519 bio_endio(bio, -EIO);
5520 return;
5521 }
5522
Chris Mason8b712842008-06-11 16:50:36 -04005523 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005524 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005525 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005526 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005527 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005528 return;
Chris Mason8b712842008-06-11 16:50:36 -04005529 }
5530
5531 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005532 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005533 * higher layers. Otherwise, the async bio makes it appear we have
5534 * made progress against dirty pages when we've really just put it
5535 * on a queue for later
5536 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005537 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005538 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005539 bio->bi_next = NULL;
5540 bio->bi_rw |= rw;
5541
5542 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005543 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005544 pending_bios = &device->pending_sync_bios;
5545 else
5546 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005547
Chris Masonffbd5172009-04-20 15:50:09 -04005548 if (pending_bios->tail)
5549 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005550
Chris Masonffbd5172009-04-20 15:50:09 -04005551 pending_bios->tail = bio;
5552 if (!pending_bios->head)
5553 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005554 if (device->running_pending)
5555 should_queue = 0;
5556
5557 spin_unlock(&device->io_lock);
5558
5559 if (should_queue)
Qu Wenruoa8c93d42014-02-28 10:46:08 +08005560 btrfs_queue_work(root->fs_info->submit_workers,
5561 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005562}
5563
Josef Bacikde1ee922012-10-19 16:50:56 -04005564static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5565 sector_t sector)
5566{
5567 struct bio_vec *prev;
5568 struct request_queue *q = bdev_get_queue(bdev);
Akinobu Mita475bf362013-11-18 22:13:18 +09005569 unsigned int max_sectors = queue_max_sectors(q);
Josef Bacikde1ee922012-10-19 16:50:56 -04005570 struct bvec_merge_data bvm = {
5571 .bi_bdev = bdev,
5572 .bi_sector = sector,
5573 .bi_rw = bio->bi_rw,
5574 };
5575
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305576 if (WARN_ON(bio->bi_vcnt == 0))
Josef Bacikde1ee922012-10-19 16:50:56 -04005577 return 1;
Josef Bacikde1ee922012-10-19 16:50:56 -04005578
5579 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005580 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005581 return 0;
5582
5583 if (!q->merge_bvec_fn)
5584 return 1;
5585
Kent Overstreet4f024f32013-10-11 15:44:27 -07005586 bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len;
Josef Bacikde1ee922012-10-19 16:50:56 -04005587 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5588 return 0;
5589 return 1;
5590}
5591
5592static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5593 struct bio *bio, u64 physical, int dev_nr,
5594 int rw, int async)
5595{
5596 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5597
5598 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005599 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005600 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005601 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005602#ifdef DEBUG
5603 {
5604 struct rcu_string *name;
5605
5606 rcu_read_lock();
5607 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005608 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005609 "(%s id %llu), size=%u\n", rw,
5610 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5611 name->str, dev->devid, bio->bi_size);
5612 rcu_read_unlock();
5613 }
5614#endif
5615 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005616
5617 btrfs_bio_counter_inc_noblocked(root->fs_info);
5618
Josef Bacikde1ee922012-10-19 16:50:56 -04005619 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005620 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005621 else
5622 btrfsic_submit_bio(rw, bio);
5623}
5624
5625static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5626 struct bio *first_bio, struct btrfs_device *dev,
5627 int dev_nr, int rw, int async)
5628{
5629 struct bio_vec *bvec = first_bio->bi_io_vec;
5630 struct bio *bio;
5631 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5632 u64 physical = bbio->stripes[dev_nr].physical;
5633
5634again:
5635 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5636 if (!bio)
5637 return -ENOMEM;
5638
5639 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5640 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5641 bvec->bv_offset) < bvec->bv_len) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07005642 u64 len = bio->bi_iter.bi_size;
Josef Bacikde1ee922012-10-19 16:50:56 -04005643
5644 atomic_inc(&bbio->stripes_pending);
5645 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5646 rw, async);
5647 physical += len;
5648 goto again;
5649 }
5650 bvec++;
5651 }
5652
5653 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5654 return 0;
5655}
5656
5657static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5658{
5659 atomic_inc(&bbio->error);
5660 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005661 /* Shoud be the original bio. */
5662 WARN_ON(bio != bbio->orig_bio);
5663
Josef Bacikde1ee922012-10-19 16:50:56 -04005664 bio->bi_private = bbio->private;
5665 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005666 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005667 bio->bi_iter.bi_sector = logical >> 9;
Miao Xie8408c712014-06-19 10:42:55 +08005668
5669 btrfs_end_bbio(bbio, bio, -EIO);
Josef Bacikde1ee922012-10-19 16:50:56 -04005670 }
5671}
5672
Chris Masonf1885912008-04-09 16:28:12 -04005673int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005674 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005675{
Chris Mason0b86a832008-03-24 15:01:56 -04005676 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005677 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005678 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005679 u64 length = 0;
5680 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005681 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005682 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005683 int dev_nr = 0;
5684 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005685 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005686
Kent Overstreet4f024f32013-10-11 15:44:27 -07005687 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005688 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005689
Miao Xiec404e0d2014-01-30 16:46:55 +08005690 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005691 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5692 mirror_num, &raid_map);
Miao Xiec404e0d2014-01-30 16:46:55 +08005693 if (ret) {
5694 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005695 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005696 }
Chris Masoncea9e442008-04-09 16:28:12 -04005697
Jan Schmidta1d3c472011-08-04 17:15:33 +02005698 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005699 bbio->orig_bio = first_bio;
5700 bbio->private = first_bio->bi_private;
5701 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005702 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005703 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5704
5705 if (raid_map) {
5706 /* In this case, map_length has been set to the length of
5707 a single stripe; not the whole write */
5708 if (rw & WRITE) {
Miao Xiec404e0d2014-01-30 16:46:55 +08005709 ret = raid56_parity_write(root, bio, bbio,
5710 raid_map, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005711 } else {
Miao Xiec404e0d2014-01-30 16:46:55 +08005712 ret = raid56_parity_recover(root, bio, bbio,
5713 raid_map, map_length,
5714 mirror_num);
David Woodhouse53b381b2013-01-29 18:40:14 -05005715 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005716 /*
5717 * FIXME, replace dosen't support raid56 yet, please fix
5718 * it in the future.
5719 */
5720 btrfs_bio_counter_dec(root->fs_info);
5721 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005722 }
5723
Chris Masoncea9e442008-04-09 16:28:12 -04005724 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005725 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005726 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005727 BUG();
5728 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005729
Chris Masond3977122009-01-05 21:25:51 -05005730 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005731 dev = bbio->stripes[dev_nr].dev;
5732 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5733 bbio_error(bbio, first_bio, logical);
5734 dev_nr++;
5735 continue;
5736 }
5737
5738 /*
5739 * Check and see if we're ok with this bio based on it's size
5740 * and offset with the given device.
5741 */
5742 if (!bio_size_ok(dev->bdev, first_bio,
5743 bbio->stripes[dev_nr].physical >> 9)) {
5744 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5745 dev_nr, rw, async_submit);
5746 BUG_ON(ret);
5747 dev_nr++;
5748 continue;
5749 }
5750
Jan Schmidta1d3c472011-08-04 17:15:33 +02005751 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005752 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005753 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005754 } else {
5755 bio = first_bio;
Miao Xiec55f1392014-06-19 10:42:54 +08005756 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
Chris Mason8790d502008-04-03 16:29:03 -04005757 }
Josef Bacik606686e2012-06-04 14:03:51 -04005758
Josef Bacikde1ee922012-10-19 16:50:56 -04005759 submit_stripe_bio(root, bbio, bio,
5760 bbio->stripes[dev_nr].physical, dev_nr, rw,
5761 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005762 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005763 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005764 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04005765 return 0;
5766}
5767
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005768struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005769 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005770{
Yan Zheng2b820322008-11-17 21:11:30 -05005771 struct btrfs_device *device;
5772 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005773
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005774 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005775 while (cur_devices) {
5776 if (!fsid ||
5777 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5778 device = __find_device(&cur_devices->devices,
5779 devid, uuid);
5780 if (device)
5781 return device;
5782 }
5783 cur_devices = cur_devices->seed;
5784 }
5785 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005786}
5787
Chris Masondfe25022008-05-13 13:46:40 -04005788static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5789 u64 devid, u8 *dev_uuid)
5790{
5791 struct btrfs_device *device;
5792 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5793
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005794 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5795 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005796 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005797
5798 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005799 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04005800 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005801
5802 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05005803 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005804
Chris Masondfe25022008-05-13 13:46:40 -04005805 return device;
5806}
5807
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005808/**
5809 * btrfs_alloc_device - allocate struct btrfs_device
5810 * @fs_info: used only for generating a new devid, can be NULL if
5811 * devid is provided (i.e. @devid != NULL).
5812 * @devid: a pointer to devid for this device. If NULL a new devid
5813 * is generated.
5814 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5815 * is generated.
5816 *
5817 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5818 * on error. Returned struct is not linked onto any lists and can be
5819 * destroyed with kfree() right away.
5820 */
5821struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5822 const u64 *devid,
5823 const u8 *uuid)
5824{
5825 struct btrfs_device *dev;
5826 u64 tmp;
5827
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305828 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005829 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005830
5831 dev = __alloc_device();
5832 if (IS_ERR(dev))
5833 return dev;
5834
5835 if (devid)
5836 tmp = *devid;
5837 else {
5838 int ret;
5839
5840 ret = find_next_devid(fs_info, &tmp);
5841 if (ret) {
5842 kfree(dev);
5843 return ERR_PTR(ret);
5844 }
5845 }
5846 dev->devid = tmp;
5847
5848 if (uuid)
5849 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5850 else
5851 generate_random_uuid(dev->uuid);
5852
Qu Wenruoa8c93d42014-02-28 10:46:08 +08005853 btrfs_init_work(&dev->work, pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005854
5855 return dev;
5856}
5857
Chris Mason0b86a832008-03-24 15:01:56 -04005858static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5859 struct extent_buffer *leaf,
5860 struct btrfs_chunk *chunk)
5861{
5862 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5863 struct map_lookup *map;
5864 struct extent_map *em;
5865 u64 logical;
5866 u64 length;
5867 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005868 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005869 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005870 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005871 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005872
Chris Masone17cade2008-04-15 15:41:47 -04005873 logical = key->offset;
5874 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005875
Chris Mason890871b2009-09-02 16:24:52 -04005876 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005877 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005878 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005879
5880 /* already mapped? */
5881 if (em && em->start <= logical && em->start + em->len > logical) {
5882 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005883 return 0;
5884 } else if (em) {
5885 free_extent_map(em);
5886 }
Chris Mason0b86a832008-03-24 15:01:56 -04005887
David Sterba172ddd62011-04-21 00:48:27 +02005888 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005889 if (!em)
5890 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005891 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5892 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005893 if (!map) {
5894 free_extent_map(em);
5895 return -ENOMEM;
5896 }
5897
Wang Shilong298a8f92014-06-19 10:42:52 +08005898 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04005899 em->bdev = (struct block_device *)map;
5900 em->start = logical;
5901 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005902 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005903 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005904 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005905
Chris Mason593060d2008-03-25 16:50:33 -04005906 map->num_stripes = num_stripes;
5907 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5908 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5909 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5910 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5911 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005912 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005913 for (i = 0; i < num_stripes; i++) {
5914 map->stripes[i].physical =
5915 btrfs_stripe_offset_nr(leaf, chunk, i);
5916 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005917 read_extent_buffer(leaf, uuid, (unsigned long)
5918 btrfs_stripe_dev_uuid_nr(chunk, i),
5919 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005920 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5921 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005922 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005923 free_extent_map(em);
5924 return -EIO;
5925 }
Chris Masondfe25022008-05-13 13:46:40 -04005926 if (!map->stripes[i].dev) {
5927 map->stripes[i].dev =
5928 add_missing_dev(root, devid, uuid);
5929 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04005930 free_extent_map(em);
5931 return -EIO;
5932 }
5933 }
5934 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005935 }
5936
Chris Mason890871b2009-09-02 16:24:52 -04005937 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005938 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04005939 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005940 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005941 free_extent_map(em);
5942
5943 return 0;
5944}
5945
Jeff Mahoney143bede2012-03-01 14:56:26 +01005946static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005947 struct btrfs_dev_item *dev_item,
5948 struct btrfs_device *device)
5949{
5950 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005951
5952 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005953 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5954 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005955 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5956 device->type = btrfs_device_type(leaf, dev_item);
5957 device->io_align = btrfs_device_io_align(leaf, dev_item);
5958 device->io_width = btrfs_device_io_width(leaf, dev_item);
5959 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005960 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005961 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005962
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02005963 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04005964 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005965}
5966
Yan Zheng2b820322008-11-17 21:11:30 -05005967static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5968{
5969 struct btrfs_fs_devices *fs_devices;
5970 int ret;
5971
Li Zefanb367e472011-12-07 11:38:24 +08005972 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05005973
5974 fs_devices = root->fs_info->fs_devices->seed;
5975 while (fs_devices) {
5976 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5977 ret = 0;
5978 goto out;
5979 }
5980 fs_devices = fs_devices->seed;
5981 }
5982
5983 fs_devices = find_fsid(fsid);
5984 if (!fs_devices) {
5985 ret = -ENOENT;
5986 goto out;
5987 }
Yan Zhenge4404d62008-12-12 10:03:26 -05005988
5989 fs_devices = clone_fs_devices(fs_devices);
5990 if (IS_ERR(fs_devices)) {
5991 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005992 goto out;
5993 }
5994
Christoph Hellwig97288f22008-12-02 06:36:09 -05005995 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05005996 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02005997 if (ret) {
5998 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005999 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006000 }
Yan Zheng2b820322008-11-17 21:11:30 -05006001
6002 if (!fs_devices->seeding) {
6003 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006004 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006005 ret = -EINVAL;
6006 goto out;
6007 }
6008
6009 fs_devices->seed = root->fs_info->fs_devices->seed;
6010 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006011out:
Yan Zheng2b820322008-11-17 21:11:30 -05006012 return ret;
6013}
6014
Chris Mason0d81ba52008-03-24 15:02:07 -04006015static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006016 struct extent_buffer *leaf,
6017 struct btrfs_dev_item *dev_item)
6018{
6019 struct btrfs_device *device;
6020 u64 devid;
6021 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006022 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006023 u8 dev_uuid[BTRFS_UUID_SIZE];
6024
Chris Mason0b86a832008-03-24 15:01:56 -04006025 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006026 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006027 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006028 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006029 BTRFS_UUID_SIZE);
6030
6031 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
6032 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05006033 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006034 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006035 }
6036
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006037 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006038 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006039 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006040 return -EIO;
6041
6042 if (!device) {
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006043 btrfs_warn(root->fs_info, "devid %llu missing", devid);
Yan Zheng2b820322008-11-17 21:11:30 -05006044 device = add_missing_dev(root, devid, dev_uuid);
6045 if (!device)
6046 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05006047 } else if (!device->missing) {
6048 /*
6049 * this happens when a device that was properly setup
6050 * in the device info lists suddenly goes bad.
6051 * device->bdev is NULL, and so we have to set
6052 * device->missing to one here
6053 */
6054 root->fs_info->fs_devices->missing_devices++;
6055 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006056 }
6057 }
6058
6059 if (device->fs_devices != root->fs_info->fs_devices) {
6060 BUG_ON(device->writeable);
6061 if (device->generation !=
6062 btrfs_device_generation(leaf, dev_item))
6063 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006064 }
Chris Mason0b86a832008-03-24 15:01:56 -04006065
6066 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006067 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006068 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006069 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006070 spin_lock(&root->fs_info->free_chunk_lock);
6071 root->fs_info->free_chunk_space += device->total_bytes -
6072 device->bytes_used;
6073 spin_unlock(&root->fs_info->free_chunk_lock);
6074 }
Chris Mason0b86a832008-03-24 15:01:56 -04006075 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006076 return ret;
6077}
6078
Yan Zhenge4404d62008-12-12 10:03:26 -05006079int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006080{
David Sterba6c417612011-04-13 15:41:04 +02006081 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006082 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006083 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006084 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04006085 u8 *ptr;
6086 unsigned long sb_ptr;
6087 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006088 u32 num_stripes;
6089 u32 array_size;
6090 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006091 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04006092 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006093
Yan Zhenge4404d62008-12-12 10:03:26 -05006094 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04006095 BTRFS_SUPER_INFO_SIZE);
6096 if (!sb)
6097 return -ENOMEM;
6098 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006099 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006100 /*
6101 * The sb extent buffer is artifical and just used to read the system array.
6102 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6103 * pages up-to-date when the page is larger: extent does not cover the
6104 * whole page and consequently check_page_uptodate does not find all
6105 * the page's extents up-to-date (the hole beyond sb),
6106 * write_extent_buffer then triggers a WARN_ON.
6107 *
6108 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6109 * but sb spans only this function. Add an explicit SetPageUptodate call
6110 * to silence the warning eg. on PowerPC 64.
6111 */
6112 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006113 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006114
Chris Masona061fc82008-05-07 11:43:44 -04006115 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006116 array_size = btrfs_super_sys_array_size(super_copy);
6117
Chris Mason0b86a832008-03-24 15:01:56 -04006118 ptr = super_copy->sys_chunk_array;
6119 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
6120 cur = 0;
6121
6122 while (cur < array_size) {
6123 disk_key = (struct btrfs_disk_key *)ptr;
6124 btrfs_disk_key_to_cpu(&key, disk_key);
6125
Chris Masona061fc82008-05-07 11:43:44 -04006126 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006127 sb_ptr += len;
6128 cur += len;
6129
Chris Mason0d81ba52008-03-24 15:02:07 -04006130 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04006131 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04006132 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006133 if (ret)
6134 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006135 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6136 len = btrfs_chunk_item_size(num_stripes);
6137 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006138 ret = -EIO;
6139 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006140 }
6141 ptr += len;
6142 sb_ptr += len;
6143 cur += len;
6144 }
Chris Masona061fc82008-05-07 11:43:44 -04006145 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006146 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006147}
6148
6149int btrfs_read_chunk_tree(struct btrfs_root *root)
6150{
6151 struct btrfs_path *path;
6152 struct extent_buffer *leaf;
6153 struct btrfs_key key;
6154 struct btrfs_key found_key;
6155 int ret;
6156 int slot;
6157
6158 root = root->fs_info->chunk_root;
6159
6160 path = btrfs_alloc_path();
6161 if (!path)
6162 return -ENOMEM;
6163
Li Zefanb367e472011-12-07 11:38:24 +08006164 mutex_lock(&uuid_mutex);
6165 lock_chunks(root);
6166
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006167 /*
6168 * Read all device items, and then all the chunk items. All
6169 * device items are found before any chunk item (their object id
6170 * is smaller than the lowest possible object id for a chunk
6171 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006172 */
6173 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6174 key.offset = 0;
6175 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006176 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006177 if (ret < 0)
6178 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006179 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006180 leaf = path->nodes[0];
6181 slot = path->slots[0];
6182 if (slot >= btrfs_header_nritems(leaf)) {
6183 ret = btrfs_next_leaf(root, path);
6184 if (ret == 0)
6185 continue;
6186 if (ret < 0)
6187 goto error;
6188 break;
6189 }
6190 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006191 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6192 struct btrfs_dev_item *dev_item;
6193 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006194 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006195 ret = read_one_dev(root, leaf, dev_item);
6196 if (ret)
6197 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006198 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6199 struct btrfs_chunk *chunk;
6200 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6201 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006202 if (ret)
6203 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006204 }
6205 path->slots[0]++;
6206 }
Chris Mason0b86a832008-03-24 15:01:56 -04006207 ret = 0;
6208error:
Li Zefanb367e472011-12-07 11:38:24 +08006209 unlock_chunks(root);
6210 mutex_unlock(&uuid_mutex);
6211
Yan Zheng2b820322008-11-17 21:11:30 -05006212 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006213 return ret;
6214}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006215
Miao Xiecb517ea2013-05-15 07:48:19 +00006216void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6217{
6218 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6219 struct btrfs_device *device;
6220
Liu Bo29cc83f2014-05-11 23:14:59 +08006221 while (fs_devices) {
6222 mutex_lock(&fs_devices->device_list_mutex);
6223 list_for_each_entry(device, &fs_devices->devices, dev_list)
6224 device->dev_root = fs_info->dev_root;
6225 mutex_unlock(&fs_devices->device_list_mutex);
6226
6227 fs_devices = fs_devices->seed;
6228 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006229}
6230
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006231static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6232{
6233 int i;
6234
6235 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6236 btrfs_dev_stat_reset(dev, i);
6237}
6238
6239int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6240{
6241 struct btrfs_key key;
6242 struct btrfs_key found_key;
6243 struct btrfs_root *dev_root = fs_info->dev_root;
6244 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6245 struct extent_buffer *eb;
6246 int slot;
6247 int ret = 0;
6248 struct btrfs_device *device;
6249 struct btrfs_path *path = NULL;
6250 int i;
6251
6252 path = btrfs_alloc_path();
6253 if (!path) {
6254 ret = -ENOMEM;
6255 goto out;
6256 }
6257
6258 mutex_lock(&fs_devices->device_list_mutex);
6259 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6260 int item_size;
6261 struct btrfs_dev_stats_item *ptr;
6262
6263 key.objectid = 0;
6264 key.type = BTRFS_DEV_STATS_KEY;
6265 key.offset = device->devid;
6266 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6267 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006268 __btrfs_reset_dev_stats(device);
6269 device->dev_stats_valid = 1;
6270 btrfs_release_path(path);
6271 continue;
6272 }
6273 slot = path->slots[0];
6274 eb = path->nodes[0];
6275 btrfs_item_key_to_cpu(eb, &found_key, slot);
6276 item_size = btrfs_item_size_nr(eb, slot);
6277
6278 ptr = btrfs_item_ptr(eb, slot,
6279 struct btrfs_dev_stats_item);
6280
6281 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6282 if (item_size >= (1 + i) * sizeof(__le64))
6283 btrfs_dev_stat_set(device, i,
6284 btrfs_dev_stats_value(eb, ptr, i));
6285 else
6286 btrfs_dev_stat_reset(device, i);
6287 }
6288
6289 device->dev_stats_valid = 1;
6290 btrfs_dev_stat_print_on_load(device);
6291 btrfs_release_path(path);
6292 }
6293 mutex_unlock(&fs_devices->device_list_mutex);
6294
6295out:
6296 btrfs_free_path(path);
6297 return ret < 0 ? ret : 0;
6298}
6299
6300static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6301 struct btrfs_root *dev_root,
6302 struct btrfs_device *device)
6303{
6304 struct btrfs_path *path;
6305 struct btrfs_key key;
6306 struct extent_buffer *eb;
6307 struct btrfs_dev_stats_item *ptr;
6308 int ret;
6309 int i;
6310
6311 key.objectid = 0;
6312 key.type = BTRFS_DEV_STATS_KEY;
6313 key.offset = device->devid;
6314
6315 path = btrfs_alloc_path();
6316 BUG_ON(!path);
6317 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6318 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006319 printk_in_rcu(KERN_WARNING "BTRFS: "
6320 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006321 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006322 goto out;
6323 }
6324
6325 if (ret == 0 &&
6326 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6327 /* need to delete old one and insert a new one */
6328 ret = btrfs_del_item(trans, dev_root, path);
6329 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006330 printk_in_rcu(KERN_WARNING "BTRFS: "
6331 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006332 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006333 goto out;
6334 }
6335 ret = 1;
6336 }
6337
6338 if (ret == 1) {
6339 /* need to insert a new item */
6340 btrfs_release_path(path);
6341 ret = btrfs_insert_empty_item(trans, dev_root, path,
6342 &key, sizeof(*ptr));
6343 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006344 printk_in_rcu(KERN_WARNING "BTRFS: "
6345 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006346 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006347 goto out;
6348 }
6349 }
6350
6351 eb = path->nodes[0];
6352 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6353 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6354 btrfs_set_dev_stats_value(eb, ptr, i,
6355 btrfs_dev_stat_read(device, i));
6356 btrfs_mark_buffer_dirty(eb);
6357
6358out:
6359 btrfs_free_path(path);
6360 return ret;
6361}
6362
6363/*
6364 * called from commit_transaction. Writes all changed device stats to disk.
6365 */
6366int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6367 struct btrfs_fs_info *fs_info)
6368{
6369 struct btrfs_root *dev_root = fs_info->dev_root;
6370 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6371 struct btrfs_device *device;
6372 int ret = 0;
6373
6374 mutex_lock(&fs_devices->device_list_mutex);
6375 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6376 if (!device->dev_stats_valid || !device->dev_stats_dirty)
6377 continue;
6378
6379 ret = update_dev_stat_item(trans, dev_root, device);
6380 if (!ret)
6381 device->dev_stats_dirty = 0;
6382 }
6383 mutex_unlock(&fs_devices->device_list_mutex);
6384
6385 return ret;
6386}
6387
Stefan Behrens442a4f62012-05-25 16:06:08 +02006388void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6389{
6390 btrfs_dev_stat_inc(dev, index);
6391 btrfs_dev_stat_print_on_error(dev);
6392}
6393
Eric Sandeen48a3b632013-04-25 20:41:01 +00006394static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006395{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006396 if (!dev->dev_stats_valid)
6397 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006398 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6399 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006400 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006401 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6402 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6403 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006404 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6405 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006406}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006407
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006408static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6409{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006410 int i;
6411
6412 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6413 if (btrfs_dev_stat_read(dev, i) != 0)
6414 break;
6415 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6416 return; /* all values == 0, suppress message */
6417
Frank Holtonefe120a2013-12-20 11:37:06 -05006418 printk_in_rcu(KERN_INFO "BTRFS: "
6419 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006420 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006421 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6422 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6423 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6424 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6425 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6426}
6427
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006428int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006429 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006430{
6431 struct btrfs_device *dev;
6432 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6433 int i;
6434
6435 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006436 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006437 mutex_unlock(&fs_devices->device_list_mutex);
6438
6439 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006440 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006441 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006442 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006443 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006444 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006445 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006446 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6447 if (stats->nr_items > i)
6448 stats->values[i] =
6449 btrfs_dev_stat_read_and_reset(dev, i);
6450 else
6451 btrfs_dev_stat_reset(dev, i);
6452 }
6453 } else {
6454 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6455 if (stats->nr_items > i)
6456 stats->values[i] = btrfs_dev_stat_read(dev, i);
6457 }
6458 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6459 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6460 return 0;
6461}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006462
6463int btrfs_scratch_superblock(struct btrfs_device *device)
6464{
6465 struct buffer_head *bh;
6466 struct btrfs_super_block *disk_super;
6467
6468 bh = btrfs_read_dev_super(device->bdev);
6469 if (!bh)
6470 return -EINVAL;
6471 disk_super = (struct btrfs_super_block *)bh->b_data;
6472
6473 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6474 set_buffer_dirty(bh);
6475 sync_dirty_buffer(bh);
6476 brelse(bh);
6477
6478 return 0;
6479}