blob: 2e078fa705a6d934d6a14cf33b12c82fbaf39500 [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
Miao Xie67a2c452014-09-03 21:35:43 +080053DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -040054static 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);
Miao Xie935e5cc2014-09-03 21:35:33 +080077 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +030078 INIT_LIST_HEAD(&fs_devs->alloc_list);
79 INIT_LIST_HEAD(&fs_devs->list);
80
81 return fs_devs;
82}
83
84/**
85 * alloc_fs_devices - allocate struct btrfs_fs_devices
86 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
87 * generated.
88 *
89 * Return: a pointer to a new &struct btrfs_fs_devices on success;
90 * ERR_PTR() on error. Returned struct is not linked onto any lists and
91 * can be destroyed with kfree() right away.
92 */
93static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
94{
95 struct btrfs_fs_devices *fs_devs;
96
97 fs_devs = __alloc_fs_devices();
98 if (IS_ERR(fs_devs))
99 return fs_devs;
100
101 if (fsid)
102 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
103 else
104 generate_random_uuid(fs_devs->fsid);
105
106 return fs_devs;
107}
108
Yan Zhenge4404d62008-12-12 10:03:26 -0500109static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
110{
111 struct btrfs_device *device;
112 WARN_ON(fs_devices->opened);
113 while (!list_empty(&fs_devices->devices)) {
114 device = list_entry(fs_devices->devices.next,
115 struct btrfs_device, dev_list);
116 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -0400117 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500118 kfree(device);
119 }
120 kfree(fs_devices);
121}
122
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000123static void btrfs_kobject_uevent(struct block_device *bdev,
124 enum kobject_action action)
125{
126 int ret;
127
128 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
129 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500130 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000131 action,
132 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
133 &disk_to_dev(bdev->bd_disk)->kobj);
134}
135
Jeff Mahoney143bede2012-03-01 14:56:26 +0100136void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400137{
138 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400139
Yan Zheng2b820322008-11-17 21:11:30 -0500140 while (!list_empty(&fs_uuids)) {
141 fs_devices = list_entry(fs_uuids.next,
142 struct btrfs_fs_devices, list);
143 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500144 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400145 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400146}
147
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300148static struct btrfs_device *__alloc_device(void)
149{
150 struct btrfs_device *dev;
151
152 dev = kzalloc(sizeof(*dev), GFP_NOFS);
153 if (!dev)
154 return ERR_PTR(-ENOMEM);
155
156 INIT_LIST_HEAD(&dev->dev_list);
157 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800158 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300159
160 spin_lock_init(&dev->io_lock);
161
162 spin_lock_init(&dev->reada_lock);
163 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800164 atomic_set(&dev->dev_stats_ccnt, 0);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300165 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT);
166 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT);
167
168 return dev;
169}
170
Chris Masona1b32a52008-09-05 16:09:51 -0400171static noinline struct btrfs_device *__find_device(struct list_head *head,
172 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400173{
174 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400175
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500176 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400177 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400178 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400179 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400180 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400181 }
182 return NULL;
183}
184
Chris Masona1b32a52008-09-05 16:09:51 -0400185static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400186{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400187 struct btrfs_fs_devices *fs_devices;
188
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500189 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400190 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
191 return fs_devices;
192 }
193 return NULL;
194}
195
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100196static int
197btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
198 int flush, struct block_device **bdev,
199 struct buffer_head **bh)
200{
201 int ret;
202
203 *bdev = blkdev_get_by_path(device_path, flags, holder);
204
205 if (IS_ERR(*bdev)) {
206 ret = PTR_ERR(*bdev);
Frank Holtonefe120a2013-12-20 11:37:06 -0500207 printk(KERN_INFO "BTRFS: open %s failed\n", device_path);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100208 goto error;
209 }
210
211 if (flush)
212 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
213 ret = set_blocksize(*bdev, 4096);
214 if (ret) {
215 blkdev_put(*bdev, flags);
216 goto error;
217 }
218 invalidate_bdev(*bdev);
219 *bh = btrfs_read_dev_super(*bdev);
220 if (!*bh) {
221 ret = -EINVAL;
222 blkdev_put(*bdev, flags);
223 goto error;
224 }
225
226 return 0;
227
228error:
229 *bdev = NULL;
230 *bh = NULL;
231 return ret;
232}
233
Chris Masonffbd5172009-04-20 15:50:09 -0400234static void requeue_list(struct btrfs_pending_bios *pending_bios,
235 struct bio *head, struct bio *tail)
236{
237
238 struct bio *old_head;
239
240 old_head = pending_bios->head;
241 pending_bios->head = head;
242 if (pending_bios->tail)
243 tail->bi_next = old_head;
244 else
245 pending_bios->tail = tail;
246}
247
Chris Mason8b712842008-06-11 16:50:36 -0400248/*
249 * we try to collect pending bios for a device so we don't get a large
250 * number of procs sending bios down to the same device. This greatly
251 * improves the schedulers ability to collect and merge the bios.
252 *
253 * But, it also turns into a long list of bios to process and that is sure
254 * to eventually make the worker thread block. The solution here is to
255 * make some progress and then put this work struct back at the end of
256 * the list if the block device is congested. This way, multiple devices
257 * can make progress from a single worker thread.
258 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100259static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400260{
261 struct bio *pending;
262 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400263 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400264 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400265 struct bio *tail;
266 struct bio *cur;
267 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400268 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400269 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400270 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400271 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400272 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000273 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400274 struct blk_plug plug;
275
276 /*
277 * this function runs all the bios we've collected for
278 * a particular device. We don't want to wander off to
279 * another device without first sending all of these down.
280 * So, setup a plug here and finish it off before we return
281 */
282 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400283
Chris Masonbedf7622009-04-03 10:32:58 -0400284 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400285 fs_info = device->dev_root->fs_info;
286 limit = btrfs_async_submit_limit(fs_info);
287 limit = limit * 2 / 3;
288
Chris Mason8b712842008-06-11 16:50:36 -0400289loop:
290 spin_lock(&device->io_lock);
291
Chris Masona6837052009-02-04 09:19:41 -0500292loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400293 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400294
Chris Mason8b712842008-06-11 16:50:36 -0400295 /* take all the bios off the list at once and process them
296 * later on (without the lock held). But, remember the
297 * tail and other pointers so the bios can be properly reinserted
298 * into the list if we hit congestion
299 */
Chris Masond84275c2009-06-09 15:39:08 -0400300 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400301 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400302 force_reg = 1;
303 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400304 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400305 force_reg = 0;
306 }
Chris Masonffbd5172009-04-20 15:50:09 -0400307
308 pending = pending_bios->head;
309 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400310 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400311
312 /*
313 * if pending was null this time around, no bios need processing
314 * at all and we can stop. Otherwise it'll loop back up again
315 * and do an additional check so no bios are missed.
316 *
317 * device->running_pending is used to synchronize with the
318 * schedule_bio code.
319 */
Chris Masonffbd5172009-04-20 15:50:09 -0400320 if (device->pending_sync_bios.head == NULL &&
321 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400322 again = 0;
323 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400324 } else {
325 again = 1;
326 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400327 }
Chris Masonffbd5172009-04-20 15:50:09 -0400328
329 pending_bios->head = NULL;
330 pending_bios->tail = NULL;
331
Chris Mason8b712842008-06-11 16:50:36 -0400332 spin_unlock(&device->io_lock);
333
Chris Masond3977122009-01-05 21:25:51 -0500334 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400335
336 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400337 /* we want to work on both lists, but do more bios on the
338 * sync list than the regular list
339 */
340 if ((num_run > 32 &&
341 pending_bios != &device->pending_sync_bios &&
342 device->pending_sync_bios.head) ||
343 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
344 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400345 spin_lock(&device->io_lock);
346 requeue_list(pending_bios, pending, tail);
347 goto loop_lock;
348 }
349
Chris Mason8b712842008-06-11 16:50:36 -0400350 cur = pending;
351 pending = pending->bi_next;
352 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400353
Josef Bacik66657b32012-08-01 15:36:24 -0400354 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400355 waitqueue_active(&fs_info->async_submit_wait))
356 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400357
358 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400359
Chris Mason2ab1ba62011-08-04 14:28:36 -0400360 /*
361 * if we're doing the sync list, record that our
362 * plug has some sync requests on it
363 *
364 * If we're doing the regular list and there are
365 * sync requests sitting around, unplug before
366 * we add more
367 */
368 if (pending_bios == &device->pending_sync_bios) {
369 sync_pending = 1;
370 } else if (sync_pending) {
371 blk_finish_plug(&plug);
372 blk_start_plug(&plug);
373 sync_pending = 0;
374 }
375
Stefan Behrens21adbd52011-11-09 13:44:05 +0100376 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400377 num_run++;
378 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100379 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400380 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400381
382 /*
383 * we made progress, there is more work to do and the bdi
384 * is now congested. Back off and let other work structs
385 * run instead
386 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400387 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500388 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400389 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400390
Chris Masonb765ead2009-04-03 10:27:10 -0400391 ioc = current->io_context;
392
393 /*
394 * the main goal here is that we don't want to
395 * block if we're going to be able to submit
396 * more requests without blocking.
397 *
398 * This code does two great things, it pokes into
399 * the elevator code from a filesystem _and_
400 * it makes assumptions about how batching works.
401 */
402 if (ioc && ioc->nr_batch_requests > 0 &&
403 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
404 (last_waited == 0 ||
405 ioc->last_waited == last_waited)) {
406 /*
407 * we want to go through our batch of
408 * requests and stop. So, we copy out
409 * the ioc->last_waited time and test
410 * against it before looping
411 */
412 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100413 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400414 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400415 continue;
416 }
Chris Mason8b712842008-06-11 16:50:36 -0400417 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400418 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500419 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400420
421 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800422 btrfs_queue_work(fs_info->submit_workers,
423 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400424 goto done;
425 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500426 /* unplug every 64 requests just for good measure */
427 if (batch_run % 64 == 0) {
428 blk_finish_plug(&plug);
429 blk_start_plug(&plug);
430 sync_pending = 0;
431 }
Chris Mason8b712842008-06-11 16:50:36 -0400432 }
Chris Masonffbd5172009-04-20 15:50:09 -0400433
Chris Mason51684082010-03-10 15:33:32 -0500434 cond_resched();
435 if (again)
436 goto loop;
437
438 spin_lock(&device->io_lock);
439 if (device->pending_bios.head || device->pending_sync_bios.head)
440 goto loop_lock;
441 spin_unlock(&device->io_lock);
442
Chris Mason8b712842008-06-11 16:50:36 -0400443done:
Chris Mason211588a2011-04-19 20:12:40 -0400444 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400445}
446
Christoph Hellwigb2950862008-12-02 09:54:17 -0500447static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400448{
449 struct btrfs_device *device;
450
451 device = container_of(work, struct btrfs_device, work);
452 run_scheduled_bios(device);
453}
454
David Sterba60999ca2014-03-26 18:26:36 +0100455/*
456 * Add new device to list of registered devices
457 *
458 * Returns:
459 * 1 - first time device is seen
460 * 0 - device already known
461 * < 0 - error
462 */
Chris Masona1b32a52008-09-05 16:09:51 -0400463static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400464 struct btrfs_super_block *disk_super,
465 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
466{
467 struct btrfs_device *device;
468 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400469 struct rcu_string *name;
David Sterba60999ca2014-03-26 18:26:36 +0100470 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400471 u64 found_transid = btrfs_super_generation(disk_super);
472
473 fs_devices = find_fsid(disk_super->fsid);
474 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300475 fs_devices = alloc_fs_devices(disk_super->fsid);
476 if (IS_ERR(fs_devices))
477 return PTR_ERR(fs_devices);
478
Chris Mason8a4b83c2008-03-24 15:02:07 -0400479 list_add(&fs_devices->list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300480
Chris Mason8a4b83c2008-03-24 15:02:07 -0400481 device = NULL;
482 } else {
Chris Masona4437552008-04-18 10:29:38 -0400483 device = __find_device(&fs_devices->devices, devid,
484 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400485 }
Miao Xie443f24f2014-07-24 11:37:15 +0800486
Chris Mason8a4b83c2008-03-24 15:02:07 -0400487 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500488 if (fs_devices->opened)
489 return -EBUSY;
490
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300491 device = btrfs_alloc_device(NULL, &devid,
492 disk_super->dev_item.uuid);
493 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400494 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300495 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400496 }
Josef Bacik606686e2012-06-04 14:03:51 -0400497
498 name = rcu_string_strdup(path, GFP_NOFS);
499 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400500 kfree(device);
501 return -ENOMEM;
502 }
Josef Bacik606686e2012-06-04 14:03:51 -0400503 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200504
Chris Masone5e9a522009-06-10 15:17:02 -0400505 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000506 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100507 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400508 mutex_unlock(&fs_devices->device_list_mutex);
509
David Sterba60999ca2014-03-26 18:26:36 +0100510 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500511 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400512 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800513 /*
514 * When FS is already mounted.
515 * 1. If you are here and if the device->name is NULL that
516 * means this device was missing at time of FS mount.
517 * 2. If you are here and if the device->name is different
518 * from 'path' that means either
519 * a. The same device disappeared and reappeared with
520 * different name. or
521 * b. The missing-disk-which-was-replaced, has
522 * reappeared now.
523 *
524 * We must allow 1 and 2a above. But 2b would be a spurious
525 * and unintentional.
526 *
527 * Further in case of 1 and 2a above, the disk at 'path'
528 * would have missed some transaction when it was away and
529 * in case of 2a the stale bdev has to be updated as well.
530 * 2b must not be allowed at all time.
531 */
532
533 /*
534 * As of now don't allow update to btrfs_fs_device through
535 * the btrfs dev scan cli, after FS has been mounted.
536 */
Anand Jain77bdae42014-07-03 18:22:06 +0800537 if (fs_devices->opened) {
Anand Jainb96de002014-07-03 18:22:05 +0800538 return -EBUSY;
Anand Jain77bdae42014-07-03 18:22:06 +0800539 } else {
540 /*
541 * That is if the FS is _not_ mounted and if you
542 * are here, that means there is more than one
543 * disk with same uuid and devid.We keep the one
544 * with larger generation number or the last-in if
545 * generation are equal.
546 */
547 if (found_transid < device->generation)
548 return -EEXIST;
549 }
Anand Jainb96de002014-07-03 18:22:05 +0800550
Josef Bacik606686e2012-06-04 14:03:51 -0400551 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000552 if (!name)
553 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400554 rcu_string_free(device->name);
555 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500556 if (device->missing) {
557 fs_devices->missing_devices--;
558 device->missing = 0;
559 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400560 }
561
Anand Jain77bdae42014-07-03 18:22:06 +0800562 /*
563 * Unmount does not free the btrfs_device struct but would zero
564 * generation along with most of the other members. So just update
565 * it back. We need it to pick the disk with largest generation
566 * (as above).
567 */
568 if (!fs_devices->opened)
569 device->generation = found_transid;
570
Chris Mason8a4b83c2008-03-24 15:02:07 -0400571 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100572
573 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400574}
575
Yan Zhenge4404d62008-12-12 10:03:26 -0500576static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
577{
578 struct btrfs_fs_devices *fs_devices;
579 struct btrfs_device *device;
580 struct btrfs_device *orig_dev;
581
Ilya Dryomov2208a372013-08-12 14:33:03 +0300582 fs_devices = alloc_fs_devices(orig->fsid);
583 if (IS_ERR(fs_devices))
584 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500585
Miao Xieadbbb862014-09-03 21:35:42 +0800586 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400587 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500588
Xiao Guangrong46224702011-04-20 10:08:47 +0000589 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500590 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400591 struct rcu_string *name;
592
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300593 device = btrfs_alloc_device(NULL, &orig_dev->devid,
594 orig_dev->uuid);
595 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500596 goto error;
597
Josef Bacik606686e2012-06-04 14:03:51 -0400598 /*
599 * This is ok to do without rcu read locked because we hold the
600 * uuid mutex so nothing we touch in here is going to disappear.
601 */
Anand Jaine755f782014-06-30 17:12:47 +0800602 if (orig_dev->name) {
603 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
604 if (!name) {
605 kfree(device);
606 goto error;
607 }
608 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400609 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500610
Yan Zhenge4404d62008-12-12 10:03:26 -0500611 list_add(&device->dev_list, &fs_devices->devices);
612 device->fs_devices = fs_devices;
613 fs_devices->num_devices++;
614 }
Miao Xieadbbb862014-09-03 21:35:42 +0800615 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500616 return fs_devices;
617error:
Miao Xieadbbb862014-09-03 21:35:42 +0800618 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500619 free_fs_devices(fs_devices);
620 return ERR_PTR(-ENOMEM);
621}
622
Stefan Behrens8dabb742012-11-06 13:15:27 +0100623void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
624 struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400625{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500626 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800627 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500628
Chris Masondfe25022008-05-13 13:46:40 -0400629 mutex_lock(&uuid_mutex);
630again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000631 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500632 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500633 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100634 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800635 (!latest_dev ||
636 device->generation > latest_dev->generation)) {
637 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500638 }
Yan Zheng2b820322008-11-17 21:11:30 -0500639 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500640 }
Yan Zheng2b820322008-11-17 21:11:30 -0500641
Stefan Behrens8dabb742012-11-06 13:15:27 +0100642 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
643 /*
644 * In the first step, keep the device which has
645 * the correct fsid and the devid that is used
646 * for the dev_replace procedure.
647 * In the second step, the dev_replace state is
648 * read from the device tree and it is known
649 * whether the procedure is really active or
650 * not, which means whether this device is
651 * used or whether it should be removed.
652 */
653 if (step == 0 || device->is_tgtdev_for_dev_replace) {
654 continue;
655 }
656 }
Yan Zheng2b820322008-11-17 21:11:30 -0500657 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100658 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500659 device->bdev = NULL;
660 fs_devices->open_devices--;
661 }
662 if (device->writeable) {
663 list_del_init(&device->dev_alloc_list);
664 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100665 if (!device->is_tgtdev_for_dev_replace)
666 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500667 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500668 list_del_init(&device->dev_list);
669 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400670 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500671 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400672 }
Yan Zheng2b820322008-11-17 21:11:30 -0500673
674 if (fs_devices->seed) {
675 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500676 goto again;
677 }
678
Miao Xie443f24f2014-07-24 11:37:15 +0800679 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500680
Chris Masondfe25022008-05-13 13:46:40 -0400681 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400682}
Chris Masona0af4692008-05-13 16:03:06 -0400683
Xiao Guangrong1f781602011-04-20 10:09:16 +0000684static void __free_device(struct work_struct *work)
685{
686 struct btrfs_device *device;
687
688 device = container_of(work, struct btrfs_device, rcu_work);
689
690 if (device->bdev)
691 blkdev_put(device->bdev, device->mode);
692
Josef Bacik606686e2012-06-04 14:03:51 -0400693 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000694 kfree(device);
695}
696
697static void free_device(struct rcu_head *head)
698{
699 struct btrfs_device *device;
700
701 device = container_of(head, struct btrfs_device, rcu);
702
703 INIT_WORK(&device->rcu_work, __free_device);
704 schedule_work(&device->rcu_work);
705}
706
Yan Zheng2b820322008-11-17 21:11:30 -0500707static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400708{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400709 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500710
Yan Zheng2b820322008-11-17 21:11:30 -0500711 if (--fs_devices->opened > 0)
712 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400713
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000714 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500715 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000716 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400717 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000718
719 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400720 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000721
Ilya Dryomovf747cab2013-10-10 20:37:29 +0300722 if (device->writeable &&
723 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500724 list_del_init(&device->dev_alloc_list);
725 fs_devices->rw_devices--;
726 }
727
Josef Bacik726551e2013-08-26 13:45:53 -0400728 if (device->missing)
729 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000730
Ilya Dryomova1e87802013-08-12 14:33:04 +0300731 new_device = btrfs_alloc_device(NULL, &device->devid,
732 device->uuid);
733 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400734
735 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400736 if (device->name) {
737 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300738 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400739 rcu_assign_pointer(new_device->name, name);
740 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300741
Xiao Guangrong1f781602011-04-20 10:09:16 +0000742 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300743 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000744
745 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400746 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000747 mutex_unlock(&fs_devices->device_list_mutex);
748
Yan Zhenge4404d62008-12-12 10:03:26 -0500749 WARN_ON(fs_devices->open_devices);
750 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500751 fs_devices->opened = 0;
752 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500753
Chris Mason8a4b83c2008-03-24 15:02:07 -0400754 return 0;
755}
756
Yan Zheng2b820322008-11-17 21:11:30 -0500757int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
758{
Yan Zhenge4404d62008-12-12 10:03:26 -0500759 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500760 int ret;
761
762 mutex_lock(&uuid_mutex);
763 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500764 if (!fs_devices->opened) {
765 seed_devices = fs_devices->seed;
766 fs_devices->seed = NULL;
767 }
Yan Zheng2b820322008-11-17 21:11:30 -0500768 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500769
770 while (seed_devices) {
771 fs_devices = seed_devices;
772 seed_devices = fs_devices->seed;
773 __btrfs_close_devices(fs_devices);
774 free_fs_devices(fs_devices);
775 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000776 /*
777 * Wait for rcu kworkers under __btrfs_close_devices
778 * to finish all blkdev_puts so device is really
779 * free when umount is done.
780 */
781 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500782 return ret;
783}
784
Yan Zhenge4404d62008-12-12 10:03:26 -0500785static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
786 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400787{
Josef Bacikd5e20032011-08-04 14:52:27 +0000788 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400789 struct block_device *bdev;
790 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400791 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +0800792 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -0400793 struct buffer_head *bh;
794 struct btrfs_super_block *disk_super;
Chris Masona0af4692008-05-13 16:03:06 -0400795 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500796 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400797 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400798
Tejun Heod4d77622010-11-13 11:55:18 +0100799 flags |= FMODE_EXCL;
800
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500801 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400802 if (device->bdev)
803 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400804 if (!device->name)
805 continue;
806
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000807 /* Just open everything we can; ignore failures here */
808 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
809 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100810 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400811
812 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000813 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400814 if (devid != device->devid)
815 goto error_brelse;
816
Yan Zheng2b820322008-11-17 21:11:30 -0500817 if (memcmp(device->uuid, disk_super->dev_item.uuid,
818 BTRFS_UUID_SIZE))
819 goto error_brelse;
820
821 device->generation = btrfs_super_generation(disk_super);
Miao Xie443f24f2014-07-24 11:37:15 +0800822 if (!latest_dev ||
823 device->generation > latest_dev->generation)
824 latest_dev = device;
Chris Masona0af4692008-05-13 16:03:06 -0400825
Yan Zheng2b820322008-11-17 21:11:30 -0500826 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
827 device->writeable = 0;
828 } else {
829 device->writeable = !bdev_read_only(bdev);
830 seeding = 0;
831 }
832
Josef Bacikd5e20032011-08-04 14:52:27 +0000833 q = bdev_get_queue(bdev);
Miao Xie90180da2014-09-03 21:35:30 +0800834 if (blk_queue_discard(q))
Josef Bacikd5e20032011-08-04 14:52:27 +0000835 device->can_discard = 1;
Josef Bacikd5e20032011-08-04 14:52:27 +0000836
Chris Mason8a4b83c2008-03-24 15:02:07 -0400837 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400838 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500839 device->mode = flags;
840
Chris Masonc2898112009-06-10 09:51:32 -0400841 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
842 fs_devices->rotating = 1;
843
Chris Masona0af4692008-05-13 16:03:06 -0400844 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300845 if (device->writeable &&
846 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500847 fs_devices->rw_devices++;
848 list_add(&device->dev_alloc_list,
849 &fs_devices->alloc_list);
850 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000851 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400852 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400853
Chris Masona0af4692008-05-13 16:03:06 -0400854error_brelse:
855 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100856 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400857 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400858 }
Chris Masona0af4692008-05-13 16:03:06 -0400859 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300860 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400861 goto out;
862 }
Yan Zheng2b820322008-11-17 21:11:30 -0500863 fs_devices->seeding = seeding;
864 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +0800865 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -0500866 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400867out:
Yan Zheng2b820322008-11-17 21:11:30 -0500868 return ret;
869}
870
871int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500872 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500873{
874 int ret;
875
876 mutex_lock(&uuid_mutex);
877 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500878 fs_devices->opened++;
879 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500880 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500881 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500882 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400883 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400884 return ret;
885}
886
David Sterba6f60cbd2013-02-15 11:31:02 -0700887/*
888 * Look for a btrfs signature on a device. This may be called out of the mount path
889 * and we are not allowed to call set_blocksize during the scan. The superblock
890 * is read via pagecache
891 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500892int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400893 struct btrfs_fs_devices **fs_devices_ret)
894{
895 struct btrfs_super_block *disk_super;
896 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700897 struct page *page;
898 void *p;
899 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400900 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400901 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400902 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700903 u64 bytenr;
904 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400905
David Sterba6f60cbd2013-02-15 11:31:02 -0700906 /*
907 * we would like to check all the supers, but that would make
908 * a btrfs mount succeed after a mkfs from a different FS.
909 * So, we need to add a special mount option to scan for
910 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
911 */
912 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100913 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500914 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700915
916 bdev = blkdev_get_by_path(path, flags, holder);
917
918 if (IS_ERR(bdev)) {
919 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100920 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700921 }
922
923 /* make sure our super fits in the device */
924 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
925 goto error_bdev_put;
926
927 /* make sure our super fits in the page */
928 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
929 goto error_bdev_put;
930
931 /* make sure our super doesn't straddle pages on disk */
932 index = bytenr >> PAGE_CACHE_SHIFT;
933 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
934 goto error_bdev_put;
935
936 /* pull in the page with our super */
937 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
938 index, GFP_NOFS);
939
940 if (IS_ERR_OR_NULL(page))
941 goto error_bdev_put;
942
943 p = kmap(page);
944
945 /* align our pointer to the offset of the super block */
946 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
947
948 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +0800949 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -0700950 goto error_unmap;
951
Xiao Guangronga3438322010-01-06 11:48:18 +0000952 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400953 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400954 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700955
Chris Mason8a4b83c2008-03-24 15:02:07 -0400956 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +0100957 if (ret > 0) {
958 if (disk_super->label[0]) {
959 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
960 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
961 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
962 } else {
963 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
964 }
965
966 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
967 ret = 0;
968 }
Josef Bacik02db0842012-06-21 16:03:58 -0400969 if (!ret && fs_devices_ret)
970 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700971
972error_unmap:
973 kunmap(page);
974 page_cache_release(page);
975
976error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100977 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400978error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100979 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400980 return ret;
981}
Chris Mason0b86a832008-03-24 15:01:56 -0400982
Miao Xie6d07bce2011-01-05 10:07:31 +0000983/* helper to account the used device space in the range */
984int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
985 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400986{
987 struct btrfs_key key;
988 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000989 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500990 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000991 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400992 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000993 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400994 struct extent_buffer *l;
995
Miao Xie6d07bce2011-01-05 10:07:31 +0000996 *length = 0;
997
Stefan Behrens63a212a2012-11-05 18:29:28 +0100998 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000999 return 0;
1000
Yan Zheng2b820322008-11-17 21:11:30 -05001001 path = btrfs_alloc_path();
1002 if (!path)
1003 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001004 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -04001005
Chris Mason0b86a832008-03-24 15:01:56 -04001006 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001007 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001008 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001009
1010 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001011 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001012 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001013 if (ret > 0) {
1014 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1015 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001016 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001017 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001018
Chris Mason0b86a832008-03-24 15:01:56 -04001019 while (1) {
1020 l = path->nodes[0];
1021 slot = path->slots[0];
1022 if (slot >= btrfs_header_nritems(l)) {
1023 ret = btrfs_next_leaf(root, path);
1024 if (ret == 0)
1025 continue;
1026 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001027 goto out;
1028
1029 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001030 }
1031 btrfs_item_key_to_cpu(l, &key, slot);
1032
1033 if (key.objectid < device->devid)
1034 goto next;
1035
1036 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001037 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001038
David Sterba962a2982014-06-04 18:41:45 +02001039 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001040 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001041
Chris Mason0b86a832008-03-24 15:01:56 -04001042 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001043 extent_end = key.offset + btrfs_dev_extent_length(l,
1044 dev_extent);
1045 if (key.offset <= start && extent_end > end) {
1046 *length = end - start + 1;
1047 break;
1048 } else if (key.offset <= start && extent_end > start)
1049 *length += extent_end - start;
1050 else if (key.offset > start && extent_end <= end)
1051 *length += extent_end - key.offset;
1052 else if (key.offset > start && key.offset <= end) {
1053 *length += end - key.offset + 1;
1054 break;
1055 } else if (key.offset > end)
1056 break;
1057
1058next:
1059 path->slots[0]++;
1060 }
1061 ret = 0;
1062out:
1063 btrfs_free_path(path);
1064 return ret;
1065}
1066
Josef Bacik6df9a952013-06-27 13:22:46 -04001067static int contains_pending_extent(struct btrfs_trans_handle *trans,
1068 struct btrfs_device *device,
1069 u64 *start, u64 len)
1070{
1071 struct extent_map *em;
1072 int ret = 0;
1073
1074 list_for_each_entry(em, &trans->transaction->pending_chunks, list) {
1075 struct map_lookup *map;
1076 int i;
1077
1078 map = (struct map_lookup *)em->bdev;
1079 for (i = 0; i < map->num_stripes; i++) {
1080 if (map->stripes[i].dev != device)
1081 continue;
1082 if (map->stripes[i].physical >= *start + len ||
1083 map->stripes[i].physical + em->orig_block_len <=
1084 *start)
1085 continue;
1086 *start = map->stripes[i].physical +
1087 em->orig_block_len;
1088 ret = 1;
1089 }
1090 }
1091
1092 return ret;
1093}
1094
1095
Chris Mason0b86a832008-03-24 15:01:56 -04001096/*
Miao Xie7bfc8372011-01-05 10:07:26 +00001097 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +00001098 * @device: the device which we search the free space in
1099 * @num_bytes: the size of the free space that we need
1100 * @start: store the start of the free space.
1101 * @len: the size of the free space. that we find, or the size of the max
1102 * free space if we don't find suitable free space
1103 *
Chris Mason0b86a832008-03-24 15:01:56 -04001104 * this uses a pretty simple search, the expectation is that it is
1105 * called very infrequently and that a given device has a small number
1106 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001107 *
1108 * @start is used to store the start of the free space if we find. But if we
1109 * don't find suitable free space, it will be used to store the start position
1110 * of the max free space.
1111 *
1112 * @len is used to store the size of the free space that we find.
1113 * But if we don't find suitable free space, it is used to store the size of
1114 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001115 */
Josef Bacik6df9a952013-06-27 13:22:46 -04001116int find_free_dev_extent(struct btrfs_trans_handle *trans,
1117 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001118 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001119{
1120 struct btrfs_key key;
1121 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001122 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001123 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001124 u64 hole_size;
1125 u64 max_hole_start;
1126 u64 max_hole_size;
1127 u64 extent_end;
1128 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001129 u64 search_end = device->total_bytes;
1130 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001131 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001132 struct extent_buffer *l;
1133
Chris Mason0b86a832008-03-24 15:01:56 -04001134 /* FIXME use last free of some kind */
1135
1136 /* we don't want to overwrite the superblock on the drive,
1137 * so we make sure to start at an offset of at least 1MB
1138 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001139 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001140
Josef Bacik6df9a952013-06-27 13:22:46 -04001141 path = btrfs_alloc_path();
1142 if (!path)
1143 return -ENOMEM;
1144again:
Miao Xie7bfc8372011-01-05 10:07:26 +00001145 max_hole_start = search_start;
1146 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +00001147 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +00001148
Stefan Behrens63a212a2012-11-05 18:29:28 +01001149 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001150 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001151 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001152 }
1153
Miao Xie7bfc8372011-01-05 10:07:26 +00001154 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001155 path->search_commit_root = 1;
1156 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001157
Chris Mason0b86a832008-03-24 15:01:56 -04001158 key.objectid = device->devid;
1159 key.offset = search_start;
1160 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001161
Li Zefan125ccb02011-12-08 15:07:24 +08001162 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001163 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001164 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001165 if (ret > 0) {
1166 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1167 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001168 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001169 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001170
Chris Mason0b86a832008-03-24 15:01:56 -04001171 while (1) {
1172 l = path->nodes[0];
1173 slot = path->slots[0];
1174 if (slot >= btrfs_header_nritems(l)) {
1175 ret = btrfs_next_leaf(root, path);
1176 if (ret == 0)
1177 continue;
1178 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001179 goto out;
1180
1181 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001182 }
1183 btrfs_item_key_to_cpu(l, &key, slot);
1184
1185 if (key.objectid < device->devid)
1186 goto next;
1187
1188 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001189 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001190
David Sterba962a2982014-06-04 18:41:45 +02001191 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001192 goto next;
1193
Miao Xie7bfc8372011-01-05 10:07:26 +00001194 if (key.offset > search_start) {
1195 hole_size = key.offset - search_start;
1196
Josef Bacik6df9a952013-06-27 13:22:46 -04001197 /*
1198 * Have to check before we set max_hole_start, otherwise
1199 * we could end up sending back this offset anyway.
1200 */
1201 if (contains_pending_extent(trans, device,
1202 &search_start,
1203 hole_size))
1204 hole_size = 0;
1205
Miao Xie7bfc8372011-01-05 10:07:26 +00001206 if (hole_size > max_hole_size) {
1207 max_hole_start = search_start;
1208 max_hole_size = hole_size;
1209 }
1210
1211 /*
1212 * If this free space is greater than which we need,
1213 * it must be the max free space that we have found
1214 * until now, so max_hole_start must point to the start
1215 * of this free space and the length of this free space
1216 * is stored in max_hole_size. Thus, we return
1217 * max_hole_start and max_hole_size and go back to the
1218 * caller.
1219 */
1220 if (hole_size >= num_bytes) {
1221 ret = 0;
1222 goto out;
1223 }
1224 }
1225
Chris Mason0b86a832008-03-24 15:01:56 -04001226 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001227 extent_end = key.offset + btrfs_dev_extent_length(l,
1228 dev_extent);
1229 if (extent_end > search_start)
1230 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001231next:
1232 path->slots[0]++;
1233 cond_resched();
1234 }
Chris Mason0b86a832008-03-24 15:01:56 -04001235
liubo38c01b92011-08-02 02:39:03 +00001236 /*
1237 * At this point, search_start should be the end of
1238 * allocated dev extents, and when shrinking the device,
1239 * search_end may be smaller than search_start.
1240 */
1241 if (search_end > search_start)
1242 hole_size = search_end - search_start;
1243
Miao Xie7bfc8372011-01-05 10:07:26 +00001244 if (hole_size > max_hole_size) {
1245 max_hole_start = search_start;
1246 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001247 }
Chris Mason0b86a832008-03-24 15:01:56 -04001248
Josef Bacik6df9a952013-06-27 13:22:46 -04001249 if (contains_pending_extent(trans, device, &search_start, hole_size)) {
1250 btrfs_release_path(path);
1251 goto again;
1252 }
1253
Miao Xie7bfc8372011-01-05 10:07:26 +00001254 /* See above. */
1255 if (hole_size < num_bytes)
1256 ret = -ENOSPC;
1257 else
1258 ret = 0;
1259
1260out:
Yan Zheng2b820322008-11-17 21:11:30 -05001261 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001262 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001263 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001264 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001265 return ret;
1266}
1267
Christoph Hellwigb2950862008-12-02 09:54:17 -05001268static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001269 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001270 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001271{
1272 int ret;
1273 struct btrfs_path *path;
1274 struct btrfs_root *root = device->dev_root;
1275 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001276 struct btrfs_key found_key;
1277 struct extent_buffer *leaf = NULL;
1278 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001279
1280 path = btrfs_alloc_path();
1281 if (!path)
1282 return -ENOMEM;
1283
1284 key.objectid = device->devid;
1285 key.offset = start;
1286 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001287again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001288 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001289 if (ret > 0) {
1290 ret = btrfs_previous_item(root, path, key.objectid,
1291 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001292 if (ret)
1293 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001294 leaf = path->nodes[0];
1295 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1296 extent = btrfs_item_ptr(leaf, path->slots[0],
1297 struct btrfs_dev_extent);
1298 BUG_ON(found_key.offset > start || found_key.offset +
1299 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001300 key = found_key;
1301 btrfs_release_path(path);
1302 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001303 } else if (ret == 0) {
1304 leaf = path->nodes[0];
1305 extent = btrfs_item_ptr(leaf, path->slots[0],
1306 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001307 } else {
1308 btrfs_error(root->fs_info, ret, "Slot search failed");
1309 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001310 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001311
Miao Xie2196d6e2014-09-03 21:35:41 +08001312 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1313
Chris Mason8f18cf12008-04-25 16:53:30 -04001314 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001315 if (ret) {
1316 btrfs_error(root->fs_info, ret,
1317 "Failed to remove dev extent item");
1318 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001319out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001320 btrfs_free_path(path);
1321 return ret;
1322}
1323
Eric Sandeen48a3b632013-04-25 20:41:01 +00001324static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1325 struct btrfs_device *device,
1326 u64 chunk_tree, u64 chunk_objectid,
1327 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001328{
1329 int ret;
1330 struct btrfs_path *path;
1331 struct btrfs_root *root = device->dev_root;
1332 struct btrfs_dev_extent *extent;
1333 struct extent_buffer *leaf;
1334 struct btrfs_key key;
1335
Chris Masondfe25022008-05-13 13:46:40 -04001336 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001337 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001338 path = btrfs_alloc_path();
1339 if (!path)
1340 return -ENOMEM;
1341
Chris Mason0b86a832008-03-24 15:01:56 -04001342 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001343 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001344 key.type = BTRFS_DEV_EXTENT_KEY;
1345 ret = btrfs_insert_empty_item(trans, root, path, &key,
1346 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001347 if (ret)
1348 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001349
1350 leaf = path->nodes[0];
1351 extent = btrfs_item_ptr(leaf, path->slots[0],
1352 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001353 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1354 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1355 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1356
1357 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001358 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001359
Chris Mason0b86a832008-03-24 15:01:56 -04001360 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1361 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001362out:
Chris Mason0b86a832008-03-24 15:01:56 -04001363 btrfs_free_path(path);
1364 return ret;
1365}
1366
Josef Bacik6df9a952013-06-27 13:22:46 -04001367static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001368{
Josef Bacik6df9a952013-06-27 13:22:46 -04001369 struct extent_map_tree *em_tree;
1370 struct extent_map *em;
1371 struct rb_node *n;
1372 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001373
Josef Bacik6df9a952013-06-27 13:22:46 -04001374 em_tree = &fs_info->mapping_tree.map_tree;
1375 read_lock(&em_tree->lock);
1376 n = rb_last(&em_tree->map);
1377 if (n) {
1378 em = rb_entry(n, struct extent_map, rb_node);
1379 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001380 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001381 read_unlock(&em_tree->lock);
1382
Chris Mason0b86a832008-03-24 15:01:56 -04001383 return ret;
1384}
1385
Ilya Dryomov53f10652013-08-12 14:33:01 +03001386static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1387 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001388{
1389 int ret;
1390 struct btrfs_key key;
1391 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001392 struct btrfs_path *path;
1393
Yan Zheng2b820322008-11-17 21:11:30 -05001394 path = btrfs_alloc_path();
1395 if (!path)
1396 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001397
1398 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1399 key.type = BTRFS_DEV_ITEM_KEY;
1400 key.offset = (u64)-1;
1401
Ilya Dryomov53f10652013-08-12 14:33:01 +03001402 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001403 if (ret < 0)
1404 goto error;
1405
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001406 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001407
Ilya Dryomov53f10652013-08-12 14:33:01 +03001408 ret = btrfs_previous_item(fs_info->chunk_root, path,
1409 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001410 BTRFS_DEV_ITEM_KEY);
1411 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001412 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001413 } else {
1414 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1415 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001416 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001417 }
1418 ret = 0;
1419error:
Yan Zheng2b820322008-11-17 21:11:30 -05001420 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001421 return ret;
1422}
1423
1424/*
1425 * the device information is stored in the chunk root
1426 * the btrfs_device struct should be fully filled in
1427 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001428static int btrfs_add_device(struct btrfs_trans_handle *trans,
1429 struct btrfs_root *root,
1430 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001431{
1432 int ret;
1433 struct btrfs_path *path;
1434 struct btrfs_dev_item *dev_item;
1435 struct extent_buffer *leaf;
1436 struct btrfs_key key;
1437 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001438
1439 root = root->fs_info->chunk_root;
1440
1441 path = btrfs_alloc_path();
1442 if (!path)
1443 return -ENOMEM;
1444
Chris Mason0b86a832008-03-24 15:01:56 -04001445 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1446 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001447 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001448
1449 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001450 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001451 if (ret)
1452 goto out;
1453
1454 leaf = path->nodes[0];
1455 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1456
1457 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001458 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001459 btrfs_set_device_type(leaf, dev_item, device->type);
1460 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1461 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1462 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001463 btrfs_set_device_total_bytes(leaf, dev_item,
1464 btrfs_device_get_disk_total_bytes(device));
1465 btrfs_set_device_bytes_used(leaf, dev_item,
1466 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001467 btrfs_set_device_group(leaf, dev_item, 0);
1468 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1469 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001470 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001471
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001472 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001473 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001474 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001475 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001476 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001477
Yan Zheng2b820322008-11-17 21:11:30 -05001478 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001479out:
1480 btrfs_free_path(path);
1481 return ret;
1482}
Chris Mason8f18cf12008-04-25 16:53:30 -04001483
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001484/*
1485 * Function to update ctime/mtime for a given device path.
1486 * Mainly used for ctime/mtime based probe like libblkid.
1487 */
1488static void update_dev_time(char *path_name)
1489{
1490 struct file *filp;
1491
1492 filp = filp_open(path_name, O_RDWR, 0);
1493 if (!filp)
1494 return;
1495 file_update_time(filp);
1496 filp_close(filp, NULL);
1497 return;
1498}
1499
Chris Masona061fc82008-05-07 11:43:44 -04001500static int btrfs_rm_dev_item(struct btrfs_root *root,
1501 struct btrfs_device *device)
1502{
1503 int ret;
1504 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001505 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001506 struct btrfs_trans_handle *trans;
1507
1508 root = root->fs_info->chunk_root;
1509
1510 path = btrfs_alloc_path();
1511 if (!path)
1512 return -ENOMEM;
1513
Yan, Zhenga22285a2010-05-16 10:48:46 -04001514 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001515 if (IS_ERR(trans)) {
1516 btrfs_free_path(path);
1517 return PTR_ERR(trans);
1518 }
Chris Masona061fc82008-05-07 11:43:44 -04001519 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1520 key.type = BTRFS_DEV_ITEM_KEY;
1521 key.offset = device->devid;
1522
1523 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1524 if (ret < 0)
1525 goto out;
1526
1527 if (ret > 0) {
1528 ret = -ENOENT;
1529 goto out;
1530 }
1531
1532 ret = btrfs_del_item(trans, root, path);
1533 if (ret)
1534 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001535out:
1536 btrfs_free_path(path);
1537 btrfs_commit_transaction(trans, root);
1538 return ret;
1539}
1540
1541int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1542{
1543 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001544 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001545 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001546 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001547 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001548 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001549 u64 all_avail;
1550 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001551 u64 num_devices;
1552 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001553 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001554 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001555 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001556
Chris Masona061fc82008-05-07 11:43:44 -04001557 mutex_lock(&uuid_mutex);
1558
Miao Xiede98ced2013-01-29 10:13:12 +00001559 do {
1560 seq = read_seqbegin(&root->fs_info->profiles_lock);
1561
1562 all_avail = root->fs_info->avail_data_alloc_bits |
1563 root->fs_info->avail_system_alloc_bits |
1564 root->fs_info->avail_metadata_alloc_bits;
1565 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001566
Stefan Behrens8dabb742012-11-06 13:15:27 +01001567 num_devices = root->fs_info->fs_devices->num_devices;
1568 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1569 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1570 WARN_ON(num_devices < 1);
1571 num_devices--;
1572 }
1573 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1574
1575 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001576 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001577 goto out;
1578 }
1579
Stefan Behrens8dabb742012-11-06 13:15:27 +01001580 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001581 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001582 goto out;
1583 }
1584
David Woodhouse53b381b2013-01-29 18:40:14 -05001585 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1586 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001587 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001588 goto out;
1589 }
1590 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1591 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001592 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001593 goto out;
1594 }
1595
Chris Masondfe25022008-05-13 13:46:40 -04001596 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001597 struct list_head *devices;
1598 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001599
Chris Masondfe25022008-05-13 13:46:40 -04001600 device = NULL;
1601 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001602 /*
1603 * It is safe to read the devices since the volume_mutex
1604 * is held.
1605 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001606 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001607 if (tmp->in_fs_metadata &&
1608 !tmp->is_tgtdev_for_dev_replace &&
1609 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001610 device = tmp;
1611 break;
1612 }
1613 }
1614 bdev = NULL;
1615 bh = NULL;
1616 disk_super = NULL;
1617 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001618 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001619 goto out;
1620 }
Chris Masondfe25022008-05-13 13:46:40 -04001621 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001622 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001623 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001624 root->fs_info->bdev_holder, 0,
1625 &bdev, &bh);
1626 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001627 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001628 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001629 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001630 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001631 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001632 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001633 if (!device) {
1634 ret = -ENOENT;
1635 goto error_brelse;
1636 }
Chris Masondfe25022008-05-13 13:46:40 -04001637 }
Yan Zheng2b820322008-11-17 21:11:30 -05001638
Stefan Behrens63a212a2012-11-05 18:29:28 +01001639 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001640 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001641 goto error_brelse;
1642 }
1643
Yan Zheng2b820322008-11-17 21:11:30 -05001644 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001645 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001646 goto error_brelse;
1647 }
1648
1649 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001650 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001651 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001652 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001653 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001654 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001655 }
Chris Masona061fc82008-05-07 11:43:44 -04001656
Carey Underwoodd7901552013-03-04 16:37:06 -06001657 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001658 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001659 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001660 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001661 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001662
Stefan Behrens63a212a2012-11-05 18:29:28 +01001663 /*
1664 * TODO: the superblock still includes this device in its num_devices
1665 * counter although write_all_supers() is not locked out. This
1666 * could give a filesystem state which requires a degraded mount.
1667 */
Chris Masona061fc82008-05-07 11:43:44 -04001668 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1669 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001670 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001671
Yan Zheng2b820322008-11-17 21:11:30 -05001672 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001673 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001674
1675 /*
1676 * the device list mutex makes sure that we don't change
1677 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001678 * the device supers. Whoever is writing all supers, should
1679 * lock the device list mutex before getting the number of
1680 * devices in the super block (super_copy). Conversely,
1681 * whoever updates the number of devices in the super block
1682 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001683 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001684
1685 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001686 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001687 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001688
Yan Zhenge4404d62008-12-12 10:03:26 -05001689 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001690 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001691
Chris Masoncd02dca2010-12-13 14:56:23 -05001692 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001693 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001694
Yan Zheng2b820322008-11-17 21:11:30 -05001695 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1696 struct btrfs_device, dev_list);
1697 if (device->bdev == root->fs_info->sb->s_bdev)
1698 root->fs_info->sb->s_bdev = next_device->bdev;
1699 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1700 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1701
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001702 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001703 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001704 /* remove sysfs entry */
1705 btrfs_kobj_rm_device(root->fs_info, device);
1706 }
Anand Jain99994cd2014-06-03 11:36:00 +08001707
Xiao Guangrong1f781602011-04-20 10:09:16 +00001708 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001709
David Sterba6c417612011-04-13 15:41:04 +02001710 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1711 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001712 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001713
Xiao Guangrong1f781602011-04-20 10:09:16 +00001714 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001715 struct btrfs_fs_devices *fs_devices;
1716 fs_devices = root->fs_info->fs_devices;
1717 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001718 if (fs_devices->seed == cur_devices) {
1719 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001720 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001721 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001722 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001723 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001724 cur_devices->seed = NULL;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001725 __btrfs_close_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001726 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001727 }
1728
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001729 root->fs_info->num_tolerated_disk_barrier_failures =
1730 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1731
Yan Zheng2b820322008-11-17 21:11:30 -05001732 /*
1733 * at this point, the device is zero sized. We want to
1734 * remove it from the devices list and zero out the old super
1735 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001736 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001737 u64 bytenr;
1738 int i;
1739
Chris Masondfe25022008-05-13 13:46:40 -04001740 /* make sure this device isn't detected as part of
1741 * the FS anymore
1742 */
1743 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1744 set_buffer_dirty(bh);
1745 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001746
1747 /* clear the mirror copies of super block on the disk
1748 * being removed, 0th copy is been taken care above and
1749 * the below would take of the rest
1750 */
1751 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1752 bytenr = btrfs_sb_offset(i);
1753 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1754 i_size_read(bdev->bd_inode))
1755 break;
1756
1757 brelse(bh);
1758 bh = __bread(bdev, bytenr / 4096,
1759 BTRFS_SUPER_INFO_SIZE);
1760 if (!bh)
1761 continue;
1762
1763 disk_super = (struct btrfs_super_block *)bh->b_data;
1764
1765 if (btrfs_super_bytenr(disk_super) != bytenr ||
1766 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1767 continue;
1768 }
1769 memset(&disk_super->magic, 0,
1770 sizeof(disk_super->magic));
1771 set_buffer_dirty(bh);
1772 sync_dirty_buffer(bh);
1773 }
Chris Masondfe25022008-05-13 13:46:40 -04001774 }
Chris Masona061fc82008-05-07 11:43:44 -04001775
Chris Masona061fc82008-05-07 11:43:44 -04001776 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001777
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001778 if (bdev) {
1779 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001780 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001781
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001782 /* Update ctime/mtime for device path for libblkid */
1783 update_dev_time(device_path);
1784 }
1785
Chris Masona061fc82008-05-07 11:43:44 -04001786error_brelse:
1787 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001788 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001789 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001790out:
1791 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001792 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001793error_undo:
1794 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001795 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001796 list_add(&device->dev_alloc_list,
1797 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001798 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001799 root->fs_info->fs_devices->rw_devices++;
1800 }
1801 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001802}
1803
Stefan Behrense93c89c2012-11-05 17:33:06 +01001804void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1805 struct btrfs_device *srcdev)
1806{
Anand Jaind51908c2014-08-13 14:24:19 +08001807 struct btrfs_fs_devices *fs_devices;
1808
Stefan Behrense93c89c2012-11-05 17:33:06 +01001809 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001810
Anand Jain25e8e912014-08-20 10:56:56 +08001811 /*
1812 * in case of fs with no seed, srcdev->fs_devices will point
1813 * to fs_devices of fs_info. However when the dev being replaced is
1814 * a seed dev it will point to the seed's local fs_devices. In short
1815 * srcdev will have its correct fs_devices in both the cases.
1816 */
1817 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08001818
Stefan Behrense93c89c2012-11-05 17:33:06 +01001819 list_del_rcu(&srcdev->dev_list);
1820 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08001821 fs_devices->num_devices--;
Miao Xie82372bc2014-09-03 21:35:44 +08001822 if (srcdev->missing)
Anand Jaind51908c2014-08-13 14:24:19 +08001823 fs_devices->missing_devices--;
Miao Xie82372bc2014-09-03 21:35:44 +08001824
1825 if (srcdev->writeable) {
1826 fs_devices->rw_devices--;
1827 /* zero out the old super if it is writable */
1828 btrfs_scratch_superblock(srcdev);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001829 }
Miao Xie90180da2014-09-03 21:35:30 +08001830
Miao Xie82372bc2014-09-03 21:35:44 +08001831 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08001832 fs_devices->open_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001833
1834 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001835
1836 /*
1837 * unless fs_devices is seed fs, num_devices shouldn't go
1838 * zero
1839 */
1840 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
1841
1842 /* if this is no devs we rather delete the fs_devices */
1843 if (!fs_devices->num_devices) {
1844 struct btrfs_fs_devices *tmp_fs_devices;
1845
1846 tmp_fs_devices = fs_info->fs_devices;
1847 while (tmp_fs_devices) {
1848 if (tmp_fs_devices->seed == fs_devices) {
1849 tmp_fs_devices->seed = fs_devices->seed;
1850 break;
1851 }
1852 tmp_fs_devices = tmp_fs_devices->seed;
1853 }
1854 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08001855 __btrfs_close_devices(fs_devices);
1856 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001857 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01001858}
1859
1860void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1861 struct btrfs_device *tgtdev)
1862{
1863 struct btrfs_device *next_device;
1864
Miao Xie67a2c452014-09-03 21:35:43 +08001865 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001866 WARN_ON(!tgtdev);
1867 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1868 if (tgtdev->bdev) {
1869 btrfs_scratch_superblock(tgtdev);
1870 fs_info->fs_devices->open_devices--;
1871 }
1872 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001873
1874 next_device = list_entry(fs_info->fs_devices->devices.next,
1875 struct btrfs_device, dev_list);
1876 if (tgtdev->bdev == fs_info->sb->s_bdev)
1877 fs_info->sb->s_bdev = next_device->bdev;
1878 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1879 fs_info->fs_devices->latest_bdev = next_device->bdev;
1880 list_del_rcu(&tgtdev->dev_list);
1881
1882 call_rcu(&tgtdev->rcu, free_device);
1883
1884 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08001885 mutex_unlock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001886}
1887
Eric Sandeen48a3b632013-04-25 20:41:01 +00001888static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1889 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001890{
1891 int ret = 0;
1892 struct btrfs_super_block *disk_super;
1893 u64 devid;
1894 u8 *dev_uuid;
1895 struct block_device *bdev;
1896 struct buffer_head *bh;
1897
1898 *device = NULL;
1899 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1900 root->fs_info->bdev_holder, 0, &bdev, &bh);
1901 if (ret)
1902 return ret;
1903 disk_super = (struct btrfs_super_block *)bh->b_data;
1904 devid = btrfs_stack_device_id(&disk_super->dev_item);
1905 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001906 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001907 disk_super->fsid);
1908 brelse(bh);
1909 if (!*device)
1910 ret = -ENOENT;
1911 blkdev_put(bdev, FMODE_READ);
1912 return ret;
1913}
1914
1915int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1916 char *device_path,
1917 struct btrfs_device **device)
1918{
1919 *device = NULL;
1920 if (strcmp(device_path, "missing") == 0) {
1921 struct list_head *devices;
1922 struct btrfs_device *tmp;
1923
1924 devices = &root->fs_info->fs_devices->devices;
1925 /*
1926 * It is safe to read the devices since the volume_mutex
1927 * is held by the caller.
1928 */
1929 list_for_each_entry(tmp, devices, dev_list) {
1930 if (tmp->in_fs_metadata && !tmp->bdev) {
1931 *device = tmp;
1932 break;
1933 }
1934 }
1935
1936 if (!*device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001937 btrfs_err(root->fs_info, "no missing device found");
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001938 return -ENOENT;
1939 }
1940
1941 return 0;
1942 } else {
1943 return btrfs_find_device_by_path(root, device_path, device);
1944 }
1945}
1946
Yan Zheng2b820322008-11-17 21:11:30 -05001947/*
1948 * does all the dirty work required for changing file system's UUID.
1949 */
Li Zefan125ccb02011-12-08 15:07:24 +08001950static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001951{
1952 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1953 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001954 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001955 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001956 struct btrfs_device *device;
1957 u64 super_flags;
1958
1959 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001960 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001961 return -EINVAL;
1962
Ilya Dryomov2208a372013-08-12 14:33:03 +03001963 seed_devices = __alloc_fs_devices();
1964 if (IS_ERR(seed_devices))
1965 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001966
Yan Zhenge4404d62008-12-12 10:03:26 -05001967 old_devices = clone_fs_devices(fs_devices);
1968 if (IS_ERR(old_devices)) {
1969 kfree(seed_devices);
1970 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001971 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001972
Yan Zheng2b820322008-11-17 21:11:30 -05001973 list_add(&old_devices->list, &fs_uuids);
1974
Yan Zhenge4404d62008-12-12 10:03:26 -05001975 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1976 seed_devices->opened = 1;
1977 INIT_LIST_HEAD(&seed_devices->devices);
1978 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001979 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001980
1981 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001982 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1983 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08001984 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05001985 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08001986
1987 lock_chunks(root);
1988 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1989 unlock_chunks(root);
Yan Zhenge4404d62008-12-12 10:03:26 -05001990
Yan Zheng2b820322008-11-17 21:11:30 -05001991 fs_devices->seeding = 0;
1992 fs_devices->num_devices = 0;
1993 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08001994 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08001995 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001996 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001997
1998 generate_random_uuid(fs_devices->fsid);
1999 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2000 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002001 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2002
Yan Zheng2b820322008-11-17 21:11:30 -05002003 super_flags = btrfs_super_flags(disk_super) &
2004 ~BTRFS_SUPER_FLAG_SEEDING;
2005 btrfs_set_super_flags(disk_super, super_flags);
2006
2007 return 0;
2008}
2009
2010/*
2011 * strore the expected generation for seed devices in device items.
2012 */
2013static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2014 struct btrfs_root *root)
2015{
2016 struct btrfs_path *path;
2017 struct extent_buffer *leaf;
2018 struct btrfs_dev_item *dev_item;
2019 struct btrfs_device *device;
2020 struct btrfs_key key;
2021 u8 fs_uuid[BTRFS_UUID_SIZE];
2022 u8 dev_uuid[BTRFS_UUID_SIZE];
2023 u64 devid;
2024 int ret;
2025
2026 path = btrfs_alloc_path();
2027 if (!path)
2028 return -ENOMEM;
2029
2030 root = root->fs_info->chunk_root;
2031 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2032 key.offset = 0;
2033 key.type = BTRFS_DEV_ITEM_KEY;
2034
2035 while (1) {
2036 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2037 if (ret < 0)
2038 goto error;
2039
2040 leaf = path->nodes[0];
2041next_slot:
2042 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2043 ret = btrfs_next_leaf(root, path);
2044 if (ret > 0)
2045 break;
2046 if (ret < 0)
2047 goto error;
2048 leaf = path->nodes[0];
2049 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002050 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002051 continue;
2052 }
2053
2054 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2055 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2056 key.type != BTRFS_DEV_ITEM_KEY)
2057 break;
2058
2059 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2060 struct btrfs_dev_item);
2061 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002062 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002063 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002064 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002065 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002066 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2067 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002068 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002069
2070 if (device->fs_devices->seeding) {
2071 btrfs_set_device_generation(leaf, dev_item,
2072 device->generation);
2073 btrfs_mark_buffer_dirty(leaf);
2074 }
2075
2076 path->slots[0]++;
2077 goto next_slot;
2078 }
2079 ret = 0;
2080error:
2081 btrfs_free_path(path);
2082 return ret;
2083}
2084
Chris Mason788f20e2008-04-28 15:29:42 -04002085int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2086{
Josef Bacikd5e20032011-08-04 14:52:27 +00002087 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002088 struct btrfs_trans_handle *trans;
2089 struct btrfs_device *device;
2090 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002091 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002092 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002093 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002094 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002095 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002096 int ret = 0;
2097
Yan Zheng2b820322008-11-17 21:11:30 -05002098 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002099 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002100
Li Zefana5d163332011-12-07 20:08:40 -05002101 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002102 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002103 if (IS_ERR(bdev))
2104 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002105
Yan Zheng2b820322008-11-17 21:11:30 -05002106 if (root->fs_info->fs_devices->seeding) {
2107 seeding_dev = 1;
2108 down_write(&sb->s_umount);
2109 mutex_lock(&uuid_mutex);
2110 }
2111
Chris Mason8c8bee12008-09-29 11:19:10 -04002112 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002113
Chris Mason788f20e2008-04-28 15:29:42 -04002114 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002115
2116 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002117 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002118 if (device->bdev == bdev) {
2119 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002120 mutex_unlock(
2121 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002122 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002123 }
2124 }
Liu Bod25628b2012-11-14 14:35:30 +00002125 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002126
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002127 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2128 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002129 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002130 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002131 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002132 }
2133
Josef Bacik606686e2012-06-04 14:03:51 -04002134 name = rcu_string_strdup(device_path, GFP_NOFS);
2135 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002136 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002137 ret = -ENOMEM;
2138 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002139 }
Josef Bacik606686e2012-06-04 14:03:51 -04002140 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002141
Yan, Zhenga22285a2010-05-16 10:48:46 -04002142 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002143 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002144 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002145 kfree(device);
2146 ret = PTR_ERR(trans);
2147 goto error;
2148 }
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;
Miao Xie935e5cc2014-09-03 21:35:33 +08002160 device->commit_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002161 device->dev_root = root->fs_info->dev_root;
2162 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002163 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002164 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002165 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002166 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002167 set_blocksize(device->bdev, 4096);
2168
Yan Zheng2b820322008-11-17 21:11:30 -05002169 if (seeding_dev) {
2170 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002171 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002172 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002173 }
2174
2175 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002176
Chris Masone5e9a522009-06-10 15:17:02 -04002177 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002178 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002179 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002180 list_add(&device->dev_alloc_list,
2181 &root->fs_info->fs_devices->alloc_list);
2182 root->fs_info->fs_devices->num_devices++;
2183 root->fs_info->fs_devices->open_devices++;
2184 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002185 root->fs_info->fs_devices->total_devices++;
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
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002195 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002196 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002197 tmp + device->total_bytes);
Chris Mason788f20e2008-04-28 15:29:42 -04002198
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002199 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002200 btrfs_set_super_num_devices(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002201 tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002202
2203 /* add sysfs device entry */
2204 btrfs_kobj_add_device(root->fs_info, device);
2205
Miao Xie2196d6e2014-09-03 21:35:41 +08002206 /*
2207 * we've got more storage, clear any full flags on the space
2208 * infos
2209 */
2210 btrfs_clear_space_info_full(root->fs_info);
2211
2212 unlock_chunks(root);
Chris Masone5e9a522009-06-10 15:17:02 -04002213 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002214
Yan Zheng2b820322008-11-17 21:11:30 -05002215 if (seeding_dev) {
Miao Xie2196d6e2014-09-03 21:35:41 +08002216 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05002217 ret = init_first_rw_device(trans, root, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002218 unlock_chunks(root);
David Sterba005d6422012-09-18 07:52:32 -06002219 if (ret) {
2220 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002221 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002222 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002223 }
2224
2225 ret = btrfs_add_device(trans, root, device);
2226 if (ret) {
2227 btrfs_abort_transaction(trans, root, ret);
2228 goto error_trans;
2229 }
2230
2231 if (seeding_dev) {
2232 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2233
Yan Zheng2b820322008-11-17 21:11:30 -05002234 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002235 if (ret) {
2236 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002237 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002238 }
Anand Jainb2373f22014-06-03 11:36:03 +08002239
2240 /* Sprouting would change fsid of the mounted root,
2241 * so rename the fsid on the sysfs
2242 */
2243 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2244 root->fs_info->fsid);
2245 if (kobject_rename(&root->fs_info->super_kobj, fsid_buf))
2246 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05002247 }
2248
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002249 root->fs_info->num_tolerated_disk_barrier_failures =
2250 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002251 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002252
2253 if (seeding_dev) {
2254 mutex_unlock(&uuid_mutex);
2255 up_write(&sb->s_umount);
2256
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002257 if (ret) /* transaction commit */
2258 return ret;
2259
Yan Zheng2b820322008-11-17 21:11:30 -05002260 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002261 if (ret < 0)
2262 btrfs_error(root->fs_info, ret,
2263 "Failed to relocate sys chunks after "
2264 "device initialization. This can be fixed "
2265 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002266 trans = btrfs_attach_transaction(root);
2267 if (IS_ERR(trans)) {
2268 if (PTR_ERR(trans) == -ENOENT)
2269 return 0;
2270 return PTR_ERR(trans);
2271 }
2272 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002273 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002274
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002275 /* Update ctime/mtime for libblkid */
2276 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002277 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002278
2279error_trans:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002280 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002281 rcu_string_free(device->name);
Anand Jain0d393762014-06-03 11:36:01 +08002282 btrfs_kobj_rm_device(root->fs_info, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002283 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002284error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002285 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002286 if (seeding_dev) {
2287 mutex_unlock(&uuid_mutex);
2288 up_write(&sb->s_umount);
2289 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002290 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002291}
2292
Stefan Behrense93c89c2012-11-05 17:33:06 +01002293int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002294 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002295 struct btrfs_device **device_out)
2296{
2297 struct request_queue *q;
2298 struct btrfs_device *device;
2299 struct block_device *bdev;
2300 struct btrfs_fs_info *fs_info = root->fs_info;
2301 struct list_head *devices;
2302 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002303 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002304 int ret = 0;
2305
2306 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002307 if (fs_info->fs_devices->seeding) {
2308 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002309 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002310 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002311
2312 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2313 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002314 if (IS_ERR(bdev)) {
2315 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002316 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002317 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002318
2319 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2320
2321 devices = &fs_info->fs_devices->devices;
2322 list_for_each_entry(device, devices, dev_list) {
2323 if (device->bdev == bdev) {
Miao Xie1c433662014-09-03 21:35:32 +08002324 btrfs_err(fs_info, "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002325 ret = -EEXIST;
2326 goto error;
2327 }
2328 }
2329
Miao Xie1c433662014-09-03 21:35:32 +08002330
Miao Xie7cc8e582014-09-03 21:35:38 +08002331 if (i_size_read(bdev->bd_inode) <
2332 btrfs_device_get_total_bytes(srcdev)) {
Miao Xie1c433662014-09-03 21:35:32 +08002333 btrfs_err(fs_info, "target device is smaller than source device!");
2334 ret = -EINVAL;
2335 goto error;
2336 }
2337
2338
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002339 device = btrfs_alloc_device(NULL, &devid, NULL);
2340 if (IS_ERR(device)) {
2341 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002342 goto error;
2343 }
2344
2345 name = rcu_string_strdup(device_path, GFP_NOFS);
2346 if (!name) {
2347 kfree(device);
2348 ret = -ENOMEM;
2349 goto error;
2350 }
2351 rcu_assign_pointer(device->name, name);
2352
2353 q = bdev_get_queue(bdev);
2354 if (blk_queue_discard(q))
2355 device->can_discard = 1;
2356 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2357 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002358 device->generation = 0;
2359 device->io_width = root->sectorsize;
2360 device->io_align = root->sectorsize;
2361 device->sector_size = root->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002362 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2363 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2364 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002365 ASSERT(list_empty(&srcdev->resized_list));
2366 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002367 device->commit_bytes_used = device->bytes_used;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002368 device->dev_root = fs_info->dev_root;
2369 device->bdev = bdev;
2370 device->in_fs_metadata = 1;
2371 device->is_tgtdev_for_dev_replace = 1;
2372 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002373 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002374 set_blocksize(device->bdev, 4096);
2375 device->fs_devices = fs_info->fs_devices;
2376 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2377 fs_info->fs_devices->num_devices++;
2378 fs_info->fs_devices->open_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002379 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2380
2381 *device_out = device;
2382 return ret;
2383
2384error:
2385 blkdev_put(bdev, FMODE_EXCL);
2386 return ret;
2387}
2388
2389void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2390 struct btrfs_device *tgtdev)
2391{
2392 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2393 tgtdev->io_width = fs_info->dev_root->sectorsize;
2394 tgtdev->io_align = fs_info->dev_root->sectorsize;
2395 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2396 tgtdev->dev_root = fs_info->dev_root;
2397 tgtdev->in_fs_metadata = 1;
2398}
2399
Chris Masond3977122009-01-05 21:25:51 -05002400static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2401 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002402{
2403 int ret;
2404 struct btrfs_path *path;
2405 struct btrfs_root *root;
2406 struct btrfs_dev_item *dev_item;
2407 struct extent_buffer *leaf;
2408 struct btrfs_key key;
2409
2410 root = device->dev_root->fs_info->chunk_root;
2411
2412 path = btrfs_alloc_path();
2413 if (!path)
2414 return -ENOMEM;
2415
2416 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2417 key.type = BTRFS_DEV_ITEM_KEY;
2418 key.offset = device->devid;
2419
2420 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2421 if (ret < 0)
2422 goto out;
2423
2424 if (ret > 0) {
2425 ret = -ENOENT;
2426 goto out;
2427 }
2428
2429 leaf = path->nodes[0];
2430 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2431
2432 btrfs_set_device_id(leaf, dev_item, device->devid);
2433 btrfs_set_device_type(leaf, dev_item, device->type);
2434 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2435 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2436 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002437 btrfs_set_device_total_bytes(leaf, dev_item,
2438 btrfs_device_get_disk_total_bytes(device));
2439 btrfs_set_device_bytes_used(leaf, dev_item,
2440 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002441 btrfs_mark_buffer_dirty(leaf);
2442
2443out:
2444 btrfs_free_path(path);
2445 return ret;
2446}
2447
Miao Xie2196d6e2014-09-03 21:35:41 +08002448int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002449 struct btrfs_device *device, u64 new_size)
2450{
2451 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002452 device->dev_root->fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002453 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002454 u64 old_total;
2455 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002456
Yan Zheng2b820322008-11-17 21:11:30 -05002457 if (!device->writeable)
2458 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002459
2460 lock_chunks(device->dev_root);
2461 old_total = btrfs_super_total_bytes(super_copy);
2462 diff = new_size - device->total_bytes;
2463
Stefan Behrens63a212a2012-11-05 18:29:28 +01002464 if (new_size <= device->total_bytes ||
Miao Xie2196d6e2014-09-03 21:35:41 +08002465 device->is_tgtdev_for_dev_replace) {
2466 unlock_chunks(device->dev_root);
Yan Zheng2b820322008-11-17 21:11:30 -05002467 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002468 }
Yan Zheng2b820322008-11-17 21:11:30 -05002469
Miao Xie935e5cc2014-09-03 21:35:33 +08002470 fs_devices = device->dev_root->fs_info->fs_devices;
2471
Chris Mason8f18cf12008-04-25 16:53:30 -04002472 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002473 device->fs_devices->total_rw_bytes += diff;
2474
Miao Xie7cc8e582014-09-03 21:35:38 +08002475 btrfs_device_set_total_bytes(device, new_size);
2476 btrfs_device_set_disk_total_bytes(device, new_size);
Chris Mason4184ea72009-03-10 12:39:20 -04002477 btrfs_clear_space_info_full(device->dev_root->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002478 if (list_empty(&device->resized_list))
2479 list_add_tail(&device->resized_list,
2480 &fs_devices->resized_devices);
Miao Xie2196d6e2014-09-03 21:35:41 +08002481 unlock_chunks(device->dev_root);
Chris Mason4184ea72009-03-10 12:39:20 -04002482
Chris Mason8f18cf12008-04-25 16:53:30 -04002483 return btrfs_update_device(trans, device);
2484}
2485
2486static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2487 struct btrfs_root *root,
2488 u64 chunk_tree, u64 chunk_objectid,
2489 u64 chunk_offset)
2490{
2491 int ret;
2492 struct btrfs_path *path;
2493 struct btrfs_key key;
2494
2495 root = root->fs_info->chunk_root;
2496 path = btrfs_alloc_path();
2497 if (!path)
2498 return -ENOMEM;
2499
2500 key.objectid = chunk_objectid;
2501 key.offset = chunk_offset;
2502 key.type = BTRFS_CHUNK_ITEM_KEY;
2503
2504 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002505 if (ret < 0)
2506 goto out;
2507 else if (ret > 0) { /* Logic error or corruption */
2508 btrfs_error(root->fs_info, -ENOENT,
2509 "Failed lookup while freeing chunk.");
2510 ret = -ENOENT;
2511 goto out;
2512 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002513
2514 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002515 if (ret < 0)
2516 btrfs_error(root->fs_info, ret,
2517 "Failed to delete chunk item.");
2518out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002519 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002520 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002521}
2522
Christoph Hellwigb2950862008-12-02 09:54:17 -05002523static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002524 chunk_offset)
2525{
David Sterba6c417612011-04-13 15:41:04 +02002526 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002527 struct btrfs_disk_key *disk_key;
2528 struct btrfs_chunk *chunk;
2529 u8 *ptr;
2530 int ret = 0;
2531 u32 num_stripes;
2532 u32 array_size;
2533 u32 len = 0;
2534 u32 cur;
2535 struct btrfs_key key;
2536
Miao Xie2196d6e2014-09-03 21:35:41 +08002537 lock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002538 array_size = btrfs_super_sys_array_size(super_copy);
2539
2540 ptr = super_copy->sys_chunk_array;
2541 cur = 0;
2542
2543 while (cur < array_size) {
2544 disk_key = (struct btrfs_disk_key *)ptr;
2545 btrfs_disk_key_to_cpu(&key, disk_key);
2546
2547 len = sizeof(*disk_key);
2548
2549 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2550 chunk = (struct btrfs_chunk *)(ptr + len);
2551 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2552 len += btrfs_chunk_item_size(num_stripes);
2553 } else {
2554 ret = -EIO;
2555 break;
2556 }
2557 if (key.objectid == chunk_objectid &&
2558 key.offset == chunk_offset) {
2559 memmove(ptr, ptr + len, array_size - (cur + len));
2560 array_size -= len;
2561 btrfs_set_super_sys_array_size(super_copy, array_size);
2562 } else {
2563 ptr += len;
2564 cur += len;
2565 }
2566 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002567 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002568 return ret;
2569}
2570
Christoph Hellwigb2950862008-12-02 09:54:17 -05002571static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002572 u64 chunk_tree, u64 chunk_objectid,
2573 u64 chunk_offset)
2574{
2575 struct extent_map_tree *em_tree;
2576 struct btrfs_root *extent_root;
2577 struct btrfs_trans_handle *trans;
Miao Xie2196d6e2014-09-03 21:35:41 +08002578 struct btrfs_device *device;
Chris Mason8f18cf12008-04-25 16:53:30 -04002579 struct extent_map *em;
2580 struct map_lookup *map;
Miao Xie2196d6e2014-09-03 21:35:41 +08002581 u64 dev_extent_len = 0;
Chris Mason8f18cf12008-04-25 16:53:30 -04002582 int ret;
2583 int i;
2584
2585 root = root->fs_info->chunk_root;
2586 extent_root = root->fs_info->extent_root;
2587 em_tree = &root->fs_info->mapping_tree.map_tree;
2588
Josef Bacikba1bf482009-09-11 16:11:19 -04002589 ret = btrfs_can_relocate(extent_root, chunk_offset);
2590 if (ret)
2591 return -ENOSPC;
2592
Chris Mason8f18cf12008-04-25 16:53:30 -04002593 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002594 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002595 if (ret)
2596 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002597
Yan, Zhenga22285a2010-05-16 10:48:46 -04002598 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002599 if (IS_ERR(trans)) {
2600 ret = PTR_ERR(trans);
2601 btrfs_std_error(root->fs_info, ret);
2602 return ret;
2603 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002604
2605 /*
2606 * step two, delete the device extents and the
2607 * chunk tree entries
2608 */
Chris Mason890871b2009-09-02 16:24:52 -04002609 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002610 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002611 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002612
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002613 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002614 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002615 map = (struct map_lookup *)em->bdev;
2616
2617 for (i = 0; i < map->num_stripes; i++) {
Miao Xie2196d6e2014-09-03 21:35:41 +08002618 device = map->stripes[i].dev;
2619 ret = btrfs_free_dev_extent(trans, device,
2620 map->stripes[i].physical,
2621 &dev_extent_len);
Chris Mason8f18cf12008-04-25 16:53:30 -04002622 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002623
Miao Xie2196d6e2014-09-03 21:35:41 +08002624 if (device->bytes_used > 0) {
2625 lock_chunks(root);
2626 btrfs_device_set_bytes_used(device,
2627 device->bytes_used - dev_extent_len);
2628 spin_lock(&root->fs_info->free_chunk_lock);
2629 root->fs_info->free_chunk_space += dev_extent_len;
2630 spin_unlock(&root->fs_info->free_chunk_lock);
2631 btrfs_clear_space_info_full(root->fs_info);
2632 unlock_chunks(root);
2633 }
2634
Chris Masondfe25022008-05-13 13:46:40 -04002635 if (map->stripes[i].dev) {
2636 ret = btrfs_update_device(trans, map->stripes[i].dev);
2637 BUG_ON(ret);
2638 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002639 }
2640 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2641 chunk_offset);
2642
2643 BUG_ON(ret);
2644
liubo1abe9b82011-03-24 11:18:59 +00002645 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2646
Chris Mason8f18cf12008-04-25 16:53:30 -04002647 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2648 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2649 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002650 }
2651
Zheng Yan1a40e232008-09-26 10:09:34 -04002652 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2653 BUG_ON(ret);
2654
Chris Mason890871b2009-09-02 16:24:52 -04002655 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002656 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002657 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002658
Chris Mason8f18cf12008-04-25 16:53:30 -04002659 /* once for the tree */
2660 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002661 /* once for us */
2662 free_extent_map(em);
2663
2664 btrfs_end_transaction(trans, root);
2665 return 0;
2666}
2667
Yan Zheng2b820322008-11-17 21:11:30 -05002668static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2669{
2670 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2671 struct btrfs_path *path;
2672 struct extent_buffer *leaf;
2673 struct btrfs_chunk *chunk;
2674 struct btrfs_key key;
2675 struct btrfs_key found_key;
2676 u64 chunk_tree = chunk_root->root_key.objectid;
2677 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002678 bool retried = false;
2679 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002680 int ret;
2681
2682 path = btrfs_alloc_path();
2683 if (!path)
2684 return -ENOMEM;
2685
Josef Bacikba1bf482009-09-11 16:11:19 -04002686again:
Yan Zheng2b820322008-11-17 21:11:30 -05002687 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2688 key.offset = (u64)-1;
2689 key.type = BTRFS_CHUNK_ITEM_KEY;
2690
2691 while (1) {
2692 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2693 if (ret < 0)
2694 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002695 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002696
2697 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2698 key.type);
2699 if (ret < 0)
2700 goto error;
2701 if (ret > 0)
2702 break;
2703
2704 leaf = path->nodes[0];
2705 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2706
2707 chunk = btrfs_item_ptr(leaf, path->slots[0],
2708 struct btrfs_chunk);
2709 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002710 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002711
2712 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2713 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2714 found_key.objectid,
2715 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002716 if (ret == -ENOSPC)
2717 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302718 else
2719 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002720 }
2721
2722 if (found_key.offset == 0)
2723 break;
2724 key.offset = found_key.offset - 1;
2725 }
2726 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002727 if (failed && !retried) {
2728 failed = 0;
2729 retried = true;
2730 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302731 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002732 ret = -ENOSPC;
2733 }
Yan Zheng2b820322008-11-17 21:11:30 -05002734error:
2735 btrfs_free_path(path);
2736 return ret;
2737}
2738
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002739static int insert_balance_item(struct btrfs_root *root,
2740 struct btrfs_balance_control *bctl)
2741{
2742 struct btrfs_trans_handle *trans;
2743 struct btrfs_balance_item *item;
2744 struct btrfs_disk_balance_args disk_bargs;
2745 struct btrfs_path *path;
2746 struct extent_buffer *leaf;
2747 struct btrfs_key key;
2748 int ret, err;
2749
2750 path = btrfs_alloc_path();
2751 if (!path)
2752 return -ENOMEM;
2753
2754 trans = btrfs_start_transaction(root, 0);
2755 if (IS_ERR(trans)) {
2756 btrfs_free_path(path);
2757 return PTR_ERR(trans);
2758 }
2759
2760 key.objectid = BTRFS_BALANCE_OBJECTID;
2761 key.type = BTRFS_BALANCE_ITEM_KEY;
2762 key.offset = 0;
2763
2764 ret = btrfs_insert_empty_item(trans, root, path, &key,
2765 sizeof(*item));
2766 if (ret)
2767 goto out;
2768
2769 leaf = path->nodes[0];
2770 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2771
2772 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2773
2774 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2775 btrfs_set_balance_data(leaf, item, &disk_bargs);
2776 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2777 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2778 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2779 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2780
2781 btrfs_set_balance_flags(leaf, item, bctl->flags);
2782
2783 btrfs_mark_buffer_dirty(leaf);
2784out:
2785 btrfs_free_path(path);
2786 err = btrfs_commit_transaction(trans, root);
2787 if (err && !ret)
2788 ret = err;
2789 return ret;
2790}
2791
2792static int del_balance_item(struct btrfs_root *root)
2793{
2794 struct btrfs_trans_handle *trans;
2795 struct btrfs_path *path;
2796 struct btrfs_key key;
2797 int ret, err;
2798
2799 path = btrfs_alloc_path();
2800 if (!path)
2801 return -ENOMEM;
2802
2803 trans = btrfs_start_transaction(root, 0);
2804 if (IS_ERR(trans)) {
2805 btrfs_free_path(path);
2806 return PTR_ERR(trans);
2807 }
2808
2809 key.objectid = BTRFS_BALANCE_OBJECTID;
2810 key.type = BTRFS_BALANCE_ITEM_KEY;
2811 key.offset = 0;
2812
2813 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2814 if (ret < 0)
2815 goto out;
2816 if (ret > 0) {
2817 ret = -ENOENT;
2818 goto out;
2819 }
2820
2821 ret = btrfs_del_item(trans, root, path);
2822out:
2823 btrfs_free_path(path);
2824 err = btrfs_commit_transaction(trans, root);
2825 if (err && !ret)
2826 ret = err;
2827 return ret;
2828}
2829
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002830/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002831 * This is a heuristic used to reduce the number of chunks balanced on
2832 * resume after balance was interrupted.
2833 */
2834static void update_balance_args(struct btrfs_balance_control *bctl)
2835{
2836 /*
2837 * Turn on soft mode for chunk types that were being converted.
2838 */
2839 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2840 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2841 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2842 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2843 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2844 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2845
2846 /*
2847 * Turn on usage filter if is not already used. The idea is
2848 * that chunks that we have already balanced should be
2849 * reasonably full. Don't do it for chunks that are being
2850 * converted - that will keep us from relocating unconverted
2851 * (albeit full) chunks.
2852 */
2853 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2854 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2855 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2856 bctl->data.usage = 90;
2857 }
2858 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2859 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2860 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2861 bctl->sys.usage = 90;
2862 }
2863 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2864 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2865 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2866 bctl->meta.usage = 90;
2867 }
2868}
2869
2870/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002871 * Should be called with both balance and volume mutexes held to
2872 * serialize other volume operations (add_dev/rm_dev/resize) with
2873 * restriper. Same goes for unset_balance_control.
2874 */
2875static void set_balance_control(struct btrfs_balance_control *bctl)
2876{
2877 struct btrfs_fs_info *fs_info = bctl->fs_info;
2878
2879 BUG_ON(fs_info->balance_ctl);
2880
2881 spin_lock(&fs_info->balance_lock);
2882 fs_info->balance_ctl = bctl;
2883 spin_unlock(&fs_info->balance_lock);
2884}
2885
2886static void unset_balance_control(struct btrfs_fs_info *fs_info)
2887{
2888 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2889
2890 BUG_ON(!fs_info->balance_ctl);
2891
2892 spin_lock(&fs_info->balance_lock);
2893 fs_info->balance_ctl = NULL;
2894 spin_unlock(&fs_info->balance_lock);
2895
2896 kfree(bctl);
2897}
2898
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002899/*
2900 * Balance filters. Return 1 if chunk should be filtered out
2901 * (should not be balanced).
2902 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002903static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002904 struct btrfs_balance_args *bargs)
2905{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002906 chunk_type = chunk_to_extended(chunk_type) &
2907 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002908
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002909 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002910 return 0;
2911
2912 return 1;
2913}
2914
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002915static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2916 struct btrfs_balance_args *bargs)
2917{
2918 struct btrfs_block_group_cache *cache;
2919 u64 chunk_used, user_thresh;
2920 int ret = 1;
2921
2922 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2923 chunk_used = btrfs_block_group_used(&cache->item);
2924
Ilya Dryomova105bb82013-01-21 15:15:56 +02002925 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002926 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002927 else if (bargs->usage > 100)
2928 user_thresh = cache->key.offset;
2929 else
2930 user_thresh = div_factor_fine(cache->key.offset,
2931 bargs->usage);
2932
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002933 if (chunk_used < user_thresh)
2934 ret = 0;
2935
2936 btrfs_put_block_group(cache);
2937 return ret;
2938}
2939
Ilya Dryomov409d4042012-01-16 22:04:47 +02002940static int chunk_devid_filter(struct extent_buffer *leaf,
2941 struct btrfs_chunk *chunk,
2942 struct btrfs_balance_args *bargs)
2943{
2944 struct btrfs_stripe *stripe;
2945 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2946 int i;
2947
2948 for (i = 0; i < num_stripes; i++) {
2949 stripe = btrfs_stripe_nr(chunk, i);
2950 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2951 return 0;
2952 }
2953
2954 return 1;
2955}
2956
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002957/* [pstart, pend) */
2958static int chunk_drange_filter(struct extent_buffer *leaf,
2959 struct btrfs_chunk *chunk,
2960 u64 chunk_offset,
2961 struct btrfs_balance_args *bargs)
2962{
2963 struct btrfs_stripe *stripe;
2964 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2965 u64 stripe_offset;
2966 u64 stripe_length;
2967 int factor;
2968 int i;
2969
2970 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2971 return 0;
2972
2973 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002974 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2975 factor = num_stripes / 2;
2976 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2977 factor = num_stripes - 1;
2978 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2979 factor = num_stripes - 2;
2980 } else {
2981 factor = num_stripes;
2982 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002983
2984 for (i = 0; i < num_stripes; i++) {
2985 stripe = btrfs_stripe_nr(chunk, i);
2986 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2987 continue;
2988
2989 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2990 stripe_length = btrfs_chunk_length(leaf, chunk);
2991 do_div(stripe_length, factor);
2992
2993 if (stripe_offset < bargs->pend &&
2994 stripe_offset + stripe_length > bargs->pstart)
2995 return 0;
2996 }
2997
2998 return 1;
2999}
3000
Ilya Dryomovea671762012-01-16 22:04:48 +02003001/* [vstart, vend) */
3002static int chunk_vrange_filter(struct extent_buffer *leaf,
3003 struct btrfs_chunk *chunk,
3004 u64 chunk_offset,
3005 struct btrfs_balance_args *bargs)
3006{
3007 if (chunk_offset < bargs->vend &&
3008 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3009 /* at least part of the chunk is inside this vrange */
3010 return 0;
3011
3012 return 1;
3013}
3014
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003015static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003016 struct btrfs_balance_args *bargs)
3017{
3018 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3019 return 0;
3020
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003021 chunk_type = chunk_to_extended(chunk_type) &
3022 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003023
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003024 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003025 return 1;
3026
3027 return 0;
3028}
3029
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003030static int should_balance_chunk(struct btrfs_root *root,
3031 struct extent_buffer *leaf,
3032 struct btrfs_chunk *chunk, u64 chunk_offset)
3033{
3034 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3035 struct btrfs_balance_args *bargs = NULL;
3036 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3037
3038 /* type filter */
3039 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3040 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3041 return 0;
3042 }
3043
3044 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3045 bargs = &bctl->data;
3046 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3047 bargs = &bctl->sys;
3048 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3049 bargs = &bctl->meta;
3050
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003051 /* profiles filter */
3052 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3053 chunk_profiles_filter(chunk_type, bargs)) {
3054 return 0;
3055 }
3056
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003057 /* usage filter */
3058 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3059 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3060 return 0;
3061 }
3062
Ilya Dryomov409d4042012-01-16 22:04:47 +02003063 /* devid filter */
3064 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3065 chunk_devid_filter(leaf, chunk, bargs)) {
3066 return 0;
3067 }
3068
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003069 /* drange filter, makes sense only with devid filter */
3070 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3071 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3072 return 0;
3073 }
3074
Ilya Dryomovea671762012-01-16 22:04:48 +02003075 /* vrange filter */
3076 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3077 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3078 return 0;
3079 }
3080
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003081 /* soft profile changing mode */
3082 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3083 chunk_soft_convert_filter(chunk_type, bargs)) {
3084 return 0;
3085 }
3086
David Sterba7d824b62014-05-07 17:37:51 +02003087 /*
3088 * limited by count, must be the last filter
3089 */
3090 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3091 if (bargs->limit == 0)
3092 return 0;
3093 else
3094 bargs->limit--;
3095 }
3096
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003097 return 1;
3098}
3099
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003100static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003101{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003102 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003103 struct btrfs_root *chunk_root = fs_info->chunk_root;
3104 struct btrfs_root *dev_root = fs_info->dev_root;
3105 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003106 struct btrfs_device *device;
3107 u64 old_size;
3108 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003109 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003110 struct btrfs_path *path;
3111 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003112 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003113 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003114 struct extent_buffer *leaf;
3115 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003116 int ret;
3117 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003118 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003119 u64 limit_data = bctl->data.limit;
3120 u64 limit_meta = bctl->meta.limit;
3121 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003122
Chris Masonec44a352008-04-28 15:29:52 -04003123 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003124 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003125 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003126 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003127 size_to_free = div_factor(old_size, 1);
3128 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003129 if (!device->writeable ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003130 btrfs_device_get_total_bytes(device) -
3131 btrfs_device_get_bytes_used(device) > size_to_free ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003132 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003133 continue;
3134
3135 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003136 if (ret == -ENOSPC)
3137 break;
Chris Masonec44a352008-04-28 15:29:52 -04003138 BUG_ON(ret);
3139
Yan, Zhenga22285a2010-05-16 10:48:46 -04003140 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003141 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003142
3143 ret = btrfs_grow_device(trans, device, old_size);
3144 BUG_ON(ret);
3145
3146 btrfs_end_transaction(trans, dev_root);
3147 }
3148
3149 /* step two, relocate all the chunks */
3150 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003151 if (!path) {
3152 ret = -ENOMEM;
3153 goto error;
3154 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003155
3156 /* zero out stat counters */
3157 spin_lock(&fs_info->balance_lock);
3158 memset(&bctl->stat, 0, sizeof(bctl->stat));
3159 spin_unlock(&fs_info->balance_lock);
3160again:
David Sterba7d824b62014-05-07 17:37:51 +02003161 if (!counting) {
3162 bctl->data.limit = limit_data;
3163 bctl->meta.limit = limit_meta;
3164 bctl->sys.limit = limit_sys;
3165 }
Chris Masonec44a352008-04-28 15:29:52 -04003166 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3167 key.offset = (u64)-1;
3168 key.type = BTRFS_CHUNK_ITEM_KEY;
3169
Chris Masond3977122009-01-05 21:25:51 -05003170 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003171 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003172 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003173 ret = -ECANCELED;
3174 goto error;
3175 }
3176
Chris Masonec44a352008-04-28 15:29:52 -04003177 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3178 if (ret < 0)
3179 goto error;
3180
3181 /*
3182 * this shouldn't happen, it means the last relocate
3183 * failed
3184 */
3185 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003186 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003187
3188 ret = btrfs_previous_item(chunk_root, path, 0,
3189 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003190 if (ret) {
3191 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003192 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003193 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003194
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003195 leaf = path->nodes[0];
3196 slot = path->slots[0];
3197 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3198
Chris Masonec44a352008-04-28 15:29:52 -04003199 if (found_key.objectid != key.objectid)
3200 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04003201
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003202 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3203
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003204 if (!counting) {
3205 spin_lock(&fs_info->balance_lock);
3206 bctl->stat.considered++;
3207 spin_unlock(&fs_info->balance_lock);
3208 }
3209
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003210 ret = should_balance_chunk(chunk_root, leaf, chunk,
3211 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003212 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003213 if (!ret)
3214 goto loop;
3215
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003216 if (counting) {
3217 spin_lock(&fs_info->balance_lock);
3218 bctl->stat.expected++;
3219 spin_unlock(&fs_info->balance_lock);
3220 goto loop;
3221 }
3222
Chris Masonec44a352008-04-28 15:29:52 -04003223 ret = btrfs_relocate_chunk(chunk_root,
3224 chunk_root->root_key.objectid,
3225 found_key.objectid,
3226 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003227 if (ret && ret != -ENOSPC)
3228 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003229 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003230 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003231 } else {
3232 spin_lock(&fs_info->balance_lock);
3233 bctl->stat.completed++;
3234 spin_unlock(&fs_info->balance_lock);
3235 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003236loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003237 if (found_key.offset == 0)
3238 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003239 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003240 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003241
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003242 if (counting) {
3243 btrfs_release_path(path);
3244 counting = false;
3245 goto again;
3246 }
Chris Masonec44a352008-04-28 15:29:52 -04003247error:
3248 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003249 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003250 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003251 enospc_errors);
3252 if (!ret)
3253 ret = -ENOSPC;
3254 }
3255
Chris Masonec44a352008-04-28 15:29:52 -04003256 return ret;
3257}
3258
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003259/**
3260 * alloc_profile_is_valid - see if a given profile is valid and reduced
3261 * @flags: profile to validate
3262 * @extended: if true @flags is treated as an extended profile
3263 */
3264static int alloc_profile_is_valid(u64 flags, int extended)
3265{
3266 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3267 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3268
3269 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3270
3271 /* 1) check that all other bits are zeroed */
3272 if (flags & ~mask)
3273 return 0;
3274
3275 /* 2) see if profile is reduced */
3276 if (flags == 0)
3277 return !extended; /* "0" is valid for usual profiles */
3278
3279 /* true if exactly one bit set */
3280 return (flags & (flags - 1)) == 0;
3281}
3282
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003283static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3284{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003285 /* cancel requested || normal exit path */
3286 return atomic_read(&fs_info->balance_cancel_req) ||
3287 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3288 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003289}
3290
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003291static void __cancel_balance(struct btrfs_fs_info *fs_info)
3292{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003293 int ret;
3294
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003295 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003296 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003297 if (ret)
3298 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003299
3300 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003301}
3302
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003303/*
3304 * Should be called with both balance and volume mutexes held
3305 */
3306int btrfs_balance(struct btrfs_balance_control *bctl,
3307 struct btrfs_ioctl_balance_args *bargs)
3308{
3309 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003310 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003311 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003312 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003313 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003314 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003315
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003316 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003317 atomic_read(&fs_info->balance_pause_req) ||
3318 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003319 ret = -EINVAL;
3320 goto out;
3321 }
3322
Ilya Dryomove4837f82012-03-27 17:09:17 +03003323 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3324 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3325 mixed = 1;
3326
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003327 /*
3328 * In case of mixed groups both data and meta should be picked,
3329 * and identical options should be given for both of them.
3330 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003331 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3332 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003333 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3334 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3335 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003336 btrfs_err(fs_info, "with mixed groups data and "
3337 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003338 ret = -EINVAL;
3339 goto out;
3340 }
3341 }
3342
Stefan Behrens8dabb742012-11-06 13:15:27 +01003343 num_devices = fs_info->fs_devices->num_devices;
3344 btrfs_dev_replace_lock(&fs_info->dev_replace);
3345 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3346 BUG_ON(num_devices < 1);
3347 num_devices--;
3348 }
3349 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003350 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003351 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003352 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003353 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003354 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003355 if (num_devices > 2)
3356 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3357 if (num_devices > 3)
3358 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3359 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003360 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3361 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3362 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003363 btrfs_err(fs_info, "unable to start balance with target "
3364 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003365 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003366 ret = -EINVAL;
3367 goto out;
3368 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003369 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3370 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3371 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003372 btrfs_err(fs_info,
3373 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003374 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003375 ret = -EINVAL;
3376 goto out;
3377 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003378 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3379 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3380 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003381 btrfs_err(fs_info,
3382 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003383 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003384 ret = -EINVAL;
3385 goto out;
3386 }
3387
Ilya Dryomove4837f82012-03-27 17:09:17 +03003388 /* allow dup'ed data chunks only in mixed mode */
3389 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003390 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003391 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003392 ret = -EINVAL;
3393 goto out;
3394 }
3395
3396 /* allow to reduce meta or sys integrity only if force set */
3397 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003398 BTRFS_BLOCK_GROUP_RAID10 |
3399 BTRFS_BLOCK_GROUP_RAID5 |
3400 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003401 do {
3402 seq = read_seqbegin(&fs_info->profiles_lock);
3403
3404 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3405 (fs_info->avail_system_alloc_bits & allowed) &&
3406 !(bctl->sys.target & allowed)) ||
3407 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3408 (fs_info->avail_metadata_alloc_bits & allowed) &&
3409 !(bctl->meta.target & allowed))) {
3410 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003411 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003412 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003413 btrfs_err(fs_info, "balance will reduce metadata "
3414 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003415 ret = -EINVAL;
3416 goto out;
3417 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003418 }
Miao Xiede98ced2013-01-29 10:13:12 +00003419 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003420
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003421 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3422 int num_tolerated_disk_barrier_failures;
3423 u64 target = bctl->sys.target;
3424
3425 num_tolerated_disk_barrier_failures =
3426 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3427 if (num_tolerated_disk_barrier_failures > 0 &&
3428 (target &
3429 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3430 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3431 num_tolerated_disk_barrier_failures = 0;
3432 else if (num_tolerated_disk_barrier_failures > 1 &&
3433 (target &
3434 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3435 num_tolerated_disk_barrier_failures = 1;
3436
3437 fs_info->num_tolerated_disk_barrier_failures =
3438 num_tolerated_disk_barrier_failures;
3439 }
3440
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003441 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003442 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003443 goto out;
3444
Ilya Dryomov59641012012-01-16 22:04:48 +02003445 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3446 BUG_ON(ret == -EEXIST);
3447 set_balance_control(bctl);
3448 } else {
3449 BUG_ON(ret != -EEXIST);
3450 spin_lock(&fs_info->balance_lock);
3451 update_balance_args(bctl);
3452 spin_unlock(&fs_info->balance_lock);
3453 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003454
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003455 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003456 mutex_unlock(&fs_info->balance_mutex);
3457
3458 ret = __btrfs_balance(fs_info);
3459
3460 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003461 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003462
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003463 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3464 fs_info->num_tolerated_disk_barrier_failures =
3465 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3466 }
3467
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003468 if (bargs) {
3469 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003470 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003471 }
3472
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003473 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3474 balance_need_close(fs_info)) {
3475 __cancel_balance(fs_info);
3476 }
3477
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003478 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003479
3480 return ret;
3481out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003482 if (bctl->flags & BTRFS_BALANCE_RESUME)
3483 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003484 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003485 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003486 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3487 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003488 return ret;
3489}
3490
3491static int balance_kthread(void *data)
3492{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003493 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003494 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003495
3496 mutex_lock(&fs_info->volume_mutex);
3497 mutex_lock(&fs_info->balance_mutex);
3498
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003499 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003500 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003501 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003502 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003503
3504 mutex_unlock(&fs_info->balance_mutex);
3505 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003506
Ilya Dryomov59641012012-01-16 22:04:48 +02003507 return ret;
3508}
3509
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003510int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3511{
3512 struct task_struct *tsk;
3513
3514 spin_lock(&fs_info->balance_lock);
3515 if (!fs_info->balance_ctl) {
3516 spin_unlock(&fs_info->balance_lock);
3517 return 0;
3518 }
3519 spin_unlock(&fs_info->balance_lock);
3520
3521 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003522 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003523 return 0;
3524 }
3525
3526 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303527 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003528}
3529
Ilya Dryomov68310a52012-06-22 12:24:12 -06003530int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003531{
Ilya Dryomov59641012012-01-16 22:04:48 +02003532 struct btrfs_balance_control *bctl;
3533 struct btrfs_balance_item *item;
3534 struct btrfs_disk_balance_args disk_bargs;
3535 struct btrfs_path *path;
3536 struct extent_buffer *leaf;
3537 struct btrfs_key key;
3538 int ret;
3539
3540 path = btrfs_alloc_path();
3541 if (!path)
3542 return -ENOMEM;
3543
Ilya Dryomov68310a52012-06-22 12:24:12 -06003544 key.objectid = BTRFS_BALANCE_OBJECTID;
3545 key.type = BTRFS_BALANCE_ITEM_KEY;
3546 key.offset = 0;
3547
3548 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3549 if (ret < 0)
3550 goto out;
3551 if (ret > 0) { /* ret = -ENOENT; */
3552 ret = 0;
3553 goto out;
3554 }
3555
Ilya Dryomov59641012012-01-16 22:04:48 +02003556 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3557 if (!bctl) {
3558 ret = -ENOMEM;
3559 goto out;
3560 }
3561
Ilya Dryomov59641012012-01-16 22:04:48 +02003562 leaf = path->nodes[0];
3563 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3564
Ilya Dryomov68310a52012-06-22 12:24:12 -06003565 bctl->fs_info = fs_info;
3566 bctl->flags = btrfs_balance_flags(leaf, item);
3567 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003568
3569 btrfs_balance_data(leaf, item, &disk_bargs);
3570 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3571 btrfs_balance_meta(leaf, item, &disk_bargs);
3572 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3573 btrfs_balance_sys(leaf, item, &disk_bargs);
3574 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3575
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003576 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3577
Ilya Dryomov68310a52012-06-22 12:24:12 -06003578 mutex_lock(&fs_info->volume_mutex);
3579 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003580
Ilya Dryomov68310a52012-06-22 12:24:12 -06003581 set_balance_control(bctl);
3582
3583 mutex_unlock(&fs_info->balance_mutex);
3584 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003585out:
3586 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003587 return ret;
3588}
3589
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003590int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3591{
3592 int ret = 0;
3593
3594 mutex_lock(&fs_info->balance_mutex);
3595 if (!fs_info->balance_ctl) {
3596 mutex_unlock(&fs_info->balance_mutex);
3597 return -ENOTCONN;
3598 }
3599
3600 if (atomic_read(&fs_info->balance_running)) {
3601 atomic_inc(&fs_info->balance_pause_req);
3602 mutex_unlock(&fs_info->balance_mutex);
3603
3604 wait_event(fs_info->balance_wait_q,
3605 atomic_read(&fs_info->balance_running) == 0);
3606
3607 mutex_lock(&fs_info->balance_mutex);
3608 /* we are good with balance_ctl ripped off from under us */
3609 BUG_ON(atomic_read(&fs_info->balance_running));
3610 atomic_dec(&fs_info->balance_pause_req);
3611 } else {
3612 ret = -ENOTCONN;
3613 }
3614
3615 mutex_unlock(&fs_info->balance_mutex);
3616 return ret;
3617}
3618
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003619int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3620{
Ilya Dryomove649e582013-10-10 20:40:21 +03003621 if (fs_info->sb->s_flags & MS_RDONLY)
3622 return -EROFS;
3623
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003624 mutex_lock(&fs_info->balance_mutex);
3625 if (!fs_info->balance_ctl) {
3626 mutex_unlock(&fs_info->balance_mutex);
3627 return -ENOTCONN;
3628 }
3629
3630 atomic_inc(&fs_info->balance_cancel_req);
3631 /*
3632 * if we are running just wait and return, balance item is
3633 * deleted in btrfs_balance in this case
3634 */
3635 if (atomic_read(&fs_info->balance_running)) {
3636 mutex_unlock(&fs_info->balance_mutex);
3637 wait_event(fs_info->balance_wait_q,
3638 atomic_read(&fs_info->balance_running) == 0);
3639 mutex_lock(&fs_info->balance_mutex);
3640 } else {
3641 /* __cancel_balance needs volume_mutex */
3642 mutex_unlock(&fs_info->balance_mutex);
3643 mutex_lock(&fs_info->volume_mutex);
3644 mutex_lock(&fs_info->balance_mutex);
3645
3646 if (fs_info->balance_ctl)
3647 __cancel_balance(fs_info);
3648
3649 mutex_unlock(&fs_info->volume_mutex);
3650 }
3651
3652 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3653 atomic_dec(&fs_info->balance_cancel_req);
3654 mutex_unlock(&fs_info->balance_mutex);
3655 return 0;
3656}
3657
Stefan Behrens803b2f52013-08-15 17:11:21 +02003658static int btrfs_uuid_scan_kthread(void *data)
3659{
3660 struct btrfs_fs_info *fs_info = data;
3661 struct btrfs_root *root = fs_info->tree_root;
3662 struct btrfs_key key;
3663 struct btrfs_key max_key;
3664 struct btrfs_path *path = NULL;
3665 int ret = 0;
3666 struct extent_buffer *eb;
3667 int slot;
3668 struct btrfs_root_item root_item;
3669 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003670 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003671
3672 path = btrfs_alloc_path();
3673 if (!path) {
3674 ret = -ENOMEM;
3675 goto out;
3676 }
3677
3678 key.objectid = 0;
3679 key.type = BTRFS_ROOT_ITEM_KEY;
3680 key.offset = 0;
3681
3682 max_key.objectid = (u64)-1;
3683 max_key.type = BTRFS_ROOT_ITEM_KEY;
3684 max_key.offset = (u64)-1;
3685
Stefan Behrens803b2f52013-08-15 17:11:21 +02003686 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003687 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003688 if (ret) {
3689 if (ret > 0)
3690 ret = 0;
3691 break;
3692 }
3693
3694 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3695 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3696 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3697 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3698 goto skip;
3699
3700 eb = path->nodes[0];
3701 slot = path->slots[0];
3702 item_size = btrfs_item_size_nr(eb, slot);
3703 if (item_size < sizeof(root_item))
3704 goto skip;
3705
Stefan Behrens803b2f52013-08-15 17:11:21 +02003706 read_extent_buffer(eb, &root_item,
3707 btrfs_item_ptr_offset(eb, slot),
3708 (int)sizeof(root_item));
3709 if (btrfs_root_refs(&root_item) == 0)
3710 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003711
3712 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3713 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3714 if (trans)
3715 goto update_tree;
3716
3717 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003718 /*
3719 * 1 - subvol uuid item
3720 * 1 - received_subvol uuid item
3721 */
3722 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3723 if (IS_ERR(trans)) {
3724 ret = PTR_ERR(trans);
3725 break;
3726 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003727 continue;
3728 } else {
3729 goto skip;
3730 }
3731update_tree:
3732 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003733 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3734 root_item.uuid,
3735 BTRFS_UUID_KEY_SUBVOL,
3736 key.objectid);
3737 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003738 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003739 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003740 break;
3741 }
3742 }
3743
3744 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003745 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3746 root_item.received_uuid,
3747 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3748 key.objectid);
3749 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003750 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003751 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003752 break;
3753 }
3754 }
3755
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003756skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003757 if (trans) {
3758 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003759 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003760 if (ret)
3761 break;
3762 }
3763
Stefan Behrens803b2f52013-08-15 17:11:21 +02003764 btrfs_release_path(path);
3765 if (key.offset < (u64)-1) {
3766 key.offset++;
3767 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3768 key.offset = 0;
3769 key.type = BTRFS_ROOT_ITEM_KEY;
3770 } else if (key.objectid < (u64)-1) {
3771 key.offset = 0;
3772 key.type = BTRFS_ROOT_ITEM_KEY;
3773 key.objectid++;
3774 } else {
3775 break;
3776 }
3777 cond_resched();
3778 }
3779
3780out:
3781 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003782 if (trans && !IS_ERR(trans))
3783 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003784 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05003785 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003786 else
3787 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003788 up(&fs_info->uuid_tree_rescan_sem);
3789 return 0;
3790}
3791
Stefan Behrens70f80172013-08-15 17:11:23 +02003792/*
3793 * Callback for btrfs_uuid_tree_iterate().
3794 * returns:
3795 * 0 check succeeded, the entry is not outdated.
3796 * < 0 if an error occured.
3797 * > 0 if the check failed, which means the caller shall remove the entry.
3798 */
3799static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3800 u8 *uuid, u8 type, u64 subid)
3801{
3802 struct btrfs_key key;
3803 int ret = 0;
3804 struct btrfs_root *subvol_root;
3805
3806 if (type != BTRFS_UUID_KEY_SUBVOL &&
3807 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3808 goto out;
3809
3810 key.objectid = subid;
3811 key.type = BTRFS_ROOT_ITEM_KEY;
3812 key.offset = (u64)-1;
3813 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3814 if (IS_ERR(subvol_root)) {
3815 ret = PTR_ERR(subvol_root);
3816 if (ret == -ENOENT)
3817 ret = 1;
3818 goto out;
3819 }
3820
3821 switch (type) {
3822 case BTRFS_UUID_KEY_SUBVOL:
3823 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3824 ret = 1;
3825 break;
3826 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3827 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3828 BTRFS_UUID_SIZE))
3829 ret = 1;
3830 break;
3831 }
3832
3833out:
3834 return ret;
3835}
3836
3837static int btrfs_uuid_rescan_kthread(void *data)
3838{
3839 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3840 int ret;
3841
3842 /*
3843 * 1st step is to iterate through the existing UUID tree and
3844 * to delete all entries that contain outdated data.
3845 * 2nd step is to add all missing entries to the UUID tree.
3846 */
3847 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3848 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003849 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003850 up(&fs_info->uuid_tree_rescan_sem);
3851 return ret;
3852 }
3853 return btrfs_uuid_scan_kthread(data);
3854}
3855
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003856int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3857{
3858 struct btrfs_trans_handle *trans;
3859 struct btrfs_root *tree_root = fs_info->tree_root;
3860 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003861 struct task_struct *task;
3862 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003863
3864 /*
3865 * 1 - root node
3866 * 1 - root item
3867 */
3868 trans = btrfs_start_transaction(tree_root, 2);
3869 if (IS_ERR(trans))
3870 return PTR_ERR(trans);
3871
3872 uuid_root = btrfs_create_tree(trans, fs_info,
3873 BTRFS_UUID_TREE_OBJECTID);
3874 if (IS_ERR(uuid_root)) {
3875 btrfs_abort_transaction(trans, tree_root,
3876 PTR_ERR(uuid_root));
3877 return PTR_ERR(uuid_root);
3878 }
3879
3880 fs_info->uuid_root = uuid_root;
3881
Stefan Behrens803b2f52013-08-15 17:11:21 +02003882 ret = btrfs_commit_transaction(trans, tree_root);
3883 if (ret)
3884 return ret;
3885
3886 down(&fs_info->uuid_tree_rescan_sem);
3887 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3888 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02003889 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003890 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02003891 up(&fs_info->uuid_tree_rescan_sem);
3892 return PTR_ERR(task);
3893 }
3894
3895 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003896}
Stefan Behrens803b2f52013-08-15 17:11:21 +02003897
Stefan Behrens70f80172013-08-15 17:11:23 +02003898int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3899{
3900 struct task_struct *task;
3901
3902 down(&fs_info->uuid_tree_rescan_sem);
3903 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3904 if (IS_ERR(task)) {
3905 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003906 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02003907 up(&fs_info->uuid_tree_rescan_sem);
3908 return PTR_ERR(task);
3909 }
3910
3911 return 0;
3912}
3913
Chris Mason8f18cf12008-04-25 16:53:30 -04003914/*
3915 * shrinking a device means finding all of the device extents past
3916 * the new size, and then following the back refs to the chunks.
3917 * The chunk relocation code actually frees the device extent
3918 */
3919int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3920{
3921 struct btrfs_trans_handle *trans;
3922 struct btrfs_root *root = device->dev_root;
3923 struct btrfs_dev_extent *dev_extent = NULL;
3924 struct btrfs_path *path;
3925 u64 length;
3926 u64 chunk_tree;
3927 u64 chunk_objectid;
3928 u64 chunk_offset;
3929 int ret;
3930 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003931 int failed = 0;
3932 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003933 struct extent_buffer *l;
3934 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003935 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003936 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08003937 u64 old_size = btrfs_device_get_total_bytes(device);
3938 u64 diff = old_size - new_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04003939
Stefan Behrens63a212a2012-11-05 18:29:28 +01003940 if (device->is_tgtdev_for_dev_replace)
3941 return -EINVAL;
3942
Chris Mason8f18cf12008-04-25 16:53:30 -04003943 path = btrfs_alloc_path();
3944 if (!path)
3945 return -ENOMEM;
3946
Chris Mason8f18cf12008-04-25 16:53:30 -04003947 path->reada = 2;
3948
Chris Mason7d9eb122008-07-08 14:19:17 -04003949 lock_chunks(root);
3950
Miao Xie7cc8e582014-09-03 21:35:38 +08003951 btrfs_device_set_total_bytes(device, new_size);
Josef Bacik2bf64752011-09-26 17:12:22 -04003952 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003953 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003954 spin_lock(&root->fs_info->free_chunk_lock);
3955 root->fs_info->free_chunk_space -= diff;
3956 spin_unlock(&root->fs_info->free_chunk_lock);
3957 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003958 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003959
Josef Bacikba1bf482009-09-11 16:11:19 -04003960again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003961 key.objectid = device->devid;
3962 key.offset = (u64)-1;
3963 key.type = BTRFS_DEV_EXTENT_KEY;
3964
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003965 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003966 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3967 if (ret < 0)
3968 goto done;
3969
3970 ret = btrfs_previous_item(root, path, 0, key.type);
3971 if (ret < 0)
3972 goto done;
3973 if (ret) {
3974 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003975 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003976 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003977 }
3978
3979 l = path->nodes[0];
3980 slot = path->slots[0];
3981 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3982
Josef Bacikba1bf482009-09-11 16:11:19 -04003983 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003984 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003985 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003986 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003987
3988 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3989 length = btrfs_dev_extent_length(l, dev_extent);
3990
Josef Bacikba1bf482009-09-11 16:11:19 -04003991 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003992 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003993 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003994 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003995
3996 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3997 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3998 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003999 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004000
4001 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
4002 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04004003 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004004 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004005 if (ret == -ENOSPC)
4006 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004007 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004008
4009 if (failed && !retried) {
4010 failed = 0;
4011 retried = true;
4012 goto again;
4013 } else if (failed && retried) {
4014 ret = -ENOSPC;
4015 lock_chunks(root);
4016
Miao Xie7cc8e582014-09-03 21:35:38 +08004017 btrfs_device_set_total_bytes(device, old_size);
Josef Bacikba1bf482009-09-11 16:11:19 -04004018 if (device->writeable)
4019 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04004020 spin_lock(&root->fs_info->free_chunk_lock);
4021 root->fs_info->free_chunk_space += diff;
4022 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04004023 unlock_chunks(root);
4024 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004025 }
4026
Chris Balld6397ba2009-04-27 07:29:03 -04004027 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004028 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004029 if (IS_ERR(trans)) {
4030 ret = PTR_ERR(trans);
4031 goto done;
4032 }
4033
Chris Balld6397ba2009-04-27 07:29:03 -04004034 lock_chunks(root);
Miao Xie7cc8e582014-09-03 21:35:38 +08004035 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004036 if (list_empty(&device->resized_list))
4037 list_add_tail(&device->resized_list,
4038 &root->fs_info->fs_devices->resized_devices);
4039
Chris Balld6397ba2009-04-27 07:29:03 -04004040 WARN_ON(diff > old_total);
4041 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4042 unlock_chunks(root);
Miao Xie2196d6e2014-09-03 21:35:41 +08004043
4044 /* Now btrfs_update_device() will change the on-disk size. */
4045 ret = btrfs_update_device(trans, device);
Chris Balld6397ba2009-04-27 07:29:03 -04004046 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004047done:
4048 btrfs_free_path(path);
4049 return ret;
4050}
4051
Li Zefan125ccb02011-12-08 15:07:24 +08004052static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004053 struct btrfs_key *key,
4054 struct btrfs_chunk *chunk, int item_size)
4055{
David Sterba6c417612011-04-13 15:41:04 +02004056 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004057 struct btrfs_disk_key disk_key;
4058 u32 array_size;
4059 u8 *ptr;
4060
Miao Xiefe48a5c2014-09-03 21:35:39 +08004061 lock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004062 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004063 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004064 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4065 unlock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004066 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004067 }
Chris Mason0b86a832008-03-24 15:01:56 -04004068
4069 ptr = super_copy->sys_chunk_array + array_size;
4070 btrfs_cpu_key_to_disk(&disk_key, key);
4071 memcpy(ptr, &disk_key, sizeof(disk_key));
4072 ptr += sizeof(disk_key);
4073 memcpy(ptr, chunk, item_size);
4074 item_size += sizeof(disk_key);
4075 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004076 unlock_chunks(root);
4077
Chris Mason0b86a832008-03-24 15:01:56 -04004078 return 0;
4079}
4080
Miao Xieb2117a32011-01-05 10:07:28 +00004081/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004082 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004083 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004084static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004085{
Arne Jansen73c5de02011-04-12 12:07:57 +02004086 const struct btrfs_device_info *di_a = a;
4087 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004088
Arne Jansen73c5de02011-04-12 12:07:57 +02004089 if (di_a->max_avail > di_b->max_avail)
4090 return -1;
4091 if (di_a->max_avail < di_b->max_avail)
4092 return 1;
4093 if (di_a->total_avail > di_b->total_avail)
4094 return -1;
4095 if (di_a->total_avail < di_b->total_avail)
4096 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004097 return 0;
4098}
4099
Eric Sandeen48a3b632013-04-25 20:41:01 +00004100static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00004101 [BTRFS_RAID_RAID10] = {
4102 .sub_stripes = 2,
4103 .dev_stripes = 1,
4104 .devs_max = 0, /* 0 == as many as possible */
4105 .devs_min = 4,
4106 .devs_increment = 2,
4107 .ncopies = 2,
4108 },
4109 [BTRFS_RAID_RAID1] = {
4110 .sub_stripes = 1,
4111 .dev_stripes = 1,
4112 .devs_max = 2,
4113 .devs_min = 2,
4114 .devs_increment = 2,
4115 .ncopies = 2,
4116 },
4117 [BTRFS_RAID_DUP] = {
4118 .sub_stripes = 1,
4119 .dev_stripes = 2,
4120 .devs_max = 1,
4121 .devs_min = 1,
4122 .devs_increment = 1,
4123 .ncopies = 2,
4124 },
4125 [BTRFS_RAID_RAID0] = {
4126 .sub_stripes = 1,
4127 .dev_stripes = 1,
4128 .devs_max = 0,
4129 .devs_min = 2,
4130 .devs_increment = 1,
4131 .ncopies = 1,
4132 },
4133 [BTRFS_RAID_SINGLE] = {
4134 .sub_stripes = 1,
4135 .dev_stripes = 1,
4136 .devs_max = 1,
4137 .devs_min = 1,
4138 .devs_increment = 1,
4139 .ncopies = 1,
4140 },
Chris Masone942f882013-02-20 14:06:05 -05004141 [BTRFS_RAID_RAID5] = {
4142 .sub_stripes = 1,
4143 .dev_stripes = 1,
4144 .devs_max = 0,
4145 .devs_min = 2,
4146 .devs_increment = 1,
4147 .ncopies = 2,
4148 },
4149 [BTRFS_RAID_RAID6] = {
4150 .sub_stripes = 1,
4151 .dev_stripes = 1,
4152 .devs_max = 0,
4153 .devs_min = 3,
4154 .devs_increment = 1,
4155 .ncopies = 3,
4156 },
Liu Bo31e50222012-11-21 14:18:10 +00004157};
4158
David Woodhouse53b381b2013-01-29 18:40:14 -05004159static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4160{
4161 /* TODO allow them to set a preferred stripe size */
4162 return 64 * 1024;
4163}
4164
4165static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4166{
David Woodhouse53b381b2013-01-29 18:40:14 -05004167 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
4168 return;
4169
Miao Xieceda0862013-04-11 10:30:16 +00004170 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004171}
4172
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004173#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4174 - sizeof(struct btrfs_item) \
4175 - sizeof(struct btrfs_chunk)) \
4176 / sizeof(struct btrfs_stripe) + 1)
4177
4178#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4179 - 2 * sizeof(struct btrfs_disk_key) \
4180 - 2 * sizeof(struct btrfs_chunk)) \
4181 / sizeof(struct btrfs_stripe) + 1)
4182
Miao Xieb2117a32011-01-05 10:07:28 +00004183static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004184 struct btrfs_root *extent_root, u64 start,
4185 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004186{
4187 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004188 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4189 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004190 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004191 struct extent_map_tree *em_tree;
4192 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004193 struct btrfs_device_info *devices_info = NULL;
4194 u64 total_avail;
4195 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004196 int data_stripes; /* number of stripes that count for
4197 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004198 int sub_stripes; /* sub_stripes info for map */
4199 int dev_stripes; /* stripes per dev */
4200 int devs_max; /* max devs to use */
4201 int devs_min; /* min devs needed */
4202 int devs_increment; /* ndevs has to be a multiple of this */
4203 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004204 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004205 u64 max_stripe_size;
4206 u64 max_chunk_size;
4207 u64 stripe_size;
4208 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004209 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004210 int ndevs;
4211 int i;
4212 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004213 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004214
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004215 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004216
Miao Xieb2117a32011-01-05 10:07:28 +00004217 if (list_empty(&fs_devices->alloc_list))
4218 return -ENOSPC;
4219
Liu Bo31e50222012-11-21 14:18:10 +00004220 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004221
Liu Bo31e50222012-11-21 14:18:10 +00004222 sub_stripes = btrfs_raid_array[index].sub_stripes;
4223 dev_stripes = btrfs_raid_array[index].dev_stripes;
4224 devs_max = btrfs_raid_array[index].devs_max;
4225 devs_min = btrfs_raid_array[index].devs_min;
4226 devs_increment = btrfs_raid_array[index].devs_increment;
4227 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004228
4229 if (type & BTRFS_BLOCK_GROUP_DATA) {
4230 max_stripe_size = 1024 * 1024 * 1024;
4231 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004232 if (!devs_max)
4233 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004234 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004235 /* for larger filesystems, use larger metadata chunks */
4236 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4237 max_stripe_size = 1024 * 1024 * 1024;
4238 else
4239 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004240 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004241 if (!devs_max)
4242 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004243 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004244 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004245 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004246 if (!devs_max)
4247 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004248 } else {
David Sterba351fd352014-05-15 16:48:20 +02004249 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004250 type);
4251 BUG_ON(1);
4252 }
4253
4254 /* we don't want a chunk larger than 10% of writeable space */
4255 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4256 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004257
4258 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
4259 GFP_NOFS);
4260 if (!devices_info)
4261 return -ENOMEM;
4262
Arne Jansen73c5de02011-04-12 12:07:57 +02004263 cur = fs_devices->alloc_list.next;
4264
4265 /*
4266 * in the first pass through the devices list, we gather information
4267 * about the available holes on each device.
4268 */
4269 ndevs = 0;
4270 while (cur != &fs_devices->alloc_list) {
4271 struct btrfs_device *device;
4272 u64 max_avail;
4273 u64 dev_offset;
4274
4275 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4276
4277 cur = cur->next;
4278
4279 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004280 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004281 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004282 continue;
4283 }
4284
Stefan Behrens63a212a2012-11-05 18:29:28 +01004285 if (!device->in_fs_metadata ||
4286 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004287 continue;
4288
4289 if (device->total_bytes > device->bytes_used)
4290 total_avail = device->total_bytes - device->bytes_used;
4291 else
4292 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004293
4294 /* If there is no space on this device, skip it. */
4295 if (total_avail == 0)
4296 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004297
Josef Bacik6df9a952013-06-27 13:22:46 -04004298 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004299 max_stripe_size * dev_stripes,
4300 &dev_offset, &max_avail);
4301 if (ret && ret != -ENOSPC)
4302 goto error;
4303
4304 if (ret == 0)
4305 max_avail = max_stripe_size * dev_stripes;
4306
4307 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4308 continue;
4309
Eric Sandeen063d0062013-01-31 00:55:01 +00004310 if (ndevs == fs_devices->rw_devices) {
4311 WARN(1, "%s: found more than %llu devices\n",
4312 __func__, fs_devices->rw_devices);
4313 break;
4314 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004315 devices_info[ndevs].dev_offset = dev_offset;
4316 devices_info[ndevs].max_avail = max_avail;
4317 devices_info[ndevs].total_avail = total_avail;
4318 devices_info[ndevs].dev = device;
4319 ++ndevs;
4320 }
4321
4322 /*
4323 * now sort the devices by hole size / available space
4324 */
4325 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4326 btrfs_cmp_device_info, NULL);
4327
4328 /* round down to number of usable stripes */
4329 ndevs -= ndevs % devs_increment;
4330
4331 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4332 ret = -ENOSPC;
4333 goto error;
4334 }
4335
4336 if (devs_max && ndevs > devs_max)
4337 ndevs = devs_max;
4338 /*
4339 * the primary goal is to maximize the number of stripes, so use as many
4340 * devices as possible, even if the stripes are not maximum sized.
4341 */
4342 stripe_size = devices_info[ndevs-1].max_avail;
4343 num_stripes = ndevs * dev_stripes;
4344
David Woodhouse53b381b2013-01-29 18:40:14 -05004345 /*
4346 * this will have to be fixed for RAID1 and RAID10 over
4347 * more drives
4348 */
4349 data_stripes = num_stripes / ncopies;
4350
David Woodhouse53b381b2013-01-29 18:40:14 -05004351 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4352 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4353 btrfs_super_stripesize(info->super_copy));
4354 data_stripes = num_stripes - 1;
4355 }
4356 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4357 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4358 btrfs_super_stripesize(info->super_copy));
4359 data_stripes = num_stripes - 2;
4360 }
Chris Mason86db2572013-02-20 16:23:40 -05004361
4362 /*
4363 * Use the number of data stripes to figure out how big this chunk
4364 * is really going to be in terms of logical address space,
4365 * and compare that answer with the max chunk size
4366 */
4367 if (stripe_size * data_stripes > max_chunk_size) {
4368 u64 mask = (1ULL << 24) - 1;
4369 stripe_size = max_chunk_size;
4370 do_div(stripe_size, data_stripes);
4371
4372 /* bump the answer up to a 16MB boundary */
4373 stripe_size = (stripe_size + mask) & ~mask;
4374
4375 /* but don't go higher than the limits we found
4376 * while searching for free extents
4377 */
4378 if (stripe_size > devices_info[ndevs-1].max_avail)
4379 stripe_size = devices_info[ndevs-1].max_avail;
4380 }
4381
Arne Jansen73c5de02011-04-12 12:07:57 +02004382 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004383
4384 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05004385 do_div(stripe_size, raid_stripe_len);
4386 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004387
Miao Xieb2117a32011-01-05 10:07:28 +00004388 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4389 if (!map) {
4390 ret = -ENOMEM;
4391 goto error;
4392 }
4393 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004394
Arne Jansen73c5de02011-04-12 12:07:57 +02004395 for (i = 0; i < ndevs; ++i) {
4396 for (j = 0; j < dev_stripes; ++j) {
4397 int s = i * dev_stripes + j;
4398 map->stripes[s].dev = devices_info[i].dev;
4399 map->stripes[s].physical = devices_info[i].dev_offset +
4400 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004401 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004402 }
Chris Mason593060d2008-03-25 16:50:33 -04004403 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004404 map->stripe_len = raid_stripe_len;
4405 map->io_align = raid_stripe_len;
4406 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004407 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004408 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004409
David Woodhouse53b381b2013-01-29 18:40:14 -05004410 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004411
Arne Jansen73c5de02011-04-12 12:07:57 +02004412 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004413
David Sterba172ddd62011-04-21 00:48:27 +02004414 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004415 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004416 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004417 ret = -ENOMEM;
4418 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004419 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004420 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004421 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004422 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004423 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004424 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004425 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004426 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004427
Chris Mason0b86a832008-03-24 15:01:56 -04004428 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004429 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004430 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004431 if (!ret) {
4432 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4433 atomic_inc(&em->refs);
4434 }
Chris Mason890871b2009-09-02 16:24:52 -04004435 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004436 if (ret) {
4437 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004438 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004439 }
Yan Zheng2b820322008-11-17 21:11:30 -05004440
Josef Bacik04487482013-01-29 15:03:37 -05004441 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4442 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4443 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004444 if (ret)
4445 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004446
Miao Xie7cc8e582014-09-03 21:35:38 +08004447 for (i = 0; i < map->num_stripes; i++) {
4448 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4449 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4450 }
Miao Xie43530c42014-09-03 21:35:36 +08004451
Miao Xie1c116182014-09-03 21:35:37 +08004452 spin_lock(&extent_root->fs_info->free_chunk_lock);
4453 extent_root->fs_info->free_chunk_space -= (stripe_size *
4454 map->num_stripes);
4455 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4456
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004457 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004458 check_raid56_incompat_flag(extent_root->fs_info, type);
4459
Miao Xieb2117a32011-01-05 10:07:28 +00004460 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004461 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004462
Josef Bacik6df9a952013-06-27 13:22:46 -04004463error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004464 write_lock(&em_tree->lock);
4465 remove_extent_mapping(em_tree, em);
4466 write_unlock(&em_tree->lock);
4467
4468 /* One for our allocation */
4469 free_extent_map(em);
4470 /* One for the tree reference */
4471 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004472error:
Miao Xieb2117a32011-01-05 10:07:28 +00004473 kfree(devices_info);
4474 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004475}
4476
Josef Bacik6df9a952013-06-27 13:22:46 -04004477int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004478 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004479 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004480{
Yan Zheng2b820322008-11-17 21:11:30 -05004481 struct btrfs_key key;
4482 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4483 struct btrfs_device *device;
4484 struct btrfs_chunk *chunk;
4485 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004486 struct extent_map_tree *em_tree;
4487 struct extent_map *em;
4488 struct map_lookup *map;
4489 size_t item_size;
4490 u64 dev_offset;
4491 u64 stripe_size;
4492 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004493 int ret;
4494
Josef Bacik6df9a952013-06-27 13:22:46 -04004495 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4496 read_lock(&em_tree->lock);
4497 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4498 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004499
Josef Bacik6df9a952013-06-27 13:22:46 -04004500 if (!em) {
4501 btrfs_crit(extent_root->fs_info, "unable to find logical "
4502 "%Lu len %Lu", chunk_offset, chunk_size);
4503 return -EINVAL;
4504 }
4505
4506 if (em->start != chunk_offset || em->len != chunk_size) {
4507 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004508 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004509 chunk_size, em->start, em->len);
4510 free_extent_map(em);
4511 return -EINVAL;
4512 }
4513
4514 map = (struct map_lookup *)em->bdev;
4515 item_size = btrfs_chunk_item_size(map->num_stripes);
4516 stripe_size = em->orig_block_len;
4517
4518 chunk = kzalloc(item_size, GFP_NOFS);
4519 if (!chunk) {
4520 ret = -ENOMEM;
4521 goto out;
4522 }
4523
4524 for (i = 0; i < map->num_stripes; i++) {
4525 device = map->stripes[i].dev;
4526 dev_offset = map->stripes[i].physical;
4527
Yan Zheng2b820322008-11-17 21:11:30 -05004528 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004529 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004530 goto out;
4531 ret = btrfs_alloc_dev_extent(trans, device,
4532 chunk_root->root_key.objectid,
4533 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4534 chunk_offset, dev_offset,
4535 stripe_size);
4536 if (ret)
4537 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004538 }
4539
Yan Zheng2b820322008-11-17 21:11:30 -05004540 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004541 for (i = 0; i < map->num_stripes; i++) {
4542 device = map->stripes[i].dev;
4543 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004544
4545 btrfs_set_stack_stripe_devid(stripe, device->devid);
4546 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4547 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4548 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004549 }
4550
4551 btrfs_set_stack_chunk_length(chunk, chunk_size);
4552 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4553 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4554 btrfs_set_stack_chunk_type(chunk, map->type);
4555 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4556 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4557 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4558 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4559 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4560
4561 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4562 key.type = BTRFS_CHUNK_ITEM_KEY;
4563 key.offset = chunk_offset;
4564
4565 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004566 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4567 /*
4568 * TODO: Cleanup of inserted chunk root in case of
4569 * failure.
4570 */
Li Zefan125ccb02011-12-08 15:07:24 +08004571 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004572 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004573 }
liubo1abe9b82011-03-24 11:18:59 +00004574
Josef Bacik6df9a952013-06-27 13:22:46 -04004575out:
Yan Zheng2b820322008-11-17 21:11:30 -05004576 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004577 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004578 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004579}
4580
4581/*
4582 * Chunk allocation falls into two parts. The first part does works
4583 * that make the new allocated chunk useable, but not do any operation
4584 * that modifies the chunk tree. The second part does the works that
4585 * require modifying the chunk tree. This division is important for the
4586 * bootstrap process of adding storage to a seed btrfs.
4587 */
4588int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4589 struct btrfs_root *extent_root, u64 type)
4590{
4591 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004592
Josef Bacik6df9a952013-06-27 13:22:46 -04004593 chunk_offset = find_next_chunk(extent_root->fs_info);
4594 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004595}
4596
Chris Masond3977122009-01-05 21:25:51 -05004597static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004598 struct btrfs_root *root,
4599 struct btrfs_device *device)
4600{
4601 u64 chunk_offset;
4602 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004603 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004604 struct btrfs_fs_info *fs_info = root->fs_info;
4605 struct btrfs_root *extent_root = fs_info->extent_root;
4606 int ret;
4607
Josef Bacik6df9a952013-06-27 13:22:46 -04004608 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004609 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004610 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4611 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004612 if (ret)
4613 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004614
Josef Bacik6df9a952013-06-27 13:22:46 -04004615 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004616 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004617 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4618 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004619 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004620}
4621
Miao Xied20983b2014-07-03 18:22:13 +08004622static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4623{
4624 int max_errors;
4625
4626 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4627 BTRFS_BLOCK_GROUP_RAID10 |
4628 BTRFS_BLOCK_GROUP_RAID5 |
4629 BTRFS_BLOCK_GROUP_DUP)) {
4630 max_errors = 1;
4631 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4632 max_errors = 2;
4633 } else {
4634 max_errors = 0;
4635 }
4636
4637 return max_errors;
4638}
4639
Yan Zheng2b820322008-11-17 21:11:30 -05004640int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4641{
4642 struct extent_map *em;
4643 struct map_lookup *map;
4644 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4645 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004646 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004647 int i;
4648
Chris Mason890871b2009-09-02 16:24:52 -04004649 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004650 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004651 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004652 if (!em)
4653 return 1;
4654
4655 map = (struct map_lookup *)em->bdev;
4656 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004657 if (map->stripes[i].dev->missing) {
4658 miss_ndevs++;
4659 continue;
4660 }
4661
Yan Zheng2b820322008-11-17 21:11:30 -05004662 if (!map->stripes[i].dev->writeable) {
4663 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004664 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004665 }
4666 }
Miao Xied20983b2014-07-03 18:22:13 +08004667
4668 /*
4669 * If the number of missing devices is larger than max errors,
4670 * we can not write the data into that chunk successfully, so
4671 * set it readonly.
4672 */
4673 if (miss_ndevs > btrfs_chunk_max_errors(map))
4674 readonly = 1;
4675end:
Yan Zheng2b820322008-11-17 21:11:30 -05004676 free_extent_map(em);
4677 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004678}
4679
4680void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4681{
David Sterbaa8067e02011-04-21 00:34:43 +02004682 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004683}
4684
4685void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4686{
4687 struct extent_map *em;
4688
Chris Masond3977122009-01-05 21:25:51 -05004689 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004690 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004691 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4692 if (em)
4693 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004694 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004695 if (!em)
4696 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004697 /* once for us */
4698 free_extent_map(em);
4699 /* once for the tree */
4700 free_extent_map(em);
4701 }
4702}
4703
Stefan Behrens5d964052012-11-05 14:59:07 +01004704int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004705{
Stefan Behrens5d964052012-11-05 14:59:07 +01004706 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004707 struct extent_map *em;
4708 struct map_lookup *map;
4709 struct extent_map_tree *em_tree = &map_tree->map_tree;
4710 int ret;
4711
Chris Mason890871b2009-09-02 16:24:52 -04004712 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004713 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004714 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004715
Josef Bacikfb7669b2013-04-23 10:53:18 -04004716 /*
4717 * We could return errors for these cases, but that could get ugly and
4718 * we'd probably do the same thing which is just not do anything else
4719 * and exit, so return 1 so the callers don't try to use other copies.
4720 */
4721 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004722 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004723 logical+len);
4724 return 1;
4725 }
4726
4727 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004728 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004729 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004730 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004731 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004732 return 1;
4733 }
4734
Chris Masonf1885912008-04-09 16:28:12 -04004735 map = (struct map_lookup *)em->bdev;
4736 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4737 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004738 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4739 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004740 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4741 ret = 2;
4742 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4743 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004744 else
4745 ret = 1;
4746 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004747
4748 btrfs_dev_replace_lock(&fs_info->dev_replace);
4749 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4750 ret++;
4751 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4752
Chris Masonf1885912008-04-09 16:28:12 -04004753 return ret;
4754}
4755
David Woodhouse53b381b2013-01-29 18:40:14 -05004756unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4757 struct btrfs_mapping_tree *map_tree,
4758 u64 logical)
4759{
4760 struct extent_map *em;
4761 struct map_lookup *map;
4762 struct extent_map_tree *em_tree = &map_tree->map_tree;
4763 unsigned long len = root->sectorsize;
4764
4765 read_lock(&em_tree->lock);
4766 em = lookup_extent_mapping(em_tree, logical, len);
4767 read_unlock(&em_tree->lock);
4768 BUG_ON(!em);
4769
4770 BUG_ON(em->start > logical || em->start + em->len < logical);
4771 map = (struct map_lookup *)em->bdev;
4772 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4773 BTRFS_BLOCK_GROUP_RAID6)) {
4774 len = map->stripe_len * nr_data_stripes(map);
4775 }
4776 free_extent_map(em);
4777 return len;
4778}
4779
4780int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4781 u64 logical, u64 len, int mirror_num)
4782{
4783 struct extent_map *em;
4784 struct map_lookup *map;
4785 struct extent_map_tree *em_tree = &map_tree->map_tree;
4786 int ret = 0;
4787
4788 read_lock(&em_tree->lock);
4789 em = lookup_extent_mapping(em_tree, logical, len);
4790 read_unlock(&em_tree->lock);
4791 BUG_ON(!em);
4792
4793 BUG_ON(em->start > logical || em->start + em->len < logical);
4794 map = (struct map_lookup *)em->bdev;
4795 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4796 BTRFS_BLOCK_GROUP_RAID6))
4797 ret = 1;
4798 free_extent_map(em);
4799 return ret;
4800}
4801
Stefan Behrens30d98612012-11-06 14:52:18 +01004802static int find_live_mirror(struct btrfs_fs_info *fs_info,
4803 struct map_lookup *map, int first, int num,
4804 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004805{
4806 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004807 int tolerance;
4808 struct btrfs_device *srcdev;
4809
4810 if (dev_replace_is_ongoing &&
4811 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4812 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4813 srcdev = fs_info->dev_replace.srcdev;
4814 else
4815 srcdev = NULL;
4816
4817 /*
4818 * try to avoid the drive that is the source drive for a
4819 * dev-replace procedure, only choose it if no other non-missing
4820 * mirror is available
4821 */
4822 for (tolerance = 0; tolerance < 2; tolerance++) {
4823 if (map->stripes[optimal].dev->bdev &&
4824 (tolerance || map->stripes[optimal].dev != srcdev))
4825 return optimal;
4826 for (i = first; i < first + num; i++) {
4827 if (map->stripes[i].dev->bdev &&
4828 (tolerance || map->stripes[i].dev != srcdev))
4829 return i;
4830 }
Chris Masondfe25022008-05-13 13:46:40 -04004831 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004832
Chris Masondfe25022008-05-13 13:46:40 -04004833 /* we couldn't find one that doesn't fail. Just return something
4834 * and the io error handling code will clean up eventually
4835 */
4836 return optimal;
4837}
4838
David Woodhouse53b381b2013-01-29 18:40:14 -05004839static inline int parity_smaller(u64 a, u64 b)
4840{
4841 return a > b;
4842}
4843
4844/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4845static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4846{
4847 struct btrfs_bio_stripe s;
4848 int i;
4849 u64 l;
4850 int again = 1;
4851
4852 while (again) {
4853 again = 0;
4854 for (i = 0; i < bbio->num_stripes - 1; i++) {
4855 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4856 s = bbio->stripes[i];
4857 l = raid_map[i];
4858 bbio->stripes[i] = bbio->stripes[i+1];
4859 raid_map[i] = raid_map[i+1];
4860 bbio->stripes[i+1] = s;
4861 raid_map[i+1] = l;
4862 again = 1;
4863 }
4864 }
4865 }
4866}
4867
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004868static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004869 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004870 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004871 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004872{
4873 struct extent_map *em;
4874 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004875 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004876 struct extent_map_tree *em_tree = &map_tree->map_tree;
4877 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004878 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004879 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004880 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004881 u64 stripe_nr_orig;
4882 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004883 u64 stripe_len;
4884 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004885 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004886 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004887 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004888 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004889 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004890 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004891 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4892 int dev_replace_is_ongoing = 0;
4893 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004894 int patch_the_first_stripe_for_dev_replace = 0;
4895 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004896 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004897
Chris Mason890871b2009-09-02 16:24:52 -04004898 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004899 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004900 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004901
Chris Mason3b951512008-04-17 11:29:12 -04004902 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004903 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004904 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004905 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004906 }
Chris Mason0b86a832008-03-24 15:01:56 -04004907
Josef Bacik9bb91872013-04-19 23:45:33 -04004908 if (em->start > logical || em->start + em->len < logical) {
4909 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02004910 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04004911 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004912 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04004913 return -EINVAL;
4914 }
4915
Chris Mason0b86a832008-03-24 15:01:56 -04004916 map = (struct map_lookup *)em->bdev;
4917 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004918
David Woodhouse53b381b2013-01-29 18:40:14 -05004919 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004920 stripe_nr = offset;
4921 /*
4922 * stripe_nr counts the total number of stripes we have to stride
4923 * to get to this block
4924 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004925 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004926
David Woodhouse53b381b2013-01-29 18:40:14 -05004927 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004928 BUG_ON(offset < stripe_offset);
4929
4930 /* stripe_offset is the offset of this block in its stripe*/
4931 stripe_offset = offset - stripe_offset;
4932
David Woodhouse53b381b2013-01-29 18:40:14 -05004933 /* if we're here for raid56, we need to know the stripe aligned start */
4934 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4935 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4936 raid56_full_stripe_start = offset;
4937
4938 /* allow a write of a full stripe, but make sure we don't
4939 * allow straddling of stripes
4940 */
4941 do_div(raid56_full_stripe_start, full_stripe_len);
4942 raid56_full_stripe_start *= full_stripe_len;
4943 }
4944
4945 if (rw & REQ_DISCARD) {
4946 /* we don't discard raid56 yet */
4947 if (map->type &
4948 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4949 ret = -EOPNOTSUPP;
4950 goto out;
4951 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004952 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004953 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4954 u64 max_len;
4955 /* For writes to RAID[56], allow a full stripeset across all disks.
4956 For other RAID types and for RAID[56] reads, just allow a single
4957 stripe (on a single disk). */
4958 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4959 (rw & REQ_WRITE)) {
4960 max_len = stripe_len * nr_data_stripes(map) -
4961 (offset - raid56_full_stripe_start);
4962 } else {
4963 /* we limit the length of each bio to what fits in a stripe */
4964 max_len = stripe_len - stripe_offset;
4965 }
4966 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004967 } else {
4968 *length = em->len - offset;
4969 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004970
David Woodhouse53b381b2013-01-29 18:40:14 -05004971 /* This is for when we're called from btrfs_merge_bio_hook() and all
4972 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004973 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004974 goto out;
4975
Stefan Behrens472262f2012-11-06 14:43:46 +01004976 btrfs_dev_replace_lock(dev_replace);
4977 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4978 if (!dev_replace_is_ongoing)
4979 btrfs_dev_replace_unlock(dev_replace);
4980
Stefan Behrensad6d6202012-11-06 15:06:47 +01004981 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4982 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4983 dev_replace->tgtdev != NULL) {
4984 /*
4985 * in dev-replace case, for repair case (that's the only
4986 * case where the mirror is selected explicitly when
4987 * calling btrfs_map_block), blocks left of the left cursor
4988 * can also be read from the target drive.
4989 * For REQ_GET_READ_MIRRORS, the target drive is added as
4990 * the last one to the array of stripes. For READ, it also
4991 * needs to be supported using the same mirror number.
4992 * If the requested block is not left of the left cursor,
4993 * EIO is returned. This can happen because btrfs_num_copies()
4994 * returns one more in the dev-replace case.
4995 */
4996 u64 tmp_length = *length;
4997 struct btrfs_bio *tmp_bbio = NULL;
4998 int tmp_num_stripes;
4999 u64 srcdev_devid = dev_replace->srcdev->devid;
5000 int index_srcdev = 0;
5001 int found = 0;
5002 u64 physical_of_found = 0;
5003
5004 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05005005 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005006 if (ret) {
5007 WARN_ON(tmp_bbio != NULL);
5008 goto out;
5009 }
5010
5011 tmp_num_stripes = tmp_bbio->num_stripes;
5012 if (mirror_num > tmp_num_stripes) {
5013 /*
5014 * REQ_GET_READ_MIRRORS does not contain this
5015 * mirror, that means that the requested area
5016 * is not left of the left cursor
5017 */
5018 ret = -EIO;
5019 kfree(tmp_bbio);
5020 goto out;
5021 }
5022
5023 /*
5024 * process the rest of the function using the mirror_num
5025 * of the source drive. Therefore look it up first.
5026 * At the end, patch the device pointer to the one of the
5027 * target drive.
5028 */
5029 for (i = 0; i < tmp_num_stripes; i++) {
5030 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
5031 /*
5032 * In case of DUP, in order to keep it
5033 * simple, only add the mirror with the
5034 * lowest physical address
5035 */
5036 if (found &&
5037 physical_of_found <=
5038 tmp_bbio->stripes[i].physical)
5039 continue;
5040 index_srcdev = i;
5041 found = 1;
5042 physical_of_found =
5043 tmp_bbio->stripes[i].physical;
5044 }
5045 }
5046
5047 if (found) {
5048 mirror_num = index_srcdev + 1;
5049 patch_the_first_stripe_for_dev_replace = 1;
5050 physical_to_patch_in_first_stripe = physical_of_found;
5051 } else {
5052 WARN_ON(1);
5053 ret = -EIO;
5054 kfree(tmp_bbio);
5055 goto out;
5056 }
5057
5058 kfree(tmp_bbio);
5059 } else if (mirror_num > map->num_stripes) {
5060 mirror_num = 0;
5061 }
5062
Chris Masonf2d8d742008-04-21 10:03:05 -04005063 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005064 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005065 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005066 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005067 do_div(stripe_nr_end, map->stripe_len);
5068 stripe_end_offset = stripe_nr_end * map->stripe_len -
5069 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005070
Li Dongyangfce3bb92011-03-24 10:24:26 +00005071 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5072 if (rw & REQ_DISCARD)
5073 num_stripes = min_t(u64, map->num_stripes,
5074 stripe_nr_end - stripe_nr_orig);
5075 stripe_index = do_div(stripe_nr, map->num_stripes);
5076 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005077 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005078 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005079 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005080 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005081 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005082 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005083 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005084 current->pid % map->num_stripes,
5085 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005086 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005087 }
Chris Mason2fff7342008-04-29 14:12:09 -04005088
Chris Mason611f0e02008-04-03 16:29:03 -04005089 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005090 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005091 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005092 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005093 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005094 } else {
5095 mirror_num = 1;
5096 }
Chris Mason2fff7342008-04-29 14:12:09 -04005097
Chris Mason321aecc2008-04-16 10:49:51 -04005098 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5099 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005100
5101 stripe_index = do_div(stripe_nr, factor);
5102 stripe_index *= map->sub_stripes;
5103
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005104 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005105 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005106 else if (rw & REQ_DISCARD)
5107 num_stripes = min_t(u64, map->sub_stripes *
5108 (stripe_nr_end - stripe_nr_orig),
5109 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005110 else if (mirror_num)
5111 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005112 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005113 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005114 stripe_index = find_live_mirror(fs_info, map,
5115 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005116 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005117 current->pid % map->sub_stripes,
5118 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005119 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005120 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005121
5122 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5123 BTRFS_BLOCK_GROUP_RAID6)) {
5124 u64 tmp;
5125
5126 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
5127 && raid_map_ret) {
5128 int i, rot;
5129
5130 /* push stripe_nr back to the start of the full stripe */
5131 stripe_nr = raid56_full_stripe_start;
5132 do_div(stripe_nr, stripe_len);
5133
5134 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5135
5136 /* RAID[56] write or recovery. Return all stripes */
5137 num_stripes = map->num_stripes;
5138 max_errors = nr_parity_stripes(map);
5139
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05305140 raid_map = kmalloc_array(num_stripes, sizeof(u64),
David Woodhouse53b381b2013-01-29 18:40:14 -05005141 GFP_NOFS);
5142 if (!raid_map) {
5143 ret = -ENOMEM;
5144 goto out;
5145 }
5146
5147 /* Work out the disk rotation on this stripe-set */
5148 tmp = stripe_nr;
5149 rot = do_div(tmp, num_stripes);
5150
5151 /* Fill in the logical address of each stripe */
5152 tmp = stripe_nr * nr_data_stripes(map);
5153 for (i = 0; i < nr_data_stripes(map); i++)
5154 raid_map[(i+rot) % num_stripes] =
5155 em->start + (tmp + i) * map->stripe_len;
5156
5157 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5158 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5159 raid_map[(i+rot+1) % num_stripes] =
5160 RAID6_Q_STRIPE;
5161
5162 *length = map->stripe_len;
5163 stripe_index = 0;
5164 stripe_offset = 0;
5165 } else {
5166 /*
5167 * Mirror #0 or #1 means the original data block.
5168 * Mirror #2 is RAID5 parity block.
5169 * Mirror #3 is RAID6 Q block.
5170 */
5171 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5172 if (mirror_num > 1)
5173 stripe_index = nr_data_stripes(map) +
5174 mirror_num - 2;
5175
5176 /* We distribute the parity blocks across stripes */
5177 tmp = stripe_nr + stripe_index;
5178 stripe_index = do_div(tmp, map->num_stripes);
5179 }
Chris Mason8790d502008-04-03 16:29:03 -04005180 } else {
5181 /*
5182 * after this do_div call, stripe_nr is the number of stripes
5183 * on this device we have to walk to find the data, and
5184 * stripe_index is the number of our device in the stripe array
5185 */
5186 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005187 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005188 }
Chris Mason593060d2008-03-25 16:50:33 -04005189 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005190
Stefan Behrens472262f2012-11-06 14:43:46 +01005191 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005192 if (dev_replace_is_ongoing) {
5193 if (rw & (REQ_WRITE | REQ_DISCARD))
5194 num_alloc_stripes <<= 1;
5195 if (rw & REQ_GET_READ_MIRRORS)
5196 num_alloc_stripes++;
5197 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005198 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08005199 if (!bbio) {
Dave Joneseb2067f2013-07-30 13:42:17 -04005200 kfree(raid_map);
Li Zefande11cc12011-12-01 12:55:47 +08005201 ret = -ENOMEM;
5202 goto out;
5203 }
5204 atomic_set(&bbio->error, 0);
5205
Li Dongyangfce3bb92011-03-24 10:24:26 +00005206 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08005207 int factor = 0;
5208 int sub_stripes = 0;
5209 u64 stripes_per_dev = 0;
5210 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005211 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005212
5213 if (map->type &
5214 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5215 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5216 sub_stripes = 1;
5217 else
5218 sub_stripes = map->sub_stripes;
5219
5220 factor = map->num_stripes / sub_stripes;
5221 stripes_per_dev = div_u64_rem(stripe_nr_end -
5222 stripe_nr_orig,
5223 factor,
5224 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005225 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5226 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005227 }
5228
Li Dongyangfce3bb92011-03-24 10:24:26 +00005229 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005230 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005231 map->stripes[stripe_index].physical +
5232 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005233 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005234
Li Zefanec9ef7a2011-12-01 14:06:42 +08005235 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5236 BTRFS_BLOCK_GROUP_RAID10)) {
5237 bbio->stripes[i].length = stripes_per_dev *
5238 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005239
Li Zefanec9ef7a2011-12-01 14:06:42 +08005240 if (i / sub_stripes < remaining_stripes)
5241 bbio->stripes[i].length +=
5242 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005243
5244 /*
5245 * Special for the first stripe and
5246 * the last stripe:
5247 *
5248 * |-------|...|-------|
5249 * |----------|
5250 * off end_off
5251 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005252 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005253 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005254 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005255
5256 if (stripe_index >= last_stripe &&
5257 stripe_index <= (last_stripe +
5258 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005259 bbio->stripes[i].length -=
5260 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005261
Li Zefanec9ef7a2011-12-01 14:06:42 +08005262 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005263 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005264 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005265 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005266
5267 stripe_index++;
5268 if (stripe_index == map->num_stripes) {
5269 /* This could only happen for RAID0/10 */
5270 stripe_index = 0;
5271 stripe_nr++;
5272 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005273 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005274 } else {
5275 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005276 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005277 map->stripes[stripe_index].physical +
5278 stripe_offset +
5279 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005280 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005281 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005282 stripe_index++;
5283 }
Chris Mason593060d2008-03-25 16:50:33 -04005284 }
Li Zefande11cc12011-12-01 12:55:47 +08005285
Miao Xied20983b2014-07-03 18:22:13 +08005286 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5287 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005288
Stefan Behrens472262f2012-11-06 14:43:46 +01005289 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5290 dev_replace->tgtdev != NULL) {
5291 int index_where_to_add;
5292 u64 srcdev_devid = dev_replace->srcdev->devid;
5293
5294 /*
5295 * duplicate the write operations while the dev replace
5296 * procedure is running. Since the copying of the old disk
5297 * to the new disk takes place at run time while the
5298 * filesystem is mounted writable, the regular write
5299 * operations to the old disk have to be duplicated to go
5300 * to the new disk as well.
5301 * Note that device->missing is handled by the caller, and
5302 * that the write to the old disk is already set up in the
5303 * stripes array.
5304 */
5305 index_where_to_add = num_stripes;
5306 for (i = 0; i < num_stripes; i++) {
5307 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5308 /* write to new disk, too */
5309 struct btrfs_bio_stripe *new =
5310 bbio->stripes + index_where_to_add;
5311 struct btrfs_bio_stripe *old =
5312 bbio->stripes + i;
5313
5314 new->physical = old->physical;
5315 new->length = old->length;
5316 new->dev = dev_replace->tgtdev;
5317 index_where_to_add++;
5318 max_errors++;
5319 }
5320 }
5321 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005322 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5323 dev_replace->tgtdev != NULL) {
5324 u64 srcdev_devid = dev_replace->srcdev->devid;
5325 int index_srcdev = 0;
5326 int found = 0;
5327 u64 physical_of_found = 0;
5328
5329 /*
5330 * During the dev-replace procedure, the target drive can
5331 * also be used to read data in case it is needed to repair
5332 * a corrupt block elsewhere. This is possible if the
5333 * requested area is left of the left cursor. In this area,
5334 * the target drive is a full copy of the source drive.
5335 */
5336 for (i = 0; i < num_stripes; i++) {
5337 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5338 /*
5339 * In case of DUP, in order to keep it
5340 * simple, only add the mirror with the
5341 * lowest physical address
5342 */
5343 if (found &&
5344 physical_of_found <=
5345 bbio->stripes[i].physical)
5346 continue;
5347 index_srcdev = i;
5348 found = 1;
5349 physical_of_found = bbio->stripes[i].physical;
5350 }
5351 }
5352 if (found) {
5353 u64 length = map->stripe_len;
5354
5355 if (physical_of_found + length <=
5356 dev_replace->cursor_left) {
5357 struct btrfs_bio_stripe *tgtdev_stripe =
5358 bbio->stripes + num_stripes;
5359
5360 tgtdev_stripe->physical = physical_of_found;
5361 tgtdev_stripe->length =
5362 bbio->stripes[index_srcdev].length;
5363 tgtdev_stripe->dev = dev_replace->tgtdev;
5364
5365 num_stripes++;
5366 }
5367 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005368 }
5369
Li Zefande11cc12011-12-01 12:55:47 +08005370 *bbio_ret = bbio;
5371 bbio->num_stripes = num_stripes;
5372 bbio->max_errors = max_errors;
5373 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005374
5375 /*
5376 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5377 * mirror_num == num_stripes + 1 && dev_replace target drive is
5378 * available as a mirror
5379 */
5380 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5381 WARN_ON(num_stripes > 1);
5382 bbio->stripes[0].dev = dev_replace->tgtdev;
5383 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5384 bbio->mirror_num = map->num_stripes + 1;
5385 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005386 if (raid_map) {
5387 sort_parity_stripes(bbio, raid_map);
5388 *raid_map_ret = raid_map;
5389 }
Chris Masoncea9e442008-04-09 16:28:12 -04005390out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005391 if (dev_replace_is_ongoing)
5392 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005393 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005394 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005395}
5396
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005397int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005398 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005399 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005400{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005401 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05005402 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04005403}
5404
Yan Zhenga512bbf2008-12-08 16:46:26 -05005405int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5406 u64 chunk_start, u64 physical, u64 devid,
5407 u64 **logical, int *naddrs, int *stripe_len)
5408{
5409 struct extent_map_tree *em_tree = &map_tree->map_tree;
5410 struct extent_map *em;
5411 struct map_lookup *map;
5412 u64 *buf;
5413 u64 bytenr;
5414 u64 length;
5415 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005416 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005417 int i, j, nr = 0;
5418
Chris Mason890871b2009-09-02 16:24:52 -04005419 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005420 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005421 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005422
Josef Bacik835d9742013-03-19 12:13:25 -04005423 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005424 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005425 chunk_start);
5426 return -EIO;
5427 }
5428
5429 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005430 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005431 em->start, chunk_start);
5432 free_extent_map(em);
5433 return -EIO;
5434 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005435 map = (struct map_lookup *)em->bdev;
5436
5437 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005438 rmap_len = map->stripe_len;
5439
Yan Zhenga512bbf2008-12-08 16:46:26 -05005440 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5441 do_div(length, map->num_stripes / map->sub_stripes);
5442 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5443 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05005444 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5445 BTRFS_BLOCK_GROUP_RAID6)) {
5446 do_div(length, nr_data_stripes(map));
5447 rmap_len = map->stripe_len * nr_data_stripes(map);
5448 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005449
5450 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005451 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005452
5453 for (i = 0; i < map->num_stripes; i++) {
5454 if (devid && map->stripes[i].dev->devid != devid)
5455 continue;
5456 if (map->stripes[i].physical > physical ||
5457 map->stripes[i].physical + length <= physical)
5458 continue;
5459
5460 stripe_nr = physical - map->stripes[i].physical;
5461 do_div(stripe_nr, map->stripe_len);
5462
5463 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5464 stripe_nr = stripe_nr * map->num_stripes + i;
5465 do_div(stripe_nr, map->sub_stripes);
5466 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5467 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005468 } /* else if RAID[56], multiply by nr_data_stripes().
5469 * Alternatively, just use rmap_len below instead of
5470 * map->stripe_len */
5471
5472 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005473 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005474 for (j = 0; j < nr; j++) {
5475 if (buf[j] == bytenr)
5476 break;
5477 }
Chris Mason934d3752008-12-08 16:43:10 -05005478 if (j == nr) {
5479 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005480 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005481 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005482 }
5483
Yan Zhenga512bbf2008-12-08 16:46:26 -05005484 *logical = buf;
5485 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005486 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005487
5488 free_extent_map(em);
5489 return 0;
5490}
5491
Miao Xie8408c712014-06-19 10:42:55 +08005492static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5493{
5494 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5495 bio_endio_nodec(bio, err);
5496 else
5497 bio_endio(bio, err);
5498 kfree(bbio);
5499}
5500
Jan Schmidta1d3c472011-08-04 17:15:33 +02005501static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005502{
Chris Mason9be33952013-05-17 18:30:14 -04005503 struct btrfs_bio *bbio = bio->bi_private;
Miao Xiec404e0d2014-01-30 16:46:55 +08005504 struct btrfs_device *dev = bbio->stripes[0].dev;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005505 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005506
Stefan Behrens442a4f62012-05-25 16:06:08 +02005507 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005508 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005509 if (err == -EIO || err == -EREMOTEIO) {
5510 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005511 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005512
5513 BUG_ON(stripe_index >= bbio->num_stripes);
5514 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005515 if (dev->bdev) {
5516 if (bio->bi_rw & WRITE)
5517 btrfs_dev_stat_inc(dev,
5518 BTRFS_DEV_STAT_WRITE_ERRS);
5519 else
5520 btrfs_dev_stat_inc(dev,
5521 BTRFS_DEV_STAT_READ_ERRS);
5522 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5523 btrfs_dev_stat_inc(dev,
5524 BTRFS_DEV_STAT_FLUSH_ERRS);
5525 btrfs_dev_stat_print_on_error(dev);
5526 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005527 }
5528 }
Chris Mason8790d502008-04-03 16:29:03 -04005529
Jan Schmidta1d3c472011-08-04 17:15:33 +02005530 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005531 is_orig_bio = 1;
5532
Miao Xiec404e0d2014-01-30 16:46:55 +08005533 btrfs_bio_counter_dec(bbio->fs_info);
5534
Jan Schmidta1d3c472011-08-04 17:15:33 +02005535 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005536 if (!is_orig_bio) {
5537 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005538 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005539 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005540
Jan Schmidta1d3c472011-08-04 17:15:33 +02005541 bio->bi_private = bbio->private;
5542 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005543 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005544 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005545 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005546 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005547 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005548 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005549 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005550 /*
5551 * this bio is actually up to date, we didn't
5552 * go over the max number of errors
5553 */
5554 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005555 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005556 }
Miao Xiec55f1392014-06-19 10:42:54 +08005557
Miao Xie8408c712014-06-19 10:42:55 +08005558 btrfs_end_bbio(bbio, bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005559 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005560 bio_put(bio);
5561 }
Chris Mason8790d502008-04-03 16:29:03 -04005562}
5563
Chris Mason8b712842008-06-11 16:50:36 -04005564/*
5565 * see run_scheduled_bios for a description of why bios are collected for
5566 * async submit.
5567 *
5568 * This will add one bio to the pending list for a device and make sure
5569 * the work struct is scheduled.
5570 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005571static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5572 struct btrfs_device *device,
5573 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005574{
5575 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005576 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005577
David Woodhouse53b381b2013-01-29 18:40:14 -05005578 if (device->missing || !device->bdev) {
5579 bio_endio(bio, -EIO);
5580 return;
5581 }
5582
Chris Mason8b712842008-06-11 16:50:36 -04005583 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005584 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005585 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005586 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005587 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005588 return;
Chris Mason8b712842008-06-11 16:50:36 -04005589 }
5590
5591 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005592 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005593 * higher layers. Otherwise, the async bio makes it appear we have
5594 * made progress against dirty pages when we've really just put it
5595 * on a queue for later
5596 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005597 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005598 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005599 bio->bi_next = NULL;
5600 bio->bi_rw |= rw;
5601
5602 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005603 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005604 pending_bios = &device->pending_sync_bios;
5605 else
5606 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005607
Chris Masonffbd5172009-04-20 15:50:09 -04005608 if (pending_bios->tail)
5609 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005610
Chris Masonffbd5172009-04-20 15:50:09 -04005611 pending_bios->tail = bio;
5612 if (!pending_bios->head)
5613 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005614 if (device->running_pending)
5615 should_queue = 0;
5616
5617 spin_unlock(&device->io_lock);
5618
5619 if (should_queue)
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +08005620 btrfs_queue_work(root->fs_info->submit_workers,
5621 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005622}
5623
Josef Bacikde1ee922012-10-19 16:50:56 -04005624static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5625 sector_t sector)
5626{
5627 struct bio_vec *prev;
5628 struct request_queue *q = bdev_get_queue(bdev);
Akinobu Mita475bf362013-11-18 22:13:18 +09005629 unsigned int max_sectors = queue_max_sectors(q);
Josef Bacikde1ee922012-10-19 16:50:56 -04005630 struct bvec_merge_data bvm = {
5631 .bi_bdev = bdev,
5632 .bi_sector = sector,
5633 .bi_rw = bio->bi_rw,
5634 };
5635
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305636 if (WARN_ON(bio->bi_vcnt == 0))
Josef Bacikde1ee922012-10-19 16:50:56 -04005637 return 1;
Josef Bacikde1ee922012-10-19 16:50:56 -04005638
5639 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005640 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005641 return 0;
5642
5643 if (!q->merge_bvec_fn)
5644 return 1;
5645
Kent Overstreet4f024f32013-10-11 15:44:27 -07005646 bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len;
Josef Bacikde1ee922012-10-19 16:50:56 -04005647 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5648 return 0;
5649 return 1;
5650}
5651
5652static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5653 struct bio *bio, u64 physical, int dev_nr,
5654 int rw, int async)
5655{
5656 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5657
5658 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005659 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005660 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005661 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005662#ifdef DEBUG
5663 {
5664 struct rcu_string *name;
5665
5666 rcu_read_lock();
5667 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005668 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005669 "(%s id %llu), size=%u\n", rw,
5670 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5671 name->str, dev->devid, bio->bi_size);
5672 rcu_read_unlock();
5673 }
5674#endif
5675 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005676
5677 btrfs_bio_counter_inc_noblocked(root->fs_info);
5678
Josef Bacikde1ee922012-10-19 16:50:56 -04005679 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005680 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005681 else
5682 btrfsic_submit_bio(rw, bio);
5683}
5684
5685static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5686 struct bio *first_bio, struct btrfs_device *dev,
5687 int dev_nr, int rw, int async)
5688{
5689 struct bio_vec *bvec = first_bio->bi_io_vec;
5690 struct bio *bio;
5691 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5692 u64 physical = bbio->stripes[dev_nr].physical;
5693
5694again:
5695 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5696 if (!bio)
5697 return -ENOMEM;
5698
5699 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5700 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5701 bvec->bv_offset) < bvec->bv_len) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07005702 u64 len = bio->bi_iter.bi_size;
Josef Bacikde1ee922012-10-19 16:50:56 -04005703
5704 atomic_inc(&bbio->stripes_pending);
5705 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5706 rw, async);
5707 physical += len;
5708 goto again;
5709 }
5710 bvec++;
5711 }
5712
5713 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5714 return 0;
5715}
5716
5717static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5718{
5719 atomic_inc(&bbio->error);
5720 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005721 /* Shoud be the original bio. */
5722 WARN_ON(bio != bbio->orig_bio);
5723
Josef Bacikde1ee922012-10-19 16:50:56 -04005724 bio->bi_private = bbio->private;
5725 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005726 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005727 bio->bi_iter.bi_sector = logical >> 9;
Miao Xie8408c712014-06-19 10:42:55 +08005728
5729 btrfs_end_bbio(bbio, bio, -EIO);
Josef Bacikde1ee922012-10-19 16:50:56 -04005730 }
5731}
5732
Chris Masonf1885912008-04-09 16:28:12 -04005733int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005734 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005735{
Chris Mason0b86a832008-03-24 15:01:56 -04005736 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005737 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005738 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005739 u64 length = 0;
5740 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005741 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005742 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005743 int dev_nr = 0;
5744 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005745 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005746
Kent Overstreet4f024f32013-10-11 15:44:27 -07005747 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005748 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005749
Miao Xiec404e0d2014-01-30 16:46:55 +08005750 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005751 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5752 mirror_num, &raid_map);
Miao Xiec404e0d2014-01-30 16:46:55 +08005753 if (ret) {
5754 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005755 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005756 }
Chris Masoncea9e442008-04-09 16:28:12 -04005757
Jan Schmidta1d3c472011-08-04 17:15:33 +02005758 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005759 bbio->orig_bio = first_bio;
5760 bbio->private = first_bio->bi_private;
5761 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005762 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005763 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5764
5765 if (raid_map) {
5766 /* In this case, map_length has been set to the length of
5767 a single stripe; not the whole write */
5768 if (rw & WRITE) {
Miao Xiec404e0d2014-01-30 16:46:55 +08005769 ret = raid56_parity_write(root, bio, bbio,
5770 raid_map, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005771 } else {
Miao Xiec404e0d2014-01-30 16:46:55 +08005772 ret = raid56_parity_recover(root, bio, bbio,
5773 raid_map, map_length,
5774 mirror_num);
David Woodhouse53b381b2013-01-29 18:40:14 -05005775 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005776 /*
5777 * FIXME, replace dosen't support raid56 yet, please fix
5778 * it in the future.
5779 */
5780 btrfs_bio_counter_dec(root->fs_info);
5781 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005782 }
5783
Chris Masoncea9e442008-04-09 16:28:12 -04005784 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005785 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005786 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005787 BUG();
5788 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005789
Chris Masond3977122009-01-05 21:25:51 -05005790 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005791 dev = bbio->stripes[dev_nr].dev;
5792 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5793 bbio_error(bbio, first_bio, logical);
5794 dev_nr++;
5795 continue;
5796 }
5797
5798 /*
5799 * Check and see if we're ok with this bio based on it's size
5800 * and offset with the given device.
5801 */
5802 if (!bio_size_ok(dev->bdev, first_bio,
5803 bbio->stripes[dev_nr].physical >> 9)) {
5804 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5805 dev_nr, rw, async_submit);
5806 BUG_ON(ret);
5807 dev_nr++;
5808 continue;
5809 }
5810
Jan Schmidta1d3c472011-08-04 17:15:33 +02005811 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005812 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005813 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005814 } else {
5815 bio = first_bio;
Miao Xiec55f1392014-06-19 10:42:54 +08005816 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
Chris Mason8790d502008-04-03 16:29:03 -04005817 }
Josef Bacik606686e2012-06-04 14:03:51 -04005818
Josef Bacikde1ee922012-10-19 16:50:56 -04005819 submit_stripe_bio(root, bbio, bio,
5820 bbio->stripes[dev_nr].physical, dev_nr, rw,
5821 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005822 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005823 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005824 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04005825 return 0;
5826}
5827
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005828struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005829 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005830{
Yan Zheng2b820322008-11-17 21:11:30 -05005831 struct btrfs_device *device;
5832 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005833
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005834 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005835 while (cur_devices) {
5836 if (!fsid ||
5837 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5838 device = __find_device(&cur_devices->devices,
5839 devid, uuid);
5840 if (device)
5841 return device;
5842 }
5843 cur_devices = cur_devices->seed;
5844 }
5845 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005846}
5847
Chris Masondfe25022008-05-13 13:46:40 -04005848static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5849 u64 devid, u8 *dev_uuid)
5850{
5851 struct btrfs_device *device;
5852 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5853
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005854 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5855 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005856 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005857
5858 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005859 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04005860 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005861
5862 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05005863 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005864
Chris Masondfe25022008-05-13 13:46:40 -04005865 return device;
5866}
5867
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005868/**
5869 * btrfs_alloc_device - allocate struct btrfs_device
5870 * @fs_info: used only for generating a new devid, can be NULL if
5871 * devid is provided (i.e. @devid != NULL).
5872 * @devid: a pointer to devid for this device. If NULL a new devid
5873 * is generated.
5874 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5875 * is generated.
5876 *
5877 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5878 * on error. Returned struct is not linked onto any lists and can be
5879 * destroyed with kfree() right away.
5880 */
5881struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5882 const u64 *devid,
5883 const u8 *uuid)
5884{
5885 struct btrfs_device *dev;
5886 u64 tmp;
5887
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305888 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005889 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005890
5891 dev = __alloc_device();
5892 if (IS_ERR(dev))
5893 return dev;
5894
5895 if (devid)
5896 tmp = *devid;
5897 else {
5898 int ret;
5899
5900 ret = find_next_devid(fs_info, &tmp);
5901 if (ret) {
5902 kfree(dev);
5903 return ERR_PTR(ret);
5904 }
5905 }
5906 dev->devid = tmp;
5907
5908 if (uuid)
5909 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5910 else
5911 generate_random_uuid(dev->uuid);
5912
Liu Bo9e0af232014-08-15 23:36:53 +08005913 btrfs_init_work(&dev->work, btrfs_submit_helper,
5914 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005915
5916 return dev;
5917}
5918
Chris Mason0b86a832008-03-24 15:01:56 -04005919static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5920 struct extent_buffer *leaf,
5921 struct btrfs_chunk *chunk)
5922{
5923 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5924 struct map_lookup *map;
5925 struct extent_map *em;
5926 u64 logical;
5927 u64 length;
5928 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005929 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005930 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005931 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005932 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005933
Chris Masone17cade2008-04-15 15:41:47 -04005934 logical = key->offset;
5935 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005936
Chris Mason890871b2009-09-02 16:24:52 -04005937 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005938 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005939 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005940
5941 /* already mapped? */
5942 if (em && em->start <= logical && em->start + em->len > logical) {
5943 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005944 return 0;
5945 } else if (em) {
5946 free_extent_map(em);
5947 }
Chris Mason0b86a832008-03-24 15:01:56 -04005948
David Sterba172ddd62011-04-21 00:48:27 +02005949 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005950 if (!em)
5951 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005952 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5953 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005954 if (!map) {
5955 free_extent_map(em);
5956 return -ENOMEM;
5957 }
5958
Wang Shilong298a8f92014-06-19 10:42:52 +08005959 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04005960 em->bdev = (struct block_device *)map;
5961 em->start = logical;
5962 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005963 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005964 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005965 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005966
Chris Mason593060d2008-03-25 16:50:33 -04005967 map->num_stripes = num_stripes;
5968 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5969 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5970 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5971 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5972 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005973 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005974 for (i = 0; i < num_stripes; i++) {
5975 map->stripes[i].physical =
5976 btrfs_stripe_offset_nr(leaf, chunk, i);
5977 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005978 read_extent_buffer(leaf, uuid, (unsigned long)
5979 btrfs_stripe_dev_uuid_nr(chunk, i),
5980 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005981 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5982 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005983 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005984 free_extent_map(em);
5985 return -EIO;
5986 }
Chris Masondfe25022008-05-13 13:46:40 -04005987 if (!map->stripes[i].dev) {
5988 map->stripes[i].dev =
5989 add_missing_dev(root, devid, uuid);
5990 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04005991 free_extent_map(em);
5992 return -EIO;
5993 }
5994 }
5995 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005996 }
5997
Chris Mason890871b2009-09-02 16:24:52 -04005998 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005999 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006000 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006001 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006002 free_extent_map(em);
6003
6004 return 0;
6005}
6006
Jeff Mahoney143bede2012-03-01 14:56:26 +01006007static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006008 struct btrfs_dev_item *dev_item,
6009 struct btrfs_device *device)
6010{
6011 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006012
6013 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006014 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6015 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006016 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006017 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006018 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006019 device->type = btrfs_device_type(leaf, dev_item);
6020 device->io_align = btrfs_device_io_align(leaf, dev_item);
6021 device->io_width = btrfs_device_io_width(leaf, dev_item);
6022 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006023 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006024 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006025
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006026 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006027 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006028}
6029
Yan Zheng2b820322008-11-17 21:11:30 -05006030static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
6031{
6032 struct btrfs_fs_devices *fs_devices;
6033 int ret;
6034
Li Zefanb367e472011-12-07 11:38:24 +08006035 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05006036
6037 fs_devices = root->fs_info->fs_devices->seed;
6038 while (fs_devices) {
6039 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
6040 ret = 0;
6041 goto out;
6042 }
6043 fs_devices = fs_devices->seed;
6044 }
6045
6046 fs_devices = find_fsid(fsid);
6047 if (!fs_devices) {
6048 ret = -ENOENT;
6049 goto out;
6050 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006051
6052 fs_devices = clone_fs_devices(fs_devices);
6053 if (IS_ERR(fs_devices)) {
6054 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006055 goto out;
6056 }
6057
Christoph Hellwig97288f22008-12-02 06:36:09 -05006058 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006059 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006060 if (ret) {
6061 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006062 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006063 }
Yan Zheng2b820322008-11-17 21:11:30 -05006064
6065 if (!fs_devices->seeding) {
6066 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006067 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006068 ret = -EINVAL;
6069 goto out;
6070 }
6071
6072 fs_devices->seed = root->fs_info->fs_devices->seed;
6073 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006074out:
Yan Zheng2b820322008-11-17 21:11:30 -05006075 return ret;
6076}
6077
Chris Mason0d81ba52008-03-24 15:02:07 -04006078static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006079 struct extent_buffer *leaf,
6080 struct btrfs_dev_item *dev_item)
6081{
6082 struct btrfs_device *device;
6083 u64 devid;
6084 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006085 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006086 u8 dev_uuid[BTRFS_UUID_SIZE];
6087
Chris Mason0b86a832008-03-24 15:01:56 -04006088 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006089 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006090 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006091 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006092 BTRFS_UUID_SIZE);
6093
6094 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
6095 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05006096 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006097 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006098 }
6099
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006100 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006101 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006102 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006103 return -EIO;
6104
6105 if (!device) {
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006106 btrfs_warn(root->fs_info, "devid %llu missing", devid);
Yan Zheng2b820322008-11-17 21:11:30 -05006107 device = add_missing_dev(root, devid, dev_uuid);
6108 if (!device)
6109 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05006110 } else if (!device->missing) {
6111 /*
6112 * this happens when a device that was properly setup
6113 * in the device info lists suddenly goes bad.
6114 * device->bdev is NULL, and so we have to set
6115 * device->missing to one here
6116 */
6117 root->fs_info->fs_devices->missing_devices++;
6118 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006119 }
6120 }
6121
6122 if (device->fs_devices != root->fs_info->fs_devices) {
6123 BUG_ON(device->writeable);
6124 if (device->generation !=
6125 btrfs_device_generation(leaf, dev_item))
6126 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006127 }
Chris Mason0b86a832008-03-24 15:01:56 -04006128
6129 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006130 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006131 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006132 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006133 spin_lock(&root->fs_info->free_chunk_lock);
6134 root->fs_info->free_chunk_space += device->total_bytes -
6135 device->bytes_used;
6136 spin_unlock(&root->fs_info->free_chunk_lock);
6137 }
Chris Mason0b86a832008-03-24 15:01:56 -04006138 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006139 return ret;
6140}
6141
Yan Zhenge4404d62008-12-12 10:03:26 -05006142int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006143{
David Sterba6c417612011-04-13 15:41:04 +02006144 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006145 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006146 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006147 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04006148 u8 *ptr;
6149 unsigned long sb_ptr;
6150 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006151 u32 num_stripes;
6152 u32 array_size;
6153 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006154 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04006155 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006156
Yan Zhenge4404d62008-12-12 10:03:26 -05006157 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04006158 BTRFS_SUPER_INFO_SIZE);
6159 if (!sb)
6160 return -ENOMEM;
6161 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006162 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006163 /*
6164 * The sb extent buffer is artifical and just used to read the system array.
6165 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6166 * pages up-to-date when the page is larger: extent does not cover the
6167 * whole page and consequently check_page_uptodate does not find all
6168 * the page's extents up-to-date (the hole beyond sb),
6169 * write_extent_buffer then triggers a WARN_ON.
6170 *
6171 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6172 * but sb spans only this function. Add an explicit SetPageUptodate call
6173 * to silence the warning eg. on PowerPC 64.
6174 */
6175 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006176 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006177
Chris Masona061fc82008-05-07 11:43:44 -04006178 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006179 array_size = btrfs_super_sys_array_size(super_copy);
6180
Chris Mason0b86a832008-03-24 15:01:56 -04006181 ptr = super_copy->sys_chunk_array;
6182 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
6183 cur = 0;
6184
6185 while (cur < array_size) {
6186 disk_key = (struct btrfs_disk_key *)ptr;
6187 btrfs_disk_key_to_cpu(&key, disk_key);
6188
Chris Masona061fc82008-05-07 11:43:44 -04006189 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006190 sb_ptr += len;
6191 cur += len;
6192
Chris Mason0d81ba52008-03-24 15:02:07 -04006193 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04006194 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04006195 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006196 if (ret)
6197 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006198 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6199 len = btrfs_chunk_item_size(num_stripes);
6200 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006201 ret = -EIO;
6202 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006203 }
6204 ptr += len;
6205 sb_ptr += len;
6206 cur += len;
6207 }
Chris Masona061fc82008-05-07 11:43:44 -04006208 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006209 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006210}
6211
6212int btrfs_read_chunk_tree(struct btrfs_root *root)
6213{
6214 struct btrfs_path *path;
6215 struct extent_buffer *leaf;
6216 struct btrfs_key key;
6217 struct btrfs_key found_key;
6218 int ret;
6219 int slot;
6220
6221 root = root->fs_info->chunk_root;
6222
6223 path = btrfs_alloc_path();
6224 if (!path)
6225 return -ENOMEM;
6226
Li Zefanb367e472011-12-07 11:38:24 +08006227 mutex_lock(&uuid_mutex);
6228 lock_chunks(root);
6229
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006230 /*
6231 * Read all device items, and then all the chunk items. All
6232 * device items are found before any chunk item (their object id
6233 * is smaller than the lowest possible object id for a chunk
6234 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006235 */
6236 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6237 key.offset = 0;
6238 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006239 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006240 if (ret < 0)
6241 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006242 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006243 leaf = path->nodes[0];
6244 slot = path->slots[0];
6245 if (slot >= btrfs_header_nritems(leaf)) {
6246 ret = btrfs_next_leaf(root, path);
6247 if (ret == 0)
6248 continue;
6249 if (ret < 0)
6250 goto error;
6251 break;
6252 }
6253 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006254 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6255 struct btrfs_dev_item *dev_item;
6256 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006257 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006258 ret = read_one_dev(root, leaf, dev_item);
6259 if (ret)
6260 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006261 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6262 struct btrfs_chunk *chunk;
6263 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6264 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006265 if (ret)
6266 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006267 }
6268 path->slots[0]++;
6269 }
Chris Mason0b86a832008-03-24 15:01:56 -04006270 ret = 0;
6271error:
Li Zefanb367e472011-12-07 11:38:24 +08006272 unlock_chunks(root);
6273 mutex_unlock(&uuid_mutex);
6274
Yan Zheng2b820322008-11-17 21:11:30 -05006275 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006276 return ret;
6277}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006278
Miao Xiecb517ea2013-05-15 07:48:19 +00006279void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6280{
6281 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6282 struct btrfs_device *device;
6283
Liu Bo29cc83f2014-05-11 23:14:59 +08006284 while (fs_devices) {
6285 mutex_lock(&fs_devices->device_list_mutex);
6286 list_for_each_entry(device, &fs_devices->devices, dev_list)
6287 device->dev_root = fs_info->dev_root;
6288 mutex_unlock(&fs_devices->device_list_mutex);
6289
6290 fs_devices = fs_devices->seed;
6291 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006292}
6293
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006294static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6295{
6296 int i;
6297
6298 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6299 btrfs_dev_stat_reset(dev, i);
6300}
6301
6302int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6303{
6304 struct btrfs_key key;
6305 struct btrfs_key found_key;
6306 struct btrfs_root *dev_root = fs_info->dev_root;
6307 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6308 struct extent_buffer *eb;
6309 int slot;
6310 int ret = 0;
6311 struct btrfs_device *device;
6312 struct btrfs_path *path = NULL;
6313 int i;
6314
6315 path = btrfs_alloc_path();
6316 if (!path) {
6317 ret = -ENOMEM;
6318 goto out;
6319 }
6320
6321 mutex_lock(&fs_devices->device_list_mutex);
6322 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6323 int item_size;
6324 struct btrfs_dev_stats_item *ptr;
6325
6326 key.objectid = 0;
6327 key.type = BTRFS_DEV_STATS_KEY;
6328 key.offset = device->devid;
6329 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6330 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006331 __btrfs_reset_dev_stats(device);
6332 device->dev_stats_valid = 1;
6333 btrfs_release_path(path);
6334 continue;
6335 }
6336 slot = path->slots[0];
6337 eb = path->nodes[0];
6338 btrfs_item_key_to_cpu(eb, &found_key, slot);
6339 item_size = btrfs_item_size_nr(eb, slot);
6340
6341 ptr = btrfs_item_ptr(eb, slot,
6342 struct btrfs_dev_stats_item);
6343
6344 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6345 if (item_size >= (1 + i) * sizeof(__le64))
6346 btrfs_dev_stat_set(device, i,
6347 btrfs_dev_stats_value(eb, ptr, i));
6348 else
6349 btrfs_dev_stat_reset(device, i);
6350 }
6351
6352 device->dev_stats_valid = 1;
6353 btrfs_dev_stat_print_on_load(device);
6354 btrfs_release_path(path);
6355 }
6356 mutex_unlock(&fs_devices->device_list_mutex);
6357
6358out:
6359 btrfs_free_path(path);
6360 return ret < 0 ? ret : 0;
6361}
6362
6363static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6364 struct btrfs_root *dev_root,
6365 struct btrfs_device *device)
6366{
6367 struct btrfs_path *path;
6368 struct btrfs_key key;
6369 struct extent_buffer *eb;
6370 struct btrfs_dev_stats_item *ptr;
6371 int ret;
6372 int i;
6373
6374 key.objectid = 0;
6375 key.type = BTRFS_DEV_STATS_KEY;
6376 key.offset = device->devid;
6377
6378 path = btrfs_alloc_path();
6379 BUG_ON(!path);
6380 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6381 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006382 printk_in_rcu(KERN_WARNING "BTRFS: "
6383 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006384 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006385 goto out;
6386 }
6387
6388 if (ret == 0 &&
6389 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6390 /* need to delete old one and insert a new one */
6391 ret = btrfs_del_item(trans, dev_root, path);
6392 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006393 printk_in_rcu(KERN_WARNING "BTRFS: "
6394 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006395 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006396 goto out;
6397 }
6398 ret = 1;
6399 }
6400
6401 if (ret == 1) {
6402 /* need to insert a new item */
6403 btrfs_release_path(path);
6404 ret = btrfs_insert_empty_item(trans, dev_root, path,
6405 &key, sizeof(*ptr));
6406 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006407 printk_in_rcu(KERN_WARNING "BTRFS: "
6408 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006409 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006410 goto out;
6411 }
6412 }
6413
6414 eb = path->nodes[0];
6415 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6416 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6417 btrfs_set_dev_stats_value(eb, ptr, i,
6418 btrfs_dev_stat_read(device, i));
6419 btrfs_mark_buffer_dirty(eb);
6420
6421out:
6422 btrfs_free_path(path);
6423 return ret;
6424}
6425
6426/*
6427 * called from commit_transaction. Writes all changed device stats to disk.
6428 */
6429int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6430 struct btrfs_fs_info *fs_info)
6431{
6432 struct btrfs_root *dev_root = fs_info->dev_root;
6433 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6434 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006435 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006436 int ret = 0;
6437
6438 mutex_lock(&fs_devices->device_list_mutex);
6439 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006440 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006441 continue;
6442
Miao Xieaddc3fa2014-07-24 11:37:11 +08006443 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006444 ret = update_dev_stat_item(trans, dev_root, device);
6445 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006446 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006447 }
6448 mutex_unlock(&fs_devices->device_list_mutex);
6449
6450 return ret;
6451}
6452
Stefan Behrens442a4f62012-05-25 16:06:08 +02006453void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6454{
6455 btrfs_dev_stat_inc(dev, index);
6456 btrfs_dev_stat_print_on_error(dev);
6457}
6458
Eric Sandeen48a3b632013-04-25 20:41:01 +00006459static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006460{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006461 if (!dev->dev_stats_valid)
6462 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006463 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6464 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006465 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006466 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6467 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6468 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006469 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6470 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006471}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006472
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006473static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6474{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006475 int i;
6476
6477 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6478 if (btrfs_dev_stat_read(dev, i) != 0)
6479 break;
6480 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6481 return; /* all values == 0, suppress message */
6482
Frank Holtonefe120a2013-12-20 11:37:06 -05006483 printk_in_rcu(KERN_INFO "BTRFS: "
6484 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006485 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006486 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6487 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6488 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6489 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6490 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6491}
6492
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006493int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006494 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006495{
6496 struct btrfs_device *dev;
6497 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6498 int i;
6499
6500 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006501 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006502 mutex_unlock(&fs_devices->device_list_mutex);
6503
6504 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006505 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006506 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006507 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006508 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006509 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006510 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006511 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6512 if (stats->nr_items > i)
6513 stats->values[i] =
6514 btrfs_dev_stat_read_and_reset(dev, i);
6515 else
6516 btrfs_dev_stat_reset(dev, i);
6517 }
6518 } else {
6519 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6520 if (stats->nr_items > i)
6521 stats->values[i] = btrfs_dev_stat_read(dev, i);
6522 }
6523 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6524 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6525 return 0;
6526}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006527
6528int btrfs_scratch_superblock(struct btrfs_device *device)
6529{
6530 struct buffer_head *bh;
6531 struct btrfs_super_block *disk_super;
6532
6533 bh = btrfs_read_dev_super(device->bdev);
6534 if (!bh)
6535 return -EINVAL;
6536 disk_super = (struct btrfs_super_block *)bh->b_data;
6537
6538 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6539 set_buffer_dirty(bh);
6540 sync_dirty_buffer(bh);
6541 brelse(bh);
6542
6543 return 0;
6544}
Miao Xie935e5cc2014-09-03 21:35:33 +08006545
6546/*
6547 * Update the size of all devices, which is used for writing out the
6548 * super blocks.
6549 */
6550void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
6551{
6552 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6553 struct btrfs_device *curr, *next;
6554
6555 if (list_empty(&fs_devices->resized_devices))
6556 return;
6557
6558 mutex_lock(&fs_devices->device_list_mutex);
6559 lock_chunks(fs_info->dev_root);
6560 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
6561 resized_list) {
6562 list_del_init(&curr->resized_list);
6563 curr->commit_total_bytes = curr->disk_total_bytes;
6564 }
6565 unlock_chunks(fs_info->dev_root);
6566 mutex_unlock(&fs_devices->device_list_mutex);
6567}
Miao Xiece7213c2014-09-03 21:35:34 +08006568
6569/* Must be invoked during the transaction commit */
6570void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
6571 struct btrfs_transaction *transaction)
6572{
6573 struct extent_map *em;
6574 struct map_lookup *map;
6575 struct btrfs_device *dev;
6576 int i;
6577
6578 if (list_empty(&transaction->pending_chunks))
6579 return;
6580
6581 /* In order to kick the device replace finish process */
6582 lock_chunks(root);
6583 list_for_each_entry(em, &transaction->pending_chunks, list) {
6584 map = (struct map_lookup *)em->bdev;
6585
6586 for (i = 0; i < map->num_stripes; i++) {
6587 dev = map->stripes[i].dev;
6588 dev->commit_bytes_used = dev->bytes_used;
6589 }
6590 }
6591 unlock_chunks(root);
6592}