blob: 7b5c04259a6e89d069e01aa0bc7b30d96696baef [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040021#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040022#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040023#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040024#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000025#include <linux/capability.h>
Stefan Behrens442a4f62012-05-25 16:06:08 +020026#include <linux/ratelimit.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020027#include <linux/kthread.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050028#include <linux/raid/pq.h>
Stefan Behrens803b2f52013-08-15 17:11:21 +020029#include <linux/semaphore.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050030#include <asm/div64.h>
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "ctree.h"
32#include "extent_map.h"
33#include "disk-io.h"
34#include "transaction.h"
35#include "print-tree.h"
36#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050037#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040038#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010039#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040040#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060041#include "math.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010042#include "dev-replace.h"
Anand Jain99994cd2014-06-03 11:36:00 +080043#include "sysfs.h"
Chris Mason0b86a832008-03-24 15:01:56 -040044
Yan Zheng2b820322008-11-17 21:11:30 -050045static int init_first_rw_device(struct btrfs_trans_handle *trans,
46 struct btrfs_root *root,
47 struct btrfs_device *device);
48static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020049static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +000050static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020051static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Yan Zheng2b820322008-11-17 21:11:30 -050052
Chris Mason8a4b83c2008-03-24 15:02:07 -040053static DEFINE_MUTEX(uuid_mutex);
54static LIST_HEAD(fs_uuids);
55
Chris Mason7d9eb122008-07-08 14:19:17 -040056static void lock_chunks(struct btrfs_root *root)
57{
Chris Mason7d9eb122008-07-08 14:19:17 -040058 mutex_lock(&root->fs_info->chunk_mutex);
59}
60
61static void unlock_chunks(struct btrfs_root *root)
62{
Chris Mason7d9eb122008-07-08 14:19:17 -040063 mutex_unlock(&root->fs_info->chunk_mutex);
64}
65
Ilya Dryomov2208a372013-08-12 14:33:03 +030066static struct btrfs_fs_devices *__alloc_fs_devices(void)
67{
68 struct btrfs_fs_devices *fs_devs;
69
70 fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS);
71 if (!fs_devs)
72 return ERR_PTR(-ENOMEM);
73
74 mutex_init(&fs_devs->device_list_mutex);
75
76 INIT_LIST_HEAD(&fs_devs->devices);
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 Wenruoa8c93d42014-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
Josef Bacik02db0842012-06-21 16:03:58 -0400586 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500587
Xiao Guangrong46224702011-04-20 10:08:47 +0000588 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500589 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400590 struct rcu_string *name;
591
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300592 device = btrfs_alloc_device(NULL, &orig_dev->devid,
593 orig_dev->uuid);
594 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500595 goto error;
596
Josef Bacik606686e2012-06-04 14:03:51 -0400597 /*
598 * This is ok to do without rcu read locked because we hold the
599 * uuid mutex so nothing we touch in here is going to disappear.
600 */
Anand Jaine755f782014-06-30 17:12:47 +0800601 if (orig_dev->name) {
602 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
603 if (!name) {
604 kfree(device);
605 goto error;
606 }
607 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400608 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500609
Yan Zhenge4404d62008-12-12 10:03:26 -0500610 list_add(&device->dev_list, &fs_devices->devices);
611 device->fs_devices = fs_devices;
612 fs_devices->num_devices++;
613 }
614 return fs_devices;
615error:
616 free_fs_devices(fs_devices);
617 return ERR_PTR(-ENOMEM);
618}
619
Stefan Behrens8dabb742012-11-06 13:15:27 +0100620void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
621 struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400622{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500623 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800624 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500625
Chris Masondfe25022008-05-13 13:46:40 -0400626 mutex_lock(&uuid_mutex);
627again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000628 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500629 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500630 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100631 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800632 (!latest_dev ||
633 device->generation > latest_dev->generation)) {
634 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500635 }
Yan Zheng2b820322008-11-17 21:11:30 -0500636 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500637 }
Yan Zheng2b820322008-11-17 21:11:30 -0500638
Stefan Behrens8dabb742012-11-06 13:15:27 +0100639 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
640 /*
641 * In the first step, keep the device which has
642 * the correct fsid and the devid that is used
643 * for the dev_replace procedure.
644 * In the second step, the dev_replace state is
645 * read from the device tree and it is known
646 * whether the procedure is really active or
647 * not, which means whether this device is
648 * used or whether it should be removed.
649 */
650 if (step == 0 || device->is_tgtdev_for_dev_replace) {
651 continue;
652 }
653 }
Yan Zheng2b820322008-11-17 21:11:30 -0500654 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100655 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500656 device->bdev = NULL;
657 fs_devices->open_devices--;
658 }
659 if (device->writeable) {
660 list_del_init(&device->dev_alloc_list);
661 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100662 if (!device->is_tgtdev_for_dev_replace)
663 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500664 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500665 list_del_init(&device->dev_list);
666 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400667 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500668 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400669 }
Yan Zheng2b820322008-11-17 21:11:30 -0500670
671 if (fs_devices->seed) {
672 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500673 goto again;
674 }
675
Miao Xie443f24f2014-07-24 11:37:15 +0800676 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500677
Chris Masondfe25022008-05-13 13:46:40 -0400678 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400679}
Chris Masona0af4692008-05-13 16:03:06 -0400680
Xiao Guangrong1f781602011-04-20 10:09:16 +0000681static void __free_device(struct work_struct *work)
682{
683 struct btrfs_device *device;
684
685 device = container_of(work, struct btrfs_device, rcu_work);
686
687 if (device->bdev)
688 blkdev_put(device->bdev, device->mode);
689
Josef Bacik606686e2012-06-04 14:03:51 -0400690 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000691 kfree(device);
692}
693
694static void free_device(struct rcu_head *head)
695{
696 struct btrfs_device *device;
697
698 device = container_of(head, struct btrfs_device, rcu);
699
700 INIT_WORK(&device->rcu_work, __free_device);
701 schedule_work(&device->rcu_work);
702}
703
Yan Zheng2b820322008-11-17 21:11:30 -0500704static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400705{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400706 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500707
Yan Zheng2b820322008-11-17 21:11:30 -0500708 if (--fs_devices->opened > 0)
709 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400710
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000711 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500712 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000713 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400714 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000715
716 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400717 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000718
Ilya Dryomovf747cab2013-10-10 20:37:29 +0300719 if (device->writeable &&
720 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500721 list_del_init(&device->dev_alloc_list);
722 fs_devices->rw_devices--;
723 }
724
Josef Bacik726551e2013-08-26 13:45:53 -0400725 if (device->missing)
726 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000727
Ilya Dryomova1e87802013-08-12 14:33:04 +0300728 new_device = btrfs_alloc_device(NULL, &device->devid,
729 device->uuid);
730 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400731
732 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400733 if (device->name) {
734 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300735 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400736 rcu_assign_pointer(new_device->name, name);
737 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300738
Xiao Guangrong1f781602011-04-20 10:09:16 +0000739 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300740 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000741
742 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400743 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000744 mutex_unlock(&fs_devices->device_list_mutex);
745
Yan Zhenge4404d62008-12-12 10:03:26 -0500746 WARN_ON(fs_devices->open_devices);
747 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500748 fs_devices->opened = 0;
749 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500750
Chris Mason8a4b83c2008-03-24 15:02:07 -0400751 return 0;
752}
753
Yan Zheng2b820322008-11-17 21:11:30 -0500754int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
755{
Yan Zhenge4404d62008-12-12 10:03:26 -0500756 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500757 int ret;
758
759 mutex_lock(&uuid_mutex);
760 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500761 if (!fs_devices->opened) {
762 seed_devices = fs_devices->seed;
763 fs_devices->seed = NULL;
764 }
Yan Zheng2b820322008-11-17 21:11:30 -0500765 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500766
767 while (seed_devices) {
768 fs_devices = seed_devices;
769 seed_devices = fs_devices->seed;
770 __btrfs_close_devices(fs_devices);
771 free_fs_devices(fs_devices);
772 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000773 /*
774 * Wait for rcu kworkers under __btrfs_close_devices
775 * to finish all blkdev_puts so device is really
776 * free when umount is done.
777 */
778 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500779 return ret;
780}
781
Yan Zhenge4404d62008-12-12 10:03:26 -0500782static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
783 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400784{
Josef Bacikd5e20032011-08-04 14:52:27 +0000785 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400786 struct block_device *bdev;
787 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400788 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +0800789 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -0400790 struct buffer_head *bh;
791 struct btrfs_super_block *disk_super;
Chris Masona0af4692008-05-13 16:03:06 -0400792 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500793 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400794 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400795
Tejun Heod4d77622010-11-13 11:55:18 +0100796 flags |= FMODE_EXCL;
797
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500798 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400799 if (device->bdev)
800 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400801 if (!device->name)
802 continue;
803
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000804 /* Just open everything we can; ignore failures here */
805 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
806 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100807 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400808
809 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000810 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400811 if (devid != device->devid)
812 goto error_brelse;
813
Yan Zheng2b820322008-11-17 21:11:30 -0500814 if (memcmp(device->uuid, disk_super->dev_item.uuid,
815 BTRFS_UUID_SIZE))
816 goto error_brelse;
817
818 device->generation = btrfs_super_generation(disk_super);
Miao Xie443f24f2014-07-24 11:37:15 +0800819 if (!latest_dev ||
820 device->generation > latest_dev->generation)
821 latest_dev = device;
Chris Masona0af4692008-05-13 16:03:06 -0400822
Yan Zheng2b820322008-11-17 21:11:30 -0500823 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
824 device->writeable = 0;
825 } else {
826 device->writeable = !bdev_read_only(bdev);
827 seeding = 0;
828 }
829
Josef Bacikd5e20032011-08-04 14:52:27 +0000830 q = bdev_get_queue(bdev);
Miao Xie90180da2014-09-03 21:35:30 +0800831 if (blk_queue_discard(q))
Josef Bacikd5e20032011-08-04 14:52:27 +0000832 device->can_discard = 1;
Josef Bacikd5e20032011-08-04 14:52:27 +0000833
Chris Mason8a4b83c2008-03-24 15:02:07 -0400834 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400835 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500836 device->mode = flags;
837
Chris Masonc2898112009-06-10 09:51:32 -0400838 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
839 fs_devices->rotating = 1;
840
Chris Masona0af4692008-05-13 16:03:06 -0400841 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300842 if (device->writeable &&
843 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500844 fs_devices->rw_devices++;
845 list_add(&device->dev_alloc_list,
846 &fs_devices->alloc_list);
847 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000848 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400849 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400850
Chris Masona0af4692008-05-13 16:03:06 -0400851error_brelse:
852 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100853 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400854 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400855 }
Chris Masona0af4692008-05-13 16:03:06 -0400856 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300857 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400858 goto out;
859 }
Yan Zheng2b820322008-11-17 21:11:30 -0500860 fs_devices->seeding = seeding;
861 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +0800862 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -0500863 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400864out:
Yan Zheng2b820322008-11-17 21:11:30 -0500865 return ret;
866}
867
868int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500869 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500870{
871 int ret;
872
873 mutex_lock(&uuid_mutex);
874 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500875 fs_devices->opened++;
876 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500877 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500878 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500879 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400880 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400881 return ret;
882}
883
David Sterba6f60cbd2013-02-15 11:31:02 -0700884/*
885 * Look for a btrfs signature on a device. This may be called out of the mount path
886 * and we are not allowed to call set_blocksize during the scan. The superblock
887 * is read via pagecache
888 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500889int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400890 struct btrfs_fs_devices **fs_devices_ret)
891{
892 struct btrfs_super_block *disk_super;
893 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700894 struct page *page;
895 void *p;
896 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400897 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400898 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400899 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700900 u64 bytenr;
901 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400902
David Sterba6f60cbd2013-02-15 11:31:02 -0700903 /*
904 * we would like to check all the supers, but that would make
905 * a btrfs mount succeed after a mkfs from a different FS.
906 * So, we need to add a special mount option to scan for
907 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
908 */
909 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100910 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500911 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700912
913 bdev = blkdev_get_by_path(path, flags, holder);
914
915 if (IS_ERR(bdev)) {
916 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100917 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700918 }
919
920 /* make sure our super fits in the device */
921 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
922 goto error_bdev_put;
923
924 /* make sure our super fits in the page */
925 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
926 goto error_bdev_put;
927
928 /* make sure our super doesn't straddle pages on disk */
929 index = bytenr >> PAGE_CACHE_SHIFT;
930 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
931 goto error_bdev_put;
932
933 /* pull in the page with our super */
934 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
935 index, GFP_NOFS);
936
937 if (IS_ERR_OR_NULL(page))
938 goto error_bdev_put;
939
940 p = kmap(page);
941
942 /* align our pointer to the offset of the super block */
943 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
944
945 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +0800946 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -0700947 goto error_unmap;
948
Xiao Guangronga3438322010-01-06 11:48:18 +0000949 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400950 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400951 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700952
Chris Mason8a4b83c2008-03-24 15:02:07 -0400953 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +0100954 if (ret > 0) {
955 if (disk_super->label[0]) {
956 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
957 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
958 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
959 } else {
960 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
961 }
962
963 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
964 ret = 0;
965 }
Josef Bacik02db0842012-06-21 16:03:58 -0400966 if (!ret && fs_devices_ret)
967 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700968
969error_unmap:
970 kunmap(page);
971 page_cache_release(page);
972
973error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100974 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400975error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100976 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400977 return ret;
978}
Chris Mason0b86a832008-03-24 15:01:56 -0400979
Miao Xie6d07bce2011-01-05 10:07:31 +0000980/* helper to account the used device space in the range */
981int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
982 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400983{
984 struct btrfs_key key;
985 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000986 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500987 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000988 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400989 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000990 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400991 struct extent_buffer *l;
992
Miao Xie6d07bce2011-01-05 10:07:31 +0000993 *length = 0;
994
Stefan Behrens63a212a2012-11-05 18:29:28 +0100995 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000996 return 0;
997
Yan Zheng2b820322008-11-17 21:11:30 -0500998 path = btrfs_alloc_path();
999 if (!path)
1000 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001001 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -04001002
Chris Mason0b86a832008-03-24 15:01:56 -04001003 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001004 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001005 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001006
1007 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001008 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001009 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001010 if (ret > 0) {
1011 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1012 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001013 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001014 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001015
Chris Mason0b86a832008-03-24 15:01:56 -04001016 while (1) {
1017 l = path->nodes[0];
1018 slot = path->slots[0];
1019 if (slot >= btrfs_header_nritems(l)) {
1020 ret = btrfs_next_leaf(root, path);
1021 if (ret == 0)
1022 continue;
1023 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001024 goto out;
1025
1026 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001027 }
1028 btrfs_item_key_to_cpu(l, &key, slot);
1029
1030 if (key.objectid < device->devid)
1031 goto next;
1032
1033 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001034 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001035
David Sterba962a2982014-06-04 18:41:45 +02001036 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001037 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001038
Chris Mason0b86a832008-03-24 15:01:56 -04001039 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001040 extent_end = key.offset + btrfs_dev_extent_length(l,
1041 dev_extent);
1042 if (key.offset <= start && extent_end > end) {
1043 *length = end - start + 1;
1044 break;
1045 } else if (key.offset <= start && extent_end > start)
1046 *length += extent_end - start;
1047 else if (key.offset > start && extent_end <= end)
1048 *length += extent_end - key.offset;
1049 else if (key.offset > start && key.offset <= end) {
1050 *length += end - key.offset + 1;
1051 break;
1052 } else if (key.offset > end)
1053 break;
1054
1055next:
1056 path->slots[0]++;
1057 }
1058 ret = 0;
1059out:
1060 btrfs_free_path(path);
1061 return ret;
1062}
1063
Josef Bacik6df9a952013-06-27 13:22:46 -04001064static int contains_pending_extent(struct btrfs_trans_handle *trans,
1065 struct btrfs_device *device,
1066 u64 *start, u64 len)
1067{
1068 struct extent_map *em;
1069 int ret = 0;
1070
1071 list_for_each_entry(em, &trans->transaction->pending_chunks, list) {
1072 struct map_lookup *map;
1073 int i;
1074
1075 map = (struct map_lookup *)em->bdev;
1076 for (i = 0; i < map->num_stripes; i++) {
1077 if (map->stripes[i].dev != device)
1078 continue;
1079 if (map->stripes[i].physical >= *start + len ||
1080 map->stripes[i].physical + em->orig_block_len <=
1081 *start)
1082 continue;
1083 *start = map->stripes[i].physical +
1084 em->orig_block_len;
1085 ret = 1;
1086 }
1087 }
1088
1089 return ret;
1090}
1091
1092
Chris Mason0b86a832008-03-24 15:01:56 -04001093/*
Miao Xie7bfc8372011-01-05 10:07:26 +00001094 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +00001095 * @device: the device which we search the free space in
1096 * @num_bytes: the size of the free space that we need
1097 * @start: store the start of the free space.
1098 * @len: the size of the free space. that we find, or the size of the max
1099 * free space if we don't find suitable free space
1100 *
Chris Mason0b86a832008-03-24 15:01:56 -04001101 * this uses a pretty simple search, the expectation is that it is
1102 * called very infrequently and that a given device has a small number
1103 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001104 *
1105 * @start is used to store the start of the free space if we find. But if we
1106 * don't find suitable free space, it will be used to store the start position
1107 * of the max free space.
1108 *
1109 * @len is used to store the size of the free space that we find.
1110 * But if we don't find suitable free space, it is used to store the size of
1111 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001112 */
Josef Bacik6df9a952013-06-27 13:22:46 -04001113int find_free_dev_extent(struct btrfs_trans_handle *trans,
1114 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001115 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001116{
1117 struct btrfs_key key;
1118 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001119 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001120 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001121 u64 hole_size;
1122 u64 max_hole_start;
1123 u64 max_hole_size;
1124 u64 extent_end;
1125 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001126 u64 search_end = device->total_bytes;
1127 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001128 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001129 struct extent_buffer *l;
1130
Chris Mason0b86a832008-03-24 15:01:56 -04001131 /* FIXME use last free of some kind */
1132
1133 /* we don't want to overwrite the superblock on the drive,
1134 * so we make sure to start at an offset of at least 1MB
1135 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001136 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001137
Josef Bacik6df9a952013-06-27 13:22:46 -04001138 path = btrfs_alloc_path();
1139 if (!path)
1140 return -ENOMEM;
1141again:
Miao Xie7bfc8372011-01-05 10:07:26 +00001142 max_hole_start = search_start;
1143 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +00001144 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +00001145
Stefan Behrens63a212a2012-11-05 18:29:28 +01001146 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001147 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001148 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001149 }
1150
Miao Xie7bfc8372011-01-05 10:07:26 +00001151 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001152 path->search_commit_root = 1;
1153 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001154
Chris Mason0b86a832008-03-24 15:01:56 -04001155 key.objectid = device->devid;
1156 key.offset = search_start;
1157 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001158
Li Zefan125ccb02011-12-08 15:07:24 +08001159 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001160 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001161 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001162 if (ret > 0) {
1163 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1164 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001165 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001166 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001167
Chris Mason0b86a832008-03-24 15:01:56 -04001168 while (1) {
1169 l = path->nodes[0];
1170 slot = path->slots[0];
1171 if (slot >= btrfs_header_nritems(l)) {
1172 ret = btrfs_next_leaf(root, path);
1173 if (ret == 0)
1174 continue;
1175 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001176 goto out;
1177
1178 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001179 }
1180 btrfs_item_key_to_cpu(l, &key, slot);
1181
1182 if (key.objectid < device->devid)
1183 goto next;
1184
1185 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001186 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001187
David Sterba962a2982014-06-04 18:41:45 +02001188 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001189 goto next;
1190
Miao Xie7bfc8372011-01-05 10:07:26 +00001191 if (key.offset > search_start) {
1192 hole_size = key.offset - search_start;
1193
Josef Bacik6df9a952013-06-27 13:22:46 -04001194 /*
1195 * Have to check before we set max_hole_start, otherwise
1196 * we could end up sending back this offset anyway.
1197 */
1198 if (contains_pending_extent(trans, device,
1199 &search_start,
1200 hole_size))
1201 hole_size = 0;
1202
Miao Xie7bfc8372011-01-05 10:07:26 +00001203 if (hole_size > max_hole_size) {
1204 max_hole_start = search_start;
1205 max_hole_size = hole_size;
1206 }
1207
1208 /*
1209 * If this free space is greater than which we need,
1210 * it must be the max free space that we have found
1211 * until now, so max_hole_start must point to the start
1212 * of this free space and the length of this free space
1213 * is stored in max_hole_size. Thus, we return
1214 * max_hole_start and max_hole_size and go back to the
1215 * caller.
1216 */
1217 if (hole_size >= num_bytes) {
1218 ret = 0;
1219 goto out;
1220 }
1221 }
1222
Chris Mason0b86a832008-03-24 15:01:56 -04001223 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001224 extent_end = key.offset + btrfs_dev_extent_length(l,
1225 dev_extent);
1226 if (extent_end > search_start)
1227 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001228next:
1229 path->slots[0]++;
1230 cond_resched();
1231 }
Chris Mason0b86a832008-03-24 15:01:56 -04001232
liubo38c01b92011-08-02 02:39:03 +00001233 /*
1234 * At this point, search_start should be the end of
1235 * allocated dev extents, and when shrinking the device,
1236 * search_end may be smaller than search_start.
1237 */
1238 if (search_end > search_start)
1239 hole_size = search_end - search_start;
1240
Miao Xie7bfc8372011-01-05 10:07:26 +00001241 if (hole_size > max_hole_size) {
1242 max_hole_start = search_start;
1243 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001244 }
Chris Mason0b86a832008-03-24 15:01:56 -04001245
Josef Bacik6df9a952013-06-27 13:22:46 -04001246 if (contains_pending_extent(trans, device, &search_start, hole_size)) {
1247 btrfs_release_path(path);
1248 goto again;
1249 }
1250
Miao Xie7bfc8372011-01-05 10:07:26 +00001251 /* See above. */
1252 if (hole_size < num_bytes)
1253 ret = -ENOSPC;
1254 else
1255 ret = 0;
1256
1257out:
Yan Zheng2b820322008-11-17 21:11:30 -05001258 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001259 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001260 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001261 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001262 return ret;
1263}
1264
Christoph Hellwigb2950862008-12-02 09:54:17 -05001265static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001266 struct btrfs_device *device,
1267 u64 start)
1268{
1269 int ret;
1270 struct btrfs_path *path;
1271 struct btrfs_root *root = device->dev_root;
1272 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001273 struct btrfs_key found_key;
1274 struct extent_buffer *leaf = NULL;
1275 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001276
1277 path = btrfs_alloc_path();
1278 if (!path)
1279 return -ENOMEM;
1280
1281 key.objectid = device->devid;
1282 key.offset = start;
1283 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001284again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001285 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001286 if (ret > 0) {
1287 ret = btrfs_previous_item(root, path, key.objectid,
1288 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001289 if (ret)
1290 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001291 leaf = path->nodes[0];
1292 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1293 extent = btrfs_item_ptr(leaf, path->slots[0],
1294 struct btrfs_dev_extent);
1295 BUG_ON(found_key.offset > start || found_key.offset +
1296 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001297 key = found_key;
1298 btrfs_release_path(path);
1299 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001300 } else if (ret == 0) {
1301 leaf = path->nodes[0];
1302 extent = btrfs_item_ptr(leaf, path->slots[0],
1303 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001304 } else {
1305 btrfs_error(root->fs_info, ret, "Slot search failed");
1306 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001307 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001308
Josef Bacik2bf64752011-09-26 17:12:22 -04001309 if (device->bytes_used > 0) {
1310 u64 len = btrfs_dev_extent_length(leaf, extent);
1311 device->bytes_used -= len;
1312 spin_lock(&root->fs_info->free_chunk_lock);
1313 root->fs_info->free_chunk_space += len;
1314 spin_unlock(&root->fs_info->free_chunk_lock);
1315 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001316 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001317 if (ret) {
1318 btrfs_error(root->fs_info, ret,
1319 "Failed to remove dev extent item");
1320 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001321out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001322 btrfs_free_path(path);
1323 return ret;
1324}
1325
Eric Sandeen48a3b632013-04-25 20:41:01 +00001326static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1327 struct btrfs_device *device,
1328 u64 chunk_tree, u64 chunk_objectid,
1329 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001330{
1331 int ret;
1332 struct btrfs_path *path;
1333 struct btrfs_root *root = device->dev_root;
1334 struct btrfs_dev_extent *extent;
1335 struct extent_buffer *leaf;
1336 struct btrfs_key key;
1337
Chris Masondfe25022008-05-13 13:46:40 -04001338 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001339 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001340 path = btrfs_alloc_path();
1341 if (!path)
1342 return -ENOMEM;
1343
Chris Mason0b86a832008-03-24 15:01:56 -04001344 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001345 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001346 key.type = BTRFS_DEV_EXTENT_KEY;
1347 ret = btrfs_insert_empty_item(trans, root, path, &key,
1348 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001349 if (ret)
1350 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001351
1352 leaf = path->nodes[0];
1353 extent = btrfs_item_ptr(leaf, path->slots[0],
1354 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001355 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1356 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1357 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1358
1359 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001360 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001361
Chris Mason0b86a832008-03-24 15:01:56 -04001362 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1363 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001364out:
Chris Mason0b86a832008-03-24 15:01:56 -04001365 btrfs_free_path(path);
1366 return ret;
1367}
1368
Josef Bacik6df9a952013-06-27 13:22:46 -04001369static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001370{
Josef Bacik6df9a952013-06-27 13:22:46 -04001371 struct extent_map_tree *em_tree;
1372 struct extent_map *em;
1373 struct rb_node *n;
1374 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001375
Josef Bacik6df9a952013-06-27 13:22:46 -04001376 em_tree = &fs_info->mapping_tree.map_tree;
1377 read_lock(&em_tree->lock);
1378 n = rb_last(&em_tree->map);
1379 if (n) {
1380 em = rb_entry(n, struct extent_map, rb_node);
1381 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001382 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001383 read_unlock(&em_tree->lock);
1384
Chris Mason0b86a832008-03-24 15:01:56 -04001385 return ret;
1386}
1387
Ilya Dryomov53f10652013-08-12 14:33:01 +03001388static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1389 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001390{
1391 int ret;
1392 struct btrfs_key key;
1393 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001394 struct btrfs_path *path;
1395
Yan Zheng2b820322008-11-17 21:11:30 -05001396 path = btrfs_alloc_path();
1397 if (!path)
1398 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001399
1400 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1401 key.type = BTRFS_DEV_ITEM_KEY;
1402 key.offset = (u64)-1;
1403
Ilya Dryomov53f10652013-08-12 14:33:01 +03001404 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001405 if (ret < 0)
1406 goto error;
1407
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001408 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001409
Ilya Dryomov53f10652013-08-12 14:33:01 +03001410 ret = btrfs_previous_item(fs_info->chunk_root, path,
1411 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001412 BTRFS_DEV_ITEM_KEY);
1413 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001414 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001415 } else {
1416 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1417 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001418 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001419 }
1420 ret = 0;
1421error:
Yan Zheng2b820322008-11-17 21:11:30 -05001422 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001423 return ret;
1424}
1425
1426/*
1427 * the device information is stored in the chunk root
1428 * the btrfs_device struct should be fully filled in
1429 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001430static int btrfs_add_device(struct btrfs_trans_handle *trans,
1431 struct btrfs_root *root,
1432 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001433{
1434 int ret;
1435 struct btrfs_path *path;
1436 struct btrfs_dev_item *dev_item;
1437 struct extent_buffer *leaf;
1438 struct btrfs_key key;
1439 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001440
1441 root = root->fs_info->chunk_root;
1442
1443 path = btrfs_alloc_path();
1444 if (!path)
1445 return -ENOMEM;
1446
Chris Mason0b86a832008-03-24 15:01:56 -04001447 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1448 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001449 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001450
1451 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001452 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001453 if (ret)
1454 goto out;
1455
1456 leaf = path->nodes[0];
1457 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1458
1459 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001460 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001461 btrfs_set_device_type(leaf, dev_item, device->type);
1462 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1463 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1464 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7df69d32014-07-24 11:37:13 +08001465 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001466 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
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;
Chris Mason7d9eb122008-07-08 14:19:17 -04001522 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001523
1524 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1525 if (ret < 0)
1526 goto out;
1527
1528 if (ret > 0) {
1529 ret = -ENOENT;
1530 goto out;
1531 }
1532
1533 ret = btrfs_del_item(trans, root, path);
1534 if (ret)
1535 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001536out:
1537 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001538 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001539 btrfs_commit_transaction(trans, root);
1540 return ret;
1541}
1542
1543int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1544{
1545 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001546 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001547 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001548 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001549 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001550 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001551 u64 all_avail;
1552 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001553 u64 num_devices;
1554 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001555 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001556 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001557 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001558
Chris Masona061fc82008-05-07 11:43:44 -04001559 mutex_lock(&uuid_mutex);
1560
Miao Xiede98ced2013-01-29 10:13:12 +00001561 do {
1562 seq = read_seqbegin(&root->fs_info->profiles_lock);
1563
1564 all_avail = root->fs_info->avail_data_alloc_bits |
1565 root->fs_info->avail_system_alloc_bits |
1566 root->fs_info->avail_metadata_alloc_bits;
1567 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001568
Stefan Behrens8dabb742012-11-06 13:15:27 +01001569 num_devices = root->fs_info->fs_devices->num_devices;
1570 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1571 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1572 WARN_ON(num_devices < 1);
1573 num_devices--;
1574 }
1575 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1576
1577 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001578 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001579 goto out;
1580 }
1581
Stefan Behrens8dabb742012-11-06 13:15:27 +01001582 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001583 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001584 goto out;
1585 }
1586
David Woodhouse53b381b2013-01-29 18:40:14 -05001587 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1588 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001589 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001590 goto out;
1591 }
1592 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1593 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001594 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001595 goto out;
1596 }
1597
Chris Masondfe25022008-05-13 13:46:40 -04001598 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001599 struct list_head *devices;
1600 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001601
Chris Masondfe25022008-05-13 13:46:40 -04001602 device = NULL;
1603 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001604 /*
1605 * It is safe to read the devices since the volume_mutex
1606 * is held.
1607 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001608 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001609 if (tmp->in_fs_metadata &&
1610 !tmp->is_tgtdev_for_dev_replace &&
1611 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001612 device = tmp;
1613 break;
1614 }
1615 }
1616 bdev = NULL;
1617 bh = NULL;
1618 disk_super = NULL;
1619 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001620 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001621 goto out;
1622 }
Chris Masondfe25022008-05-13 13:46:40 -04001623 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001624 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001625 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001626 root->fs_info->bdev_holder, 0,
1627 &bdev, &bh);
1628 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001629 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001630 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001631 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001632 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001633 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001634 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001635 if (!device) {
1636 ret = -ENOENT;
1637 goto error_brelse;
1638 }
Chris Masondfe25022008-05-13 13:46:40 -04001639 }
Yan Zheng2b820322008-11-17 21:11:30 -05001640
Stefan Behrens63a212a2012-11-05 18:29:28 +01001641 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001642 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001643 goto error_brelse;
1644 }
1645
Yan Zheng2b820322008-11-17 21:11:30 -05001646 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001647 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001648 goto error_brelse;
1649 }
1650
1651 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001652 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001653 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001654 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001655 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001656 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001657 }
Chris Masona061fc82008-05-07 11:43:44 -04001658
Carey Underwoodd7901552013-03-04 16:37:06 -06001659 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001660 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001661 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001662 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001663 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001664
Stefan Behrens63a212a2012-11-05 18:29:28 +01001665 /*
1666 * TODO: the superblock still includes this device in its num_devices
1667 * counter although write_all_supers() is not locked out. This
1668 * could give a filesystem state which requires a degraded mount.
1669 */
Chris Masona061fc82008-05-07 11:43:44 -04001670 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1671 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001672 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001673
Josef Bacik2bf64752011-09-26 17:12:22 -04001674 spin_lock(&root->fs_info->free_chunk_lock);
1675 root->fs_info->free_chunk_space = device->total_bytes -
1676 device->bytes_used;
1677 spin_unlock(&root->fs_info->free_chunk_lock);
1678
Yan Zheng2b820322008-11-17 21:11:30 -05001679 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001680 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001681
1682 /*
1683 * the device list mutex makes sure that we don't change
1684 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001685 * the device supers. Whoever is writing all supers, should
1686 * lock the device list mutex before getting the number of
1687 * devices in the super block (super_copy). Conversely,
1688 * whoever updates the number of devices in the super block
1689 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001690 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001691
1692 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001693 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001694 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001695
Yan Zhenge4404d62008-12-12 10:03:26 -05001696 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001697 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001698
Chris Masoncd02dca2010-12-13 14:56:23 -05001699 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001700 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001701
Yan Zheng2b820322008-11-17 21:11:30 -05001702 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1703 struct btrfs_device, dev_list);
1704 if (device->bdev == root->fs_info->sb->s_bdev)
1705 root->fs_info->sb->s_bdev = next_device->bdev;
1706 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1707 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1708
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001709 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001710 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001711 /* remove sysfs entry */
1712 btrfs_kobj_rm_device(root->fs_info, device);
1713 }
Anand Jain99994cd2014-06-03 11:36:00 +08001714
Xiao Guangrong1f781602011-04-20 10:09:16 +00001715 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001716
David Sterba6c417612011-04-13 15:41:04 +02001717 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1718 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001719 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001720
Xiao Guangrong1f781602011-04-20 10:09:16 +00001721 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001722 struct btrfs_fs_devices *fs_devices;
1723 fs_devices = root->fs_info->fs_devices;
1724 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001725 if (fs_devices->seed == cur_devices) {
1726 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001727 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001728 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001729 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001730 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001731 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001732 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001733 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001734 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001735 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001736 }
1737
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001738 root->fs_info->num_tolerated_disk_barrier_failures =
1739 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1740
Yan Zheng2b820322008-11-17 21:11:30 -05001741 /*
1742 * at this point, the device is zero sized. We want to
1743 * remove it from the devices list and zero out the old super
1744 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001745 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001746 u64 bytenr;
1747 int i;
1748
Chris Masondfe25022008-05-13 13:46:40 -04001749 /* make sure this device isn't detected as part of
1750 * the FS anymore
1751 */
1752 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1753 set_buffer_dirty(bh);
1754 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001755
1756 /* clear the mirror copies of super block on the disk
1757 * being removed, 0th copy is been taken care above and
1758 * the below would take of the rest
1759 */
1760 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1761 bytenr = btrfs_sb_offset(i);
1762 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1763 i_size_read(bdev->bd_inode))
1764 break;
1765
1766 brelse(bh);
1767 bh = __bread(bdev, bytenr / 4096,
1768 BTRFS_SUPER_INFO_SIZE);
1769 if (!bh)
1770 continue;
1771
1772 disk_super = (struct btrfs_super_block *)bh->b_data;
1773
1774 if (btrfs_super_bytenr(disk_super) != bytenr ||
1775 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1776 continue;
1777 }
1778 memset(&disk_super->magic, 0,
1779 sizeof(disk_super->magic));
1780 set_buffer_dirty(bh);
1781 sync_dirty_buffer(bh);
1782 }
Chris Masondfe25022008-05-13 13:46:40 -04001783 }
Chris Masona061fc82008-05-07 11:43:44 -04001784
Chris Masona061fc82008-05-07 11:43:44 -04001785 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001786
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001787 if (bdev) {
1788 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001789 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001790
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001791 /* Update ctime/mtime for device path for libblkid */
1792 update_dev_time(device_path);
1793 }
1794
Chris Masona061fc82008-05-07 11:43:44 -04001795error_brelse:
1796 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001797 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001798 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001799out:
1800 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001801 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001802error_undo:
1803 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001804 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001805 list_add(&device->dev_alloc_list,
1806 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001807 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001808 root->fs_info->fs_devices->rw_devices++;
1809 }
1810 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001811}
1812
Stefan Behrense93c89c2012-11-05 17:33:06 +01001813void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1814 struct btrfs_device *srcdev)
1815{
Anand Jaind51908c2014-08-13 14:24:19 +08001816 struct btrfs_fs_devices *fs_devices;
1817
Stefan Behrense93c89c2012-11-05 17:33:06 +01001818 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001819
Anand Jain25e8e912014-08-20 10:56:56 +08001820 /*
1821 * in case of fs with no seed, srcdev->fs_devices will point
1822 * to fs_devices of fs_info. However when the dev being replaced is
1823 * a seed dev it will point to the seed's local fs_devices. In short
1824 * srcdev will have its correct fs_devices in both the cases.
1825 */
1826 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08001827
Stefan Behrense93c89c2012-11-05 17:33:06 +01001828 list_del_rcu(&srcdev->dev_list);
1829 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08001830 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001831 if (srcdev->missing) {
Anand Jaind51908c2014-08-13 14:24:19 +08001832 fs_devices->missing_devices--;
Anand Jainb2efedc2014-08-13 14:24:24 +08001833 if (!fs_devices->seeding)
1834 fs_devices->rw_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001835 }
Miao Xie90180da2014-09-03 21:35:30 +08001836
Ilya Dryomov13572722013-10-02 20:41:01 +03001837 if (srcdev->bdev) {
Anand Jaind51908c2014-08-13 14:24:19 +08001838 fs_devices->open_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001839
Miao Xieff61d172014-07-24 11:37:06 +08001840 /*
1841 * zero out the old super if it is not writable
1842 * (e.g. seed device)
1843 */
1844 if (srcdev->writeable)
1845 btrfs_scratch_superblock(srcdev);
Ilya Dryomov13572722013-10-02 20:41:01 +03001846 }
1847
Stefan Behrense93c89c2012-11-05 17:33:06 +01001848 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001849
1850 /*
1851 * unless fs_devices is seed fs, num_devices shouldn't go
1852 * zero
1853 */
1854 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
1855
1856 /* if this is no devs we rather delete the fs_devices */
1857 if (!fs_devices->num_devices) {
1858 struct btrfs_fs_devices *tmp_fs_devices;
1859
1860 tmp_fs_devices = fs_info->fs_devices;
1861 while (tmp_fs_devices) {
1862 if (tmp_fs_devices->seed == fs_devices) {
1863 tmp_fs_devices->seed = fs_devices->seed;
1864 break;
1865 }
1866 tmp_fs_devices = tmp_fs_devices->seed;
1867 }
1868 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08001869 __btrfs_close_devices(fs_devices);
1870 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001871 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01001872}
1873
1874void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1875 struct btrfs_device *tgtdev)
1876{
1877 struct btrfs_device *next_device;
1878
1879 WARN_ON(!tgtdev);
1880 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1881 if (tgtdev->bdev) {
1882 btrfs_scratch_superblock(tgtdev);
1883 fs_info->fs_devices->open_devices--;
1884 }
1885 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001886
1887 next_device = list_entry(fs_info->fs_devices->devices.next,
1888 struct btrfs_device, dev_list);
1889 if (tgtdev->bdev == fs_info->sb->s_bdev)
1890 fs_info->sb->s_bdev = next_device->bdev;
1891 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1892 fs_info->fs_devices->latest_bdev = next_device->bdev;
1893 list_del_rcu(&tgtdev->dev_list);
1894
1895 call_rcu(&tgtdev->rcu, free_device);
1896
1897 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1898}
1899
Eric Sandeen48a3b632013-04-25 20:41:01 +00001900static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1901 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001902{
1903 int ret = 0;
1904 struct btrfs_super_block *disk_super;
1905 u64 devid;
1906 u8 *dev_uuid;
1907 struct block_device *bdev;
1908 struct buffer_head *bh;
1909
1910 *device = NULL;
1911 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1912 root->fs_info->bdev_holder, 0, &bdev, &bh);
1913 if (ret)
1914 return ret;
1915 disk_super = (struct btrfs_super_block *)bh->b_data;
1916 devid = btrfs_stack_device_id(&disk_super->dev_item);
1917 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001918 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001919 disk_super->fsid);
1920 brelse(bh);
1921 if (!*device)
1922 ret = -ENOENT;
1923 blkdev_put(bdev, FMODE_READ);
1924 return ret;
1925}
1926
1927int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1928 char *device_path,
1929 struct btrfs_device **device)
1930{
1931 *device = NULL;
1932 if (strcmp(device_path, "missing") == 0) {
1933 struct list_head *devices;
1934 struct btrfs_device *tmp;
1935
1936 devices = &root->fs_info->fs_devices->devices;
1937 /*
1938 * It is safe to read the devices since the volume_mutex
1939 * is held by the caller.
1940 */
1941 list_for_each_entry(tmp, devices, dev_list) {
1942 if (tmp->in_fs_metadata && !tmp->bdev) {
1943 *device = tmp;
1944 break;
1945 }
1946 }
1947
1948 if (!*device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001949 btrfs_err(root->fs_info, "no missing device found");
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001950 return -ENOENT;
1951 }
1952
1953 return 0;
1954 } else {
1955 return btrfs_find_device_by_path(root, device_path, device);
1956 }
1957}
1958
Yan Zheng2b820322008-11-17 21:11:30 -05001959/*
1960 * does all the dirty work required for changing file system's UUID.
1961 */
Li Zefan125ccb02011-12-08 15:07:24 +08001962static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001963{
1964 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1965 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001966 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001967 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001968 struct btrfs_device *device;
1969 u64 super_flags;
1970
1971 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001972 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001973 return -EINVAL;
1974
Ilya Dryomov2208a372013-08-12 14:33:03 +03001975 seed_devices = __alloc_fs_devices();
1976 if (IS_ERR(seed_devices))
1977 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001978
Yan Zhenge4404d62008-12-12 10:03:26 -05001979 old_devices = clone_fs_devices(fs_devices);
1980 if (IS_ERR(old_devices)) {
1981 kfree(seed_devices);
1982 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001983 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001984
Yan Zheng2b820322008-11-17 21:11:30 -05001985 list_add(&old_devices->list, &fs_uuids);
1986
Yan Zhenge4404d62008-12-12 10:03:26 -05001987 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1988 seed_devices->opened = 1;
1989 INIT_LIST_HEAD(&seed_devices->devices);
1990 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001991 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001992
1993 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001994 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1995 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001996
Yan Zhenge4404d62008-12-12 10:03:26 -05001997 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1998 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1999 device->fs_devices = seed_devices;
2000 }
2001
Yan Zheng2b820322008-11-17 21:11:30 -05002002 fs_devices->seeding = 0;
2003 fs_devices->num_devices = 0;
2004 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002005 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002006 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002007 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002008
2009 generate_random_uuid(fs_devices->fsid);
2010 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2011 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002012 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2013
Yan Zheng2b820322008-11-17 21:11:30 -05002014 super_flags = btrfs_super_flags(disk_super) &
2015 ~BTRFS_SUPER_FLAG_SEEDING;
2016 btrfs_set_super_flags(disk_super, super_flags);
2017
2018 return 0;
2019}
2020
2021/*
2022 * strore the expected generation for seed devices in device items.
2023 */
2024static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2025 struct btrfs_root *root)
2026{
2027 struct btrfs_path *path;
2028 struct extent_buffer *leaf;
2029 struct btrfs_dev_item *dev_item;
2030 struct btrfs_device *device;
2031 struct btrfs_key key;
2032 u8 fs_uuid[BTRFS_UUID_SIZE];
2033 u8 dev_uuid[BTRFS_UUID_SIZE];
2034 u64 devid;
2035 int ret;
2036
2037 path = btrfs_alloc_path();
2038 if (!path)
2039 return -ENOMEM;
2040
2041 root = root->fs_info->chunk_root;
2042 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2043 key.offset = 0;
2044 key.type = BTRFS_DEV_ITEM_KEY;
2045
2046 while (1) {
2047 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2048 if (ret < 0)
2049 goto error;
2050
2051 leaf = path->nodes[0];
2052next_slot:
2053 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2054 ret = btrfs_next_leaf(root, path);
2055 if (ret > 0)
2056 break;
2057 if (ret < 0)
2058 goto error;
2059 leaf = path->nodes[0];
2060 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002061 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002062 continue;
2063 }
2064
2065 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2066 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2067 key.type != BTRFS_DEV_ITEM_KEY)
2068 break;
2069
2070 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2071 struct btrfs_dev_item);
2072 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002073 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002074 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002075 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002076 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002077 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2078 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002079 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002080
2081 if (device->fs_devices->seeding) {
2082 btrfs_set_device_generation(leaf, dev_item,
2083 device->generation);
2084 btrfs_mark_buffer_dirty(leaf);
2085 }
2086
2087 path->slots[0]++;
2088 goto next_slot;
2089 }
2090 ret = 0;
2091error:
2092 btrfs_free_path(path);
2093 return ret;
2094}
2095
Chris Mason788f20e2008-04-28 15:29:42 -04002096int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2097{
Josef Bacikd5e20032011-08-04 14:52:27 +00002098 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002099 struct btrfs_trans_handle *trans;
2100 struct btrfs_device *device;
2101 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002102 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002103 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002104 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002105 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002106 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002107 int ret = 0;
2108
Yan Zheng2b820322008-11-17 21:11:30 -05002109 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002110 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002111
Li Zefana5d16332011-12-07 20:08:40 -05002112 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002113 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002114 if (IS_ERR(bdev))
2115 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002116
Yan Zheng2b820322008-11-17 21:11:30 -05002117 if (root->fs_info->fs_devices->seeding) {
2118 seeding_dev = 1;
2119 down_write(&sb->s_umount);
2120 mutex_lock(&uuid_mutex);
2121 }
2122
Chris Mason8c8bee12008-09-29 11:19:10 -04002123 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002124
Chris Mason788f20e2008-04-28 15:29:42 -04002125 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002126
2127 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002128 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002129 if (device->bdev == bdev) {
2130 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002131 mutex_unlock(
2132 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002133 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002134 }
2135 }
Liu Bod25628b2012-11-14 14:35:30 +00002136 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002137
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002138 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2139 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002140 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002141 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002142 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002143 }
2144
Josef Bacik606686e2012-06-04 14:03:51 -04002145 name = rcu_string_strdup(device_path, GFP_NOFS);
2146 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002147 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002148 ret = -ENOMEM;
2149 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002150 }
Josef Bacik606686e2012-06-04 14:03:51 -04002151 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002152
Yan, Zhenga22285a2010-05-16 10:48:46 -04002153 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002154 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002155 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002156 kfree(device);
2157 ret = PTR_ERR(trans);
2158 goto error;
2159 }
2160
Yan Zheng2b820322008-11-17 21:11:30 -05002161 lock_chunks(root);
2162
Josef Bacikd5e20032011-08-04 14:52:27 +00002163 q = bdev_get_queue(bdev);
2164 if (blk_queue_discard(q))
2165 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002166 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002167 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002168 device->io_width = root->sectorsize;
2169 device->io_align = root->sectorsize;
2170 device->sector_size = root->sectorsize;
2171 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002172 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002173 device->commit_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002174 device->dev_root = root->fs_info->dev_root;
2175 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002176 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002177 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002178 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002179 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002180 set_blocksize(device->bdev, 4096);
2181
Yan Zheng2b820322008-11-17 21:11:30 -05002182 if (seeding_dev) {
2183 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002184 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002185 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002186 }
2187
2188 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002189
Chris Masone5e9a522009-06-10 15:17:02 -04002190 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002191 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002192 list_add(&device->dev_alloc_list,
2193 &root->fs_info->fs_devices->alloc_list);
2194 root->fs_info->fs_devices->num_devices++;
2195 root->fs_info->fs_devices->open_devices++;
2196 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002197 root->fs_info->fs_devices->total_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -05002198 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2199
Josef Bacik2bf64752011-09-26 17:12:22 -04002200 spin_lock(&root->fs_info->free_chunk_lock);
2201 root->fs_info->free_chunk_space += device->total_bytes;
2202 spin_unlock(&root->fs_info->free_chunk_lock);
2203
Chris Masonc2898112009-06-10 09:51:32 -04002204 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2205 root->fs_info->fs_devices->rotating = 1;
2206
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002207 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002208 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002209 tmp + device->total_bytes);
Chris Mason788f20e2008-04-28 15:29:42 -04002210
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002211 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002212 btrfs_set_super_num_devices(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002213 tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002214
2215 /* add sysfs device entry */
2216 btrfs_kobj_add_device(root->fs_info, device);
2217
Chris Masone5e9a522009-06-10 15:17:02 -04002218 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002219
Yan Zheng2b820322008-11-17 21:11:30 -05002220 if (seeding_dev) {
Anand Jainb2373f22014-06-03 11:36:03 +08002221 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002222 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002223 if (ret) {
2224 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002225 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002226 }
Yan Zheng2b820322008-11-17 21:11:30 -05002227 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002228 if (ret) {
2229 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002230 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002231 }
Anand Jainb2373f22014-06-03 11:36:03 +08002232
2233 /* Sprouting would change fsid of the mounted root,
2234 * so rename the fsid on the sysfs
2235 */
2236 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2237 root->fs_info->fsid);
2238 if (kobject_rename(&root->fs_info->super_kobj, fsid_buf))
2239 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05002240 } else {
2241 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002242 if (ret) {
2243 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002244 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002245 }
Yan Zheng2b820322008-11-17 21:11:30 -05002246 }
2247
Chris Mason913d9522009-03-10 13:17:18 -04002248 /*
2249 * we've got more storage, clear any full flags on the space
2250 * infos
2251 */
2252 btrfs_clear_space_info_full(root->fs_info);
2253
Chris Mason7d9eb122008-07-08 14:19:17 -04002254 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002255 root->fs_info->num_tolerated_disk_barrier_failures =
2256 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002257 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002258
2259 if (seeding_dev) {
2260 mutex_unlock(&uuid_mutex);
2261 up_write(&sb->s_umount);
2262
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002263 if (ret) /* transaction commit */
2264 return ret;
2265
Yan Zheng2b820322008-11-17 21:11:30 -05002266 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002267 if (ret < 0)
2268 btrfs_error(root->fs_info, ret,
2269 "Failed to relocate sys chunks after "
2270 "device initialization. This can be fixed "
2271 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002272 trans = btrfs_attach_transaction(root);
2273 if (IS_ERR(trans)) {
2274 if (PTR_ERR(trans) == -ENOENT)
2275 return 0;
2276 return PTR_ERR(trans);
2277 }
2278 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002279 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002280
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002281 /* Update ctime/mtime for libblkid */
2282 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002283 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002284
2285error_trans:
2286 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002287 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002288 rcu_string_free(device->name);
Anand Jain0d393762014-06-03 11:36:01 +08002289 btrfs_kobj_rm_device(root->fs_info, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002290 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002291error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002292 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002293 if (seeding_dev) {
2294 mutex_unlock(&uuid_mutex);
2295 up_write(&sb->s_umount);
2296 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002297 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002298}
2299
Stefan Behrense93c89c2012-11-05 17:33:06 +01002300int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002301 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002302 struct btrfs_device **device_out)
2303{
2304 struct request_queue *q;
2305 struct btrfs_device *device;
2306 struct block_device *bdev;
2307 struct btrfs_fs_info *fs_info = root->fs_info;
2308 struct list_head *devices;
2309 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002310 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002311 int ret = 0;
2312
2313 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002314 if (fs_info->fs_devices->seeding) {
2315 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002316 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002317 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002318
2319 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2320 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002321 if (IS_ERR(bdev)) {
2322 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002323 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002324 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002325
2326 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2327
2328 devices = &fs_info->fs_devices->devices;
2329 list_for_each_entry(device, devices, dev_list) {
2330 if (device->bdev == bdev) {
Miao Xie1c433662014-09-03 21:35:32 +08002331 btrfs_err(fs_info, "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002332 ret = -EEXIST;
2333 goto error;
2334 }
2335 }
2336
Miao Xie1c433662014-09-03 21:35:32 +08002337
2338 if (i_size_read(bdev->bd_inode) < srcdev->total_bytes) {
2339 btrfs_err(fs_info, "target device is smaller than source device!");
2340 ret = -EINVAL;
2341 goto error;
2342 }
2343
2344
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002345 device = btrfs_alloc_device(NULL, &devid, NULL);
2346 if (IS_ERR(device)) {
2347 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002348 goto error;
2349 }
2350
2351 name = rcu_string_strdup(device_path, GFP_NOFS);
2352 if (!name) {
2353 kfree(device);
2354 ret = -ENOMEM;
2355 goto error;
2356 }
2357 rcu_assign_pointer(device->name, name);
2358
2359 q = bdev_get_queue(bdev);
2360 if (blk_queue_discard(q))
2361 device->can_discard = 1;
2362 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2363 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002364 device->generation = 0;
2365 device->io_width = root->sectorsize;
2366 device->io_align = root->sectorsize;
2367 device->sector_size = root->sectorsize;
Miao Xie1c433662014-09-03 21:35:32 +08002368 device->total_bytes = srcdev->total_bytes;
2369 device->disk_total_bytes = srcdev->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002370 ASSERT(list_empty(&srcdev->resized_list));
2371 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xie1c433662014-09-03 21:35:32 +08002372 device->bytes_used = srcdev->bytes_used;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002373 device->dev_root = fs_info->dev_root;
2374 device->bdev = bdev;
2375 device->in_fs_metadata = 1;
2376 device->is_tgtdev_for_dev_replace = 1;
2377 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002378 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002379 set_blocksize(device->bdev, 4096);
2380 device->fs_devices = fs_info->fs_devices;
2381 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2382 fs_info->fs_devices->num_devices++;
2383 fs_info->fs_devices->open_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002384 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2385
2386 *device_out = device;
2387 return ret;
2388
2389error:
2390 blkdev_put(bdev, FMODE_EXCL);
2391 return ret;
2392}
2393
2394void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2395 struct btrfs_device *tgtdev)
2396{
2397 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2398 tgtdev->io_width = fs_info->dev_root->sectorsize;
2399 tgtdev->io_align = fs_info->dev_root->sectorsize;
2400 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2401 tgtdev->dev_root = fs_info->dev_root;
2402 tgtdev->in_fs_metadata = 1;
2403}
2404
Chris Masond3977122009-01-05 21:25:51 -05002405static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2406 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002407{
2408 int ret;
2409 struct btrfs_path *path;
2410 struct btrfs_root *root;
2411 struct btrfs_dev_item *dev_item;
2412 struct extent_buffer *leaf;
2413 struct btrfs_key key;
2414
2415 root = device->dev_root->fs_info->chunk_root;
2416
2417 path = btrfs_alloc_path();
2418 if (!path)
2419 return -ENOMEM;
2420
2421 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2422 key.type = BTRFS_DEV_ITEM_KEY;
2423 key.offset = device->devid;
2424
2425 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2426 if (ret < 0)
2427 goto out;
2428
2429 if (ret > 0) {
2430 ret = -ENOENT;
2431 goto out;
2432 }
2433
2434 leaf = path->nodes[0];
2435 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2436
2437 btrfs_set_device_id(leaf, dev_item, device->devid);
2438 btrfs_set_device_type(leaf, dev_item, device->type);
2439 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2440 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2441 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002442 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002443 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2444 btrfs_mark_buffer_dirty(leaf);
2445
2446out:
2447 btrfs_free_path(path);
2448 return ret;
2449}
2450
Chris Mason7d9eb122008-07-08 14:19:17 -04002451static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002452 struct btrfs_device *device, u64 new_size)
2453{
2454 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002455 device->dev_root->fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002456 struct btrfs_fs_devices *fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002457 u64 old_total = btrfs_super_total_bytes(super_copy);
2458 u64 diff = new_size - device->total_bytes;
2459
Yan Zheng2b820322008-11-17 21:11:30 -05002460 if (!device->writeable)
2461 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002462 if (new_size <= device->total_bytes ||
2463 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002464 return -EINVAL;
2465
Miao Xie935e5cc2014-09-03 21:35:33 +08002466 fs_devices = device->dev_root->fs_info->fs_devices;
2467
Chris Mason8f18cf12008-04-25 16:53:30 -04002468 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002469 device->fs_devices->total_rw_bytes += diff;
2470
2471 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002472 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002473 btrfs_clear_space_info_full(device->dev_root->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002474 if (list_empty(&device->resized_list))
2475 list_add_tail(&device->resized_list,
2476 &fs_devices->resized_devices);
Chris Mason4184ea72009-03-10 12:39:20 -04002477
Chris Mason8f18cf12008-04-25 16:53:30 -04002478 return btrfs_update_device(trans, device);
2479}
2480
Chris Mason7d9eb122008-07-08 14:19:17 -04002481int btrfs_grow_device(struct btrfs_trans_handle *trans,
2482 struct btrfs_device *device, u64 new_size)
2483{
2484 int ret;
2485 lock_chunks(device->dev_root);
2486 ret = __btrfs_grow_device(trans, device, new_size);
2487 unlock_chunks(device->dev_root);
2488 return ret;
2489}
2490
Chris Mason8f18cf12008-04-25 16:53:30 -04002491static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2492 struct btrfs_root *root,
2493 u64 chunk_tree, u64 chunk_objectid,
2494 u64 chunk_offset)
2495{
2496 int ret;
2497 struct btrfs_path *path;
2498 struct btrfs_key key;
2499
2500 root = root->fs_info->chunk_root;
2501 path = btrfs_alloc_path();
2502 if (!path)
2503 return -ENOMEM;
2504
2505 key.objectid = chunk_objectid;
2506 key.offset = chunk_offset;
2507 key.type = BTRFS_CHUNK_ITEM_KEY;
2508
2509 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002510 if (ret < 0)
2511 goto out;
2512 else if (ret > 0) { /* Logic error or corruption */
2513 btrfs_error(root->fs_info, -ENOENT,
2514 "Failed lookup while freeing chunk.");
2515 ret = -ENOENT;
2516 goto out;
2517 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002518
2519 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002520 if (ret < 0)
2521 btrfs_error(root->fs_info, ret,
2522 "Failed to delete chunk item.");
2523out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002524 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002525 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002526}
2527
Christoph Hellwigb2950862008-12-02 09:54:17 -05002528static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002529 chunk_offset)
2530{
David Sterba6c417612011-04-13 15:41:04 +02002531 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002532 struct btrfs_disk_key *disk_key;
2533 struct btrfs_chunk *chunk;
2534 u8 *ptr;
2535 int ret = 0;
2536 u32 num_stripes;
2537 u32 array_size;
2538 u32 len = 0;
2539 u32 cur;
2540 struct btrfs_key key;
2541
2542 array_size = btrfs_super_sys_array_size(super_copy);
2543
2544 ptr = super_copy->sys_chunk_array;
2545 cur = 0;
2546
2547 while (cur < array_size) {
2548 disk_key = (struct btrfs_disk_key *)ptr;
2549 btrfs_disk_key_to_cpu(&key, disk_key);
2550
2551 len = sizeof(*disk_key);
2552
2553 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2554 chunk = (struct btrfs_chunk *)(ptr + len);
2555 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2556 len += btrfs_chunk_item_size(num_stripes);
2557 } else {
2558 ret = -EIO;
2559 break;
2560 }
2561 if (key.objectid == chunk_objectid &&
2562 key.offset == chunk_offset) {
2563 memmove(ptr, ptr + len, array_size - (cur + len));
2564 array_size -= len;
2565 btrfs_set_super_sys_array_size(super_copy, array_size);
2566 } else {
2567 ptr += len;
2568 cur += len;
2569 }
2570 }
2571 return ret;
2572}
2573
Christoph Hellwigb2950862008-12-02 09:54:17 -05002574static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002575 u64 chunk_tree, u64 chunk_objectid,
2576 u64 chunk_offset)
2577{
2578 struct extent_map_tree *em_tree;
2579 struct btrfs_root *extent_root;
2580 struct btrfs_trans_handle *trans;
2581 struct extent_map *em;
2582 struct map_lookup *map;
2583 int ret;
2584 int i;
2585
2586 root = root->fs_info->chunk_root;
2587 extent_root = root->fs_info->extent_root;
2588 em_tree = &root->fs_info->mapping_tree.map_tree;
2589
Josef Bacikba1bf482009-09-11 16:11:19 -04002590 ret = btrfs_can_relocate(extent_root, chunk_offset);
2591 if (ret)
2592 return -ENOSPC;
2593
Chris Mason8f18cf12008-04-25 16:53:30 -04002594 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002595 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002596 if (ret)
2597 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002598
Yan, Zhenga22285a2010-05-16 10:48:46 -04002599 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002600 if (IS_ERR(trans)) {
2601 ret = PTR_ERR(trans);
2602 btrfs_std_error(root->fs_info, ret);
2603 return ret;
2604 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002605
Chris Mason7d9eb122008-07-08 14:19:17 -04002606 lock_chunks(root);
2607
Chris Mason8f18cf12008-04-25 16:53:30 -04002608 /*
2609 * step two, delete the device extents and the
2610 * chunk tree entries
2611 */
Chris Mason890871b2009-09-02 16:24:52 -04002612 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002613 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002614 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002615
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002616 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002617 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002618 map = (struct map_lookup *)em->bdev;
2619
2620 for (i = 0; i < map->num_stripes; i++) {
2621 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2622 map->stripes[i].physical);
2623 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002624
Chris Masondfe25022008-05-13 13:46:40 -04002625 if (map->stripes[i].dev) {
2626 ret = btrfs_update_device(trans, map->stripes[i].dev);
2627 BUG_ON(ret);
2628 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002629 }
2630 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2631 chunk_offset);
2632
2633 BUG_ON(ret);
2634
liubo1abe9b82011-03-24 11:18:59 +00002635 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2636
Chris Mason8f18cf12008-04-25 16:53:30 -04002637 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2638 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2639 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002640 }
2641
Zheng Yan1a40e232008-09-26 10:09:34 -04002642 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2643 BUG_ON(ret);
2644
Chris Mason890871b2009-09-02 16:24:52 -04002645 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002646 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002647 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002648
Chris Mason8f18cf12008-04-25 16:53:30 -04002649 /* once for the tree */
2650 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002651 /* once for us */
2652 free_extent_map(em);
2653
Chris Mason7d9eb122008-07-08 14:19:17 -04002654 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002655 btrfs_end_transaction(trans, root);
2656 return 0;
2657}
2658
Yan Zheng2b820322008-11-17 21:11:30 -05002659static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2660{
2661 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2662 struct btrfs_path *path;
2663 struct extent_buffer *leaf;
2664 struct btrfs_chunk *chunk;
2665 struct btrfs_key key;
2666 struct btrfs_key found_key;
2667 u64 chunk_tree = chunk_root->root_key.objectid;
2668 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002669 bool retried = false;
2670 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002671 int ret;
2672
2673 path = btrfs_alloc_path();
2674 if (!path)
2675 return -ENOMEM;
2676
Josef Bacikba1bf482009-09-11 16:11:19 -04002677again:
Yan Zheng2b820322008-11-17 21:11:30 -05002678 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2679 key.offset = (u64)-1;
2680 key.type = BTRFS_CHUNK_ITEM_KEY;
2681
2682 while (1) {
2683 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2684 if (ret < 0)
2685 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002686 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002687
2688 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2689 key.type);
2690 if (ret < 0)
2691 goto error;
2692 if (ret > 0)
2693 break;
2694
2695 leaf = path->nodes[0];
2696 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2697
2698 chunk = btrfs_item_ptr(leaf, path->slots[0],
2699 struct btrfs_chunk);
2700 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002701 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002702
2703 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2704 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2705 found_key.objectid,
2706 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002707 if (ret == -ENOSPC)
2708 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302709 else
2710 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002711 }
2712
2713 if (found_key.offset == 0)
2714 break;
2715 key.offset = found_key.offset - 1;
2716 }
2717 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002718 if (failed && !retried) {
2719 failed = 0;
2720 retried = true;
2721 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302722 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002723 ret = -ENOSPC;
2724 }
Yan Zheng2b820322008-11-17 21:11:30 -05002725error:
2726 btrfs_free_path(path);
2727 return ret;
2728}
2729
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002730static int insert_balance_item(struct btrfs_root *root,
2731 struct btrfs_balance_control *bctl)
2732{
2733 struct btrfs_trans_handle *trans;
2734 struct btrfs_balance_item *item;
2735 struct btrfs_disk_balance_args disk_bargs;
2736 struct btrfs_path *path;
2737 struct extent_buffer *leaf;
2738 struct btrfs_key key;
2739 int ret, err;
2740
2741 path = btrfs_alloc_path();
2742 if (!path)
2743 return -ENOMEM;
2744
2745 trans = btrfs_start_transaction(root, 0);
2746 if (IS_ERR(trans)) {
2747 btrfs_free_path(path);
2748 return PTR_ERR(trans);
2749 }
2750
2751 key.objectid = BTRFS_BALANCE_OBJECTID;
2752 key.type = BTRFS_BALANCE_ITEM_KEY;
2753 key.offset = 0;
2754
2755 ret = btrfs_insert_empty_item(trans, root, path, &key,
2756 sizeof(*item));
2757 if (ret)
2758 goto out;
2759
2760 leaf = path->nodes[0];
2761 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2762
2763 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2764
2765 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2766 btrfs_set_balance_data(leaf, item, &disk_bargs);
2767 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2768 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2769 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2770 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2771
2772 btrfs_set_balance_flags(leaf, item, bctl->flags);
2773
2774 btrfs_mark_buffer_dirty(leaf);
2775out:
2776 btrfs_free_path(path);
2777 err = btrfs_commit_transaction(trans, root);
2778 if (err && !ret)
2779 ret = err;
2780 return ret;
2781}
2782
2783static int del_balance_item(struct btrfs_root *root)
2784{
2785 struct btrfs_trans_handle *trans;
2786 struct btrfs_path *path;
2787 struct btrfs_key key;
2788 int ret, err;
2789
2790 path = btrfs_alloc_path();
2791 if (!path)
2792 return -ENOMEM;
2793
2794 trans = btrfs_start_transaction(root, 0);
2795 if (IS_ERR(trans)) {
2796 btrfs_free_path(path);
2797 return PTR_ERR(trans);
2798 }
2799
2800 key.objectid = BTRFS_BALANCE_OBJECTID;
2801 key.type = BTRFS_BALANCE_ITEM_KEY;
2802 key.offset = 0;
2803
2804 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2805 if (ret < 0)
2806 goto out;
2807 if (ret > 0) {
2808 ret = -ENOENT;
2809 goto out;
2810 }
2811
2812 ret = btrfs_del_item(trans, root, path);
2813out:
2814 btrfs_free_path(path);
2815 err = btrfs_commit_transaction(trans, root);
2816 if (err && !ret)
2817 ret = err;
2818 return ret;
2819}
2820
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002821/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002822 * This is a heuristic used to reduce the number of chunks balanced on
2823 * resume after balance was interrupted.
2824 */
2825static void update_balance_args(struct btrfs_balance_control *bctl)
2826{
2827 /*
2828 * Turn on soft mode for chunk types that were being converted.
2829 */
2830 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2831 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2832 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2833 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2834 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2835 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2836
2837 /*
2838 * Turn on usage filter if is not already used. The idea is
2839 * that chunks that we have already balanced should be
2840 * reasonably full. Don't do it for chunks that are being
2841 * converted - that will keep us from relocating unconverted
2842 * (albeit full) chunks.
2843 */
2844 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2845 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2846 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2847 bctl->data.usage = 90;
2848 }
2849 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2850 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2851 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2852 bctl->sys.usage = 90;
2853 }
2854 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2855 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2856 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2857 bctl->meta.usage = 90;
2858 }
2859}
2860
2861/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002862 * Should be called with both balance and volume mutexes held to
2863 * serialize other volume operations (add_dev/rm_dev/resize) with
2864 * restriper. Same goes for unset_balance_control.
2865 */
2866static void set_balance_control(struct btrfs_balance_control *bctl)
2867{
2868 struct btrfs_fs_info *fs_info = bctl->fs_info;
2869
2870 BUG_ON(fs_info->balance_ctl);
2871
2872 spin_lock(&fs_info->balance_lock);
2873 fs_info->balance_ctl = bctl;
2874 spin_unlock(&fs_info->balance_lock);
2875}
2876
2877static void unset_balance_control(struct btrfs_fs_info *fs_info)
2878{
2879 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2880
2881 BUG_ON(!fs_info->balance_ctl);
2882
2883 spin_lock(&fs_info->balance_lock);
2884 fs_info->balance_ctl = NULL;
2885 spin_unlock(&fs_info->balance_lock);
2886
2887 kfree(bctl);
2888}
2889
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002890/*
2891 * Balance filters. Return 1 if chunk should be filtered out
2892 * (should not be balanced).
2893 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002894static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002895 struct btrfs_balance_args *bargs)
2896{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002897 chunk_type = chunk_to_extended(chunk_type) &
2898 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002899
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002900 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002901 return 0;
2902
2903 return 1;
2904}
2905
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002906static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2907 struct btrfs_balance_args *bargs)
2908{
2909 struct btrfs_block_group_cache *cache;
2910 u64 chunk_used, user_thresh;
2911 int ret = 1;
2912
2913 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2914 chunk_used = btrfs_block_group_used(&cache->item);
2915
Ilya Dryomova105bb82013-01-21 15:15:56 +02002916 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002917 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002918 else if (bargs->usage > 100)
2919 user_thresh = cache->key.offset;
2920 else
2921 user_thresh = div_factor_fine(cache->key.offset,
2922 bargs->usage);
2923
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002924 if (chunk_used < user_thresh)
2925 ret = 0;
2926
2927 btrfs_put_block_group(cache);
2928 return ret;
2929}
2930
Ilya Dryomov409d4042012-01-16 22:04:47 +02002931static int chunk_devid_filter(struct extent_buffer *leaf,
2932 struct btrfs_chunk *chunk,
2933 struct btrfs_balance_args *bargs)
2934{
2935 struct btrfs_stripe *stripe;
2936 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2937 int i;
2938
2939 for (i = 0; i < num_stripes; i++) {
2940 stripe = btrfs_stripe_nr(chunk, i);
2941 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2942 return 0;
2943 }
2944
2945 return 1;
2946}
2947
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002948/* [pstart, pend) */
2949static int chunk_drange_filter(struct extent_buffer *leaf,
2950 struct btrfs_chunk *chunk,
2951 u64 chunk_offset,
2952 struct btrfs_balance_args *bargs)
2953{
2954 struct btrfs_stripe *stripe;
2955 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2956 u64 stripe_offset;
2957 u64 stripe_length;
2958 int factor;
2959 int i;
2960
2961 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2962 return 0;
2963
2964 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002965 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2966 factor = num_stripes / 2;
2967 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2968 factor = num_stripes - 1;
2969 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2970 factor = num_stripes - 2;
2971 } else {
2972 factor = num_stripes;
2973 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002974
2975 for (i = 0; i < num_stripes; i++) {
2976 stripe = btrfs_stripe_nr(chunk, i);
2977 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2978 continue;
2979
2980 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2981 stripe_length = btrfs_chunk_length(leaf, chunk);
2982 do_div(stripe_length, factor);
2983
2984 if (stripe_offset < bargs->pend &&
2985 stripe_offset + stripe_length > bargs->pstart)
2986 return 0;
2987 }
2988
2989 return 1;
2990}
2991
Ilya Dryomovea671762012-01-16 22:04:48 +02002992/* [vstart, vend) */
2993static int chunk_vrange_filter(struct extent_buffer *leaf,
2994 struct btrfs_chunk *chunk,
2995 u64 chunk_offset,
2996 struct btrfs_balance_args *bargs)
2997{
2998 if (chunk_offset < bargs->vend &&
2999 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3000 /* at least part of the chunk is inside this vrange */
3001 return 0;
3002
3003 return 1;
3004}
3005
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003006static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003007 struct btrfs_balance_args *bargs)
3008{
3009 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3010 return 0;
3011
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003012 chunk_type = chunk_to_extended(chunk_type) &
3013 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003014
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003015 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003016 return 1;
3017
3018 return 0;
3019}
3020
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003021static int should_balance_chunk(struct btrfs_root *root,
3022 struct extent_buffer *leaf,
3023 struct btrfs_chunk *chunk, u64 chunk_offset)
3024{
3025 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3026 struct btrfs_balance_args *bargs = NULL;
3027 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3028
3029 /* type filter */
3030 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3031 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3032 return 0;
3033 }
3034
3035 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3036 bargs = &bctl->data;
3037 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3038 bargs = &bctl->sys;
3039 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3040 bargs = &bctl->meta;
3041
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003042 /* profiles filter */
3043 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3044 chunk_profiles_filter(chunk_type, bargs)) {
3045 return 0;
3046 }
3047
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003048 /* usage filter */
3049 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3050 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3051 return 0;
3052 }
3053
Ilya Dryomov409d4042012-01-16 22:04:47 +02003054 /* devid filter */
3055 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3056 chunk_devid_filter(leaf, chunk, bargs)) {
3057 return 0;
3058 }
3059
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003060 /* drange filter, makes sense only with devid filter */
3061 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3062 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3063 return 0;
3064 }
3065
Ilya Dryomovea671762012-01-16 22:04:48 +02003066 /* vrange filter */
3067 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3068 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3069 return 0;
3070 }
3071
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003072 /* soft profile changing mode */
3073 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3074 chunk_soft_convert_filter(chunk_type, bargs)) {
3075 return 0;
3076 }
3077
David Sterba7d824b62014-05-07 17:37:51 +02003078 /*
3079 * limited by count, must be the last filter
3080 */
3081 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3082 if (bargs->limit == 0)
3083 return 0;
3084 else
3085 bargs->limit--;
3086 }
3087
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003088 return 1;
3089}
3090
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003091static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003092{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003093 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003094 struct btrfs_root *chunk_root = fs_info->chunk_root;
3095 struct btrfs_root *dev_root = fs_info->dev_root;
3096 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003097 struct btrfs_device *device;
3098 u64 old_size;
3099 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003100 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003101 struct btrfs_path *path;
3102 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003103 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003104 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003105 struct extent_buffer *leaf;
3106 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003107 int ret;
3108 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003109 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003110 u64 limit_data = bctl->data.limit;
3111 u64 limit_meta = bctl->meta.limit;
3112 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003113
Chris Masonec44a352008-04-28 15:29:52 -04003114 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003115 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003116 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04003117 old_size = device->total_bytes;
3118 size_to_free = div_factor(old_size, 1);
3119 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003120 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003121 device->total_bytes - device->bytes_used > size_to_free ||
3122 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003123 continue;
3124
3125 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003126 if (ret == -ENOSPC)
3127 break;
Chris Masonec44a352008-04-28 15:29:52 -04003128 BUG_ON(ret);
3129
Yan, Zhenga22285a2010-05-16 10:48:46 -04003130 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003131 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003132
3133 ret = btrfs_grow_device(trans, device, old_size);
3134 BUG_ON(ret);
3135
3136 btrfs_end_transaction(trans, dev_root);
3137 }
3138
3139 /* step two, relocate all the chunks */
3140 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003141 if (!path) {
3142 ret = -ENOMEM;
3143 goto error;
3144 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003145
3146 /* zero out stat counters */
3147 spin_lock(&fs_info->balance_lock);
3148 memset(&bctl->stat, 0, sizeof(bctl->stat));
3149 spin_unlock(&fs_info->balance_lock);
3150again:
David Sterba7d824b62014-05-07 17:37:51 +02003151 if (!counting) {
3152 bctl->data.limit = limit_data;
3153 bctl->meta.limit = limit_meta;
3154 bctl->sys.limit = limit_sys;
3155 }
Chris Masonec44a352008-04-28 15:29:52 -04003156 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3157 key.offset = (u64)-1;
3158 key.type = BTRFS_CHUNK_ITEM_KEY;
3159
Chris Masond3977122009-01-05 21:25:51 -05003160 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003161 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003162 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003163 ret = -ECANCELED;
3164 goto error;
3165 }
3166
Chris Masonec44a352008-04-28 15:29:52 -04003167 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3168 if (ret < 0)
3169 goto error;
3170
3171 /*
3172 * this shouldn't happen, it means the last relocate
3173 * failed
3174 */
3175 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003176 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003177
3178 ret = btrfs_previous_item(chunk_root, path, 0,
3179 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003180 if (ret) {
3181 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003182 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003183 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003184
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003185 leaf = path->nodes[0];
3186 slot = path->slots[0];
3187 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3188
Chris Masonec44a352008-04-28 15:29:52 -04003189 if (found_key.objectid != key.objectid)
3190 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04003191
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003192 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3193
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003194 if (!counting) {
3195 spin_lock(&fs_info->balance_lock);
3196 bctl->stat.considered++;
3197 spin_unlock(&fs_info->balance_lock);
3198 }
3199
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003200 ret = should_balance_chunk(chunk_root, leaf, chunk,
3201 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003202 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003203 if (!ret)
3204 goto loop;
3205
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003206 if (counting) {
3207 spin_lock(&fs_info->balance_lock);
3208 bctl->stat.expected++;
3209 spin_unlock(&fs_info->balance_lock);
3210 goto loop;
3211 }
3212
Chris Masonec44a352008-04-28 15:29:52 -04003213 ret = btrfs_relocate_chunk(chunk_root,
3214 chunk_root->root_key.objectid,
3215 found_key.objectid,
3216 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003217 if (ret && ret != -ENOSPC)
3218 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003219 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003220 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003221 } else {
3222 spin_lock(&fs_info->balance_lock);
3223 bctl->stat.completed++;
3224 spin_unlock(&fs_info->balance_lock);
3225 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003226loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003227 if (found_key.offset == 0)
3228 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003229 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003230 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003231
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003232 if (counting) {
3233 btrfs_release_path(path);
3234 counting = false;
3235 goto again;
3236 }
Chris Masonec44a352008-04-28 15:29:52 -04003237error:
3238 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003239 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003240 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003241 enospc_errors);
3242 if (!ret)
3243 ret = -ENOSPC;
3244 }
3245
Chris Masonec44a352008-04-28 15:29:52 -04003246 return ret;
3247}
3248
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003249/**
3250 * alloc_profile_is_valid - see if a given profile is valid and reduced
3251 * @flags: profile to validate
3252 * @extended: if true @flags is treated as an extended profile
3253 */
3254static int alloc_profile_is_valid(u64 flags, int extended)
3255{
3256 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3257 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3258
3259 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3260
3261 /* 1) check that all other bits are zeroed */
3262 if (flags & ~mask)
3263 return 0;
3264
3265 /* 2) see if profile is reduced */
3266 if (flags == 0)
3267 return !extended; /* "0" is valid for usual profiles */
3268
3269 /* true if exactly one bit set */
3270 return (flags & (flags - 1)) == 0;
3271}
3272
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003273static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3274{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003275 /* cancel requested || normal exit path */
3276 return atomic_read(&fs_info->balance_cancel_req) ||
3277 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3278 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003279}
3280
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003281static void __cancel_balance(struct btrfs_fs_info *fs_info)
3282{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003283 int ret;
3284
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003285 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003286 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003287 if (ret)
3288 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003289
3290 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003291}
3292
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003293/*
3294 * Should be called with both balance and volume mutexes held
3295 */
3296int btrfs_balance(struct btrfs_balance_control *bctl,
3297 struct btrfs_ioctl_balance_args *bargs)
3298{
3299 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003300 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003301 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003302 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003303 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003304 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003305
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003306 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003307 atomic_read(&fs_info->balance_pause_req) ||
3308 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003309 ret = -EINVAL;
3310 goto out;
3311 }
3312
Ilya Dryomove4837f82012-03-27 17:09:17 +03003313 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3314 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3315 mixed = 1;
3316
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003317 /*
3318 * In case of mixed groups both data and meta should be picked,
3319 * and identical options should be given for both of them.
3320 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003321 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3322 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003323 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3324 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3325 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003326 btrfs_err(fs_info, "with mixed groups data and "
3327 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003328 ret = -EINVAL;
3329 goto out;
3330 }
3331 }
3332
Stefan Behrens8dabb742012-11-06 13:15:27 +01003333 num_devices = fs_info->fs_devices->num_devices;
3334 btrfs_dev_replace_lock(&fs_info->dev_replace);
3335 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3336 BUG_ON(num_devices < 1);
3337 num_devices--;
3338 }
3339 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003340 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003341 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003342 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003343 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003344 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003345 if (num_devices > 2)
3346 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3347 if (num_devices > 3)
3348 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3349 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003350 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3351 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3352 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003353 btrfs_err(fs_info, "unable to start balance with target "
3354 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003355 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003356 ret = -EINVAL;
3357 goto out;
3358 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003359 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3360 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3361 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003362 btrfs_err(fs_info,
3363 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003364 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003365 ret = -EINVAL;
3366 goto out;
3367 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003368 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3369 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3370 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003371 btrfs_err(fs_info,
3372 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003373 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003374 ret = -EINVAL;
3375 goto out;
3376 }
3377
Ilya Dryomove4837f82012-03-27 17:09:17 +03003378 /* allow dup'ed data chunks only in mixed mode */
3379 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003380 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003381 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003382 ret = -EINVAL;
3383 goto out;
3384 }
3385
3386 /* allow to reduce meta or sys integrity only if force set */
3387 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003388 BTRFS_BLOCK_GROUP_RAID10 |
3389 BTRFS_BLOCK_GROUP_RAID5 |
3390 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003391 do {
3392 seq = read_seqbegin(&fs_info->profiles_lock);
3393
3394 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3395 (fs_info->avail_system_alloc_bits & allowed) &&
3396 !(bctl->sys.target & allowed)) ||
3397 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3398 (fs_info->avail_metadata_alloc_bits & allowed) &&
3399 !(bctl->meta.target & allowed))) {
3400 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003401 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003402 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003403 btrfs_err(fs_info, "balance will reduce metadata "
3404 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003405 ret = -EINVAL;
3406 goto out;
3407 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003408 }
Miao Xiede98ced2013-01-29 10:13:12 +00003409 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003410
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003411 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3412 int num_tolerated_disk_barrier_failures;
3413 u64 target = bctl->sys.target;
3414
3415 num_tolerated_disk_barrier_failures =
3416 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3417 if (num_tolerated_disk_barrier_failures > 0 &&
3418 (target &
3419 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3420 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3421 num_tolerated_disk_barrier_failures = 0;
3422 else if (num_tolerated_disk_barrier_failures > 1 &&
3423 (target &
3424 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3425 num_tolerated_disk_barrier_failures = 1;
3426
3427 fs_info->num_tolerated_disk_barrier_failures =
3428 num_tolerated_disk_barrier_failures;
3429 }
3430
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003431 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003432 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003433 goto out;
3434
Ilya Dryomov59641012012-01-16 22:04:48 +02003435 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3436 BUG_ON(ret == -EEXIST);
3437 set_balance_control(bctl);
3438 } else {
3439 BUG_ON(ret != -EEXIST);
3440 spin_lock(&fs_info->balance_lock);
3441 update_balance_args(bctl);
3442 spin_unlock(&fs_info->balance_lock);
3443 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003444
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003445 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003446 mutex_unlock(&fs_info->balance_mutex);
3447
3448 ret = __btrfs_balance(fs_info);
3449
3450 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003451 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003452
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003453 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3454 fs_info->num_tolerated_disk_barrier_failures =
3455 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3456 }
3457
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003458 if (bargs) {
3459 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003460 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003461 }
3462
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003463 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3464 balance_need_close(fs_info)) {
3465 __cancel_balance(fs_info);
3466 }
3467
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003468 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003469
3470 return ret;
3471out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003472 if (bctl->flags & BTRFS_BALANCE_RESUME)
3473 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003474 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003475 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003476 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3477 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003478 return ret;
3479}
3480
3481static int balance_kthread(void *data)
3482{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003483 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003484 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003485
3486 mutex_lock(&fs_info->volume_mutex);
3487 mutex_lock(&fs_info->balance_mutex);
3488
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003489 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003490 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003491 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003492 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003493
3494 mutex_unlock(&fs_info->balance_mutex);
3495 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003496
Ilya Dryomov59641012012-01-16 22:04:48 +02003497 return ret;
3498}
3499
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003500int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3501{
3502 struct task_struct *tsk;
3503
3504 spin_lock(&fs_info->balance_lock);
3505 if (!fs_info->balance_ctl) {
3506 spin_unlock(&fs_info->balance_lock);
3507 return 0;
3508 }
3509 spin_unlock(&fs_info->balance_lock);
3510
3511 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003512 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003513 return 0;
3514 }
3515
3516 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303517 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003518}
3519
Ilya Dryomov68310a52012-06-22 12:24:12 -06003520int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003521{
Ilya Dryomov59641012012-01-16 22:04:48 +02003522 struct btrfs_balance_control *bctl;
3523 struct btrfs_balance_item *item;
3524 struct btrfs_disk_balance_args disk_bargs;
3525 struct btrfs_path *path;
3526 struct extent_buffer *leaf;
3527 struct btrfs_key key;
3528 int ret;
3529
3530 path = btrfs_alloc_path();
3531 if (!path)
3532 return -ENOMEM;
3533
Ilya Dryomov68310a52012-06-22 12:24:12 -06003534 key.objectid = BTRFS_BALANCE_OBJECTID;
3535 key.type = BTRFS_BALANCE_ITEM_KEY;
3536 key.offset = 0;
3537
3538 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3539 if (ret < 0)
3540 goto out;
3541 if (ret > 0) { /* ret = -ENOENT; */
3542 ret = 0;
3543 goto out;
3544 }
3545
Ilya Dryomov59641012012-01-16 22:04:48 +02003546 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3547 if (!bctl) {
3548 ret = -ENOMEM;
3549 goto out;
3550 }
3551
Ilya Dryomov59641012012-01-16 22:04:48 +02003552 leaf = path->nodes[0];
3553 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3554
Ilya Dryomov68310a52012-06-22 12:24:12 -06003555 bctl->fs_info = fs_info;
3556 bctl->flags = btrfs_balance_flags(leaf, item);
3557 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003558
3559 btrfs_balance_data(leaf, item, &disk_bargs);
3560 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3561 btrfs_balance_meta(leaf, item, &disk_bargs);
3562 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3563 btrfs_balance_sys(leaf, item, &disk_bargs);
3564 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3565
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003566 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3567
Ilya Dryomov68310a52012-06-22 12:24:12 -06003568 mutex_lock(&fs_info->volume_mutex);
3569 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003570
Ilya Dryomov68310a52012-06-22 12:24:12 -06003571 set_balance_control(bctl);
3572
3573 mutex_unlock(&fs_info->balance_mutex);
3574 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003575out:
3576 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003577 return ret;
3578}
3579
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003580int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3581{
3582 int ret = 0;
3583
3584 mutex_lock(&fs_info->balance_mutex);
3585 if (!fs_info->balance_ctl) {
3586 mutex_unlock(&fs_info->balance_mutex);
3587 return -ENOTCONN;
3588 }
3589
3590 if (atomic_read(&fs_info->balance_running)) {
3591 atomic_inc(&fs_info->balance_pause_req);
3592 mutex_unlock(&fs_info->balance_mutex);
3593
3594 wait_event(fs_info->balance_wait_q,
3595 atomic_read(&fs_info->balance_running) == 0);
3596
3597 mutex_lock(&fs_info->balance_mutex);
3598 /* we are good with balance_ctl ripped off from under us */
3599 BUG_ON(atomic_read(&fs_info->balance_running));
3600 atomic_dec(&fs_info->balance_pause_req);
3601 } else {
3602 ret = -ENOTCONN;
3603 }
3604
3605 mutex_unlock(&fs_info->balance_mutex);
3606 return ret;
3607}
3608
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003609int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3610{
Ilya Dryomove649e582013-10-10 20:40:21 +03003611 if (fs_info->sb->s_flags & MS_RDONLY)
3612 return -EROFS;
3613
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003614 mutex_lock(&fs_info->balance_mutex);
3615 if (!fs_info->balance_ctl) {
3616 mutex_unlock(&fs_info->balance_mutex);
3617 return -ENOTCONN;
3618 }
3619
3620 atomic_inc(&fs_info->balance_cancel_req);
3621 /*
3622 * if we are running just wait and return, balance item is
3623 * deleted in btrfs_balance in this case
3624 */
3625 if (atomic_read(&fs_info->balance_running)) {
3626 mutex_unlock(&fs_info->balance_mutex);
3627 wait_event(fs_info->balance_wait_q,
3628 atomic_read(&fs_info->balance_running) == 0);
3629 mutex_lock(&fs_info->balance_mutex);
3630 } else {
3631 /* __cancel_balance needs volume_mutex */
3632 mutex_unlock(&fs_info->balance_mutex);
3633 mutex_lock(&fs_info->volume_mutex);
3634 mutex_lock(&fs_info->balance_mutex);
3635
3636 if (fs_info->balance_ctl)
3637 __cancel_balance(fs_info);
3638
3639 mutex_unlock(&fs_info->volume_mutex);
3640 }
3641
3642 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3643 atomic_dec(&fs_info->balance_cancel_req);
3644 mutex_unlock(&fs_info->balance_mutex);
3645 return 0;
3646}
3647
Stefan Behrens803b2f52013-08-15 17:11:21 +02003648static int btrfs_uuid_scan_kthread(void *data)
3649{
3650 struct btrfs_fs_info *fs_info = data;
3651 struct btrfs_root *root = fs_info->tree_root;
3652 struct btrfs_key key;
3653 struct btrfs_key max_key;
3654 struct btrfs_path *path = NULL;
3655 int ret = 0;
3656 struct extent_buffer *eb;
3657 int slot;
3658 struct btrfs_root_item root_item;
3659 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003660 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003661
3662 path = btrfs_alloc_path();
3663 if (!path) {
3664 ret = -ENOMEM;
3665 goto out;
3666 }
3667
3668 key.objectid = 0;
3669 key.type = BTRFS_ROOT_ITEM_KEY;
3670 key.offset = 0;
3671
3672 max_key.objectid = (u64)-1;
3673 max_key.type = BTRFS_ROOT_ITEM_KEY;
3674 max_key.offset = (u64)-1;
3675
Stefan Behrens803b2f52013-08-15 17:11:21 +02003676 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003677 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003678 if (ret) {
3679 if (ret > 0)
3680 ret = 0;
3681 break;
3682 }
3683
3684 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3685 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3686 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3687 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3688 goto skip;
3689
3690 eb = path->nodes[0];
3691 slot = path->slots[0];
3692 item_size = btrfs_item_size_nr(eb, slot);
3693 if (item_size < sizeof(root_item))
3694 goto skip;
3695
Stefan Behrens803b2f52013-08-15 17:11:21 +02003696 read_extent_buffer(eb, &root_item,
3697 btrfs_item_ptr_offset(eb, slot),
3698 (int)sizeof(root_item));
3699 if (btrfs_root_refs(&root_item) == 0)
3700 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003701
3702 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3703 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3704 if (trans)
3705 goto update_tree;
3706
3707 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003708 /*
3709 * 1 - subvol uuid item
3710 * 1 - received_subvol uuid item
3711 */
3712 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3713 if (IS_ERR(trans)) {
3714 ret = PTR_ERR(trans);
3715 break;
3716 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003717 continue;
3718 } else {
3719 goto skip;
3720 }
3721update_tree:
3722 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003723 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3724 root_item.uuid,
3725 BTRFS_UUID_KEY_SUBVOL,
3726 key.objectid);
3727 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003728 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003729 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003730 break;
3731 }
3732 }
3733
3734 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003735 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3736 root_item.received_uuid,
3737 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3738 key.objectid);
3739 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003740 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003741 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003742 break;
3743 }
3744 }
3745
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003746skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003747 if (trans) {
3748 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003749 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003750 if (ret)
3751 break;
3752 }
3753
Stefan Behrens803b2f52013-08-15 17:11:21 +02003754 btrfs_release_path(path);
3755 if (key.offset < (u64)-1) {
3756 key.offset++;
3757 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3758 key.offset = 0;
3759 key.type = BTRFS_ROOT_ITEM_KEY;
3760 } else if (key.objectid < (u64)-1) {
3761 key.offset = 0;
3762 key.type = BTRFS_ROOT_ITEM_KEY;
3763 key.objectid++;
3764 } else {
3765 break;
3766 }
3767 cond_resched();
3768 }
3769
3770out:
3771 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003772 if (trans && !IS_ERR(trans))
3773 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003774 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05003775 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003776 else
3777 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003778 up(&fs_info->uuid_tree_rescan_sem);
3779 return 0;
3780}
3781
Stefan Behrens70f80172013-08-15 17:11:23 +02003782/*
3783 * Callback for btrfs_uuid_tree_iterate().
3784 * returns:
3785 * 0 check succeeded, the entry is not outdated.
3786 * < 0 if an error occured.
3787 * > 0 if the check failed, which means the caller shall remove the entry.
3788 */
3789static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3790 u8 *uuid, u8 type, u64 subid)
3791{
3792 struct btrfs_key key;
3793 int ret = 0;
3794 struct btrfs_root *subvol_root;
3795
3796 if (type != BTRFS_UUID_KEY_SUBVOL &&
3797 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3798 goto out;
3799
3800 key.objectid = subid;
3801 key.type = BTRFS_ROOT_ITEM_KEY;
3802 key.offset = (u64)-1;
3803 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3804 if (IS_ERR(subvol_root)) {
3805 ret = PTR_ERR(subvol_root);
3806 if (ret == -ENOENT)
3807 ret = 1;
3808 goto out;
3809 }
3810
3811 switch (type) {
3812 case BTRFS_UUID_KEY_SUBVOL:
3813 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3814 ret = 1;
3815 break;
3816 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3817 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3818 BTRFS_UUID_SIZE))
3819 ret = 1;
3820 break;
3821 }
3822
3823out:
3824 return ret;
3825}
3826
3827static int btrfs_uuid_rescan_kthread(void *data)
3828{
3829 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3830 int ret;
3831
3832 /*
3833 * 1st step is to iterate through the existing UUID tree and
3834 * to delete all entries that contain outdated data.
3835 * 2nd step is to add all missing entries to the UUID tree.
3836 */
3837 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3838 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003839 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003840 up(&fs_info->uuid_tree_rescan_sem);
3841 return ret;
3842 }
3843 return btrfs_uuid_scan_kthread(data);
3844}
3845
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003846int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3847{
3848 struct btrfs_trans_handle *trans;
3849 struct btrfs_root *tree_root = fs_info->tree_root;
3850 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003851 struct task_struct *task;
3852 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003853
3854 /*
3855 * 1 - root node
3856 * 1 - root item
3857 */
3858 trans = btrfs_start_transaction(tree_root, 2);
3859 if (IS_ERR(trans))
3860 return PTR_ERR(trans);
3861
3862 uuid_root = btrfs_create_tree(trans, fs_info,
3863 BTRFS_UUID_TREE_OBJECTID);
3864 if (IS_ERR(uuid_root)) {
3865 btrfs_abort_transaction(trans, tree_root,
3866 PTR_ERR(uuid_root));
3867 return PTR_ERR(uuid_root);
3868 }
3869
3870 fs_info->uuid_root = uuid_root;
3871
Stefan Behrens803b2f52013-08-15 17:11:21 +02003872 ret = btrfs_commit_transaction(trans, tree_root);
3873 if (ret)
3874 return ret;
3875
3876 down(&fs_info->uuid_tree_rescan_sem);
3877 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3878 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02003879 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003880 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02003881 up(&fs_info->uuid_tree_rescan_sem);
3882 return PTR_ERR(task);
3883 }
3884
3885 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003886}
Stefan Behrens803b2f52013-08-15 17:11:21 +02003887
Stefan Behrens70f80172013-08-15 17:11:23 +02003888int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3889{
3890 struct task_struct *task;
3891
3892 down(&fs_info->uuid_tree_rescan_sem);
3893 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3894 if (IS_ERR(task)) {
3895 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003896 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02003897 up(&fs_info->uuid_tree_rescan_sem);
3898 return PTR_ERR(task);
3899 }
3900
3901 return 0;
3902}
3903
Chris Mason8f18cf12008-04-25 16:53:30 -04003904/*
3905 * shrinking a device means finding all of the device extents past
3906 * the new size, and then following the back refs to the chunks.
3907 * The chunk relocation code actually frees the device extent
3908 */
3909int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3910{
3911 struct btrfs_trans_handle *trans;
3912 struct btrfs_root *root = device->dev_root;
3913 struct btrfs_dev_extent *dev_extent = NULL;
3914 struct btrfs_path *path;
3915 u64 length;
3916 u64 chunk_tree;
3917 u64 chunk_objectid;
3918 u64 chunk_offset;
3919 int ret;
3920 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003921 int failed = 0;
3922 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003923 struct extent_buffer *l;
3924 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003925 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003926 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003927 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003928 u64 diff = device->total_bytes - new_size;
3929
Stefan Behrens63a212a2012-11-05 18:29:28 +01003930 if (device->is_tgtdev_for_dev_replace)
3931 return -EINVAL;
3932
Chris Mason8f18cf12008-04-25 16:53:30 -04003933 path = btrfs_alloc_path();
3934 if (!path)
3935 return -ENOMEM;
3936
Chris Mason8f18cf12008-04-25 16:53:30 -04003937 path->reada = 2;
3938
Chris Mason7d9eb122008-07-08 14:19:17 -04003939 lock_chunks(root);
3940
Chris Mason8f18cf12008-04-25 16:53:30 -04003941 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003942 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003943 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003944 spin_lock(&root->fs_info->free_chunk_lock);
3945 root->fs_info->free_chunk_space -= diff;
3946 spin_unlock(&root->fs_info->free_chunk_lock);
3947 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003948 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003949
Josef Bacikba1bf482009-09-11 16:11:19 -04003950again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003951 key.objectid = device->devid;
3952 key.offset = (u64)-1;
3953 key.type = BTRFS_DEV_EXTENT_KEY;
3954
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003955 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003956 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3957 if (ret < 0)
3958 goto done;
3959
3960 ret = btrfs_previous_item(root, path, 0, key.type);
3961 if (ret < 0)
3962 goto done;
3963 if (ret) {
3964 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003965 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003966 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003967 }
3968
3969 l = path->nodes[0];
3970 slot = path->slots[0];
3971 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3972
Josef Bacikba1bf482009-09-11 16:11:19 -04003973 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003974 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003975 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003976 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003977
3978 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3979 length = btrfs_dev_extent_length(l, dev_extent);
3980
Josef Bacikba1bf482009-09-11 16:11:19 -04003981 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003982 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003983 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003984 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003985
3986 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3987 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3988 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003989 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003990
3991 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3992 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003993 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003994 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003995 if (ret == -ENOSPC)
3996 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003997 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003998
3999 if (failed && !retried) {
4000 failed = 0;
4001 retried = true;
4002 goto again;
4003 } else if (failed && retried) {
4004 ret = -ENOSPC;
4005 lock_chunks(root);
4006
4007 device->total_bytes = old_size;
4008 if (device->writeable)
4009 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04004010 spin_lock(&root->fs_info->free_chunk_lock);
4011 root->fs_info->free_chunk_space += diff;
4012 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04004013 unlock_chunks(root);
4014 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004015 }
4016
Chris Balld6397ba2009-04-27 07:29:03 -04004017 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004018 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004019 if (IS_ERR(trans)) {
4020 ret = PTR_ERR(trans);
4021 goto done;
4022 }
4023
Chris Balld6397ba2009-04-27 07:29:03 -04004024 lock_chunks(root);
Chris Balld6397ba2009-04-27 07:29:03 -04004025 device->disk_total_bytes = new_size;
Miao Xie935e5cc2014-09-03 21:35:33 +08004026 if (list_empty(&device->resized_list))
4027 list_add_tail(&device->resized_list,
4028 &root->fs_info->fs_devices->resized_devices);
4029
Chris Balld6397ba2009-04-27 07:29:03 -04004030 /* Now btrfs_update_device() will change the on-disk size. */
4031 ret = btrfs_update_device(trans, device);
4032 if (ret) {
4033 unlock_chunks(root);
4034 btrfs_end_transaction(trans, root);
4035 goto done;
4036 }
4037 WARN_ON(diff > old_total);
4038 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4039 unlock_chunks(root);
4040 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004041done:
4042 btrfs_free_path(path);
4043 return ret;
4044}
4045
Li Zefan125ccb02011-12-08 15:07:24 +08004046static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004047 struct btrfs_key *key,
4048 struct btrfs_chunk *chunk, int item_size)
4049{
David Sterba6c417612011-04-13 15:41:04 +02004050 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004051 struct btrfs_disk_key disk_key;
4052 u32 array_size;
4053 u8 *ptr;
4054
4055 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004056 if (array_size + item_size + sizeof(disk_key)
4057 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
Chris Mason0b86a832008-03-24 15:01:56 -04004058 return -EFBIG;
4059
4060 ptr = super_copy->sys_chunk_array + array_size;
4061 btrfs_cpu_key_to_disk(&disk_key, key);
4062 memcpy(ptr, &disk_key, sizeof(disk_key));
4063 ptr += sizeof(disk_key);
4064 memcpy(ptr, chunk, item_size);
4065 item_size += sizeof(disk_key);
4066 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
4067 return 0;
4068}
4069
Miao Xieb2117a32011-01-05 10:07:28 +00004070/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004071 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004072 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004073static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004074{
Arne Jansen73c5de02011-04-12 12:07:57 +02004075 const struct btrfs_device_info *di_a = a;
4076 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004077
Arne Jansen73c5de02011-04-12 12:07:57 +02004078 if (di_a->max_avail > di_b->max_avail)
4079 return -1;
4080 if (di_a->max_avail < di_b->max_avail)
4081 return 1;
4082 if (di_a->total_avail > di_b->total_avail)
4083 return -1;
4084 if (di_a->total_avail < di_b->total_avail)
4085 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004086 return 0;
4087}
4088
Eric Sandeen48a3b632013-04-25 20:41:01 +00004089static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00004090 [BTRFS_RAID_RAID10] = {
4091 .sub_stripes = 2,
4092 .dev_stripes = 1,
4093 .devs_max = 0, /* 0 == as many as possible */
4094 .devs_min = 4,
4095 .devs_increment = 2,
4096 .ncopies = 2,
4097 },
4098 [BTRFS_RAID_RAID1] = {
4099 .sub_stripes = 1,
4100 .dev_stripes = 1,
4101 .devs_max = 2,
4102 .devs_min = 2,
4103 .devs_increment = 2,
4104 .ncopies = 2,
4105 },
4106 [BTRFS_RAID_DUP] = {
4107 .sub_stripes = 1,
4108 .dev_stripes = 2,
4109 .devs_max = 1,
4110 .devs_min = 1,
4111 .devs_increment = 1,
4112 .ncopies = 2,
4113 },
4114 [BTRFS_RAID_RAID0] = {
4115 .sub_stripes = 1,
4116 .dev_stripes = 1,
4117 .devs_max = 0,
4118 .devs_min = 2,
4119 .devs_increment = 1,
4120 .ncopies = 1,
4121 },
4122 [BTRFS_RAID_SINGLE] = {
4123 .sub_stripes = 1,
4124 .dev_stripes = 1,
4125 .devs_max = 1,
4126 .devs_min = 1,
4127 .devs_increment = 1,
4128 .ncopies = 1,
4129 },
Chris Masone942f882013-02-20 14:06:05 -05004130 [BTRFS_RAID_RAID5] = {
4131 .sub_stripes = 1,
4132 .dev_stripes = 1,
4133 .devs_max = 0,
4134 .devs_min = 2,
4135 .devs_increment = 1,
4136 .ncopies = 2,
4137 },
4138 [BTRFS_RAID_RAID6] = {
4139 .sub_stripes = 1,
4140 .dev_stripes = 1,
4141 .devs_max = 0,
4142 .devs_min = 3,
4143 .devs_increment = 1,
4144 .ncopies = 3,
4145 },
Liu Bo31e50222012-11-21 14:18:10 +00004146};
4147
David Woodhouse53b381b2013-01-29 18:40:14 -05004148static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4149{
4150 /* TODO allow them to set a preferred stripe size */
4151 return 64 * 1024;
4152}
4153
4154static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4155{
David Woodhouse53b381b2013-01-29 18:40:14 -05004156 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
4157 return;
4158
Miao Xieceda0862013-04-11 10:30:16 +00004159 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004160}
4161
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004162#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4163 - sizeof(struct btrfs_item) \
4164 - sizeof(struct btrfs_chunk)) \
4165 / sizeof(struct btrfs_stripe) + 1)
4166
4167#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4168 - 2 * sizeof(struct btrfs_disk_key) \
4169 - 2 * sizeof(struct btrfs_chunk)) \
4170 / sizeof(struct btrfs_stripe) + 1)
4171
Miao Xieb2117a32011-01-05 10:07:28 +00004172static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004173 struct btrfs_root *extent_root, u64 start,
4174 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004175{
4176 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004177 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4178 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004179 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004180 struct extent_map_tree *em_tree;
4181 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004182 struct btrfs_device_info *devices_info = NULL;
4183 u64 total_avail;
4184 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004185 int data_stripes; /* number of stripes that count for
4186 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004187 int sub_stripes; /* sub_stripes info for map */
4188 int dev_stripes; /* stripes per dev */
4189 int devs_max; /* max devs to use */
4190 int devs_min; /* min devs needed */
4191 int devs_increment; /* ndevs has to be a multiple of this */
4192 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004193 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004194 u64 max_stripe_size;
4195 u64 max_chunk_size;
4196 u64 stripe_size;
4197 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004198 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004199 int ndevs;
4200 int i;
4201 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004202 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004203
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004204 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004205
Miao Xieb2117a32011-01-05 10:07:28 +00004206 if (list_empty(&fs_devices->alloc_list))
4207 return -ENOSPC;
4208
Liu Bo31e50222012-11-21 14:18:10 +00004209 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004210
Liu Bo31e50222012-11-21 14:18:10 +00004211 sub_stripes = btrfs_raid_array[index].sub_stripes;
4212 dev_stripes = btrfs_raid_array[index].dev_stripes;
4213 devs_max = btrfs_raid_array[index].devs_max;
4214 devs_min = btrfs_raid_array[index].devs_min;
4215 devs_increment = btrfs_raid_array[index].devs_increment;
4216 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004217
4218 if (type & BTRFS_BLOCK_GROUP_DATA) {
4219 max_stripe_size = 1024 * 1024 * 1024;
4220 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004221 if (!devs_max)
4222 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004223 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004224 /* for larger filesystems, use larger metadata chunks */
4225 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4226 max_stripe_size = 1024 * 1024 * 1024;
4227 else
4228 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004229 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004230 if (!devs_max)
4231 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004232 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004233 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004234 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004235 if (!devs_max)
4236 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004237 } else {
David Sterba351fd352014-05-15 16:48:20 +02004238 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004239 type);
4240 BUG_ON(1);
4241 }
4242
4243 /* we don't want a chunk larger than 10% of writeable space */
4244 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4245 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004246
4247 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
4248 GFP_NOFS);
4249 if (!devices_info)
4250 return -ENOMEM;
4251
Arne Jansen73c5de02011-04-12 12:07:57 +02004252 cur = fs_devices->alloc_list.next;
4253
4254 /*
4255 * in the first pass through the devices list, we gather information
4256 * about the available holes on each device.
4257 */
4258 ndevs = 0;
4259 while (cur != &fs_devices->alloc_list) {
4260 struct btrfs_device *device;
4261 u64 max_avail;
4262 u64 dev_offset;
4263
4264 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4265
4266 cur = cur->next;
4267
4268 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004269 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004270 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004271 continue;
4272 }
4273
Stefan Behrens63a212a2012-11-05 18:29:28 +01004274 if (!device->in_fs_metadata ||
4275 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004276 continue;
4277
4278 if (device->total_bytes > device->bytes_used)
4279 total_avail = device->total_bytes - device->bytes_used;
4280 else
4281 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004282
4283 /* If there is no space on this device, skip it. */
4284 if (total_avail == 0)
4285 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004286
Josef Bacik6df9a952013-06-27 13:22:46 -04004287 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004288 max_stripe_size * dev_stripes,
4289 &dev_offset, &max_avail);
4290 if (ret && ret != -ENOSPC)
4291 goto error;
4292
4293 if (ret == 0)
4294 max_avail = max_stripe_size * dev_stripes;
4295
4296 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4297 continue;
4298
Eric Sandeen063d0062013-01-31 00:55:01 +00004299 if (ndevs == fs_devices->rw_devices) {
4300 WARN(1, "%s: found more than %llu devices\n",
4301 __func__, fs_devices->rw_devices);
4302 break;
4303 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004304 devices_info[ndevs].dev_offset = dev_offset;
4305 devices_info[ndevs].max_avail = max_avail;
4306 devices_info[ndevs].total_avail = total_avail;
4307 devices_info[ndevs].dev = device;
4308 ++ndevs;
4309 }
4310
4311 /*
4312 * now sort the devices by hole size / available space
4313 */
4314 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4315 btrfs_cmp_device_info, NULL);
4316
4317 /* round down to number of usable stripes */
4318 ndevs -= ndevs % devs_increment;
4319
4320 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4321 ret = -ENOSPC;
4322 goto error;
4323 }
4324
4325 if (devs_max && ndevs > devs_max)
4326 ndevs = devs_max;
4327 /*
4328 * the primary goal is to maximize the number of stripes, so use as many
4329 * devices as possible, even if the stripes are not maximum sized.
4330 */
4331 stripe_size = devices_info[ndevs-1].max_avail;
4332 num_stripes = ndevs * dev_stripes;
4333
David Woodhouse53b381b2013-01-29 18:40:14 -05004334 /*
4335 * this will have to be fixed for RAID1 and RAID10 over
4336 * more drives
4337 */
4338 data_stripes = num_stripes / ncopies;
4339
David Woodhouse53b381b2013-01-29 18:40:14 -05004340 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4341 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4342 btrfs_super_stripesize(info->super_copy));
4343 data_stripes = num_stripes - 1;
4344 }
4345 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4346 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4347 btrfs_super_stripesize(info->super_copy));
4348 data_stripes = num_stripes - 2;
4349 }
Chris Mason86db2572013-02-20 16:23:40 -05004350
4351 /*
4352 * Use the number of data stripes to figure out how big this chunk
4353 * is really going to be in terms of logical address space,
4354 * and compare that answer with the max chunk size
4355 */
4356 if (stripe_size * data_stripes > max_chunk_size) {
4357 u64 mask = (1ULL << 24) - 1;
4358 stripe_size = max_chunk_size;
4359 do_div(stripe_size, data_stripes);
4360
4361 /* bump the answer up to a 16MB boundary */
4362 stripe_size = (stripe_size + mask) & ~mask;
4363
4364 /* but don't go higher than the limits we found
4365 * while searching for free extents
4366 */
4367 if (stripe_size > devices_info[ndevs-1].max_avail)
4368 stripe_size = devices_info[ndevs-1].max_avail;
4369 }
4370
Arne Jansen73c5de02011-04-12 12:07:57 +02004371 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004372
4373 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05004374 do_div(stripe_size, raid_stripe_len);
4375 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004376
Miao Xieb2117a32011-01-05 10:07:28 +00004377 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4378 if (!map) {
4379 ret = -ENOMEM;
4380 goto error;
4381 }
4382 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004383
Arne Jansen73c5de02011-04-12 12:07:57 +02004384 for (i = 0; i < ndevs; ++i) {
4385 for (j = 0; j < dev_stripes; ++j) {
4386 int s = i * dev_stripes + j;
4387 map->stripes[s].dev = devices_info[i].dev;
4388 map->stripes[s].physical = devices_info[i].dev_offset +
4389 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004390 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004391 }
Chris Mason593060d2008-03-25 16:50:33 -04004392 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004393 map->stripe_len = raid_stripe_len;
4394 map->io_align = raid_stripe_len;
4395 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004396 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004397 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004398
David Woodhouse53b381b2013-01-29 18:40:14 -05004399 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004400
Arne Jansen73c5de02011-04-12 12:07:57 +02004401 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004402
David Sterba172ddd62011-04-21 00:48:27 +02004403 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004404 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004405 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004406 ret = -ENOMEM;
4407 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004408 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004409 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004410 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004411 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004412 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004413 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004414 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004415 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004416
Chris Mason0b86a832008-03-24 15:01:56 -04004417 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004418 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004419 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004420 if (!ret) {
4421 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4422 atomic_inc(&em->refs);
4423 }
Chris Mason890871b2009-09-02 16:24:52 -04004424 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004425 if (ret) {
4426 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004427 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004428 }
Yan Zheng2b820322008-11-17 21:11:30 -05004429
Josef Bacik04487482013-01-29 15:03:37 -05004430 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4431 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4432 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004433 if (ret)
4434 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004435
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004436 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004437 check_raid56_incompat_flag(extent_root->fs_info, type);
4438
Miao Xieb2117a32011-01-05 10:07:28 +00004439 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004440 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004441
Josef Bacik6df9a952013-06-27 13:22:46 -04004442error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004443 write_lock(&em_tree->lock);
4444 remove_extent_mapping(em_tree, em);
4445 write_unlock(&em_tree->lock);
4446
4447 /* One for our allocation */
4448 free_extent_map(em);
4449 /* One for the tree reference */
4450 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004451error:
Miao Xieb2117a32011-01-05 10:07:28 +00004452 kfree(devices_info);
4453 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004454}
4455
Josef Bacik6df9a952013-06-27 13:22:46 -04004456int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004457 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004458 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004459{
Yan Zheng2b820322008-11-17 21:11:30 -05004460 struct btrfs_key key;
4461 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4462 struct btrfs_device *device;
4463 struct btrfs_chunk *chunk;
4464 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004465 struct extent_map_tree *em_tree;
4466 struct extent_map *em;
4467 struct map_lookup *map;
4468 size_t item_size;
4469 u64 dev_offset;
4470 u64 stripe_size;
4471 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004472 int ret;
4473
Josef Bacik6df9a952013-06-27 13:22:46 -04004474 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4475 read_lock(&em_tree->lock);
4476 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4477 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004478
Josef Bacik6df9a952013-06-27 13:22:46 -04004479 if (!em) {
4480 btrfs_crit(extent_root->fs_info, "unable to find logical "
4481 "%Lu len %Lu", chunk_offset, chunk_size);
4482 return -EINVAL;
4483 }
4484
4485 if (em->start != chunk_offset || em->len != chunk_size) {
4486 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004487 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004488 chunk_size, em->start, em->len);
4489 free_extent_map(em);
4490 return -EINVAL;
4491 }
4492
4493 map = (struct map_lookup *)em->bdev;
4494 item_size = btrfs_chunk_item_size(map->num_stripes);
4495 stripe_size = em->orig_block_len;
4496
4497 chunk = kzalloc(item_size, GFP_NOFS);
4498 if (!chunk) {
4499 ret = -ENOMEM;
4500 goto out;
4501 }
4502
4503 for (i = 0; i < map->num_stripes; i++) {
4504 device = map->stripes[i].dev;
4505 dev_offset = map->stripes[i].physical;
4506
Yan Zheng2b820322008-11-17 21:11:30 -05004507 device->bytes_used += stripe_size;
4508 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004509 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004510 goto out;
4511 ret = btrfs_alloc_dev_extent(trans, device,
4512 chunk_root->root_key.objectid,
4513 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4514 chunk_offset, dev_offset,
4515 stripe_size);
4516 if (ret)
4517 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004518 }
4519
Josef Bacik2bf64752011-09-26 17:12:22 -04004520 spin_lock(&extent_root->fs_info->free_chunk_lock);
4521 extent_root->fs_info->free_chunk_space -= (stripe_size *
4522 map->num_stripes);
4523 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4524
Yan Zheng2b820322008-11-17 21:11:30 -05004525 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004526 for (i = 0; i < map->num_stripes; i++) {
4527 device = map->stripes[i].dev;
4528 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004529
4530 btrfs_set_stack_stripe_devid(stripe, device->devid);
4531 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4532 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4533 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004534 }
4535
4536 btrfs_set_stack_chunk_length(chunk, chunk_size);
4537 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4538 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4539 btrfs_set_stack_chunk_type(chunk, map->type);
4540 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4541 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4542 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4543 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4544 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4545
4546 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4547 key.type = BTRFS_CHUNK_ITEM_KEY;
4548 key.offset = chunk_offset;
4549
4550 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004551 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4552 /*
4553 * TODO: Cleanup of inserted chunk root in case of
4554 * failure.
4555 */
Li Zefan125ccb02011-12-08 15:07:24 +08004556 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004557 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004558 }
liubo1abe9b82011-03-24 11:18:59 +00004559
Josef Bacik6df9a952013-06-27 13:22:46 -04004560out:
Yan Zheng2b820322008-11-17 21:11:30 -05004561 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004562 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004563 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004564}
4565
4566/*
4567 * Chunk allocation falls into two parts. The first part does works
4568 * that make the new allocated chunk useable, but not do any operation
4569 * that modifies the chunk tree. The second part does the works that
4570 * require modifying the chunk tree. This division is important for the
4571 * bootstrap process of adding storage to a seed btrfs.
4572 */
4573int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4574 struct btrfs_root *extent_root, u64 type)
4575{
4576 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004577
Josef Bacik6df9a952013-06-27 13:22:46 -04004578 chunk_offset = find_next_chunk(extent_root->fs_info);
4579 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004580}
4581
Chris Masond3977122009-01-05 21:25:51 -05004582static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004583 struct btrfs_root *root,
4584 struct btrfs_device *device)
4585{
4586 u64 chunk_offset;
4587 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004588 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004589 struct btrfs_fs_info *fs_info = root->fs_info;
4590 struct btrfs_root *extent_root = fs_info->extent_root;
4591 int ret;
4592
Josef Bacik6df9a952013-06-27 13:22:46 -04004593 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004594 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004595 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4596 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004597 if (ret)
4598 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004599
Josef Bacik6df9a952013-06-27 13:22:46 -04004600 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004601 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004602 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4603 alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06004604 if (ret) {
4605 btrfs_abort_transaction(trans, root, ret);
4606 goto out;
4607 }
Yan Zheng2b820322008-11-17 21:11:30 -05004608
4609 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004610 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004611 btrfs_abort_transaction(trans, root, ret);
David Sterba005d6422012-09-18 07:52:32 -06004612out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004613 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004614}
4615
Miao Xied20983b2014-07-03 18:22:13 +08004616static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4617{
4618 int max_errors;
4619
4620 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4621 BTRFS_BLOCK_GROUP_RAID10 |
4622 BTRFS_BLOCK_GROUP_RAID5 |
4623 BTRFS_BLOCK_GROUP_DUP)) {
4624 max_errors = 1;
4625 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4626 max_errors = 2;
4627 } else {
4628 max_errors = 0;
4629 }
4630
4631 return max_errors;
4632}
4633
Yan Zheng2b820322008-11-17 21:11:30 -05004634int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4635{
4636 struct extent_map *em;
4637 struct map_lookup *map;
4638 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4639 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004640 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004641 int i;
4642
Chris Mason890871b2009-09-02 16:24:52 -04004643 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004644 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004645 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004646 if (!em)
4647 return 1;
4648
4649 map = (struct map_lookup *)em->bdev;
4650 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004651 if (map->stripes[i].dev->missing) {
4652 miss_ndevs++;
4653 continue;
4654 }
4655
Yan Zheng2b820322008-11-17 21:11:30 -05004656 if (!map->stripes[i].dev->writeable) {
4657 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004658 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004659 }
4660 }
Miao Xied20983b2014-07-03 18:22:13 +08004661
4662 /*
4663 * If the number of missing devices is larger than max errors,
4664 * we can not write the data into that chunk successfully, so
4665 * set it readonly.
4666 */
4667 if (miss_ndevs > btrfs_chunk_max_errors(map))
4668 readonly = 1;
4669end:
Yan Zheng2b820322008-11-17 21:11:30 -05004670 free_extent_map(em);
4671 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004672}
4673
4674void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4675{
David Sterbaa8067e02011-04-21 00:34:43 +02004676 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004677}
4678
4679void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4680{
4681 struct extent_map *em;
4682
Chris Masond3977122009-01-05 21:25:51 -05004683 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004684 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004685 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4686 if (em)
4687 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004688 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004689 if (!em)
4690 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004691 /* once for us */
4692 free_extent_map(em);
4693 /* once for the tree */
4694 free_extent_map(em);
4695 }
4696}
4697
Stefan Behrens5d964052012-11-05 14:59:07 +01004698int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004699{
Stefan Behrens5d964052012-11-05 14:59:07 +01004700 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004701 struct extent_map *em;
4702 struct map_lookup *map;
4703 struct extent_map_tree *em_tree = &map_tree->map_tree;
4704 int ret;
4705
Chris Mason890871b2009-09-02 16:24:52 -04004706 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004707 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004708 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004709
Josef Bacikfb7669b2013-04-23 10:53:18 -04004710 /*
4711 * We could return errors for these cases, but that could get ugly and
4712 * we'd probably do the same thing which is just not do anything else
4713 * and exit, so return 1 so the callers don't try to use other copies.
4714 */
4715 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004716 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004717 logical+len);
4718 return 1;
4719 }
4720
4721 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004722 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004723 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004724 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004725 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004726 return 1;
4727 }
4728
Chris Masonf1885912008-04-09 16:28:12 -04004729 map = (struct map_lookup *)em->bdev;
4730 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4731 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004732 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4733 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004734 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4735 ret = 2;
4736 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4737 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004738 else
4739 ret = 1;
4740 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004741
4742 btrfs_dev_replace_lock(&fs_info->dev_replace);
4743 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4744 ret++;
4745 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4746
Chris Masonf1885912008-04-09 16:28:12 -04004747 return ret;
4748}
4749
David Woodhouse53b381b2013-01-29 18:40:14 -05004750unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4751 struct btrfs_mapping_tree *map_tree,
4752 u64 logical)
4753{
4754 struct extent_map *em;
4755 struct map_lookup *map;
4756 struct extent_map_tree *em_tree = &map_tree->map_tree;
4757 unsigned long len = root->sectorsize;
4758
4759 read_lock(&em_tree->lock);
4760 em = lookup_extent_mapping(em_tree, logical, len);
4761 read_unlock(&em_tree->lock);
4762 BUG_ON(!em);
4763
4764 BUG_ON(em->start > logical || em->start + em->len < logical);
4765 map = (struct map_lookup *)em->bdev;
4766 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4767 BTRFS_BLOCK_GROUP_RAID6)) {
4768 len = map->stripe_len * nr_data_stripes(map);
4769 }
4770 free_extent_map(em);
4771 return len;
4772}
4773
4774int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4775 u64 logical, u64 len, int mirror_num)
4776{
4777 struct extent_map *em;
4778 struct map_lookup *map;
4779 struct extent_map_tree *em_tree = &map_tree->map_tree;
4780 int ret = 0;
4781
4782 read_lock(&em_tree->lock);
4783 em = lookup_extent_mapping(em_tree, logical, len);
4784 read_unlock(&em_tree->lock);
4785 BUG_ON(!em);
4786
4787 BUG_ON(em->start > logical || em->start + em->len < logical);
4788 map = (struct map_lookup *)em->bdev;
4789 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4790 BTRFS_BLOCK_GROUP_RAID6))
4791 ret = 1;
4792 free_extent_map(em);
4793 return ret;
4794}
4795
Stefan Behrens30d98612012-11-06 14:52:18 +01004796static int find_live_mirror(struct btrfs_fs_info *fs_info,
4797 struct map_lookup *map, int first, int num,
4798 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004799{
4800 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004801 int tolerance;
4802 struct btrfs_device *srcdev;
4803
4804 if (dev_replace_is_ongoing &&
4805 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4806 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4807 srcdev = fs_info->dev_replace.srcdev;
4808 else
4809 srcdev = NULL;
4810
4811 /*
4812 * try to avoid the drive that is the source drive for a
4813 * dev-replace procedure, only choose it if no other non-missing
4814 * mirror is available
4815 */
4816 for (tolerance = 0; tolerance < 2; tolerance++) {
4817 if (map->stripes[optimal].dev->bdev &&
4818 (tolerance || map->stripes[optimal].dev != srcdev))
4819 return optimal;
4820 for (i = first; i < first + num; i++) {
4821 if (map->stripes[i].dev->bdev &&
4822 (tolerance || map->stripes[i].dev != srcdev))
4823 return i;
4824 }
Chris Masondfe25022008-05-13 13:46:40 -04004825 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004826
Chris Masondfe25022008-05-13 13:46:40 -04004827 /* we couldn't find one that doesn't fail. Just return something
4828 * and the io error handling code will clean up eventually
4829 */
4830 return optimal;
4831}
4832
David Woodhouse53b381b2013-01-29 18:40:14 -05004833static inline int parity_smaller(u64 a, u64 b)
4834{
4835 return a > b;
4836}
4837
4838/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4839static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4840{
4841 struct btrfs_bio_stripe s;
4842 int i;
4843 u64 l;
4844 int again = 1;
4845
4846 while (again) {
4847 again = 0;
4848 for (i = 0; i < bbio->num_stripes - 1; i++) {
4849 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4850 s = bbio->stripes[i];
4851 l = raid_map[i];
4852 bbio->stripes[i] = bbio->stripes[i+1];
4853 raid_map[i] = raid_map[i+1];
4854 bbio->stripes[i+1] = s;
4855 raid_map[i+1] = l;
4856 again = 1;
4857 }
4858 }
4859 }
4860}
4861
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004862static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004863 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004864 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004865 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004866{
4867 struct extent_map *em;
4868 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004869 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004870 struct extent_map_tree *em_tree = &map_tree->map_tree;
4871 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004872 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004873 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004874 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004875 u64 stripe_nr_orig;
4876 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004877 u64 stripe_len;
4878 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004879 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004880 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004881 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004882 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004883 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004884 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004885 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4886 int dev_replace_is_ongoing = 0;
4887 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004888 int patch_the_first_stripe_for_dev_replace = 0;
4889 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004890 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004891
Chris Mason890871b2009-09-02 16:24:52 -04004892 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004893 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004894 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004895
Chris Mason3b951512008-04-17 11:29:12 -04004896 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004897 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004898 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004899 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004900 }
Chris Mason0b86a832008-03-24 15:01:56 -04004901
Josef Bacik9bb91872013-04-19 23:45:33 -04004902 if (em->start > logical || em->start + em->len < logical) {
4903 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02004904 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04004905 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004906 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04004907 return -EINVAL;
4908 }
4909
Chris Mason0b86a832008-03-24 15:01:56 -04004910 map = (struct map_lookup *)em->bdev;
4911 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004912
David Woodhouse53b381b2013-01-29 18:40:14 -05004913 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004914 stripe_nr = offset;
4915 /*
4916 * stripe_nr counts the total number of stripes we have to stride
4917 * to get to this block
4918 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004919 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004920
David Woodhouse53b381b2013-01-29 18:40:14 -05004921 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004922 BUG_ON(offset < stripe_offset);
4923
4924 /* stripe_offset is the offset of this block in its stripe*/
4925 stripe_offset = offset - stripe_offset;
4926
David Woodhouse53b381b2013-01-29 18:40:14 -05004927 /* if we're here for raid56, we need to know the stripe aligned start */
4928 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4929 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4930 raid56_full_stripe_start = offset;
4931
4932 /* allow a write of a full stripe, but make sure we don't
4933 * allow straddling of stripes
4934 */
4935 do_div(raid56_full_stripe_start, full_stripe_len);
4936 raid56_full_stripe_start *= full_stripe_len;
4937 }
4938
4939 if (rw & REQ_DISCARD) {
4940 /* we don't discard raid56 yet */
4941 if (map->type &
4942 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4943 ret = -EOPNOTSUPP;
4944 goto out;
4945 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004946 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004947 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4948 u64 max_len;
4949 /* For writes to RAID[56], allow a full stripeset across all disks.
4950 For other RAID types and for RAID[56] reads, just allow a single
4951 stripe (on a single disk). */
4952 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4953 (rw & REQ_WRITE)) {
4954 max_len = stripe_len * nr_data_stripes(map) -
4955 (offset - raid56_full_stripe_start);
4956 } else {
4957 /* we limit the length of each bio to what fits in a stripe */
4958 max_len = stripe_len - stripe_offset;
4959 }
4960 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004961 } else {
4962 *length = em->len - offset;
4963 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004964
David Woodhouse53b381b2013-01-29 18:40:14 -05004965 /* This is for when we're called from btrfs_merge_bio_hook() and all
4966 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004967 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004968 goto out;
4969
Stefan Behrens472262f2012-11-06 14:43:46 +01004970 btrfs_dev_replace_lock(dev_replace);
4971 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4972 if (!dev_replace_is_ongoing)
4973 btrfs_dev_replace_unlock(dev_replace);
4974
Stefan Behrensad6d6202012-11-06 15:06:47 +01004975 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4976 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4977 dev_replace->tgtdev != NULL) {
4978 /*
4979 * in dev-replace case, for repair case (that's the only
4980 * case where the mirror is selected explicitly when
4981 * calling btrfs_map_block), blocks left of the left cursor
4982 * can also be read from the target drive.
4983 * For REQ_GET_READ_MIRRORS, the target drive is added as
4984 * the last one to the array of stripes. For READ, it also
4985 * needs to be supported using the same mirror number.
4986 * If the requested block is not left of the left cursor,
4987 * EIO is returned. This can happen because btrfs_num_copies()
4988 * returns one more in the dev-replace case.
4989 */
4990 u64 tmp_length = *length;
4991 struct btrfs_bio *tmp_bbio = NULL;
4992 int tmp_num_stripes;
4993 u64 srcdev_devid = dev_replace->srcdev->devid;
4994 int index_srcdev = 0;
4995 int found = 0;
4996 u64 physical_of_found = 0;
4997
4998 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05004999 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005000 if (ret) {
5001 WARN_ON(tmp_bbio != NULL);
5002 goto out;
5003 }
5004
5005 tmp_num_stripes = tmp_bbio->num_stripes;
5006 if (mirror_num > tmp_num_stripes) {
5007 /*
5008 * REQ_GET_READ_MIRRORS does not contain this
5009 * mirror, that means that the requested area
5010 * is not left of the left cursor
5011 */
5012 ret = -EIO;
5013 kfree(tmp_bbio);
5014 goto out;
5015 }
5016
5017 /*
5018 * process the rest of the function using the mirror_num
5019 * of the source drive. Therefore look it up first.
5020 * At the end, patch the device pointer to the one of the
5021 * target drive.
5022 */
5023 for (i = 0; i < tmp_num_stripes; i++) {
5024 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
5025 /*
5026 * In case of DUP, in order to keep it
5027 * simple, only add the mirror with the
5028 * lowest physical address
5029 */
5030 if (found &&
5031 physical_of_found <=
5032 tmp_bbio->stripes[i].physical)
5033 continue;
5034 index_srcdev = i;
5035 found = 1;
5036 physical_of_found =
5037 tmp_bbio->stripes[i].physical;
5038 }
5039 }
5040
5041 if (found) {
5042 mirror_num = index_srcdev + 1;
5043 patch_the_first_stripe_for_dev_replace = 1;
5044 physical_to_patch_in_first_stripe = physical_of_found;
5045 } else {
5046 WARN_ON(1);
5047 ret = -EIO;
5048 kfree(tmp_bbio);
5049 goto out;
5050 }
5051
5052 kfree(tmp_bbio);
5053 } else if (mirror_num > map->num_stripes) {
5054 mirror_num = 0;
5055 }
5056
Chris Masonf2d8d742008-04-21 10:03:05 -04005057 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005058 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005059 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005060 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005061 do_div(stripe_nr_end, map->stripe_len);
5062 stripe_end_offset = stripe_nr_end * map->stripe_len -
5063 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005064
Li Dongyangfce3bb92011-03-24 10:24:26 +00005065 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5066 if (rw & REQ_DISCARD)
5067 num_stripes = min_t(u64, map->num_stripes,
5068 stripe_nr_end - stripe_nr_orig);
5069 stripe_index = do_div(stripe_nr, map->num_stripes);
5070 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005071 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005072 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005073 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005074 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005075 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005076 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005077 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005078 current->pid % map->num_stripes,
5079 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005080 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005081 }
Chris Mason2fff7342008-04-29 14:12:09 -04005082
Chris Mason611f0e02008-04-03 16:29:03 -04005083 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005084 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005085 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005086 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005087 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005088 } else {
5089 mirror_num = 1;
5090 }
Chris Mason2fff7342008-04-29 14:12:09 -04005091
Chris Mason321aecc2008-04-16 10:49:51 -04005092 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5093 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005094
5095 stripe_index = do_div(stripe_nr, factor);
5096 stripe_index *= map->sub_stripes;
5097
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005098 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005099 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005100 else if (rw & REQ_DISCARD)
5101 num_stripes = min_t(u64, map->sub_stripes *
5102 (stripe_nr_end - stripe_nr_orig),
5103 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005104 else if (mirror_num)
5105 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005106 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005107 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005108 stripe_index = find_live_mirror(fs_info, map,
5109 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005110 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005111 current->pid % map->sub_stripes,
5112 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005113 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005114 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005115
5116 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5117 BTRFS_BLOCK_GROUP_RAID6)) {
5118 u64 tmp;
5119
5120 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
5121 && raid_map_ret) {
5122 int i, rot;
5123
5124 /* push stripe_nr back to the start of the full stripe */
5125 stripe_nr = raid56_full_stripe_start;
5126 do_div(stripe_nr, stripe_len);
5127
5128 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5129
5130 /* RAID[56] write or recovery. Return all stripes */
5131 num_stripes = map->num_stripes;
5132 max_errors = nr_parity_stripes(map);
5133
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05305134 raid_map = kmalloc_array(num_stripes, sizeof(u64),
David Woodhouse53b381b2013-01-29 18:40:14 -05005135 GFP_NOFS);
5136 if (!raid_map) {
5137 ret = -ENOMEM;
5138 goto out;
5139 }
5140
5141 /* Work out the disk rotation on this stripe-set */
5142 tmp = stripe_nr;
5143 rot = do_div(tmp, num_stripes);
5144
5145 /* Fill in the logical address of each stripe */
5146 tmp = stripe_nr * nr_data_stripes(map);
5147 for (i = 0; i < nr_data_stripes(map); i++)
5148 raid_map[(i+rot) % num_stripes] =
5149 em->start + (tmp + i) * map->stripe_len;
5150
5151 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5152 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5153 raid_map[(i+rot+1) % num_stripes] =
5154 RAID6_Q_STRIPE;
5155
5156 *length = map->stripe_len;
5157 stripe_index = 0;
5158 stripe_offset = 0;
5159 } else {
5160 /*
5161 * Mirror #0 or #1 means the original data block.
5162 * Mirror #2 is RAID5 parity block.
5163 * Mirror #3 is RAID6 Q block.
5164 */
5165 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5166 if (mirror_num > 1)
5167 stripe_index = nr_data_stripes(map) +
5168 mirror_num - 2;
5169
5170 /* We distribute the parity blocks across stripes */
5171 tmp = stripe_nr + stripe_index;
5172 stripe_index = do_div(tmp, map->num_stripes);
5173 }
Chris Mason8790d502008-04-03 16:29:03 -04005174 } else {
5175 /*
5176 * after this do_div call, stripe_nr is the number of stripes
5177 * on this device we have to walk to find the data, and
5178 * stripe_index is the number of our device in the stripe array
5179 */
5180 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005181 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005182 }
Chris Mason593060d2008-03-25 16:50:33 -04005183 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005184
Stefan Behrens472262f2012-11-06 14:43:46 +01005185 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005186 if (dev_replace_is_ongoing) {
5187 if (rw & (REQ_WRITE | REQ_DISCARD))
5188 num_alloc_stripes <<= 1;
5189 if (rw & REQ_GET_READ_MIRRORS)
5190 num_alloc_stripes++;
5191 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005192 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08005193 if (!bbio) {
Dave Joneseb2067f2013-07-30 13:42:17 -04005194 kfree(raid_map);
Li Zefande11cc12011-12-01 12:55:47 +08005195 ret = -ENOMEM;
5196 goto out;
5197 }
5198 atomic_set(&bbio->error, 0);
5199
Li Dongyangfce3bb92011-03-24 10:24:26 +00005200 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08005201 int factor = 0;
5202 int sub_stripes = 0;
5203 u64 stripes_per_dev = 0;
5204 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005205 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005206
5207 if (map->type &
5208 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5209 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5210 sub_stripes = 1;
5211 else
5212 sub_stripes = map->sub_stripes;
5213
5214 factor = map->num_stripes / sub_stripes;
5215 stripes_per_dev = div_u64_rem(stripe_nr_end -
5216 stripe_nr_orig,
5217 factor,
5218 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005219 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5220 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005221 }
5222
Li Dongyangfce3bb92011-03-24 10:24:26 +00005223 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005224 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005225 map->stripes[stripe_index].physical +
5226 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005227 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005228
Li Zefanec9ef7a2011-12-01 14:06:42 +08005229 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5230 BTRFS_BLOCK_GROUP_RAID10)) {
5231 bbio->stripes[i].length = stripes_per_dev *
5232 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005233
Li Zefanec9ef7a2011-12-01 14:06:42 +08005234 if (i / sub_stripes < remaining_stripes)
5235 bbio->stripes[i].length +=
5236 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005237
5238 /*
5239 * Special for the first stripe and
5240 * the last stripe:
5241 *
5242 * |-------|...|-------|
5243 * |----------|
5244 * off end_off
5245 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005246 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005247 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005248 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005249
5250 if (stripe_index >= last_stripe &&
5251 stripe_index <= (last_stripe +
5252 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005253 bbio->stripes[i].length -=
5254 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005255
Li Zefanec9ef7a2011-12-01 14:06:42 +08005256 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005257 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005258 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005259 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005260
5261 stripe_index++;
5262 if (stripe_index == map->num_stripes) {
5263 /* This could only happen for RAID0/10 */
5264 stripe_index = 0;
5265 stripe_nr++;
5266 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005267 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005268 } else {
5269 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005270 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005271 map->stripes[stripe_index].physical +
5272 stripe_offset +
5273 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005274 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005275 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005276 stripe_index++;
5277 }
Chris Mason593060d2008-03-25 16:50:33 -04005278 }
Li Zefande11cc12011-12-01 12:55:47 +08005279
Miao Xied20983b2014-07-03 18:22:13 +08005280 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5281 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005282
Stefan Behrens472262f2012-11-06 14:43:46 +01005283 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5284 dev_replace->tgtdev != NULL) {
5285 int index_where_to_add;
5286 u64 srcdev_devid = dev_replace->srcdev->devid;
5287
5288 /*
5289 * duplicate the write operations while the dev replace
5290 * procedure is running. Since the copying of the old disk
5291 * to the new disk takes place at run time while the
5292 * filesystem is mounted writable, the regular write
5293 * operations to the old disk have to be duplicated to go
5294 * to the new disk as well.
5295 * Note that device->missing is handled by the caller, and
5296 * that the write to the old disk is already set up in the
5297 * stripes array.
5298 */
5299 index_where_to_add = num_stripes;
5300 for (i = 0; i < num_stripes; i++) {
5301 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5302 /* write to new disk, too */
5303 struct btrfs_bio_stripe *new =
5304 bbio->stripes + index_where_to_add;
5305 struct btrfs_bio_stripe *old =
5306 bbio->stripes + i;
5307
5308 new->physical = old->physical;
5309 new->length = old->length;
5310 new->dev = dev_replace->tgtdev;
5311 index_where_to_add++;
5312 max_errors++;
5313 }
5314 }
5315 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005316 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5317 dev_replace->tgtdev != NULL) {
5318 u64 srcdev_devid = dev_replace->srcdev->devid;
5319 int index_srcdev = 0;
5320 int found = 0;
5321 u64 physical_of_found = 0;
5322
5323 /*
5324 * During the dev-replace procedure, the target drive can
5325 * also be used to read data in case it is needed to repair
5326 * a corrupt block elsewhere. This is possible if the
5327 * requested area is left of the left cursor. In this area,
5328 * the target drive is a full copy of the source drive.
5329 */
5330 for (i = 0; i < num_stripes; i++) {
5331 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5332 /*
5333 * In case of DUP, in order to keep it
5334 * simple, only add the mirror with the
5335 * lowest physical address
5336 */
5337 if (found &&
5338 physical_of_found <=
5339 bbio->stripes[i].physical)
5340 continue;
5341 index_srcdev = i;
5342 found = 1;
5343 physical_of_found = bbio->stripes[i].physical;
5344 }
5345 }
5346 if (found) {
5347 u64 length = map->stripe_len;
5348
5349 if (physical_of_found + length <=
5350 dev_replace->cursor_left) {
5351 struct btrfs_bio_stripe *tgtdev_stripe =
5352 bbio->stripes + num_stripes;
5353
5354 tgtdev_stripe->physical = physical_of_found;
5355 tgtdev_stripe->length =
5356 bbio->stripes[index_srcdev].length;
5357 tgtdev_stripe->dev = dev_replace->tgtdev;
5358
5359 num_stripes++;
5360 }
5361 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005362 }
5363
Li Zefande11cc12011-12-01 12:55:47 +08005364 *bbio_ret = bbio;
5365 bbio->num_stripes = num_stripes;
5366 bbio->max_errors = max_errors;
5367 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005368
5369 /*
5370 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5371 * mirror_num == num_stripes + 1 && dev_replace target drive is
5372 * available as a mirror
5373 */
5374 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5375 WARN_ON(num_stripes > 1);
5376 bbio->stripes[0].dev = dev_replace->tgtdev;
5377 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5378 bbio->mirror_num = map->num_stripes + 1;
5379 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005380 if (raid_map) {
5381 sort_parity_stripes(bbio, raid_map);
5382 *raid_map_ret = raid_map;
5383 }
Chris Masoncea9e442008-04-09 16:28:12 -04005384out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005385 if (dev_replace_is_ongoing)
5386 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005387 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005388 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005389}
5390
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005391int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005392 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005393 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005394{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005395 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05005396 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04005397}
5398
Yan Zhenga512bbf2008-12-08 16:46:26 -05005399int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5400 u64 chunk_start, u64 physical, u64 devid,
5401 u64 **logical, int *naddrs, int *stripe_len)
5402{
5403 struct extent_map_tree *em_tree = &map_tree->map_tree;
5404 struct extent_map *em;
5405 struct map_lookup *map;
5406 u64 *buf;
5407 u64 bytenr;
5408 u64 length;
5409 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005410 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005411 int i, j, nr = 0;
5412
Chris Mason890871b2009-09-02 16:24:52 -04005413 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005414 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005415 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005416
Josef Bacik835d9742013-03-19 12:13:25 -04005417 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005418 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005419 chunk_start);
5420 return -EIO;
5421 }
5422
5423 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005424 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005425 em->start, chunk_start);
5426 free_extent_map(em);
5427 return -EIO;
5428 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005429 map = (struct map_lookup *)em->bdev;
5430
5431 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005432 rmap_len = map->stripe_len;
5433
Yan Zhenga512bbf2008-12-08 16:46:26 -05005434 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5435 do_div(length, map->num_stripes / map->sub_stripes);
5436 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5437 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05005438 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5439 BTRFS_BLOCK_GROUP_RAID6)) {
5440 do_div(length, nr_data_stripes(map));
5441 rmap_len = map->stripe_len * nr_data_stripes(map);
5442 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005443
5444 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005445 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005446
5447 for (i = 0; i < map->num_stripes; i++) {
5448 if (devid && map->stripes[i].dev->devid != devid)
5449 continue;
5450 if (map->stripes[i].physical > physical ||
5451 map->stripes[i].physical + length <= physical)
5452 continue;
5453
5454 stripe_nr = physical - map->stripes[i].physical;
5455 do_div(stripe_nr, map->stripe_len);
5456
5457 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5458 stripe_nr = stripe_nr * map->num_stripes + i;
5459 do_div(stripe_nr, map->sub_stripes);
5460 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5461 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005462 } /* else if RAID[56], multiply by nr_data_stripes().
5463 * Alternatively, just use rmap_len below instead of
5464 * map->stripe_len */
5465
5466 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005467 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005468 for (j = 0; j < nr; j++) {
5469 if (buf[j] == bytenr)
5470 break;
5471 }
Chris Mason934d3752008-12-08 16:43:10 -05005472 if (j == nr) {
5473 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005474 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005475 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005476 }
5477
Yan Zhenga512bbf2008-12-08 16:46:26 -05005478 *logical = buf;
5479 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005480 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005481
5482 free_extent_map(em);
5483 return 0;
5484}
5485
Miao Xie8408c712014-06-19 10:42:55 +08005486static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5487{
5488 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5489 bio_endio_nodec(bio, err);
5490 else
5491 bio_endio(bio, err);
5492 kfree(bbio);
5493}
5494
Jan Schmidta1d3c472011-08-04 17:15:33 +02005495static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005496{
Chris Mason9be33952013-05-17 18:30:14 -04005497 struct btrfs_bio *bbio = bio->bi_private;
Miao Xiec404e0d2014-01-30 16:46:55 +08005498 struct btrfs_device *dev = bbio->stripes[0].dev;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005499 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005500
Stefan Behrens442a4f62012-05-25 16:06:08 +02005501 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005502 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005503 if (err == -EIO || err == -EREMOTEIO) {
5504 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005505 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005506
5507 BUG_ON(stripe_index >= bbio->num_stripes);
5508 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005509 if (dev->bdev) {
5510 if (bio->bi_rw & WRITE)
5511 btrfs_dev_stat_inc(dev,
5512 BTRFS_DEV_STAT_WRITE_ERRS);
5513 else
5514 btrfs_dev_stat_inc(dev,
5515 BTRFS_DEV_STAT_READ_ERRS);
5516 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5517 btrfs_dev_stat_inc(dev,
5518 BTRFS_DEV_STAT_FLUSH_ERRS);
5519 btrfs_dev_stat_print_on_error(dev);
5520 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005521 }
5522 }
Chris Mason8790d502008-04-03 16:29:03 -04005523
Jan Schmidta1d3c472011-08-04 17:15:33 +02005524 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005525 is_orig_bio = 1;
5526
Miao Xiec404e0d2014-01-30 16:46:55 +08005527 btrfs_bio_counter_dec(bbio->fs_info);
5528
Jan Schmidta1d3c472011-08-04 17:15:33 +02005529 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005530 if (!is_orig_bio) {
5531 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005532 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005533 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005534
Jan Schmidta1d3c472011-08-04 17:15:33 +02005535 bio->bi_private = bbio->private;
5536 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005537 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005538 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005539 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005540 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005541 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005542 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005543 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005544 /*
5545 * this bio is actually up to date, we didn't
5546 * go over the max number of errors
5547 */
5548 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005549 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005550 }
Miao Xiec55f1392014-06-19 10:42:54 +08005551
Miao Xie8408c712014-06-19 10:42:55 +08005552 btrfs_end_bbio(bbio, bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005553 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005554 bio_put(bio);
5555 }
Chris Mason8790d502008-04-03 16:29:03 -04005556}
5557
Chris Mason8b712842008-06-11 16:50:36 -04005558/*
5559 * see run_scheduled_bios for a description of why bios are collected for
5560 * async submit.
5561 *
5562 * This will add one bio to the pending list for a device and make sure
5563 * the work struct is scheduled.
5564 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005565static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5566 struct btrfs_device *device,
5567 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005568{
5569 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005570 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005571
David Woodhouse53b381b2013-01-29 18:40:14 -05005572 if (device->missing || !device->bdev) {
5573 bio_endio(bio, -EIO);
5574 return;
5575 }
5576
Chris Mason8b712842008-06-11 16:50:36 -04005577 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005578 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005579 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005580 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005581 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005582 return;
Chris Mason8b712842008-06-11 16:50:36 -04005583 }
5584
5585 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005586 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005587 * higher layers. Otherwise, the async bio makes it appear we have
5588 * made progress against dirty pages when we've really just put it
5589 * on a queue for later
5590 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005591 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005592 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005593 bio->bi_next = NULL;
5594 bio->bi_rw |= rw;
5595
5596 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005597 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005598 pending_bios = &device->pending_sync_bios;
5599 else
5600 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005601
Chris Masonffbd5172009-04-20 15:50:09 -04005602 if (pending_bios->tail)
5603 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005604
Chris Masonffbd5172009-04-20 15:50:09 -04005605 pending_bios->tail = bio;
5606 if (!pending_bios->head)
5607 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005608 if (device->running_pending)
5609 should_queue = 0;
5610
5611 spin_unlock(&device->io_lock);
5612
5613 if (should_queue)
Qu Wenruoa8c93d42014-02-28 10:46:08 +08005614 btrfs_queue_work(root->fs_info->submit_workers,
5615 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005616}
5617
Josef Bacikde1ee922012-10-19 16:50:56 -04005618static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5619 sector_t sector)
5620{
5621 struct bio_vec *prev;
5622 struct request_queue *q = bdev_get_queue(bdev);
Akinobu Mita475bf362013-11-18 22:13:18 +09005623 unsigned int max_sectors = queue_max_sectors(q);
Josef Bacikde1ee922012-10-19 16:50:56 -04005624 struct bvec_merge_data bvm = {
5625 .bi_bdev = bdev,
5626 .bi_sector = sector,
5627 .bi_rw = bio->bi_rw,
5628 };
5629
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305630 if (WARN_ON(bio->bi_vcnt == 0))
Josef Bacikde1ee922012-10-19 16:50:56 -04005631 return 1;
Josef Bacikde1ee922012-10-19 16:50:56 -04005632
5633 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005634 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005635 return 0;
5636
5637 if (!q->merge_bvec_fn)
5638 return 1;
5639
Kent Overstreet4f024f32013-10-11 15:44:27 -07005640 bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len;
Josef Bacikde1ee922012-10-19 16:50:56 -04005641 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5642 return 0;
5643 return 1;
5644}
5645
5646static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5647 struct bio *bio, u64 physical, int dev_nr,
5648 int rw, int async)
5649{
5650 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5651
5652 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005653 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005654 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005655 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005656#ifdef DEBUG
5657 {
5658 struct rcu_string *name;
5659
5660 rcu_read_lock();
5661 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005662 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005663 "(%s id %llu), size=%u\n", rw,
5664 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5665 name->str, dev->devid, bio->bi_size);
5666 rcu_read_unlock();
5667 }
5668#endif
5669 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005670
5671 btrfs_bio_counter_inc_noblocked(root->fs_info);
5672
Josef Bacikde1ee922012-10-19 16:50:56 -04005673 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005674 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005675 else
5676 btrfsic_submit_bio(rw, bio);
5677}
5678
5679static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5680 struct bio *first_bio, struct btrfs_device *dev,
5681 int dev_nr, int rw, int async)
5682{
5683 struct bio_vec *bvec = first_bio->bi_io_vec;
5684 struct bio *bio;
5685 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5686 u64 physical = bbio->stripes[dev_nr].physical;
5687
5688again:
5689 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5690 if (!bio)
5691 return -ENOMEM;
5692
5693 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5694 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5695 bvec->bv_offset) < bvec->bv_len) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07005696 u64 len = bio->bi_iter.bi_size;
Josef Bacikde1ee922012-10-19 16:50:56 -04005697
5698 atomic_inc(&bbio->stripes_pending);
5699 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5700 rw, async);
5701 physical += len;
5702 goto again;
5703 }
5704 bvec++;
5705 }
5706
5707 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5708 return 0;
5709}
5710
5711static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5712{
5713 atomic_inc(&bbio->error);
5714 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005715 /* Shoud be the original bio. */
5716 WARN_ON(bio != bbio->orig_bio);
5717
Josef Bacikde1ee922012-10-19 16:50:56 -04005718 bio->bi_private = bbio->private;
5719 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005720 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005721 bio->bi_iter.bi_sector = logical >> 9;
Miao Xie8408c712014-06-19 10:42:55 +08005722
5723 btrfs_end_bbio(bbio, bio, -EIO);
Josef Bacikde1ee922012-10-19 16:50:56 -04005724 }
5725}
5726
Chris Masonf1885912008-04-09 16:28:12 -04005727int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005728 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005729{
Chris Mason0b86a832008-03-24 15:01:56 -04005730 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005731 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005732 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005733 u64 length = 0;
5734 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005735 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005736 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005737 int dev_nr = 0;
5738 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005739 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005740
Kent Overstreet4f024f32013-10-11 15:44:27 -07005741 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005742 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005743
Miao Xiec404e0d2014-01-30 16:46:55 +08005744 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005745 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5746 mirror_num, &raid_map);
Miao Xiec404e0d2014-01-30 16:46:55 +08005747 if (ret) {
5748 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005749 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005750 }
Chris Masoncea9e442008-04-09 16:28:12 -04005751
Jan Schmidta1d3c472011-08-04 17:15:33 +02005752 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005753 bbio->orig_bio = first_bio;
5754 bbio->private = first_bio->bi_private;
5755 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005756 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005757 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5758
5759 if (raid_map) {
5760 /* In this case, map_length has been set to the length of
5761 a single stripe; not the whole write */
5762 if (rw & WRITE) {
Miao Xiec404e0d2014-01-30 16:46:55 +08005763 ret = raid56_parity_write(root, bio, bbio,
5764 raid_map, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005765 } else {
Miao Xiec404e0d2014-01-30 16:46:55 +08005766 ret = raid56_parity_recover(root, bio, bbio,
5767 raid_map, map_length,
5768 mirror_num);
David Woodhouse53b381b2013-01-29 18:40:14 -05005769 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005770 /*
5771 * FIXME, replace dosen't support raid56 yet, please fix
5772 * it in the future.
5773 */
5774 btrfs_bio_counter_dec(root->fs_info);
5775 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005776 }
5777
Chris Masoncea9e442008-04-09 16:28:12 -04005778 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005779 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005780 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005781 BUG();
5782 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005783
Chris Masond3977122009-01-05 21:25:51 -05005784 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005785 dev = bbio->stripes[dev_nr].dev;
5786 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5787 bbio_error(bbio, first_bio, logical);
5788 dev_nr++;
5789 continue;
5790 }
5791
5792 /*
5793 * Check and see if we're ok with this bio based on it's size
5794 * and offset with the given device.
5795 */
5796 if (!bio_size_ok(dev->bdev, first_bio,
5797 bbio->stripes[dev_nr].physical >> 9)) {
5798 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5799 dev_nr, rw, async_submit);
5800 BUG_ON(ret);
5801 dev_nr++;
5802 continue;
5803 }
5804
Jan Schmidta1d3c472011-08-04 17:15:33 +02005805 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005806 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005807 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005808 } else {
5809 bio = first_bio;
Miao Xiec55f1392014-06-19 10:42:54 +08005810 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
Chris Mason8790d502008-04-03 16:29:03 -04005811 }
Josef Bacik606686e2012-06-04 14:03:51 -04005812
Josef Bacikde1ee922012-10-19 16:50:56 -04005813 submit_stripe_bio(root, bbio, bio,
5814 bbio->stripes[dev_nr].physical, dev_nr, rw,
5815 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005816 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005817 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005818 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04005819 return 0;
5820}
5821
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005822struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005823 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005824{
Yan Zheng2b820322008-11-17 21:11:30 -05005825 struct btrfs_device *device;
5826 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005827
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005828 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005829 while (cur_devices) {
5830 if (!fsid ||
5831 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5832 device = __find_device(&cur_devices->devices,
5833 devid, uuid);
5834 if (device)
5835 return device;
5836 }
5837 cur_devices = cur_devices->seed;
5838 }
5839 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005840}
5841
Chris Masondfe25022008-05-13 13:46:40 -04005842static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5843 u64 devid, u8 *dev_uuid)
5844{
5845 struct btrfs_device *device;
5846 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5847
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005848 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5849 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005850 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005851
5852 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005853 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04005854 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005855
5856 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05005857 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005858
Chris Masondfe25022008-05-13 13:46:40 -04005859 return device;
5860}
5861
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005862/**
5863 * btrfs_alloc_device - allocate struct btrfs_device
5864 * @fs_info: used only for generating a new devid, can be NULL if
5865 * devid is provided (i.e. @devid != NULL).
5866 * @devid: a pointer to devid for this device. If NULL a new devid
5867 * is generated.
5868 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5869 * is generated.
5870 *
5871 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5872 * on error. Returned struct is not linked onto any lists and can be
5873 * destroyed with kfree() right away.
5874 */
5875struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5876 const u64 *devid,
5877 const u8 *uuid)
5878{
5879 struct btrfs_device *dev;
5880 u64 tmp;
5881
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305882 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005883 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005884
5885 dev = __alloc_device();
5886 if (IS_ERR(dev))
5887 return dev;
5888
5889 if (devid)
5890 tmp = *devid;
5891 else {
5892 int ret;
5893
5894 ret = find_next_devid(fs_info, &tmp);
5895 if (ret) {
5896 kfree(dev);
5897 return ERR_PTR(ret);
5898 }
5899 }
5900 dev->devid = tmp;
5901
5902 if (uuid)
5903 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5904 else
5905 generate_random_uuid(dev->uuid);
5906
Liu Bo9e0af232014-08-15 23:36:53 +08005907 btrfs_init_work(&dev->work, btrfs_submit_helper,
5908 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005909
5910 return dev;
5911}
5912
Chris Mason0b86a832008-03-24 15:01:56 -04005913static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5914 struct extent_buffer *leaf,
5915 struct btrfs_chunk *chunk)
5916{
5917 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5918 struct map_lookup *map;
5919 struct extent_map *em;
5920 u64 logical;
5921 u64 length;
5922 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005923 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005924 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005925 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005926 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005927
Chris Masone17cade2008-04-15 15:41:47 -04005928 logical = key->offset;
5929 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005930
Chris Mason890871b2009-09-02 16:24:52 -04005931 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005932 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005933 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005934
5935 /* already mapped? */
5936 if (em && em->start <= logical && em->start + em->len > logical) {
5937 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005938 return 0;
5939 } else if (em) {
5940 free_extent_map(em);
5941 }
Chris Mason0b86a832008-03-24 15:01:56 -04005942
David Sterba172ddd62011-04-21 00:48:27 +02005943 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005944 if (!em)
5945 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005946 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5947 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005948 if (!map) {
5949 free_extent_map(em);
5950 return -ENOMEM;
5951 }
5952
Wang Shilong298a8f92014-06-19 10:42:52 +08005953 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04005954 em->bdev = (struct block_device *)map;
5955 em->start = logical;
5956 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005957 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005958 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005959 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005960
Chris Mason593060d2008-03-25 16:50:33 -04005961 map->num_stripes = num_stripes;
5962 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5963 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5964 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5965 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5966 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005967 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005968 for (i = 0; i < num_stripes; i++) {
5969 map->stripes[i].physical =
5970 btrfs_stripe_offset_nr(leaf, chunk, i);
5971 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005972 read_extent_buffer(leaf, uuid, (unsigned long)
5973 btrfs_stripe_dev_uuid_nr(chunk, i),
5974 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005975 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5976 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005977 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005978 free_extent_map(em);
5979 return -EIO;
5980 }
Chris Masondfe25022008-05-13 13:46:40 -04005981 if (!map->stripes[i].dev) {
5982 map->stripes[i].dev =
5983 add_missing_dev(root, devid, uuid);
5984 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04005985 free_extent_map(em);
5986 return -EIO;
5987 }
5988 }
5989 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005990 }
5991
Chris Mason890871b2009-09-02 16:24:52 -04005992 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005993 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04005994 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005995 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005996 free_extent_map(em);
5997
5998 return 0;
5999}
6000
Jeff Mahoney143bede2012-03-01 14:56:26 +01006001static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006002 struct btrfs_dev_item *dev_item,
6003 struct btrfs_device *device)
6004{
6005 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006006
6007 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006008 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6009 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006010 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006011 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
6012 device->type = btrfs_device_type(leaf, dev_item);
6013 device->io_align = btrfs_device_io_align(leaf, dev_item);
6014 device->io_width = btrfs_device_io_width(leaf, dev_item);
6015 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006016 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006017 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006018
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006019 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006020 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006021}
6022
Yan Zheng2b820322008-11-17 21:11:30 -05006023static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
6024{
6025 struct btrfs_fs_devices *fs_devices;
6026 int ret;
6027
Li Zefanb367e472011-12-07 11:38:24 +08006028 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05006029
6030 fs_devices = root->fs_info->fs_devices->seed;
6031 while (fs_devices) {
6032 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
6033 ret = 0;
6034 goto out;
6035 }
6036 fs_devices = fs_devices->seed;
6037 }
6038
6039 fs_devices = find_fsid(fsid);
6040 if (!fs_devices) {
6041 ret = -ENOENT;
6042 goto out;
6043 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006044
6045 fs_devices = clone_fs_devices(fs_devices);
6046 if (IS_ERR(fs_devices)) {
6047 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006048 goto out;
6049 }
6050
Christoph Hellwig97288f22008-12-02 06:36:09 -05006051 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006052 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006053 if (ret) {
6054 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006055 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006056 }
Yan Zheng2b820322008-11-17 21:11:30 -05006057
6058 if (!fs_devices->seeding) {
6059 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006060 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006061 ret = -EINVAL;
6062 goto out;
6063 }
6064
6065 fs_devices->seed = root->fs_info->fs_devices->seed;
6066 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006067out:
Yan Zheng2b820322008-11-17 21:11:30 -05006068 return ret;
6069}
6070
Chris Mason0d81ba52008-03-24 15:02:07 -04006071static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006072 struct extent_buffer *leaf,
6073 struct btrfs_dev_item *dev_item)
6074{
6075 struct btrfs_device *device;
6076 u64 devid;
6077 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006078 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006079 u8 dev_uuid[BTRFS_UUID_SIZE];
6080
Chris Mason0b86a832008-03-24 15:01:56 -04006081 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006082 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006083 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006084 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006085 BTRFS_UUID_SIZE);
6086
6087 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
6088 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05006089 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006090 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006091 }
6092
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006093 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006094 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006095 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006096 return -EIO;
6097
6098 if (!device) {
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006099 btrfs_warn(root->fs_info, "devid %llu missing", devid);
Yan Zheng2b820322008-11-17 21:11:30 -05006100 device = add_missing_dev(root, devid, dev_uuid);
6101 if (!device)
6102 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05006103 } else if (!device->missing) {
6104 /*
6105 * this happens when a device that was properly setup
6106 * in the device info lists suddenly goes bad.
6107 * device->bdev is NULL, and so we have to set
6108 * device->missing to one here
6109 */
6110 root->fs_info->fs_devices->missing_devices++;
6111 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006112 }
6113 }
6114
6115 if (device->fs_devices != root->fs_info->fs_devices) {
6116 BUG_ON(device->writeable);
6117 if (device->generation !=
6118 btrfs_device_generation(leaf, dev_item))
6119 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006120 }
Chris Mason0b86a832008-03-24 15:01:56 -04006121
6122 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006123 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006124 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006125 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006126 spin_lock(&root->fs_info->free_chunk_lock);
6127 root->fs_info->free_chunk_space += device->total_bytes -
6128 device->bytes_used;
6129 spin_unlock(&root->fs_info->free_chunk_lock);
6130 }
Chris Mason0b86a832008-03-24 15:01:56 -04006131 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006132 return ret;
6133}
6134
Yan Zhenge4404d62008-12-12 10:03:26 -05006135int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006136{
David Sterba6c417612011-04-13 15:41:04 +02006137 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006138 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006139 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006140 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04006141 u8 *ptr;
6142 unsigned long sb_ptr;
6143 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006144 u32 num_stripes;
6145 u32 array_size;
6146 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006147 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04006148 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006149
Yan Zhenge4404d62008-12-12 10:03:26 -05006150 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04006151 BTRFS_SUPER_INFO_SIZE);
6152 if (!sb)
6153 return -ENOMEM;
6154 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006155 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006156 /*
6157 * The sb extent buffer is artifical and just used to read the system array.
6158 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6159 * pages up-to-date when the page is larger: extent does not cover the
6160 * whole page and consequently check_page_uptodate does not find all
6161 * the page's extents up-to-date (the hole beyond sb),
6162 * write_extent_buffer then triggers a WARN_ON.
6163 *
6164 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6165 * but sb spans only this function. Add an explicit SetPageUptodate call
6166 * to silence the warning eg. on PowerPC 64.
6167 */
6168 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006169 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006170
Chris Masona061fc82008-05-07 11:43:44 -04006171 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006172 array_size = btrfs_super_sys_array_size(super_copy);
6173
Chris Mason0b86a832008-03-24 15:01:56 -04006174 ptr = super_copy->sys_chunk_array;
6175 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
6176 cur = 0;
6177
6178 while (cur < array_size) {
6179 disk_key = (struct btrfs_disk_key *)ptr;
6180 btrfs_disk_key_to_cpu(&key, disk_key);
6181
Chris Masona061fc82008-05-07 11:43:44 -04006182 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006183 sb_ptr += len;
6184 cur += len;
6185
Chris Mason0d81ba52008-03-24 15:02:07 -04006186 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04006187 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04006188 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006189 if (ret)
6190 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006191 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6192 len = btrfs_chunk_item_size(num_stripes);
6193 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006194 ret = -EIO;
6195 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006196 }
6197 ptr += len;
6198 sb_ptr += len;
6199 cur += len;
6200 }
Chris Masona061fc82008-05-07 11:43:44 -04006201 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006202 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006203}
6204
6205int btrfs_read_chunk_tree(struct btrfs_root *root)
6206{
6207 struct btrfs_path *path;
6208 struct extent_buffer *leaf;
6209 struct btrfs_key key;
6210 struct btrfs_key found_key;
6211 int ret;
6212 int slot;
6213
6214 root = root->fs_info->chunk_root;
6215
6216 path = btrfs_alloc_path();
6217 if (!path)
6218 return -ENOMEM;
6219
Li Zefanb367e472011-12-07 11:38:24 +08006220 mutex_lock(&uuid_mutex);
6221 lock_chunks(root);
6222
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006223 /*
6224 * Read all device items, and then all the chunk items. All
6225 * device items are found before any chunk item (their object id
6226 * is smaller than the lowest possible object id for a chunk
6227 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006228 */
6229 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6230 key.offset = 0;
6231 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006232 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006233 if (ret < 0)
6234 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006235 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006236 leaf = path->nodes[0];
6237 slot = path->slots[0];
6238 if (slot >= btrfs_header_nritems(leaf)) {
6239 ret = btrfs_next_leaf(root, path);
6240 if (ret == 0)
6241 continue;
6242 if (ret < 0)
6243 goto error;
6244 break;
6245 }
6246 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006247 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6248 struct btrfs_dev_item *dev_item;
6249 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006250 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006251 ret = read_one_dev(root, leaf, dev_item);
6252 if (ret)
6253 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006254 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6255 struct btrfs_chunk *chunk;
6256 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6257 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006258 if (ret)
6259 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006260 }
6261 path->slots[0]++;
6262 }
Chris Mason0b86a832008-03-24 15:01:56 -04006263 ret = 0;
6264error:
Li Zefanb367e472011-12-07 11:38:24 +08006265 unlock_chunks(root);
6266 mutex_unlock(&uuid_mutex);
6267
Yan Zheng2b820322008-11-17 21:11:30 -05006268 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006269 return ret;
6270}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006271
Miao Xiecb517ea2013-05-15 07:48:19 +00006272void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6273{
6274 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6275 struct btrfs_device *device;
6276
Liu Bo29cc83f2014-05-11 23:14:59 +08006277 while (fs_devices) {
6278 mutex_lock(&fs_devices->device_list_mutex);
6279 list_for_each_entry(device, &fs_devices->devices, dev_list)
6280 device->dev_root = fs_info->dev_root;
6281 mutex_unlock(&fs_devices->device_list_mutex);
6282
6283 fs_devices = fs_devices->seed;
6284 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006285}
6286
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006287static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6288{
6289 int i;
6290
6291 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6292 btrfs_dev_stat_reset(dev, i);
6293}
6294
6295int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6296{
6297 struct btrfs_key key;
6298 struct btrfs_key found_key;
6299 struct btrfs_root *dev_root = fs_info->dev_root;
6300 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6301 struct extent_buffer *eb;
6302 int slot;
6303 int ret = 0;
6304 struct btrfs_device *device;
6305 struct btrfs_path *path = NULL;
6306 int i;
6307
6308 path = btrfs_alloc_path();
6309 if (!path) {
6310 ret = -ENOMEM;
6311 goto out;
6312 }
6313
6314 mutex_lock(&fs_devices->device_list_mutex);
6315 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6316 int item_size;
6317 struct btrfs_dev_stats_item *ptr;
6318
6319 key.objectid = 0;
6320 key.type = BTRFS_DEV_STATS_KEY;
6321 key.offset = device->devid;
6322 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6323 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006324 __btrfs_reset_dev_stats(device);
6325 device->dev_stats_valid = 1;
6326 btrfs_release_path(path);
6327 continue;
6328 }
6329 slot = path->slots[0];
6330 eb = path->nodes[0];
6331 btrfs_item_key_to_cpu(eb, &found_key, slot);
6332 item_size = btrfs_item_size_nr(eb, slot);
6333
6334 ptr = btrfs_item_ptr(eb, slot,
6335 struct btrfs_dev_stats_item);
6336
6337 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6338 if (item_size >= (1 + i) * sizeof(__le64))
6339 btrfs_dev_stat_set(device, i,
6340 btrfs_dev_stats_value(eb, ptr, i));
6341 else
6342 btrfs_dev_stat_reset(device, i);
6343 }
6344
6345 device->dev_stats_valid = 1;
6346 btrfs_dev_stat_print_on_load(device);
6347 btrfs_release_path(path);
6348 }
6349 mutex_unlock(&fs_devices->device_list_mutex);
6350
6351out:
6352 btrfs_free_path(path);
6353 return ret < 0 ? ret : 0;
6354}
6355
6356static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6357 struct btrfs_root *dev_root,
6358 struct btrfs_device *device)
6359{
6360 struct btrfs_path *path;
6361 struct btrfs_key key;
6362 struct extent_buffer *eb;
6363 struct btrfs_dev_stats_item *ptr;
6364 int ret;
6365 int i;
6366
6367 key.objectid = 0;
6368 key.type = BTRFS_DEV_STATS_KEY;
6369 key.offset = device->devid;
6370
6371 path = btrfs_alloc_path();
6372 BUG_ON(!path);
6373 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6374 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006375 printk_in_rcu(KERN_WARNING "BTRFS: "
6376 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006377 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006378 goto out;
6379 }
6380
6381 if (ret == 0 &&
6382 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6383 /* need to delete old one and insert a new one */
6384 ret = btrfs_del_item(trans, dev_root, path);
6385 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006386 printk_in_rcu(KERN_WARNING "BTRFS: "
6387 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006388 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006389 goto out;
6390 }
6391 ret = 1;
6392 }
6393
6394 if (ret == 1) {
6395 /* need to insert a new item */
6396 btrfs_release_path(path);
6397 ret = btrfs_insert_empty_item(trans, dev_root, path,
6398 &key, sizeof(*ptr));
6399 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006400 printk_in_rcu(KERN_WARNING "BTRFS: "
6401 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006402 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006403 goto out;
6404 }
6405 }
6406
6407 eb = path->nodes[0];
6408 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6409 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6410 btrfs_set_dev_stats_value(eb, ptr, i,
6411 btrfs_dev_stat_read(device, i));
6412 btrfs_mark_buffer_dirty(eb);
6413
6414out:
6415 btrfs_free_path(path);
6416 return ret;
6417}
6418
6419/*
6420 * called from commit_transaction. Writes all changed device stats to disk.
6421 */
6422int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6423 struct btrfs_fs_info *fs_info)
6424{
6425 struct btrfs_root *dev_root = fs_info->dev_root;
6426 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6427 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006428 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006429 int ret = 0;
6430
6431 mutex_lock(&fs_devices->device_list_mutex);
6432 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006433 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006434 continue;
6435
Miao Xieaddc3fa2014-07-24 11:37:11 +08006436 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006437 ret = update_dev_stat_item(trans, dev_root, device);
6438 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006439 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006440 }
6441 mutex_unlock(&fs_devices->device_list_mutex);
6442
6443 return ret;
6444}
6445
Stefan Behrens442a4f62012-05-25 16:06:08 +02006446void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6447{
6448 btrfs_dev_stat_inc(dev, index);
6449 btrfs_dev_stat_print_on_error(dev);
6450}
6451
Eric Sandeen48a3b632013-04-25 20:41:01 +00006452static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006453{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006454 if (!dev->dev_stats_valid)
6455 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006456 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6457 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006458 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006459 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6460 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6461 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006462 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6463 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006464}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006465
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006466static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6467{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006468 int i;
6469
6470 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6471 if (btrfs_dev_stat_read(dev, i) != 0)
6472 break;
6473 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6474 return; /* all values == 0, suppress message */
6475
Frank Holtonefe120a2013-12-20 11:37:06 -05006476 printk_in_rcu(KERN_INFO "BTRFS: "
6477 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006478 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006479 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6480 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6481 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6482 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6483 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6484}
6485
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006486int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006487 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006488{
6489 struct btrfs_device *dev;
6490 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6491 int i;
6492
6493 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006494 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006495 mutex_unlock(&fs_devices->device_list_mutex);
6496
6497 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006498 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006499 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006500 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006501 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006502 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006503 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006504 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6505 if (stats->nr_items > i)
6506 stats->values[i] =
6507 btrfs_dev_stat_read_and_reset(dev, i);
6508 else
6509 btrfs_dev_stat_reset(dev, i);
6510 }
6511 } else {
6512 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6513 if (stats->nr_items > i)
6514 stats->values[i] = btrfs_dev_stat_read(dev, i);
6515 }
6516 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6517 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6518 return 0;
6519}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006520
6521int btrfs_scratch_superblock(struct btrfs_device *device)
6522{
6523 struct buffer_head *bh;
6524 struct btrfs_super_block *disk_super;
6525
6526 bh = btrfs_read_dev_super(device->bdev);
6527 if (!bh)
6528 return -EINVAL;
6529 disk_super = (struct btrfs_super_block *)bh->b_data;
6530
6531 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6532 set_buffer_dirty(bh);
6533 sync_dirty_buffer(bh);
6534 brelse(bh);
6535
6536 return 0;
6537}
Miao Xie935e5cc2014-09-03 21:35:33 +08006538
6539/*
6540 * Update the size of all devices, which is used for writing out the
6541 * super blocks.
6542 */
6543void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
6544{
6545 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6546 struct btrfs_device *curr, *next;
6547
6548 if (list_empty(&fs_devices->resized_devices))
6549 return;
6550
6551 mutex_lock(&fs_devices->device_list_mutex);
6552 lock_chunks(fs_info->dev_root);
6553 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
6554 resized_list) {
6555 list_del_init(&curr->resized_list);
6556 curr->commit_total_bytes = curr->disk_total_bytes;
6557 }
6558 unlock_chunks(fs_info->dev_root);
6559 mutex_unlock(&fs_devices->device_list_mutex);
6560}