blob: d47289c715c814f47c7842349afc942640310238 [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040021#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040022#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040023#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040024#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000025#include <linux/capability.h>
Stefan Behrens442a4f62012-05-25 16:06:08 +020026#include <linux/ratelimit.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020027#include <linux/kthread.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050028#include <linux/raid/pq.h>
Stefan Behrens803b2f52013-08-15 17:11:21 +020029#include <linux/semaphore.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050030#include <asm/div64.h>
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "ctree.h"
32#include "extent_map.h"
33#include "disk-io.h"
34#include "transaction.h"
35#include "print-tree.h"
36#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050037#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040038#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010039#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040040#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060041#include "math.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010042#include "dev-replace.h"
Anand Jain99994cd2014-06-03 11:36:00 +080043#include "sysfs.h"
Chris Mason0b86a832008-03-24 15:01:56 -040044
Yan Zheng2b820322008-11-17 21:11:30 -050045static int init_first_rw_device(struct btrfs_trans_handle *trans,
46 struct btrfs_root *root,
47 struct btrfs_device *device);
48static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020049static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +000050static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020051static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Yan Zheng2b820322008-11-17 21:11:30 -050052
Miao Xie67a2c452014-09-03 21:35:43 +080053DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -040054static LIST_HEAD(fs_uuids);
55
Chris Mason7d9eb122008-07-08 14:19:17 -040056static void lock_chunks(struct btrfs_root *root)
57{
Chris Mason7d9eb122008-07-08 14:19:17 -040058 mutex_lock(&root->fs_info->chunk_mutex);
59}
60
61static void unlock_chunks(struct btrfs_root *root)
62{
Chris Mason7d9eb122008-07-08 14:19:17 -040063 mutex_unlock(&root->fs_info->chunk_mutex);
64}
65
Ilya Dryomov2208a372013-08-12 14:33:03 +030066static struct btrfs_fs_devices *__alloc_fs_devices(void)
67{
68 struct btrfs_fs_devices *fs_devs;
69
70 fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS);
71 if (!fs_devs)
72 return ERR_PTR(-ENOMEM);
73
74 mutex_init(&fs_devs->device_list_mutex);
75
76 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +080077 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +030078 INIT_LIST_HEAD(&fs_devs->alloc_list);
79 INIT_LIST_HEAD(&fs_devs->list);
80
81 return fs_devs;
82}
83
84/**
85 * alloc_fs_devices - allocate struct btrfs_fs_devices
86 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
87 * generated.
88 *
89 * Return: a pointer to a new &struct btrfs_fs_devices on success;
90 * ERR_PTR() on error. Returned struct is not linked onto any lists and
91 * can be destroyed with kfree() right away.
92 */
93static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
94{
95 struct btrfs_fs_devices *fs_devs;
96
97 fs_devs = __alloc_fs_devices();
98 if (IS_ERR(fs_devs))
99 return fs_devs;
100
101 if (fsid)
102 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
103 else
104 generate_random_uuid(fs_devs->fsid);
105
106 return fs_devs;
107}
108
Yan Zhenge4404d62008-12-12 10:03:26 -0500109static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
110{
111 struct btrfs_device *device;
112 WARN_ON(fs_devices->opened);
113 while (!list_empty(&fs_devices->devices)) {
114 device = list_entry(fs_devices->devices.next,
115 struct btrfs_device, dev_list);
116 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -0400117 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500118 kfree(device);
119 }
120 kfree(fs_devices);
121}
122
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000123static void btrfs_kobject_uevent(struct block_device *bdev,
124 enum kobject_action action)
125{
126 int ret;
127
128 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
129 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500130 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000131 action,
132 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
133 &disk_to_dev(bdev->bd_disk)->kobj);
134}
135
Jeff Mahoney143bede2012-03-01 14:56:26 +0100136void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400137{
138 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400139
Yan Zheng2b820322008-11-17 21:11:30 -0500140 while (!list_empty(&fs_uuids)) {
141 fs_devices = list_entry(fs_uuids.next,
142 struct btrfs_fs_devices, list);
143 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500144 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400145 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400146}
147
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300148static struct btrfs_device *__alloc_device(void)
149{
150 struct btrfs_device *dev;
151
152 dev = kzalloc(sizeof(*dev), GFP_NOFS);
153 if (!dev)
154 return ERR_PTR(-ENOMEM);
155
156 INIT_LIST_HEAD(&dev->dev_list);
157 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800158 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300159
160 spin_lock_init(&dev->io_lock);
161
162 spin_lock_init(&dev->reada_lock);
163 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800164 atomic_set(&dev->dev_stats_ccnt, 0);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300165 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT);
166 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT);
167
168 return dev;
169}
170
Chris Masona1b32a52008-09-05 16:09:51 -0400171static noinline struct btrfs_device *__find_device(struct list_head *head,
172 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400173{
174 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400175
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500176 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400177 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400178 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400179 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400180 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400181 }
182 return NULL;
183}
184
Chris Masona1b32a52008-09-05 16:09:51 -0400185static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400186{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400187 struct btrfs_fs_devices *fs_devices;
188
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500189 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400190 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
191 return fs_devices;
192 }
193 return NULL;
194}
195
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100196static int
197btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
198 int flush, struct block_device **bdev,
199 struct buffer_head **bh)
200{
201 int ret;
202
203 *bdev = blkdev_get_by_path(device_path, flags, holder);
204
205 if (IS_ERR(*bdev)) {
206 ret = PTR_ERR(*bdev);
Frank Holtonefe120a2013-12-20 11:37:06 -0500207 printk(KERN_INFO "BTRFS: open %s failed\n", device_path);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100208 goto error;
209 }
210
211 if (flush)
212 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
213 ret = set_blocksize(*bdev, 4096);
214 if (ret) {
215 blkdev_put(*bdev, flags);
216 goto error;
217 }
218 invalidate_bdev(*bdev);
219 *bh = btrfs_read_dev_super(*bdev);
220 if (!*bh) {
221 ret = -EINVAL;
222 blkdev_put(*bdev, flags);
223 goto error;
224 }
225
226 return 0;
227
228error:
229 *bdev = NULL;
230 *bh = NULL;
231 return ret;
232}
233
Chris Masonffbd5172009-04-20 15:50:09 -0400234static void requeue_list(struct btrfs_pending_bios *pending_bios,
235 struct bio *head, struct bio *tail)
236{
237
238 struct bio *old_head;
239
240 old_head = pending_bios->head;
241 pending_bios->head = head;
242 if (pending_bios->tail)
243 tail->bi_next = old_head;
244 else
245 pending_bios->tail = tail;
246}
247
Chris Mason8b712842008-06-11 16:50:36 -0400248/*
249 * we try to collect pending bios for a device so we don't get a large
250 * number of procs sending bios down to the same device. This greatly
251 * improves the schedulers ability to collect and merge the bios.
252 *
253 * But, it also turns into a long list of bios to process and that is sure
254 * to eventually make the worker thread block. The solution here is to
255 * make some progress and then put this work struct back at the end of
256 * the list if the block device is congested. This way, multiple devices
257 * can make progress from a single worker thread.
258 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100259static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400260{
261 struct bio *pending;
262 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400263 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400264 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400265 struct bio *tail;
266 struct bio *cur;
267 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400268 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400269 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400270 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400271 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400272 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000273 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400274 struct blk_plug plug;
275
276 /*
277 * this function runs all the bios we've collected for
278 * a particular device. We don't want to wander off to
279 * another device without first sending all of these down.
280 * So, setup a plug here and finish it off before we return
281 */
282 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400283
Chris Masonbedf7622009-04-03 10:32:58 -0400284 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400285 fs_info = device->dev_root->fs_info;
286 limit = btrfs_async_submit_limit(fs_info);
287 limit = limit * 2 / 3;
288
Chris Mason8b712842008-06-11 16:50:36 -0400289loop:
290 spin_lock(&device->io_lock);
291
Chris Masona6837052009-02-04 09:19:41 -0500292loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400293 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400294
Chris Mason8b712842008-06-11 16:50:36 -0400295 /* take all the bios off the list at once and process them
296 * later on (without the lock held). But, remember the
297 * tail and other pointers so the bios can be properly reinserted
298 * into the list if we hit congestion
299 */
Chris Masond84275c2009-06-09 15:39:08 -0400300 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400301 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400302 force_reg = 1;
303 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400304 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400305 force_reg = 0;
306 }
Chris Masonffbd5172009-04-20 15:50:09 -0400307
308 pending = pending_bios->head;
309 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400310 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400311
312 /*
313 * if pending was null this time around, no bios need processing
314 * at all and we can stop. Otherwise it'll loop back up again
315 * and do an additional check so no bios are missed.
316 *
317 * device->running_pending is used to synchronize with the
318 * schedule_bio code.
319 */
Chris Masonffbd5172009-04-20 15:50:09 -0400320 if (device->pending_sync_bios.head == NULL &&
321 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400322 again = 0;
323 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400324 } else {
325 again = 1;
326 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400327 }
Chris Masonffbd5172009-04-20 15:50:09 -0400328
329 pending_bios->head = NULL;
330 pending_bios->tail = NULL;
331
Chris Mason8b712842008-06-11 16:50:36 -0400332 spin_unlock(&device->io_lock);
333
Chris Masond3977122009-01-05 21:25:51 -0500334 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400335
336 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400337 /* we want to work on both lists, but do more bios on the
338 * sync list than the regular list
339 */
340 if ((num_run > 32 &&
341 pending_bios != &device->pending_sync_bios &&
342 device->pending_sync_bios.head) ||
343 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
344 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400345 spin_lock(&device->io_lock);
346 requeue_list(pending_bios, pending, tail);
347 goto loop_lock;
348 }
349
Chris Mason8b712842008-06-11 16:50:36 -0400350 cur = pending;
351 pending = pending->bi_next;
352 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400353
Josef Bacik66657b32012-08-01 15:36:24 -0400354 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400355 waitqueue_active(&fs_info->async_submit_wait))
356 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400357
358 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400359
Chris Mason2ab1ba62011-08-04 14:28:36 -0400360 /*
361 * if we're doing the sync list, record that our
362 * plug has some sync requests on it
363 *
364 * If we're doing the regular list and there are
365 * sync requests sitting around, unplug before
366 * we add more
367 */
368 if (pending_bios == &device->pending_sync_bios) {
369 sync_pending = 1;
370 } else if (sync_pending) {
371 blk_finish_plug(&plug);
372 blk_start_plug(&plug);
373 sync_pending = 0;
374 }
375
Stefan Behrens21adbd52011-11-09 13:44:05 +0100376 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400377 num_run++;
378 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100379 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400380 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400381
382 /*
383 * we made progress, there is more work to do and the bdi
384 * is now congested. Back off and let other work structs
385 * run instead
386 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400387 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500388 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400389 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400390
Chris Masonb765ead2009-04-03 10:27:10 -0400391 ioc = current->io_context;
392
393 /*
394 * the main goal here is that we don't want to
395 * block if we're going to be able to submit
396 * more requests without blocking.
397 *
398 * This code does two great things, it pokes into
399 * the elevator code from a filesystem _and_
400 * it makes assumptions about how batching works.
401 */
402 if (ioc && ioc->nr_batch_requests > 0 &&
403 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
404 (last_waited == 0 ||
405 ioc->last_waited == last_waited)) {
406 /*
407 * we want to go through our batch of
408 * requests and stop. So, we copy out
409 * the ioc->last_waited time and test
410 * against it before looping
411 */
412 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100413 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400414 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400415 continue;
416 }
Chris Mason8b712842008-06-11 16:50:36 -0400417 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400418 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500419 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400420
421 spin_unlock(&device->io_lock);
Qu 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 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700534 * For now, we do allow update to btrfs_fs_device through the
535 * btrfs dev scan cli after FS has been mounted. We're still
536 * tracking a problem where systems fail mount by subvolume id
537 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800538 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700539 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800540 /*
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 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700547 return -EEXIST;
Anand Jain77bdae42014-07-03 18:22:06 +0800548 }
Anand Jainb96de002014-07-03 18:22:05 +0800549
Josef Bacik606686e2012-06-04 14:03:51 -0400550 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000551 if (!name)
552 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400553 rcu_string_free(device->name);
554 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500555 if (device->missing) {
556 fs_devices->missing_devices--;
557 device->missing = 0;
558 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400559 }
560
Anand Jain77bdae42014-07-03 18:22:06 +0800561 /*
562 * Unmount does not free the btrfs_device struct but would zero
563 * generation along with most of the other members. So just update
564 * it back. We need it to pick the disk with largest generation
565 * (as above).
566 */
567 if (!fs_devices->opened)
568 device->generation = found_transid;
569
Chris Mason8a4b83c2008-03-24 15:02:07 -0400570 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100571
572 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400573}
574
Yan Zhenge4404d62008-12-12 10:03:26 -0500575static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
576{
577 struct btrfs_fs_devices *fs_devices;
578 struct btrfs_device *device;
579 struct btrfs_device *orig_dev;
580
Ilya Dryomov2208a372013-08-12 14:33:03 +0300581 fs_devices = alloc_fs_devices(orig->fsid);
582 if (IS_ERR(fs_devices))
583 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500584
Miao Xieadbbb862014-09-03 21:35:42 +0800585 mutex_lock(&orig->device_list_mutex);
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 }
Miao Xieadbbb862014-09-03 21:35:42 +0800614 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500615 return fs_devices;
616error:
Miao Xieadbbb862014-09-03 21:35:42 +0800617 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500618 free_fs_devices(fs_devices);
619 return ERR_PTR(-ENOMEM);
620}
621
Stefan Behrens8dabb742012-11-06 13:15:27 +0100622void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
623 struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400624{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500625 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800626 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500627
Chris Masondfe25022008-05-13 13:46:40 -0400628 mutex_lock(&uuid_mutex);
629again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000630 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500631 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500632 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100633 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800634 (!latest_dev ||
635 device->generation > latest_dev->generation)) {
636 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500637 }
Yan Zheng2b820322008-11-17 21:11:30 -0500638 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500639 }
Yan Zheng2b820322008-11-17 21:11:30 -0500640
Stefan Behrens8dabb742012-11-06 13:15:27 +0100641 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
642 /*
643 * In the first step, keep the device which has
644 * the correct fsid and the devid that is used
645 * for the dev_replace procedure.
646 * In the second step, the dev_replace state is
647 * read from the device tree and it is known
648 * whether the procedure is really active or
649 * not, which means whether this device is
650 * used or whether it should be removed.
651 */
652 if (step == 0 || device->is_tgtdev_for_dev_replace) {
653 continue;
654 }
655 }
Yan Zheng2b820322008-11-17 21:11:30 -0500656 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100657 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500658 device->bdev = NULL;
659 fs_devices->open_devices--;
660 }
661 if (device->writeable) {
662 list_del_init(&device->dev_alloc_list);
663 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100664 if (!device->is_tgtdev_for_dev_replace)
665 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500666 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500667 list_del_init(&device->dev_list);
668 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400669 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500670 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400671 }
Yan Zheng2b820322008-11-17 21:11:30 -0500672
673 if (fs_devices->seed) {
674 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500675 goto again;
676 }
677
Miao Xie443f24f2014-07-24 11:37:15 +0800678 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500679
Chris Masondfe25022008-05-13 13:46:40 -0400680 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400681}
Chris Masona0af4692008-05-13 16:03:06 -0400682
Xiao Guangrong1f781602011-04-20 10:09:16 +0000683static void __free_device(struct work_struct *work)
684{
685 struct btrfs_device *device;
686
687 device = container_of(work, struct btrfs_device, rcu_work);
688
689 if (device->bdev)
690 blkdev_put(device->bdev, device->mode);
691
Josef Bacik606686e2012-06-04 14:03:51 -0400692 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000693 kfree(device);
694}
695
696static void free_device(struct rcu_head *head)
697{
698 struct btrfs_device *device;
699
700 device = container_of(head, struct btrfs_device, rcu);
701
702 INIT_WORK(&device->rcu_work, __free_device);
703 schedule_work(&device->rcu_work);
704}
705
Yan Zheng2b820322008-11-17 21:11:30 -0500706static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400707{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400708 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500709
Yan Zheng2b820322008-11-17 21:11:30 -0500710 if (--fs_devices->opened > 0)
711 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400712
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000713 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500714 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000715 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400716 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000717
718 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400719 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000720
Ilya Dryomovf747cab2013-10-10 20:37:29 +0300721 if (device->writeable &&
722 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500723 list_del_init(&device->dev_alloc_list);
724 fs_devices->rw_devices--;
725 }
726
Josef Bacik726551e2013-08-26 13:45:53 -0400727 if (device->missing)
728 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000729
Ilya Dryomova1e87802013-08-12 14:33:04 +0300730 new_device = btrfs_alloc_device(NULL, &device->devid,
731 device->uuid);
732 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400733
734 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400735 if (device->name) {
736 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300737 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400738 rcu_assign_pointer(new_device->name, name);
739 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300740
Xiao Guangrong1f781602011-04-20 10:09:16 +0000741 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300742 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000743
744 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400745 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000746 mutex_unlock(&fs_devices->device_list_mutex);
747
Yan Zhenge4404d62008-12-12 10:03:26 -0500748 WARN_ON(fs_devices->open_devices);
749 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500750 fs_devices->opened = 0;
751 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500752
Chris Mason8a4b83c2008-03-24 15:02:07 -0400753 return 0;
754}
755
Yan Zheng2b820322008-11-17 21:11:30 -0500756int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
757{
Yan Zhenge4404d62008-12-12 10:03:26 -0500758 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500759 int ret;
760
761 mutex_lock(&uuid_mutex);
762 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500763 if (!fs_devices->opened) {
764 seed_devices = fs_devices->seed;
765 fs_devices->seed = NULL;
766 }
Yan Zheng2b820322008-11-17 21:11:30 -0500767 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500768
769 while (seed_devices) {
770 fs_devices = seed_devices;
771 seed_devices = fs_devices->seed;
772 __btrfs_close_devices(fs_devices);
773 free_fs_devices(fs_devices);
774 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000775 /*
776 * Wait for rcu kworkers under __btrfs_close_devices
777 * to finish all blkdev_puts so device is really
778 * free when umount is done.
779 */
780 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500781 return ret;
782}
783
Yan Zhenge4404d62008-12-12 10:03:26 -0500784static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
785 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400786{
Josef Bacikd5e20032011-08-04 14:52:27 +0000787 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400788 struct block_device *bdev;
789 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400790 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +0800791 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -0400792 struct buffer_head *bh;
793 struct btrfs_super_block *disk_super;
Chris Masona0af4692008-05-13 16:03:06 -0400794 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500795 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400796 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400797
Tejun Heod4d77622010-11-13 11:55:18 +0100798 flags |= FMODE_EXCL;
799
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500800 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400801 if (device->bdev)
802 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400803 if (!device->name)
804 continue;
805
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000806 /* Just open everything we can; ignore failures here */
807 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
808 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100809 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400810
811 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000812 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400813 if (devid != device->devid)
814 goto error_brelse;
815
Yan Zheng2b820322008-11-17 21:11:30 -0500816 if (memcmp(device->uuid, disk_super->dev_item.uuid,
817 BTRFS_UUID_SIZE))
818 goto error_brelse;
819
820 device->generation = btrfs_super_generation(disk_super);
Miao Xie443f24f2014-07-24 11:37:15 +0800821 if (!latest_dev ||
822 device->generation > latest_dev->generation)
823 latest_dev = device;
Chris Masona0af4692008-05-13 16:03:06 -0400824
Yan Zheng2b820322008-11-17 21:11:30 -0500825 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
826 device->writeable = 0;
827 } else {
828 device->writeable = !bdev_read_only(bdev);
829 seeding = 0;
830 }
831
Josef Bacikd5e20032011-08-04 14:52:27 +0000832 q = bdev_get_queue(bdev);
Miao Xie90180da2014-09-03 21:35:30 +0800833 if (blk_queue_discard(q))
Josef Bacikd5e20032011-08-04 14:52:27 +0000834 device->can_discard = 1;
Josef Bacikd5e20032011-08-04 14:52:27 +0000835
Chris Mason8a4b83c2008-03-24 15:02:07 -0400836 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400837 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500838 device->mode = flags;
839
Chris Masonc2898112009-06-10 09:51:32 -0400840 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
841 fs_devices->rotating = 1;
842
Chris Masona0af4692008-05-13 16:03:06 -0400843 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300844 if (device->writeable &&
845 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500846 fs_devices->rw_devices++;
847 list_add(&device->dev_alloc_list,
848 &fs_devices->alloc_list);
849 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000850 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400851 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400852
Chris Masona0af4692008-05-13 16:03:06 -0400853error_brelse:
854 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100855 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400856 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400857 }
Chris Masona0af4692008-05-13 16:03:06 -0400858 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300859 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400860 goto out;
861 }
Yan Zheng2b820322008-11-17 21:11:30 -0500862 fs_devices->seeding = seeding;
863 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +0800864 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -0500865 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400866out:
Yan Zheng2b820322008-11-17 21:11:30 -0500867 return ret;
868}
869
870int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500871 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500872{
873 int ret;
874
875 mutex_lock(&uuid_mutex);
876 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500877 fs_devices->opened++;
878 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500879 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500880 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500881 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400882 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400883 return ret;
884}
885
David Sterba6f60cbd2013-02-15 11:31:02 -0700886/*
887 * Look for a btrfs signature on a device. This may be called out of the mount path
888 * and we are not allowed to call set_blocksize during the scan. The superblock
889 * is read via pagecache
890 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500891int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400892 struct btrfs_fs_devices **fs_devices_ret)
893{
894 struct btrfs_super_block *disk_super;
895 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700896 struct page *page;
897 void *p;
898 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400899 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400900 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400901 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700902 u64 bytenr;
903 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400904
David Sterba6f60cbd2013-02-15 11:31:02 -0700905 /*
906 * we would like to check all the supers, but that would make
907 * a btrfs mount succeed after a mkfs from a different FS.
908 * So, we need to add a special mount option to scan for
909 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
910 */
911 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100912 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500913 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700914
915 bdev = blkdev_get_by_path(path, flags, holder);
916
917 if (IS_ERR(bdev)) {
918 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100919 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700920 }
921
922 /* make sure our super fits in the device */
923 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
924 goto error_bdev_put;
925
926 /* make sure our super fits in the page */
927 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
928 goto error_bdev_put;
929
930 /* make sure our super doesn't straddle pages on disk */
931 index = bytenr >> PAGE_CACHE_SHIFT;
932 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
933 goto error_bdev_put;
934
935 /* pull in the page with our super */
936 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
937 index, GFP_NOFS);
938
939 if (IS_ERR_OR_NULL(page))
940 goto error_bdev_put;
941
942 p = kmap(page);
943
944 /* align our pointer to the offset of the super block */
945 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
946
947 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +0800948 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -0700949 goto error_unmap;
950
Xiao Guangronga3438322010-01-06 11:48:18 +0000951 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400952 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400953 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700954
Chris Mason8a4b83c2008-03-24 15:02:07 -0400955 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +0100956 if (ret > 0) {
957 if (disk_super->label[0]) {
958 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
959 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
960 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
961 } else {
962 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
963 }
964
965 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
966 ret = 0;
967 }
Josef Bacik02db0842012-06-21 16:03:58 -0400968 if (!ret && fs_devices_ret)
969 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700970
971error_unmap:
972 kunmap(page);
973 page_cache_release(page);
974
975error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100976 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400977error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100978 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400979 return ret;
980}
Chris Mason0b86a832008-03-24 15:01:56 -0400981
Miao Xie6d07bce2011-01-05 10:07:31 +0000982/* helper to account the used device space in the range */
983int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
984 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400985{
986 struct btrfs_key key;
987 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000988 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500989 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000990 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400991 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000992 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400993 struct extent_buffer *l;
994
Miao Xie6d07bce2011-01-05 10:07:31 +0000995 *length = 0;
996
Stefan Behrens63a212a2012-11-05 18:29:28 +0100997 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000998 return 0;
999
Yan Zheng2b820322008-11-17 21:11:30 -05001000 path = btrfs_alloc_path();
1001 if (!path)
1002 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001003 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -04001004
Chris Mason0b86a832008-03-24 15:01:56 -04001005 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001006 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001007 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001008
1009 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001010 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001011 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001012 if (ret > 0) {
1013 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1014 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001015 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001016 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001017
Chris Mason0b86a832008-03-24 15:01:56 -04001018 while (1) {
1019 l = path->nodes[0];
1020 slot = path->slots[0];
1021 if (slot >= btrfs_header_nritems(l)) {
1022 ret = btrfs_next_leaf(root, path);
1023 if (ret == 0)
1024 continue;
1025 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001026 goto out;
1027
1028 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001029 }
1030 btrfs_item_key_to_cpu(l, &key, slot);
1031
1032 if (key.objectid < device->devid)
1033 goto next;
1034
1035 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001036 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001037
David Sterba962a2982014-06-04 18:41:45 +02001038 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001039 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001040
Chris Mason0b86a832008-03-24 15:01:56 -04001041 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001042 extent_end = key.offset + btrfs_dev_extent_length(l,
1043 dev_extent);
1044 if (key.offset <= start && extent_end > end) {
1045 *length = end - start + 1;
1046 break;
1047 } else if (key.offset <= start && extent_end > start)
1048 *length += extent_end - start;
1049 else if (key.offset > start && extent_end <= end)
1050 *length += extent_end - key.offset;
1051 else if (key.offset > start && key.offset <= end) {
1052 *length += end - key.offset + 1;
1053 break;
1054 } else if (key.offset > end)
1055 break;
1056
1057next:
1058 path->slots[0]++;
1059 }
1060 ret = 0;
1061out:
1062 btrfs_free_path(path);
1063 return ret;
1064}
1065
Josef Bacik6df9a952013-06-27 13:22:46 -04001066static int contains_pending_extent(struct btrfs_trans_handle *trans,
1067 struct btrfs_device *device,
1068 u64 *start, u64 len)
1069{
1070 struct extent_map *em;
1071 int ret = 0;
1072
1073 list_for_each_entry(em, &trans->transaction->pending_chunks, list) {
1074 struct map_lookup *map;
1075 int i;
1076
1077 map = (struct map_lookup *)em->bdev;
1078 for (i = 0; i < map->num_stripes; i++) {
1079 if (map->stripes[i].dev != device)
1080 continue;
1081 if (map->stripes[i].physical >= *start + len ||
1082 map->stripes[i].physical + em->orig_block_len <=
1083 *start)
1084 continue;
1085 *start = map->stripes[i].physical +
1086 em->orig_block_len;
1087 ret = 1;
1088 }
1089 }
1090
1091 return ret;
1092}
1093
1094
Chris Mason0b86a832008-03-24 15:01:56 -04001095/*
Miao Xie7bfc8372011-01-05 10:07:26 +00001096 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +00001097 * @device: the device which we search the free space in
1098 * @num_bytes: the size of the free space that we need
1099 * @start: store the start of the free space.
1100 * @len: the size of the free space. that we find, or the size of the max
1101 * free space if we don't find suitable free space
1102 *
Chris Mason0b86a832008-03-24 15:01:56 -04001103 * this uses a pretty simple search, the expectation is that it is
1104 * called very infrequently and that a given device has a small number
1105 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001106 *
1107 * @start is used to store the start of the free space if we find. But if we
1108 * don't find suitable free space, it will be used to store the start position
1109 * of the max free space.
1110 *
1111 * @len is used to store the size of the free space that we find.
1112 * But if we don't find suitable free space, it is used to store the size of
1113 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001114 */
Josef Bacik6df9a952013-06-27 13:22:46 -04001115int find_free_dev_extent(struct btrfs_trans_handle *trans,
1116 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001117 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001118{
1119 struct btrfs_key key;
1120 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001121 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001122 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001123 u64 hole_size;
1124 u64 max_hole_start;
1125 u64 max_hole_size;
1126 u64 extent_end;
1127 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001128 u64 search_end = device->total_bytes;
1129 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001130 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001131 struct extent_buffer *l;
1132
Chris Mason0b86a832008-03-24 15:01:56 -04001133 /* FIXME use last free of some kind */
1134
1135 /* we don't want to overwrite the superblock on the drive,
1136 * so we make sure to start at an offset of at least 1MB
1137 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001138 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001139
Josef Bacik6df9a952013-06-27 13:22:46 -04001140 path = btrfs_alloc_path();
1141 if (!path)
1142 return -ENOMEM;
1143again:
Miao Xie7bfc8372011-01-05 10:07:26 +00001144 max_hole_start = search_start;
1145 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +00001146 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +00001147
Stefan Behrens63a212a2012-11-05 18:29:28 +01001148 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001149 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001150 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001151 }
1152
Miao Xie7bfc8372011-01-05 10:07:26 +00001153 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001154 path->search_commit_root = 1;
1155 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001156
Chris Mason0b86a832008-03-24 15:01:56 -04001157 key.objectid = device->devid;
1158 key.offset = search_start;
1159 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001160
Li Zefan125ccb02011-12-08 15:07:24 +08001161 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001162 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001163 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001164 if (ret > 0) {
1165 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1166 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001167 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001168 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001169
Chris Mason0b86a832008-03-24 15:01:56 -04001170 while (1) {
1171 l = path->nodes[0];
1172 slot = path->slots[0];
1173 if (slot >= btrfs_header_nritems(l)) {
1174 ret = btrfs_next_leaf(root, path);
1175 if (ret == 0)
1176 continue;
1177 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001178 goto out;
1179
1180 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001181 }
1182 btrfs_item_key_to_cpu(l, &key, slot);
1183
1184 if (key.objectid < device->devid)
1185 goto next;
1186
1187 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001188 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001189
David Sterba962a2982014-06-04 18:41:45 +02001190 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001191 goto next;
1192
Miao Xie7bfc8372011-01-05 10:07:26 +00001193 if (key.offset > search_start) {
1194 hole_size = key.offset - search_start;
1195
Josef Bacik6df9a952013-06-27 13:22:46 -04001196 /*
1197 * Have to check before we set max_hole_start, otherwise
1198 * we could end up sending back this offset anyway.
1199 */
1200 if (contains_pending_extent(trans, device,
1201 &search_start,
1202 hole_size))
1203 hole_size = 0;
1204
Miao Xie7bfc8372011-01-05 10:07:26 +00001205 if (hole_size > max_hole_size) {
1206 max_hole_start = search_start;
1207 max_hole_size = hole_size;
1208 }
1209
1210 /*
1211 * If this free space is greater than which we need,
1212 * it must be the max free space that we have found
1213 * until now, so max_hole_start must point to the start
1214 * of this free space and the length of this free space
1215 * is stored in max_hole_size. Thus, we return
1216 * max_hole_start and max_hole_size and go back to the
1217 * caller.
1218 */
1219 if (hole_size >= num_bytes) {
1220 ret = 0;
1221 goto out;
1222 }
1223 }
1224
Chris Mason0b86a832008-03-24 15:01:56 -04001225 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001226 extent_end = key.offset + btrfs_dev_extent_length(l,
1227 dev_extent);
1228 if (extent_end > search_start)
1229 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001230next:
1231 path->slots[0]++;
1232 cond_resched();
1233 }
Chris Mason0b86a832008-03-24 15:01:56 -04001234
liubo38c01b92011-08-02 02:39:03 +00001235 /*
1236 * At this point, search_start should be the end of
1237 * allocated dev extents, and when shrinking the device,
1238 * search_end may be smaller than search_start.
1239 */
1240 if (search_end > search_start)
1241 hole_size = search_end - search_start;
1242
Miao Xie7bfc8372011-01-05 10:07:26 +00001243 if (hole_size > max_hole_size) {
1244 max_hole_start = search_start;
1245 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001246 }
Chris Mason0b86a832008-03-24 15:01:56 -04001247
Josef Bacik6df9a952013-06-27 13:22:46 -04001248 if (contains_pending_extent(trans, device, &search_start, hole_size)) {
1249 btrfs_release_path(path);
1250 goto again;
1251 }
1252
Miao Xie7bfc8372011-01-05 10:07:26 +00001253 /* See above. */
1254 if (hole_size < num_bytes)
1255 ret = -ENOSPC;
1256 else
1257 ret = 0;
1258
1259out:
Yan Zheng2b820322008-11-17 21:11:30 -05001260 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001261 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001262 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001263 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001264 return ret;
1265}
1266
Christoph Hellwigb2950862008-12-02 09:54:17 -05001267static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001268 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001269 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001270{
1271 int ret;
1272 struct btrfs_path *path;
1273 struct btrfs_root *root = device->dev_root;
1274 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001275 struct btrfs_key found_key;
1276 struct extent_buffer *leaf = NULL;
1277 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001278
1279 path = btrfs_alloc_path();
1280 if (!path)
1281 return -ENOMEM;
1282
1283 key.objectid = device->devid;
1284 key.offset = start;
1285 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001286again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001287 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001288 if (ret > 0) {
1289 ret = btrfs_previous_item(root, path, key.objectid,
1290 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001291 if (ret)
1292 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001293 leaf = path->nodes[0];
1294 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1295 extent = btrfs_item_ptr(leaf, path->slots[0],
1296 struct btrfs_dev_extent);
1297 BUG_ON(found_key.offset > start || found_key.offset +
1298 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001299 key = found_key;
1300 btrfs_release_path(path);
1301 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001302 } else if (ret == 0) {
1303 leaf = path->nodes[0];
1304 extent = btrfs_item_ptr(leaf, path->slots[0],
1305 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001306 } else {
1307 btrfs_error(root->fs_info, ret, "Slot search failed");
1308 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001309 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001310
Miao Xie2196d6e2014-09-03 21:35:41 +08001311 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1312
Chris Mason8f18cf12008-04-25 16:53:30 -04001313 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001314 if (ret) {
1315 btrfs_error(root->fs_info, ret,
1316 "Failed to remove dev extent item");
1317 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001318out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001319 btrfs_free_path(path);
1320 return ret;
1321}
1322
Eric Sandeen48a3b632013-04-25 20:41:01 +00001323static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1324 struct btrfs_device *device,
1325 u64 chunk_tree, u64 chunk_objectid,
1326 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001327{
1328 int ret;
1329 struct btrfs_path *path;
1330 struct btrfs_root *root = device->dev_root;
1331 struct btrfs_dev_extent *extent;
1332 struct extent_buffer *leaf;
1333 struct btrfs_key key;
1334
Chris Masondfe25022008-05-13 13:46:40 -04001335 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001336 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001337 path = btrfs_alloc_path();
1338 if (!path)
1339 return -ENOMEM;
1340
Chris Mason0b86a832008-03-24 15:01:56 -04001341 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001342 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001343 key.type = BTRFS_DEV_EXTENT_KEY;
1344 ret = btrfs_insert_empty_item(trans, root, path, &key,
1345 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001346 if (ret)
1347 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001348
1349 leaf = path->nodes[0];
1350 extent = btrfs_item_ptr(leaf, path->slots[0],
1351 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001352 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1353 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1354 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1355
1356 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001357 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001358
Chris Mason0b86a832008-03-24 15:01:56 -04001359 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1360 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001361out:
Chris Mason0b86a832008-03-24 15:01:56 -04001362 btrfs_free_path(path);
1363 return ret;
1364}
1365
Josef Bacik6df9a952013-06-27 13:22:46 -04001366static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001367{
Josef Bacik6df9a952013-06-27 13:22:46 -04001368 struct extent_map_tree *em_tree;
1369 struct extent_map *em;
1370 struct rb_node *n;
1371 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001372
Josef Bacik6df9a952013-06-27 13:22:46 -04001373 em_tree = &fs_info->mapping_tree.map_tree;
1374 read_lock(&em_tree->lock);
1375 n = rb_last(&em_tree->map);
1376 if (n) {
1377 em = rb_entry(n, struct extent_map, rb_node);
1378 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001379 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001380 read_unlock(&em_tree->lock);
1381
Chris Mason0b86a832008-03-24 15:01:56 -04001382 return ret;
1383}
1384
Ilya Dryomov53f10652013-08-12 14:33:01 +03001385static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1386 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001387{
1388 int ret;
1389 struct btrfs_key key;
1390 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001391 struct btrfs_path *path;
1392
Yan Zheng2b820322008-11-17 21:11:30 -05001393 path = btrfs_alloc_path();
1394 if (!path)
1395 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001396
1397 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1398 key.type = BTRFS_DEV_ITEM_KEY;
1399 key.offset = (u64)-1;
1400
Ilya Dryomov53f10652013-08-12 14:33:01 +03001401 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001402 if (ret < 0)
1403 goto error;
1404
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001405 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001406
Ilya Dryomov53f10652013-08-12 14:33:01 +03001407 ret = btrfs_previous_item(fs_info->chunk_root, path,
1408 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001409 BTRFS_DEV_ITEM_KEY);
1410 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001411 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001412 } else {
1413 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1414 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001415 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001416 }
1417 ret = 0;
1418error:
Yan Zheng2b820322008-11-17 21:11:30 -05001419 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001420 return ret;
1421}
1422
1423/*
1424 * the device information is stored in the chunk root
1425 * the btrfs_device struct should be fully filled in
1426 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001427static int btrfs_add_device(struct btrfs_trans_handle *trans,
1428 struct btrfs_root *root,
1429 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001430{
1431 int ret;
1432 struct btrfs_path *path;
1433 struct btrfs_dev_item *dev_item;
1434 struct extent_buffer *leaf;
1435 struct btrfs_key key;
1436 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001437
1438 root = root->fs_info->chunk_root;
1439
1440 path = btrfs_alloc_path();
1441 if (!path)
1442 return -ENOMEM;
1443
Chris Mason0b86a832008-03-24 15:01:56 -04001444 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1445 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001446 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001447
1448 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001449 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001450 if (ret)
1451 goto out;
1452
1453 leaf = path->nodes[0];
1454 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1455
1456 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001457 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001458 btrfs_set_device_type(leaf, dev_item, device->type);
1459 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1460 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1461 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001462 btrfs_set_device_total_bytes(leaf, dev_item,
1463 btrfs_device_get_disk_total_bytes(device));
1464 btrfs_set_device_bytes_used(leaf, dev_item,
1465 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001466 btrfs_set_device_group(leaf, dev_item, 0);
1467 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1468 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001469 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001470
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001471 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001472 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001473 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001474 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001475 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001476
Yan Zheng2b820322008-11-17 21:11:30 -05001477 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001478out:
1479 btrfs_free_path(path);
1480 return ret;
1481}
Chris Mason8f18cf12008-04-25 16:53:30 -04001482
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001483/*
1484 * Function to update ctime/mtime for a given device path.
1485 * Mainly used for ctime/mtime based probe like libblkid.
1486 */
1487static void update_dev_time(char *path_name)
1488{
1489 struct file *filp;
1490
1491 filp = filp_open(path_name, O_RDWR, 0);
1492 if (!filp)
1493 return;
1494 file_update_time(filp);
1495 filp_close(filp, NULL);
1496 return;
1497}
1498
Chris Masona061fc82008-05-07 11:43:44 -04001499static int btrfs_rm_dev_item(struct btrfs_root *root,
1500 struct btrfs_device *device)
1501{
1502 int ret;
1503 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001504 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001505 struct btrfs_trans_handle *trans;
1506
1507 root = root->fs_info->chunk_root;
1508
1509 path = btrfs_alloc_path();
1510 if (!path)
1511 return -ENOMEM;
1512
Yan, Zhenga22285a2010-05-16 10:48:46 -04001513 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001514 if (IS_ERR(trans)) {
1515 btrfs_free_path(path);
1516 return PTR_ERR(trans);
1517 }
Chris Masona061fc82008-05-07 11:43:44 -04001518 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1519 key.type = BTRFS_DEV_ITEM_KEY;
1520 key.offset = device->devid;
1521
1522 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1523 if (ret < 0)
1524 goto out;
1525
1526 if (ret > 0) {
1527 ret = -ENOENT;
1528 goto out;
1529 }
1530
1531 ret = btrfs_del_item(trans, root, path);
1532 if (ret)
1533 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001534out:
1535 btrfs_free_path(path);
1536 btrfs_commit_transaction(trans, root);
1537 return ret;
1538}
1539
1540int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1541{
1542 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001543 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001544 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001545 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001546 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001547 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001548 u64 all_avail;
1549 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001550 u64 num_devices;
1551 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001552 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001553 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001554 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001555
Chris Masona061fc82008-05-07 11:43:44 -04001556 mutex_lock(&uuid_mutex);
1557
Miao Xiede98ced2013-01-29 10:13:12 +00001558 do {
1559 seq = read_seqbegin(&root->fs_info->profiles_lock);
1560
1561 all_avail = root->fs_info->avail_data_alloc_bits |
1562 root->fs_info->avail_system_alloc_bits |
1563 root->fs_info->avail_metadata_alloc_bits;
1564 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001565
Stefan Behrens8dabb742012-11-06 13:15:27 +01001566 num_devices = root->fs_info->fs_devices->num_devices;
1567 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1568 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1569 WARN_ON(num_devices < 1);
1570 num_devices--;
1571 }
1572 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1573
1574 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001575 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001576 goto out;
1577 }
1578
Stefan Behrens8dabb742012-11-06 13:15:27 +01001579 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001580 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001581 goto out;
1582 }
1583
David Woodhouse53b381b2013-01-29 18:40:14 -05001584 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1585 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001586 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001587 goto out;
1588 }
1589 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1590 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001591 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001592 goto out;
1593 }
1594
Chris Masondfe25022008-05-13 13:46:40 -04001595 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001596 struct list_head *devices;
1597 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001598
Chris Masondfe25022008-05-13 13:46:40 -04001599 device = NULL;
1600 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001601 /*
1602 * It is safe to read the devices since the volume_mutex
1603 * is held.
1604 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001605 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001606 if (tmp->in_fs_metadata &&
1607 !tmp->is_tgtdev_for_dev_replace &&
1608 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001609 device = tmp;
1610 break;
1611 }
1612 }
1613 bdev = NULL;
1614 bh = NULL;
1615 disk_super = NULL;
1616 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001617 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001618 goto out;
1619 }
Chris Masondfe25022008-05-13 13:46:40 -04001620 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001621 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001622 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001623 root->fs_info->bdev_holder, 0,
1624 &bdev, &bh);
1625 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001626 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001627 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001628 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001629 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001630 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001631 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001632 if (!device) {
1633 ret = -ENOENT;
1634 goto error_brelse;
1635 }
Chris Masondfe25022008-05-13 13:46:40 -04001636 }
Yan Zheng2b820322008-11-17 21:11:30 -05001637
Stefan Behrens63a212a2012-11-05 18:29:28 +01001638 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001639 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001640 goto error_brelse;
1641 }
1642
Yan Zheng2b820322008-11-17 21:11:30 -05001643 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001644 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001645 goto error_brelse;
1646 }
1647
1648 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001649 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001650 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001651 device->fs_devices->rw_devices--;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001652 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001653 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001654 }
Chris Masona061fc82008-05-07 11:43:44 -04001655
Carey Underwoodd7901552013-03-04 16:37:06 -06001656 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001657 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001658 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001659 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001660 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001661
Stefan Behrens63a212a2012-11-05 18:29:28 +01001662 /*
1663 * TODO: the superblock still includes this device in its num_devices
1664 * counter although write_all_supers() is not locked out. This
1665 * could give a filesystem state which requires a degraded mount.
1666 */
Chris Masona061fc82008-05-07 11:43:44 -04001667 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1668 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001669 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001670
Yan Zheng2b820322008-11-17 21:11:30 -05001671 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001672 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001673
1674 /*
1675 * the device list mutex makes sure that we don't change
1676 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001677 * the device supers. Whoever is writing all supers, should
1678 * lock the device list mutex before getting the number of
1679 * devices in the super block (super_copy). Conversely,
1680 * whoever updates the number of devices in the super block
1681 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001682 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001683
1684 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001685 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001686 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001687
Yan Zhenge4404d62008-12-12 10:03:26 -05001688 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001689 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001690
Chris Masoncd02dca2010-12-13 14:56:23 -05001691 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001692 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001693
Yan Zheng2b820322008-11-17 21:11:30 -05001694 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1695 struct btrfs_device, dev_list);
1696 if (device->bdev == root->fs_info->sb->s_bdev)
1697 root->fs_info->sb->s_bdev = next_device->bdev;
1698 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1699 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1700
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001701 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001702 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001703 /* remove sysfs entry */
1704 btrfs_kobj_rm_device(root->fs_info, device);
1705 }
Anand Jain99994cd2014-06-03 11:36:00 +08001706
Xiao Guangrong1f781602011-04-20 10:09:16 +00001707 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001708
David Sterba6c417612011-04-13 15:41:04 +02001709 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1710 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001711 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001712
Xiao Guangrong1f781602011-04-20 10:09:16 +00001713 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001714 struct btrfs_fs_devices *fs_devices;
1715 fs_devices = root->fs_info->fs_devices;
1716 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001717 if (fs_devices->seed == cur_devices) {
1718 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001719 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001720 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001721 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001722 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001723 cur_devices->seed = NULL;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001724 __btrfs_close_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001725 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001726 }
1727
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001728 root->fs_info->num_tolerated_disk_barrier_failures =
1729 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1730
Yan Zheng2b820322008-11-17 21:11:30 -05001731 /*
1732 * at this point, the device is zero sized. We want to
1733 * remove it from the devices list and zero out the old super
1734 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001735 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001736 u64 bytenr;
1737 int i;
1738
Chris Masondfe25022008-05-13 13:46:40 -04001739 /* make sure this device isn't detected as part of
1740 * the FS anymore
1741 */
1742 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1743 set_buffer_dirty(bh);
1744 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001745
1746 /* clear the mirror copies of super block on the disk
1747 * being removed, 0th copy is been taken care above and
1748 * the below would take of the rest
1749 */
1750 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1751 bytenr = btrfs_sb_offset(i);
1752 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1753 i_size_read(bdev->bd_inode))
1754 break;
1755
1756 brelse(bh);
1757 bh = __bread(bdev, bytenr / 4096,
1758 BTRFS_SUPER_INFO_SIZE);
1759 if (!bh)
1760 continue;
1761
1762 disk_super = (struct btrfs_super_block *)bh->b_data;
1763
1764 if (btrfs_super_bytenr(disk_super) != bytenr ||
1765 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1766 continue;
1767 }
1768 memset(&disk_super->magic, 0,
1769 sizeof(disk_super->magic));
1770 set_buffer_dirty(bh);
1771 sync_dirty_buffer(bh);
1772 }
Chris Masondfe25022008-05-13 13:46:40 -04001773 }
Chris Masona061fc82008-05-07 11:43:44 -04001774
Chris Masona061fc82008-05-07 11:43:44 -04001775 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001776
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001777 if (bdev) {
1778 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001779 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001780
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001781 /* Update ctime/mtime for device path for libblkid */
1782 update_dev_time(device_path);
1783 }
1784
Chris Masona061fc82008-05-07 11:43:44 -04001785error_brelse:
1786 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001787 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001788 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001789out:
1790 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001791 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001792error_undo:
1793 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001794 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001795 list_add(&device->dev_alloc_list,
1796 &root->fs_info->fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001797 device->fs_devices->rw_devices++;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001798 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001799 }
1800 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001801}
1802
Stefan Behrense93c89c2012-11-05 17:33:06 +01001803void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1804 struct btrfs_device *srcdev)
1805{
Anand Jaind51908c2014-08-13 14:24:19 +08001806 struct btrfs_fs_devices *fs_devices;
1807
Stefan Behrense93c89c2012-11-05 17:33:06 +01001808 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001809
Anand Jain25e8e912014-08-20 10:56:56 +08001810 /*
1811 * in case of fs with no seed, srcdev->fs_devices will point
1812 * to fs_devices of fs_info. However when the dev being replaced is
1813 * a seed dev it will point to the seed's local fs_devices. In short
1814 * srcdev will have its correct fs_devices in both the cases.
1815 */
1816 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08001817
Stefan Behrense93c89c2012-11-05 17:33:06 +01001818 list_del_rcu(&srcdev->dev_list);
1819 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08001820 fs_devices->num_devices--;
Miao Xie82372bc2014-09-03 21:35:44 +08001821 if (srcdev->missing)
Anand Jaind51908c2014-08-13 14:24:19 +08001822 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001823
Miao Xie82372bc2014-09-03 21:35:44 +08001824 if (srcdev->writeable) {
1825 fs_devices->rw_devices--;
1826 /* zero out the old super if it is writable */
1827 btrfs_scratch_superblock(srcdev);
Ilya Dryomov13572722013-10-02 20:41:01 +03001828 }
1829
Miao Xie82372bc2014-09-03 21:35:44 +08001830 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08001831 fs_devices->open_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001832
Stefan Behrense93c89c2012-11-05 17:33:06 +01001833 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001834
1835 /*
1836 * unless fs_devices is seed fs, num_devices shouldn't go
1837 * zero
1838 */
1839 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
1840
1841 /* if this is no devs we rather delete the fs_devices */
1842 if (!fs_devices->num_devices) {
1843 struct btrfs_fs_devices *tmp_fs_devices;
1844
1845 tmp_fs_devices = fs_info->fs_devices;
1846 while (tmp_fs_devices) {
1847 if (tmp_fs_devices->seed == fs_devices) {
1848 tmp_fs_devices->seed = fs_devices->seed;
1849 break;
1850 }
1851 tmp_fs_devices = tmp_fs_devices->seed;
1852 }
1853 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08001854 __btrfs_close_devices(fs_devices);
1855 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001856 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01001857}
1858
1859void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1860 struct btrfs_device *tgtdev)
1861{
1862 struct btrfs_device *next_device;
1863
Miao Xie67a2c452014-09-03 21:35:43 +08001864 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001865 WARN_ON(!tgtdev);
1866 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1867 if (tgtdev->bdev) {
1868 btrfs_scratch_superblock(tgtdev);
1869 fs_info->fs_devices->open_devices--;
1870 }
1871 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001872
1873 next_device = list_entry(fs_info->fs_devices->devices.next,
1874 struct btrfs_device, dev_list);
1875 if (tgtdev->bdev == fs_info->sb->s_bdev)
1876 fs_info->sb->s_bdev = next_device->bdev;
1877 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1878 fs_info->fs_devices->latest_bdev = next_device->bdev;
1879 list_del_rcu(&tgtdev->dev_list);
1880
1881 call_rcu(&tgtdev->rcu, free_device);
1882
1883 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08001884 mutex_unlock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001885}
1886
Eric Sandeen48a3b632013-04-25 20:41:01 +00001887static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1888 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001889{
1890 int ret = 0;
1891 struct btrfs_super_block *disk_super;
1892 u64 devid;
1893 u8 *dev_uuid;
1894 struct block_device *bdev;
1895 struct buffer_head *bh;
1896
1897 *device = NULL;
1898 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1899 root->fs_info->bdev_holder, 0, &bdev, &bh);
1900 if (ret)
1901 return ret;
1902 disk_super = (struct btrfs_super_block *)bh->b_data;
1903 devid = btrfs_stack_device_id(&disk_super->dev_item);
1904 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001905 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001906 disk_super->fsid);
1907 brelse(bh);
1908 if (!*device)
1909 ret = -ENOENT;
1910 blkdev_put(bdev, FMODE_READ);
1911 return ret;
1912}
1913
1914int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1915 char *device_path,
1916 struct btrfs_device **device)
1917{
1918 *device = NULL;
1919 if (strcmp(device_path, "missing") == 0) {
1920 struct list_head *devices;
1921 struct btrfs_device *tmp;
1922
1923 devices = &root->fs_info->fs_devices->devices;
1924 /*
1925 * It is safe to read the devices since the volume_mutex
1926 * is held by the caller.
1927 */
1928 list_for_each_entry(tmp, devices, dev_list) {
1929 if (tmp->in_fs_metadata && !tmp->bdev) {
1930 *device = tmp;
1931 break;
1932 }
1933 }
1934
1935 if (!*device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001936 btrfs_err(root->fs_info, "no missing device found");
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001937 return -ENOENT;
1938 }
1939
1940 return 0;
1941 } else {
1942 return btrfs_find_device_by_path(root, device_path, device);
1943 }
1944}
1945
Yan Zheng2b820322008-11-17 21:11:30 -05001946/*
1947 * does all the dirty work required for changing file system's UUID.
1948 */
Li Zefan125ccb02011-12-08 15:07:24 +08001949static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001950{
1951 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1952 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001953 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001954 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001955 struct btrfs_device *device;
1956 u64 super_flags;
1957
1958 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001959 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001960 return -EINVAL;
1961
Ilya Dryomov2208a372013-08-12 14:33:03 +03001962 seed_devices = __alloc_fs_devices();
1963 if (IS_ERR(seed_devices))
1964 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001965
Yan Zhenge4404d62008-12-12 10:03:26 -05001966 old_devices = clone_fs_devices(fs_devices);
1967 if (IS_ERR(old_devices)) {
1968 kfree(seed_devices);
1969 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001970 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001971
Yan Zheng2b820322008-11-17 21:11:30 -05001972 list_add(&old_devices->list, &fs_uuids);
1973
Yan Zhenge4404d62008-12-12 10:03:26 -05001974 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1975 seed_devices->opened = 1;
1976 INIT_LIST_HEAD(&seed_devices->devices);
1977 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001978 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001979
1980 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001981 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1982 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08001983 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05001984 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08001985
1986 lock_chunks(root);
1987 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1988 unlock_chunks(root);
Yan Zhenge4404d62008-12-12 10:03:26 -05001989
Yan Zheng2b820322008-11-17 21:11:30 -05001990 fs_devices->seeding = 0;
1991 fs_devices->num_devices = 0;
1992 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08001993 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08001994 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001995 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001996
1997 generate_random_uuid(fs_devices->fsid);
1998 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1999 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002000 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2001
Yan Zheng2b820322008-11-17 21:11:30 -05002002 super_flags = btrfs_super_flags(disk_super) &
2003 ~BTRFS_SUPER_FLAG_SEEDING;
2004 btrfs_set_super_flags(disk_super, super_flags);
2005
2006 return 0;
2007}
2008
2009/*
2010 * strore the expected generation for seed devices in device items.
2011 */
2012static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2013 struct btrfs_root *root)
2014{
2015 struct btrfs_path *path;
2016 struct extent_buffer *leaf;
2017 struct btrfs_dev_item *dev_item;
2018 struct btrfs_device *device;
2019 struct btrfs_key key;
2020 u8 fs_uuid[BTRFS_UUID_SIZE];
2021 u8 dev_uuid[BTRFS_UUID_SIZE];
2022 u64 devid;
2023 int ret;
2024
2025 path = btrfs_alloc_path();
2026 if (!path)
2027 return -ENOMEM;
2028
2029 root = root->fs_info->chunk_root;
2030 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2031 key.offset = 0;
2032 key.type = BTRFS_DEV_ITEM_KEY;
2033
2034 while (1) {
2035 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2036 if (ret < 0)
2037 goto error;
2038
2039 leaf = path->nodes[0];
2040next_slot:
2041 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2042 ret = btrfs_next_leaf(root, path);
2043 if (ret > 0)
2044 break;
2045 if (ret < 0)
2046 goto error;
2047 leaf = path->nodes[0];
2048 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002049 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002050 continue;
2051 }
2052
2053 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2054 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2055 key.type != BTRFS_DEV_ITEM_KEY)
2056 break;
2057
2058 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2059 struct btrfs_dev_item);
2060 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002061 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002062 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002063 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002064 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002065 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2066 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002067 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002068
2069 if (device->fs_devices->seeding) {
2070 btrfs_set_device_generation(leaf, dev_item,
2071 device->generation);
2072 btrfs_mark_buffer_dirty(leaf);
2073 }
2074
2075 path->slots[0]++;
2076 goto next_slot;
2077 }
2078 ret = 0;
2079error:
2080 btrfs_free_path(path);
2081 return ret;
2082}
2083
Chris Mason788f20e2008-04-28 15:29:42 -04002084int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2085{
Josef Bacikd5e20032011-08-04 14:52:27 +00002086 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002087 struct btrfs_trans_handle *trans;
2088 struct btrfs_device *device;
2089 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002090 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002091 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002092 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002093 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002094 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002095 int ret = 0;
2096
Yan Zheng2b820322008-11-17 21:11:30 -05002097 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002098 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002099
Li Zefana5d16332011-12-07 20:08:40 -05002100 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002101 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002102 if (IS_ERR(bdev))
2103 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002104
Yan Zheng2b820322008-11-17 21:11:30 -05002105 if (root->fs_info->fs_devices->seeding) {
2106 seeding_dev = 1;
2107 down_write(&sb->s_umount);
2108 mutex_lock(&uuid_mutex);
2109 }
2110
Chris Mason8c8bee12008-09-29 11:19:10 -04002111 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002112
Chris Mason788f20e2008-04-28 15:29:42 -04002113 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002114
2115 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002116 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002117 if (device->bdev == bdev) {
2118 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002119 mutex_unlock(
2120 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002121 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002122 }
2123 }
Liu Bod25628b2012-11-14 14:35:30 +00002124 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002125
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002126 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2127 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002128 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002129 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002130 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002131 }
2132
Josef Bacik606686e2012-06-04 14:03:51 -04002133 name = rcu_string_strdup(device_path, GFP_NOFS);
2134 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002135 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002136 ret = -ENOMEM;
2137 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002138 }
Josef Bacik606686e2012-06-04 14:03:51 -04002139 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002140
Yan, Zhenga22285a2010-05-16 10:48:46 -04002141 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002142 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002143 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002144 kfree(device);
2145 ret = PTR_ERR(trans);
2146 goto error;
2147 }
2148
Josef Bacikd5e20032011-08-04 14:52:27 +00002149 q = bdev_get_queue(bdev);
2150 if (blk_queue_discard(q))
2151 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002152 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002153 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002154 device->io_width = root->sectorsize;
2155 device->io_align = root->sectorsize;
2156 device->sector_size = root->sectorsize;
2157 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002158 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002159 device->commit_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002160 device->dev_root = root->fs_info->dev_root;
2161 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002162 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002163 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002164 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002165 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002166 set_blocksize(device->bdev, 4096);
2167
Yan Zheng2b820322008-11-17 21:11:30 -05002168 if (seeding_dev) {
2169 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002170 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002171 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002172 }
2173
2174 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002175
Chris Masone5e9a522009-06-10 15:17:02 -04002176 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002177 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002178 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002179 list_add(&device->dev_alloc_list,
2180 &root->fs_info->fs_devices->alloc_list);
2181 root->fs_info->fs_devices->num_devices++;
2182 root->fs_info->fs_devices->open_devices++;
2183 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002184 root->fs_info->fs_devices->total_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -05002185 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2186
Josef Bacik2bf64752011-09-26 17:12:22 -04002187 spin_lock(&root->fs_info->free_chunk_lock);
2188 root->fs_info->free_chunk_space += device->total_bytes;
2189 spin_unlock(&root->fs_info->free_chunk_lock);
2190
Chris Masonc2898112009-06-10 09:51:32 -04002191 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2192 root->fs_info->fs_devices->rotating = 1;
2193
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002194 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002195 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002196 tmp + device->total_bytes);
Chris Mason788f20e2008-04-28 15:29:42 -04002197
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002198 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002199 btrfs_set_super_num_devices(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002200 tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002201
2202 /* add sysfs device entry */
2203 btrfs_kobj_add_device(root->fs_info, device);
2204
Miao Xie2196d6e2014-09-03 21:35:41 +08002205 /*
2206 * we've got more storage, clear any full flags on the space
2207 * infos
2208 */
2209 btrfs_clear_space_info_full(root->fs_info);
2210
2211 unlock_chunks(root);
Chris Masone5e9a522009-06-10 15:17:02 -04002212 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002213
Yan Zheng2b820322008-11-17 21:11:30 -05002214 if (seeding_dev) {
Miao Xie2196d6e2014-09-03 21:35:41 +08002215 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05002216 ret = init_first_rw_device(trans, root, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002217 unlock_chunks(root);
David Sterba005d6422012-09-18 07:52:32 -06002218 if (ret) {
2219 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002220 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002221 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002222 }
2223
2224 ret = btrfs_add_device(trans, root, device);
2225 if (ret) {
2226 btrfs_abort_transaction(trans, root, ret);
2227 goto error_trans;
2228 }
2229
2230 if (seeding_dev) {
2231 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2232
Yan Zheng2b820322008-11-17 21:11:30 -05002233 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002234 if (ret) {
2235 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002236 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002237 }
Anand Jainb2373f22014-06-03 11:36:03 +08002238
2239 /* Sprouting would change fsid of the mounted root,
2240 * so rename the fsid on the sysfs
2241 */
2242 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2243 root->fs_info->fsid);
2244 if (kobject_rename(&root->fs_info->super_kobj, fsid_buf))
2245 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05002246 }
2247
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002248 root->fs_info->num_tolerated_disk_barrier_failures =
2249 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002250 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002251
2252 if (seeding_dev) {
2253 mutex_unlock(&uuid_mutex);
2254 up_write(&sb->s_umount);
2255
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002256 if (ret) /* transaction commit */
2257 return ret;
2258
Yan Zheng2b820322008-11-17 21:11:30 -05002259 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002260 if (ret < 0)
2261 btrfs_error(root->fs_info, ret,
2262 "Failed to relocate sys chunks after "
2263 "device initialization. This can be fixed "
2264 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002265 trans = btrfs_attach_transaction(root);
2266 if (IS_ERR(trans)) {
2267 if (PTR_ERR(trans) == -ENOENT)
2268 return 0;
2269 return PTR_ERR(trans);
2270 }
2271 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002272 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002273
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002274 /* Update ctime/mtime for libblkid */
2275 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002276 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002277
2278error_trans:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002279 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002280 rcu_string_free(device->name);
Anand Jain0d393762014-06-03 11:36:01 +08002281 btrfs_kobj_rm_device(root->fs_info, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002282 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002283error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002284 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002285 if (seeding_dev) {
2286 mutex_unlock(&uuid_mutex);
2287 up_write(&sb->s_umount);
2288 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002289 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002290}
2291
Stefan Behrense93c89c2012-11-05 17:33:06 +01002292int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002293 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002294 struct btrfs_device **device_out)
2295{
2296 struct request_queue *q;
2297 struct btrfs_device *device;
2298 struct block_device *bdev;
2299 struct btrfs_fs_info *fs_info = root->fs_info;
2300 struct list_head *devices;
2301 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002302 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002303 int ret = 0;
2304
2305 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002306 if (fs_info->fs_devices->seeding) {
2307 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002308 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002309 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002310
2311 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2312 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002313 if (IS_ERR(bdev)) {
2314 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002315 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002316 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002317
2318 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2319
2320 devices = &fs_info->fs_devices->devices;
2321 list_for_each_entry(device, devices, dev_list) {
2322 if (device->bdev == bdev) {
Miao Xie1c433662014-09-03 21:35:32 +08002323 btrfs_err(fs_info, "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002324 ret = -EEXIST;
2325 goto error;
2326 }
2327 }
2328
Miao Xie1c433662014-09-03 21:35:32 +08002329
Miao Xie7cc8e582014-09-03 21:35:38 +08002330 if (i_size_read(bdev->bd_inode) <
2331 btrfs_device_get_total_bytes(srcdev)) {
Miao Xie1c433662014-09-03 21:35:32 +08002332 btrfs_err(fs_info, "target device is smaller than source device!");
2333 ret = -EINVAL;
2334 goto error;
2335 }
2336
2337
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002338 device = btrfs_alloc_device(NULL, &devid, NULL);
2339 if (IS_ERR(device)) {
2340 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002341 goto error;
2342 }
2343
2344 name = rcu_string_strdup(device_path, GFP_NOFS);
2345 if (!name) {
2346 kfree(device);
2347 ret = -ENOMEM;
2348 goto error;
2349 }
2350 rcu_assign_pointer(device->name, name);
2351
2352 q = bdev_get_queue(bdev);
2353 if (blk_queue_discard(q))
2354 device->can_discard = 1;
2355 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2356 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002357 device->generation = 0;
2358 device->io_width = root->sectorsize;
2359 device->io_align = root->sectorsize;
2360 device->sector_size = root->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002361 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2362 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2363 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002364 ASSERT(list_empty(&srcdev->resized_list));
2365 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002366 device->commit_bytes_used = device->bytes_used;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002367 device->dev_root = fs_info->dev_root;
2368 device->bdev = bdev;
2369 device->in_fs_metadata = 1;
2370 device->is_tgtdev_for_dev_replace = 1;
2371 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002372 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002373 set_blocksize(device->bdev, 4096);
2374 device->fs_devices = fs_info->fs_devices;
2375 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2376 fs_info->fs_devices->num_devices++;
2377 fs_info->fs_devices->open_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002378 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2379
2380 *device_out = device;
2381 return ret;
2382
2383error:
2384 blkdev_put(bdev, FMODE_EXCL);
2385 return ret;
2386}
2387
2388void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2389 struct btrfs_device *tgtdev)
2390{
2391 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2392 tgtdev->io_width = fs_info->dev_root->sectorsize;
2393 tgtdev->io_align = fs_info->dev_root->sectorsize;
2394 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2395 tgtdev->dev_root = fs_info->dev_root;
2396 tgtdev->in_fs_metadata = 1;
2397}
2398
Chris Masond3977122009-01-05 21:25:51 -05002399static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2400 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002401{
2402 int ret;
2403 struct btrfs_path *path;
2404 struct btrfs_root *root;
2405 struct btrfs_dev_item *dev_item;
2406 struct extent_buffer *leaf;
2407 struct btrfs_key key;
2408
2409 root = device->dev_root->fs_info->chunk_root;
2410
2411 path = btrfs_alloc_path();
2412 if (!path)
2413 return -ENOMEM;
2414
2415 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2416 key.type = BTRFS_DEV_ITEM_KEY;
2417 key.offset = device->devid;
2418
2419 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2420 if (ret < 0)
2421 goto out;
2422
2423 if (ret > 0) {
2424 ret = -ENOENT;
2425 goto out;
2426 }
2427
2428 leaf = path->nodes[0];
2429 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2430
2431 btrfs_set_device_id(leaf, dev_item, device->devid);
2432 btrfs_set_device_type(leaf, dev_item, device->type);
2433 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2434 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2435 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002436 btrfs_set_device_total_bytes(leaf, dev_item,
2437 btrfs_device_get_disk_total_bytes(device));
2438 btrfs_set_device_bytes_used(leaf, dev_item,
2439 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002440 btrfs_mark_buffer_dirty(leaf);
2441
2442out:
2443 btrfs_free_path(path);
2444 return ret;
2445}
2446
Miao Xie2196d6e2014-09-03 21:35:41 +08002447int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002448 struct btrfs_device *device, u64 new_size)
2449{
2450 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002451 device->dev_root->fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002452 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002453 u64 old_total;
2454 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002455
Yan Zheng2b820322008-11-17 21:11:30 -05002456 if (!device->writeable)
2457 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002458
2459 lock_chunks(device->dev_root);
2460 old_total = btrfs_super_total_bytes(super_copy);
2461 diff = new_size - device->total_bytes;
2462
Stefan Behrens63a212a2012-11-05 18:29:28 +01002463 if (new_size <= device->total_bytes ||
Miao Xie2196d6e2014-09-03 21:35:41 +08002464 device->is_tgtdev_for_dev_replace) {
2465 unlock_chunks(device->dev_root);
Yan Zheng2b820322008-11-17 21:11:30 -05002466 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002467 }
Yan Zheng2b820322008-11-17 21:11:30 -05002468
Miao Xie935e5cc2014-09-03 21:35:33 +08002469 fs_devices = device->dev_root->fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002470
2471 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002472 device->fs_devices->total_rw_bytes += diff;
2473
Miao Xie7cc8e582014-09-03 21:35:38 +08002474 btrfs_device_set_total_bytes(device, new_size);
2475 btrfs_device_set_disk_total_bytes(device, new_size);
Chris Mason4184ea72009-03-10 12:39:20 -04002476 btrfs_clear_space_info_full(device->dev_root->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002477 if (list_empty(&device->resized_list))
2478 list_add_tail(&device->resized_list,
2479 &fs_devices->resized_devices);
Miao Xie2196d6e2014-09-03 21:35:41 +08002480 unlock_chunks(device->dev_root);
Chris Mason4184ea72009-03-10 12:39:20 -04002481
Chris Mason8f18cf12008-04-25 16:53:30 -04002482 return btrfs_update_device(trans, device);
2483}
2484
2485static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2486 struct btrfs_root *root,
2487 u64 chunk_tree, u64 chunk_objectid,
2488 u64 chunk_offset)
2489{
2490 int ret;
2491 struct btrfs_path *path;
2492 struct btrfs_key key;
2493
2494 root = root->fs_info->chunk_root;
2495 path = btrfs_alloc_path();
2496 if (!path)
2497 return -ENOMEM;
2498
2499 key.objectid = chunk_objectid;
2500 key.offset = chunk_offset;
2501 key.type = BTRFS_CHUNK_ITEM_KEY;
2502
2503 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002504 if (ret < 0)
2505 goto out;
2506 else if (ret > 0) { /* Logic error or corruption */
2507 btrfs_error(root->fs_info, -ENOENT,
2508 "Failed lookup while freeing chunk.");
2509 ret = -ENOENT;
2510 goto out;
2511 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002512
2513 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002514 if (ret < 0)
2515 btrfs_error(root->fs_info, ret,
2516 "Failed to delete chunk item.");
2517out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002518 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002519 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002520}
2521
Christoph Hellwigb2950862008-12-02 09:54:17 -05002522static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002523 chunk_offset)
2524{
David Sterba6c417612011-04-13 15:41:04 +02002525 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002526 struct btrfs_disk_key *disk_key;
2527 struct btrfs_chunk *chunk;
2528 u8 *ptr;
2529 int ret = 0;
2530 u32 num_stripes;
2531 u32 array_size;
2532 u32 len = 0;
2533 u32 cur;
2534 struct btrfs_key key;
2535
Miao Xie2196d6e2014-09-03 21:35:41 +08002536 lock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002537 array_size = btrfs_super_sys_array_size(super_copy);
2538
2539 ptr = super_copy->sys_chunk_array;
2540 cur = 0;
2541
2542 while (cur < array_size) {
2543 disk_key = (struct btrfs_disk_key *)ptr;
2544 btrfs_disk_key_to_cpu(&key, disk_key);
2545
2546 len = sizeof(*disk_key);
2547
2548 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2549 chunk = (struct btrfs_chunk *)(ptr + len);
2550 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2551 len += btrfs_chunk_item_size(num_stripes);
2552 } else {
2553 ret = -EIO;
2554 break;
2555 }
2556 if (key.objectid == chunk_objectid &&
2557 key.offset == chunk_offset) {
2558 memmove(ptr, ptr + len, array_size - (cur + len));
2559 array_size -= len;
2560 btrfs_set_super_sys_array_size(super_copy, array_size);
2561 } else {
2562 ptr += len;
2563 cur += len;
2564 }
2565 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002566 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002567 return ret;
2568}
2569
Josef Bacik47ab2a62014-09-18 11:20:02 -04002570int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2571 struct btrfs_root *root, u64 chunk_offset)
2572{
2573 struct extent_map_tree *em_tree;
2574 struct extent_map *em;
2575 struct btrfs_root *extent_root = root->fs_info->extent_root;
2576 struct map_lookup *map;
2577 u64 dev_extent_len = 0;
2578 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2579 u64 chunk_tree = root->fs_info->chunk_root->objectid;
2580 int i, ret = 0;
2581
2582 /* Just in case */
2583 root = root->fs_info->chunk_root;
2584 em_tree = &root->fs_info->mapping_tree.map_tree;
2585
2586 read_lock(&em_tree->lock);
2587 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2588 read_unlock(&em_tree->lock);
2589
2590 if (!em || em->start > chunk_offset ||
2591 em->start + em->len < chunk_offset) {
2592 /*
2593 * This is a logic error, but we don't want to just rely on the
2594 * user having built with ASSERT enabled, so if ASSERT doens't
2595 * do anything we still error out.
2596 */
2597 ASSERT(0);
2598 if (em)
2599 free_extent_map(em);
2600 return -EINVAL;
2601 }
2602 map = (struct map_lookup *)em->bdev;
2603
2604 for (i = 0; i < map->num_stripes; i++) {
2605 struct btrfs_device *device = map->stripes[i].dev;
2606 ret = btrfs_free_dev_extent(trans, device,
2607 map->stripes[i].physical,
2608 &dev_extent_len);
2609 if (ret) {
2610 btrfs_abort_transaction(trans, root, ret);
2611 goto out;
2612 }
2613
2614 if (device->bytes_used > 0) {
2615 lock_chunks(root);
2616 btrfs_device_set_bytes_used(device,
2617 device->bytes_used - dev_extent_len);
2618 spin_lock(&root->fs_info->free_chunk_lock);
2619 root->fs_info->free_chunk_space += dev_extent_len;
2620 spin_unlock(&root->fs_info->free_chunk_lock);
2621 btrfs_clear_space_info_full(root->fs_info);
2622 unlock_chunks(root);
2623 }
2624
2625 if (map->stripes[i].dev) {
2626 ret = btrfs_update_device(trans, map->stripes[i].dev);
2627 if (ret) {
2628 btrfs_abort_transaction(trans, root, ret);
2629 goto out;
2630 }
2631 }
2632 }
2633 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2634 chunk_offset);
2635 if (ret) {
2636 btrfs_abort_transaction(trans, root, ret);
2637 goto out;
2638 }
2639
2640 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2641
2642 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2643 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2644 if (ret) {
2645 btrfs_abort_transaction(trans, root, ret);
2646 goto out;
2647 }
2648 }
2649
2650 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2651 if (ret) {
2652 btrfs_abort_transaction(trans, extent_root, ret);
2653 goto out;
2654 }
2655
2656 write_lock(&em_tree->lock);
2657 remove_extent_mapping(em_tree, em);
2658 write_unlock(&em_tree->lock);
2659
2660 /* once for the tree */
2661 free_extent_map(em);
2662out:
2663 /* once for us */
2664 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002665 return ret;
2666}
2667
Christoph Hellwigb2950862008-12-02 09:54:17 -05002668static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002669 u64 chunk_tree, u64 chunk_objectid,
2670 u64 chunk_offset)
2671{
Chris Mason8f18cf12008-04-25 16:53:30 -04002672 struct btrfs_root *extent_root;
2673 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002674 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002675
2676 root = root->fs_info->chunk_root;
2677 extent_root = root->fs_info->extent_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002678
Josef Bacikba1bf482009-09-11 16:11:19 -04002679 ret = btrfs_can_relocate(extent_root, chunk_offset);
2680 if (ret)
2681 return -ENOSPC;
2682
Chris Mason8f18cf12008-04-25 16:53:30 -04002683 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002684 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002685 if (ret)
2686 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002687
Yan, Zhenga22285a2010-05-16 10:48:46 -04002688 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002689 if (IS_ERR(trans)) {
2690 ret = PTR_ERR(trans);
2691 btrfs_std_error(root->fs_info, ret);
2692 return ret;
2693 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002694
2695 /*
2696 * step two, delete the device extents and the
2697 * chunk tree entries
2698 */
Josef Bacik47ab2a62014-09-18 11:20:02 -04002699 ret = btrfs_remove_chunk(trans, root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002700 btrfs_end_transaction(trans, root);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002701 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002702}
2703
Yan Zheng2b820322008-11-17 21:11:30 -05002704static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2705{
2706 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2707 struct btrfs_path *path;
2708 struct extent_buffer *leaf;
2709 struct btrfs_chunk *chunk;
2710 struct btrfs_key key;
2711 struct btrfs_key found_key;
2712 u64 chunk_tree = chunk_root->root_key.objectid;
2713 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002714 bool retried = false;
2715 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002716 int ret;
2717
2718 path = btrfs_alloc_path();
2719 if (!path)
2720 return -ENOMEM;
2721
Josef Bacikba1bf482009-09-11 16:11:19 -04002722again:
Yan Zheng2b820322008-11-17 21:11:30 -05002723 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2724 key.offset = (u64)-1;
2725 key.type = BTRFS_CHUNK_ITEM_KEY;
2726
2727 while (1) {
2728 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2729 if (ret < 0)
2730 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002731 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002732
2733 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2734 key.type);
2735 if (ret < 0)
2736 goto error;
2737 if (ret > 0)
2738 break;
2739
2740 leaf = path->nodes[0];
2741 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2742
2743 chunk = btrfs_item_ptr(leaf, path->slots[0],
2744 struct btrfs_chunk);
2745 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002746 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002747
2748 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2749 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2750 found_key.objectid,
2751 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002752 if (ret == -ENOSPC)
2753 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302754 else
2755 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002756 }
2757
2758 if (found_key.offset == 0)
2759 break;
2760 key.offset = found_key.offset - 1;
2761 }
2762 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002763 if (failed && !retried) {
2764 failed = 0;
2765 retried = true;
2766 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302767 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002768 ret = -ENOSPC;
2769 }
Yan Zheng2b820322008-11-17 21:11:30 -05002770error:
2771 btrfs_free_path(path);
2772 return ret;
2773}
2774
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002775static int insert_balance_item(struct btrfs_root *root,
2776 struct btrfs_balance_control *bctl)
2777{
2778 struct btrfs_trans_handle *trans;
2779 struct btrfs_balance_item *item;
2780 struct btrfs_disk_balance_args disk_bargs;
2781 struct btrfs_path *path;
2782 struct extent_buffer *leaf;
2783 struct btrfs_key key;
2784 int ret, err;
2785
2786 path = btrfs_alloc_path();
2787 if (!path)
2788 return -ENOMEM;
2789
2790 trans = btrfs_start_transaction(root, 0);
2791 if (IS_ERR(trans)) {
2792 btrfs_free_path(path);
2793 return PTR_ERR(trans);
2794 }
2795
2796 key.objectid = BTRFS_BALANCE_OBJECTID;
2797 key.type = BTRFS_BALANCE_ITEM_KEY;
2798 key.offset = 0;
2799
2800 ret = btrfs_insert_empty_item(trans, root, path, &key,
2801 sizeof(*item));
2802 if (ret)
2803 goto out;
2804
2805 leaf = path->nodes[0];
2806 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2807
2808 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2809
2810 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2811 btrfs_set_balance_data(leaf, item, &disk_bargs);
2812 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2813 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2814 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2815 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2816
2817 btrfs_set_balance_flags(leaf, item, bctl->flags);
2818
2819 btrfs_mark_buffer_dirty(leaf);
2820out:
2821 btrfs_free_path(path);
2822 err = btrfs_commit_transaction(trans, root);
2823 if (err && !ret)
2824 ret = err;
2825 return ret;
2826}
2827
2828static int del_balance_item(struct btrfs_root *root)
2829{
2830 struct btrfs_trans_handle *trans;
2831 struct btrfs_path *path;
2832 struct btrfs_key key;
2833 int ret, err;
2834
2835 path = btrfs_alloc_path();
2836 if (!path)
2837 return -ENOMEM;
2838
2839 trans = btrfs_start_transaction(root, 0);
2840 if (IS_ERR(trans)) {
2841 btrfs_free_path(path);
2842 return PTR_ERR(trans);
2843 }
2844
2845 key.objectid = BTRFS_BALANCE_OBJECTID;
2846 key.type = BTRFS_BALANCE_ITEM_KEY;
2847 key.offset = 0;
2848
2849 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2850 if (ret < 0)
2851 goto out;
2852 if (ret > 0) {
2853 ret = -ENOENT;
2854 goto out;
2855 }
2856
2857 ret = btrfs_del_item(trans, root, path);
2858out:
2859 btrfs_free_path(path);
2860 err = btrfs_commit_transaction(trans, root);
2861 if (err && !ret)
2862 ret = err;
2863 return ret;
2864}
2865
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002866/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002867 * This is a heuristic used to reduce the number of chunks balanced on
2868 * resume after balance was interrupted.
2869 */
2870static void update_balance_args(struct btrfs_balance_control *bctl)
2871{
2872 /*
2873 * Turn on soft mode for chunk types that were being converted.
2874 */
2875 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2876 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2877 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2878 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2879 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2880 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2881
2882 /*
2883 * Turn on usage filter if is not already used. The idea is
2884 * that chunks that we have already balanced should be
2885 * reasonably full. Don't do it for chunks that are being
2886 * converted - that will keep us from relocating unconverted
2887 * (albeit full) chunks.
2888 */
2889 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2890 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2891 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2892 bctl->data.usage = 90;
2893 }
2894 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2895 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2896 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2897 bctl->sys.usage = 90;
2898 }
2899 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2900 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2901 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2902 bctl->meta.usage = 90;
2903 }
2904}
2905
2906/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002907 * Should be called with both balance and volume mutexes held to
2908 * serialize other volume operations (add_dev/rm_dev/resize) with
2909 * restriper. Same goes for unset_balance_control.
2910 */
2911static void set_balance_control(struct btrfs_balance_control *bctl)
2912{
2913 struct btrfs_fs_info *fs_info = bctl->fs_info;
2914
2915 BUG_ON(fs_info->balance_ctl);
2916
2917 spin_lock(&fs_info->balance_lock);
2918 fs_info->balance_ctl = bctl;
2919 spin_unlock(&fs_info->balance_lock);
2920}
2921
2922static void unset_balance_control(struct btrfs_fs_info *fs_info)
2923{
2924 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2925
2926 BUG_ON(!fs_info->balance_ctl);
2927
2928 spin_lock(&fs_info->balance_lock);
2929 fs_info->balance_ctl = NULL;
2930 spin_unlock(&fs_info->balance_lock);
2931
2932 kfree(bctl);
2933}
2934
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002935/*
2936 * Balance filters. Return 1 if chunk should be filtered out
2937 * (should not be balanced).
2938 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002939static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002940 struct btrfs_balance_args *bargs)
2941{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002942 chunk_type = chunk_to_extended(chunk_type) &
2943 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002944
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002945 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002946 return 0;
2947
2948 return 1;
2949}
2950
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002951static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2952 struct btrfs_balance_args *bargs)
2953{
2954 struct btrfs_block_group_cache *cache;
2955 u64 chunk_used, user_thresh;
2956 int ret = 1;
2957
2958 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2959 chunk_used = btrfs_block_group_used(&cache->item);
2960
Ilya Dryomova105bb82013-01-21 15:15:56 +02002961 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002962 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002963 else if (bargs->usage > 100)
2964 user_thresh = cache->key.offset;
2965 else
2966 user_thresh = div_factor_fine(cache->key.offset,
2967 bargs->usage);
2968
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002969 if (chunk_used < user_thresh)
2970 ret = 0;
2971
2972 btrfs_put_block_group(cache);
2973 return ret;
2974}
2975
Ilya Dryomov409d4042012-01-16 22:04:47 +02002976static int chunk_devid_filter(struct extent_buffer *leaf,
2977 struct btrfs_chunk *chunk,
2978 struct btrfs_balance_args *bargs)
2979{
2980 struct btrfs_stripe *stripe;
2981 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2982 int i;
2983
2984 for (i = 0; i < num_stripes; i++) {
2985 stripe = btrfs_stripe_nr(chunk, i);
2986 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2987 return 0;
2988 }
2989
2990 return 1;
2991}
2992
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002993/* [pstart, pend) */
2994static int chunk_drange_filter(struct extent_buffer *leaf,
2995 struct btrfs_chunk *chunk,
2996 u64 chunk_offset,
2997 struct btrfs_balance_args *bargs)
2998{
2999 struct btrfs_stripe *stripe;
3000 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3001 u64 stripe_offset;
3002 u64 stripe_length;
3003 int factor;
3004 int i;
3005
3006 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3007 return 0;
3008
3009 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003010 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3011 factor = num_stripes / 2;
3012 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3013 factor = num_stripes - 1;
3014 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3015 factor = num_stripes - 2;
3016 } else {
3017 factor = num_stripes;
3018 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003019
3020 for (i = 0; i < num_stripes; i++) {
3021 stripe = btrfs_stripe_nr(chunk, i);
3022 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3023 continue;
3024
3025 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3026 stripe_length = btrfs_chunk_length(leaf, chunk);
3027 do_div(stripe_length, factor);
3028
3029 if (stripe_offset < bargs->pend &&
3030 stripe_offset + stripe_length > bargs->pstart)
3031 return 0;
3032 }
3033
3034 return 1;
3035}
3036
Ilya Dryomovea671762012-01-16 22:04:48 +02003037/* [vstart, vend) */
3038static int chunk_vrange_filter(struct extent_buffer *leaf,
3039 struct btrfs_chunk *chunk,
3040 u64 chunk_offset,
3041 struct btrfs_balance_args *bargs)
3042{
3043 if (chunk_offset < bargs->vend &&
3044 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3045 /* at least part of the chunk is inside this vrange */
3046 return 0;
3047
3048 return 1;
3049}
3050
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003051static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003052 struct btrfs_balance_args *bargs)
3053{
3054 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3055 return 0;
3056
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003057 chunk_type = chunk_to_extended(chunk_type) &
3058 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003059
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003060 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003061 return 1;
3062
3063 return 0;
3064}
3065
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003066static int should_balance_chunk(struct btrfs_root *root,
3067 struct extent_buffer *leaf,
3068 struct btrfs_chunk *chunk, u64 chunk_offset)
3069{
3070 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3071 struct btrfs_balance_args *bargs = NULL;
3072 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3073
3074 /* type filter */
3075 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3076 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3077 return 0;
3078 }
3079
3080 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3081 bargs = &bctl->data;
3082 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3083 bargs = &bctl->sys;
3084 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3085 bargs = &bctl->meta;
3086
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003087 /* profiles filter */
3088 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3089 chunk_profiles_filter(chunk_type, bargs)) {
3090 return 0;
3091 }
3092
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003093 /* usage filter */
3094 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3095 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3096 return 0;
3097 }
3098
Ilya Dryomov409d4042012-01-16 22:04:47 +02003099 /* devid filter */
3100 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3101 chunk_devid_filter(leaf, chunk, bargs)) {
3102 return 0;
3103 }
3104
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003105 /* drange filter, makes sense only with devid filter */
3106 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3107 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3108 return 0;
3109 }
3110
Ilya Dryomovea671762012-01-16 22:04:48 +02003111 /* vrange filter */
3112 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3113 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3114 return 0;
3115 }
3116
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003117 /* soft profile changing mode */
3118 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3119 chunk_soft_convert_filter(chunk_type, bargs)) {
3120 return 0;
3121 }
3122
David Sterba7d824b62014-05-07 17:37:51 +02003123 /*
3124 * limited by count, must be the last filter
3125 */
3126 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3127 if (bargs->limit == 0)
3128 return 0;
3129 else
3130 bargs->limit--;
3131 }
3132
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003133 return 1;
3134}
3135
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003136static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003137{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003138 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003139 struct btrfs_root *chunk_root = fs_info->chunk_root;
3140 struct btrfs_root *dev_root = fs_info->dev_root;
3141 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003142 struct btrfs_device *device;
3143 u64 old_size;
3144 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003145 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003146 struct btrfs_path *path;
3147 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003148 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003149 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003150 struct extent_buffer *leaf;
3151 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003152 int ret;
3153 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003154 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003155 u64 limit_data = bctl->data.limit;
3156 u64 limit_meta = bctl->meta.limit;
3157 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003158
Chris Masonec44a352008-04-28 15:29:52 -04003159 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003160 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003161 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003162 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003163 size_to_free = div_factor(old_size, 1);
3164 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003165 if (!device->writeable ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003166 btrfs_device_get_total_bytes(device) -
3167 btrfs_device_get_bytes_used(device) > size_to_free ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003168 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003169 continue;
3170
3171 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003172 if (ret == -ENOSPC)
3173 break;
Chris Masonec44a352008-04-28 15:29:52 -04003174 BUG_ON(ret);
3175
Yan, Zhenga22285a2010-05-16 10:48:46 -04003176 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003177 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003178
3179 ret = btrfs_grow_device(trans, device, old_size);
3180 BUG_ON(ret);
3181
3182 btrfs_end_transaction(trans, dev_root);
3183 }
3184
3185 /* step two, relocate all the chunks */
3186 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003187 if (!path) {
3188 ret = -ENOMEM;
3189 goto error;
3190 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003191
3192 /* zero out stat counters */
3193 spin_lock(&fs_info->balance_lock);
3194 memset(&bctl->stat, 0, sizeof(bctl->stat));
3195 spin_unlock(&fs_info->balance_lock);
3196again:
David Sterba7d824b62014-05-07 17:37:51 +02003197 if (!counting) {
3198 bctl->data.limit = limit_data;
3199 bctl->meta.limit = limit_meta;
3200 bctl->sys.limit = limit_sys;
3201 }
Chris Masonec44a352008-04-28 15:29:52 -04003202 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3203 key.offset = (u64)-1;
3204 key.type = BTRFS_CHUNK_ITEM_KEY;
3205
Chris Masond3977122009-01-05 21:25:51 -05003206 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003207 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003208 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003209 ret = -ECANCELED;
3210 goto error;
3211 }
3212
Chris Masonec44a352008-04-28 15:29:52 -04003213 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3214 if (ret < 0)
3215 goto error;
3216
3217 /*
3218 * this shouldn't happen, it means the last relocate
3219 * failed
3220 */
3221 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003222 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003223
3224 ret = btrfs_previous_item(chunk_root, path, 0,
3225 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003226 if (ret) {
3227 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003228 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003229 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003230
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003231 leaf = path->nodes[0];
3232 slot = path->slots[0];
3233 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3234
Chris Masonec44a352008-04-28 15:29:52 -04003235 if (found_key.objectid != key.objectid)
3236 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04003237
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003238 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3239
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003240 if (!counting) {
3241 spin_lock(&fs_info->balance_lock);
3242 bctl->stat.considered++;
3243 spin_unlock(&fs_info->balance_lock);
3244 }
3245
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003246 ret = should_balance_chunk(chunk_root, leaf, chunk,
3247 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003248 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003249 if (!ret)
3250 goto loop;
3251
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003252 if (counting) {
3253 spin_lock(&fs_info->balance_lock);
3254 bctl->stat.expected++;
3255 spin_unlock(&fs_info->balance_lock);
3256 goto loop;
3257 }
3258
Chris Masonec44a352008-04-28 15:29:52 -04003259 ret = btrfs_relocate_chunk(chunk_root,
3260 chunk_root->root_key.objectid,
3261 found_key.objectid,
3262 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003263 if (ret && ret != -ENOSPC)
3264 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003265 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003266 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003267 } else {
3268 spin_lock(&fs_info->balance_lock);
3269 bctl->stat.completed++;
3270 spin_unlock(&fs_info->balance_lock);
3271 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003272loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003273 if (found_key.offset == 0)
3274 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003275 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003276 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003277
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003278 if (counting) {
3279 btrfs_release_path(path);
3280 counting = false;
3281 goto again;
3282 }
Chris Masonec44a352008-04-28 15:29:52 -04003283error:
3284 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003285 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003286 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003287 enospc_errors);
3288 if (!ret)
3289 ret = -ENOSPC;
3290 }
3291
Chris Masonec44a352008-04-28 15:29:52 -04003292 return ret;
3293}
3294
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003295/**
3296 * alloc_profile_is_valid - see if a given profile is valid and reduced
3297 * @flags: profile to validate
3298 * @extended: if true @flags is treated as an extended profile
3299 */
3300static int alloc_profile_is_valid(u64 flags, int extended)
3301{
3302 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3303 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3304
3305 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3306
3307 /* 1) check that all other bits are zeroed */
3308 if (flags & ~mask)
3309 return 0;
3310
3311 /* 2) see if profile is reduced */
3312 if (flags == 0)
3313 return !extended; /* "0" is valid for usual profiles */
3314
3315 /* true if exactly one bit set */
3316 return (flags & (flags - 1)) == 0;
3317}
3318
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003319static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3320{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003321 /* cancel requested || normal exit path */
3322 return atomic_read(&fs_info->balance_cancel_req) ||
3323 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3324 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003325}
3326
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003327static void __cancel_balance(struct btrfs_fs_info *fs_info)
3328{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003329 int ret;
3330
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003331 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003332 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003333 if (ret)
3334 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003335
3336 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003337}
3338
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003339/*
3340 * Should be called with both balance and volume mutexes held
3341 */
3342int btrfs_balance(struct btrfs_balance_control *bctl,
3343 struct btrfs_ioctl_balance_args *bargs)
3344{
3345 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003346 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003347 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003348 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003349 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003350 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003351
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003352 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003353 atomic_read(&fs_info->balance_pause_req) ||
3354 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003355 ret = -EINVAL;
3356 goto out;
3357 }
3358
Ilya Dryomove4837f82012-03-27 17:09:17 +03003359 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3360 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3361 mixed = 1;
3362
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003363 /*
3364 * In case of mixed groups both data and meta should be picked,
3365 * and identical options should be given for both of them.
3366 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003367 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3368 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003369 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3370 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3371 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003372 btrfs_err(fs_info, "with mixed groups data and "
3373 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003374 ret = -EINVAL;
3375 goto out;
3376 }
3377 }
3378
Stefan Behrens8dabb742012-11-06 13:15:27 +01003379 num_devices = fs_info->fs_devices->num_devices;
3380 btrfs_dev_replace_lock(&fs_info->dev_replace);
3381 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3382 BUG_ON(num_devices < 1);
3383 num_devices--;
3384 }
3385 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003386 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003387 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003388 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003389 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003390 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003391 if (num_devices > 2)
3392 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3393 if (num_devices > 3)
3394 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3395 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003396 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3397 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3398 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003399 btrfs_err(fs_info, "unable to start balance with target "
3400 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003401 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003402 ret = -EINVAL;
3403 goto out;
3404 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003405 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3406 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3407 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003408 btrfs_err(fs_info,
3409 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003410 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003411 ret = -EINVAL;
3412 goto out;
3413 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003414 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3415 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3416 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003417 btrfs_err(fs_info,
3418 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003419 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003420 ret = -EINVAL;
3421 goto out;
3422 }
3423
Ilya Dryomove4837f82012-03-27 17:09:17 +03003424 /* allow dup'ed data chunks only in mixed mode */
3425 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003426 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003427 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003428 ret = -EINVAL;
3429 goto out;
3430 }
3431
3432 /* allow to reduce meta or sys integrity only if force set */
3433 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003434 BTRFS_BLOCK_GROUP_RAID10 |
3435 BTRFS_BLOCK_GROUP_RAID5 |
3436 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003437 do {
3438 seq = read_seqbegin(&fs_info->profiles_lock);
3439
3440 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3441 (fs_info->avail_system_alloc_bits & allowed) &&
3442 !(bctl->sys.target & allowed)) ||
3443 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3444 (fs_info->avail_metadata_alloc_bits & allowed) &&
3445 !(bctl->meta.target & allowed))) {
3446 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003447 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003448 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003449 btrfs_err(fs_info, "balance will reduce metadata "
3450 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003451 ret = -EINVAL;
3452 goto out;
3453 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003454 }
Miao Xiede98ced2013-01-29 10:13:12 +00003455 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003456
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003457 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3458 int num_tolerated_disk_barrier_failures;
3459 u64 target = bctl->sys.target;
3460
3461 num_tolerated_disk_barrier_failures =
3462 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3463 if (num_tolerated_disk_barrier_failures > 0 &&
3464 (target &
3465 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3466 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3467 num_tolerated_disk_barrier_failures = 0;
3468 else if (num_tolerated_disk_barrier_failures > 1 &&
3469 (target &
3470 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3471 num_tolerated_disk_barrier_failures = 1;
3472
3473 fs_info->num_tolerated_disk_barrier_failures =
3474 num_tolerated_disk_barrier_failures;
3475 }
3476
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003477 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003478 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003479 goto out;
3480
Ilya Dryomov59641012012-01-16 22:04:48 +02003481 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3482 BUG_ON(ret == -EEXIST);
3483 set_balance_control(bctl);
3484 } else {
3485 BUG_ON(ret != -EEXIST);
3486 spin_lock(&fs_info->balance_lock);
3487 update_balance_args(bctl);
3488 spin_unlock(&fs_info->balance_lock);
3489 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003490
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003491 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003492 mutex_unlock(&fs_info->balance_mutex);
3493
3494 ret = __btrfs_balance(fs_info);
3495
3496 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003497 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003498
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003499 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3500 fs_info->num_tolerated_disk_barrier_failures =
3501 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3502 }
3503
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003504 if (bargs) {
3505 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003506 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003507 }
3508
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003509 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3510 balance_need_close(fs_info)) {
3511 __cancel_balance(fs_info);
3512 }
3513
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003514 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003515
3516 return ret;
3517out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003518 if (bctl->flags & BTRFS_BALANCE_RESUME)
3519 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003520 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003521 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003522 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3523 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003524 return ret;
3525}
3526
3527static int balance_kthread(void *data)
3528{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003529 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003530 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003531
3532 mutex_lock(&fs_info->volume_mutex);
3533 mutex_lock(&fs_info->balance_mutex);
3534
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003535 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003536 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003537 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003538 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003539
3540 mutex_unlock(&fs_info->balance_mutex);
3541 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003542
Ilya Dryomov59641012012-01-16 22:04:48 +02003543 return ret;
3544}
3545
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003546int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3547{
3548 struct task_struct *tsk;
3549
3550 spin_lock(&fs_info->balance_lock);
3551 if (!fs_info->balance_ctl) {
3552 spin_unlock(&fs_info->balance_lock);
3553 return 0;
3554 }
3555 spin_unlock(&fs_info->balance_lock);
3556
3557 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003558 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003559 return 0;
3560 }
3561
3562 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303563 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003564}
3565
Ilya Dryomov68310a52012-06-22 12:24:12 -06003566int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003567{
Ilya Dryomov59641012012-01-16 22:04:48 +02003568 struct btrfs_balance_control *bctl;
3569 struct btrfs_balance_item *item;
3570 struct btrfs_disk_balance_args disk_bargs;
3571 struct btrfs_path *path;
3572 struct extent_buffer *leaf;
3573 struct btrfs_key key;
3574 int ret;
3575
3576 path = btrfs_alloc_path();
3577 if (!path)
3578 return -ENOMEM;
3579
Ilya Dryomov68310a52012-06-22 12:24:12 -06003580 key.objectid = BTRFS_BALANCE_OBJECTID;
3581 key.type = BTRFS_BALANCE_ITEM_KEY;
3582 key.offset = 0;
3583
3584 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3585 if (ret < 0)
3586 goto out;
3587 if (ret > 0) { /* ret = -ENOENT; */
3588 ret = 0;
3589 goto out;
3590 }
3591
Ilya Dryomov59641012012-01-16 22:04:48 +02003592 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3593 if (!bctl) {
3594 ret = -ENOMEM;
3595 goto out;
3596 }
3597
Ilya Dryomov59641012012-01-16 22:04:48 +02003598 leaf = path->nodes[0];
3599 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3600
Ilya Dryomov68310a52012-06-22 12:24:12 -06003601 bctl->fs_info = fs_info;
3602 bctl->flags = btrfs_balance_flags(leaf, item);
3603 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003604
3605 btrfs_balance_data(leaf, item, &disk_bargs);
3606 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3607 btrfs_balance_meta(leaf, item, &disk_bargs);
3608 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3609 btrfs_balance_sys(leaf, item, &disk_bargs);
3610 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3611
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003612 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3613
Ilya Dryomov68310a52012-06-22 12:24:12 -06003614 mutex_lock(&fs_info->volume_mutex);
3615 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003616
Ilya Dryomov68310a52012-06-22 12:24:12 -06003617 set_balance_control(bctl);
3618
3619 mutex_unlock(&fs_info->balance_mutex);
3620 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003621out:
3622 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003623 return ret;
3624}
3625
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003626int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3627{
3628 int ret = 0;
3629
3630 mutex_lock(&fs_info->balance_mutex);
3631 if (!fs_info->balance_ctl) {
3632 mutex_unlock(&fs_info->balance_mutex);
3633 return -ENOTCONN;
3634 }
3635
3636 if (atomic_read(&fs_info->balance_running)) {
3637 atomic_inc(&fs_info->balance_pause_req);
3638 mutex_unlock(&fs_info->balance_mutex);
3639
3640 wait_event(fs_info->balance_wait_q,
3641 atomic_read(&fs_info->balance_running) == 0);
3642
3643 mutex_lock(&fs_info->balance_mutex);
3644 /* we are good with balance_ctl ripped off from under us */
3645 BUG_ON(atomic_read(&fs_info->balance_running));
3646 atomic_dec(&fs_info->balance_pause_req);
3647 } else {
3648 ret = -ENOTCONN;
3649 }
3650
3651 mutex_unlock(&fs_info->balance_mutex);
3652 return ret;
3653}
3654
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003655int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3656{
Ilya Dryomove649e582013-10-10 20:40:21 +03003657 if (fs_info->sb->s_flags & MS_RDONLY)
3658 return -EROFS;
3659
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003660 mutex_lock(&fs_info->balance_mutex);
3661 if (!fs_info->balance_ctl) {
3662 mutex_unlock(&fs_info->balance_mutex);
3663 return -ENOTCONN;
3664 }
3665
3666 atomic_inc(&fs_info->balance_cancel_req);
3667 /*
3668 * if we are running just wait and return, balance item is
3669 * deleted in btrfs_balance in this case
3670 */
3671 if (atomic_read(&fs_info->balance_running)) {
3672 mutex_unlock(&fs_info->balance_mutex);
3673 wait_event(fs_info->balance_wait_q,
3674 atomic_read(&fs_info->balance_running) == 0);
3675 mutex_lock(&fs_info->balance_mutex);
3676 } else {
3677 /* __cancel_balance needs volume_mutex */
3678 mutex_unlock(&fs_info->balance_mutex);
3679 mutex_lock(&fs_info->volume_mutex);
3680 mutex_lock(&fs_info->balance_mutex);
3681
3682 if (fs_info->balance_ctl)
3683 __cancel_balance(fs_info);
3684
3685 mutex_unlock(&fs_info->volume_mutex);
3686 }
3687
3688 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3689 atomic_dec(&fs_info->balance_cancel_req);
3690 mutex_unlock(&fs_info->balance_mutex);
3691 return 0;
3692}
3693
Stefan Behrens803b2f52013-08-15 17:11:21 +02003694static int btrfs_uuid_scan_kthread(void *data)
3695{
3696 struct btrfs_fs_info *fs_info = data;
3697 struct btrfs_root *root = fs_info->tree_root;
3698 struct btrfs_key key;
3699 struct btrfs_key max_key;
3700 struct btrfs_path *path = NULL;
3701 int ret = 0;
3702 struct extent_buffer *eb;
3703 int slot;
3704 struct btrfs_root_item root_item;
3705 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003706 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003707
3708 path = btrfs_alloc_path();
3709 if (!path) {
3710 ret = -ENOMEM;
3711 goto out;
3712 }
3713
3714 key.objectid = 0;
3715 key.type = BTRFS_ROOT_ITEM_KEY;
3716 key.offset = 0;
3717
3718 max_key.objectid = (u64)-1;
3719 max_key.type = BTRFS_ROOT_ITEM_KEY;
3720 max_key.offset = (u64)-1;
3721
Stefan Behrens803b2f52013-08-15 17:11:21 +02003722 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003723 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003724 if (ret) {
3725 if (ret > 0)
3726 ret = 0;
3727 break;
3728 }
3729
3730 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3731 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3732 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3733 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3734 goto skip;
3735
3736 eb = path->nodes[0];
3737 slot = path->slots[0];
3738 item_size = btrfs_item_size_nr(eb, slot);
3739 if (item_size < sizeof(root_item))
3740 goto skip;
3741
Stefan Behrens803b2f52013-08-15 17:11:21 +02003742 read_extent_buffer(eb, &root_item,
3743 btrfs_item_ptr_offset(eb, slot),
3744 (int)sizeof(root_item));
3745 if (btrfs_root_refs(&root_item) == 0)
3746 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003747
3748 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3749 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3750 if (trans)
3751 goto update_tree;
3752
3753 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003754 /*
3755 * 1 - subvol uuid item
3756 * 1 - received_subvol uuid item
3757 */
3758 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3759 if (IS_ERR(trans)) {
3760 ret = PTR_ERR(trans);
3761 break;
3762 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003763 continue;
3764 } else {
3765 goto skip;
3766 }
3767update_tree:
3768 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003769 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3770 root_item.uuid,
3771 BTRFS_UUID_KEY_SUBVOL,
3772 key.objectid);
3773 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003774 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003775 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003776 break;
3777 }
3778 }
3779
3780 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003781 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3782 root_item.received_uuid,
3783 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3784 key.objectid);
3785 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003786 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003787 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003788 break;
3789 }
3790 }
3791
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003792skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003793 if (trans) {
3794 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003795 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003796 if (ret)
3797 break;
3798 }
3799
Stefan Behrens803b2f52013-08-15 17:11:21 +02003800 btrfs_release_path(path);
3801 if (key.offset < (u64)-1) {
3802 key.offset++;
3803 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3804 key.offset = 0;
3805 key.type = BTRFS_ROOT_ITEM_KEY;
3806 } else if (key.objectid < (u64)-1) {
3807 key.offset = 0;
3808 key.type = BTRFS_ROOT_ITEM_KEY;
3809 key.objectid++;
3810 } else {
3811 break;
3812 }
3813 cond_resched();
3814 }
3815
3816out:
3817 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003818 if (trans && !IS_ERR(trans))
3819 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003820 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05003821 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003822 else
3823 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003824 up(&fs_info->uuid_tree_rescan_sem);
3825 return 0;
3826}
3827
Stefan Behrens70f80172013-08-15 17:11:23 +02003828/*
3829 * Callback for btrfs_uuid_tree_iterate().
3830 * returns:
3831 * 0 check succeeded, the entry is not outdated.
3832 * < 0 if an error occured.
3833 * > 0 if the check failed, which means the caller shall remove the entry.
3834 */
3835static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3836 u8 *uuid, u8 type, u64 subid)
3837{
3838 struct btrfs_key key;
3839 int ret = 0;
3840 struct btrfs_root *subvol_root;
3841
3842 if (type != BTRFS_UUID_KEY_SUBVOL &&
3843 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3844 goto out;
3845
3846 key.objectid = subid;
3847 key.type = BTRFS_ROOT_ITEM_KEY;
3848 key.offset = (u64)-1;
3849 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3850 if (IS_ERR(subvol_root)) {
3851 ret = PTR_ERR(subvol_root);
3852 if (ret == -ENOENT)
3853 ret = 1;
3854 goto out;
3855 }
3856
3857 switch (type) {
3858 case BTRFS_UUID_KEY_SUBVOL:
3859 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3860 ret = 1;
3861 break;
3862 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3863 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3864 BTRFS_UUID_SIZE))
3865 ret = 1;
3866 break;
3867 }
3868
3869out:
3870 return ret;
3871}
3872
3873static int btrfs_uuid_rescan_kthread(void *data)
3874{
3875 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3876 int ret;
3877
3878 /*
3879 * 1st step is to iterate through the existing UUID tree and
3880 * to delete all entries that contain outdated data.
3881 * 2nd step is to add all missing entries to the UUID tree.
3882 */
3883 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3884 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003885 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003886 up(&fs_info->uuid_tree_rescan_sem);
3887 return ret;
3888 }
3889 return btrfs_uuid_scan_kthread(data);
3890}
3891
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003892int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3893{
3894 struct btrfs_trans_handle *trans;
3895 struct btrfs_root *tree_root = fs_info->tree_root;
3896 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003897 struct task_struct *task;
3898 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003899
3900 /*
3901 * 1 - root node
3902 * 1 - root item
3903 */
3904 trans = btrfs_start_transaction(tree_root, 2);
3905 if (IS_ERR(trans))
3906 return PTR_ERR(trans);
3907
3908 uuid_root = btrfs_create_tree(trans, fs_info,
3909 BTRFS_UUID_TREE_OBJECTID);
3910 if (IS_ERR(uuid_root)) {
3911 btrfs_abort_transaction(trans, tree_root,
3912 PTR_ERR(uuid_root));
3913 return PTR_ERR(uuid_root);
3914 }
3915
3916 fs_info->uuid_root = uuid_root;
3917
Stefan Behrens803b2f52013-08-15 17:11:21 +02003918 ret = btrfs_commit_transaction(trans, tree_root);
3919 if (ret)
3920 return ret;
3921
3922 down(&fs_info->uuid_tree_rescan_sem);
3923 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3924 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02003925 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003926 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02003927 up(&fs_info->uuid_tree_rescan_sem);
3928 return PTR_ERR(task);
3929 }
3930
3931 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003932}
Stefan Behrens803b2f52013-08-15 17:11:21 +02003933
Stefan Behrens70f80172013-08-15 17:11:23 +02003934int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3935{
3936 struct task_struct *task;
3937
3938 down(&fs_info->uuid_tree_rescan_sem);
3939 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3940 if (IS_ERR(task)) {
3941 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003942 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02003943 up(&fs_info->uuid_tree_rescan_sem);
3944 return PTR_ERR(task);
3945 }
3946
3947 return 0;
3948}
3949
Chris Mason8f18cf12008-04-25 16:53:30 -04003950/*
3951 * shrinking a device means finding all of the device extents past
3952 * the new size, and then following the back refs to the chunks.
3953 * The chunk relocation code actually frees the device extent
3954 */
3955int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3956{
3957 struct btrfs_trans_handle *trans;
3958 struct btrfs_root *root = device->dev_root;
3959 struct btrfs_dev_extent *dev_extent = NULL;
3960 struct btrfs_path *path;
3961 u64 length;
3962 u64 chunk_tree;
3963 u64 chunk_objectid;
3964 u64 chunk_offset;
3965 int ret;
3966 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003967 int failed = 0;
3968 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003969 struct extent_buffer *l;
3970 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003971 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003972 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08003973 u64 old_size = btrfs_device_get_total_bytes(device);
3974 u64 diff = old_size - new_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04003975
Stefan Behrens63a212a2012-11-05 18:29:28 +01003976 if (device->is_tgtdev_for_dev_replace)
3977 return -EINVAL;
3978
Chris Mason8f18cf12008-04-25 16:53:30 -04003979 path = btrfs_alloc_path();
3980 if (!path)
3981 return -ENOMEM;
3982
Chris Mason8f18cf12008-04-25 16:53:30 -04003983 path->reada = 2;
3984
Chris Mason7d9eb122008-07-08 14:19:17 -04003985 lock_chunks(root);
3986
Miao Xie7cc8e582014-09-03 21:35:38 +08003987 btrfs_device_set_total_bytes(device, new_size);
Josef Bacik2bf64752011-09-26 17:12:22 -04003988 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003989 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003990 spin_lock(&root->fs_info->free_chunk_lock);
3991 root->fs_info->free_chunk_space -= diff;
3992 spin_unlock(&root->fs_info->free_chunk_lock);
3993 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003994 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003995
Josef Bacikba1bf482009-09-11 16:11:19 -04003996again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003997 key.objectid = device->devid;
3998 key.offset = (u64)-1;
3999 key.type = BTRFS_DEV_EXTENT_KEY;
4000
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004001 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04004002 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4003 if (ret < 0)
4004 goto done;
4005
4006 ret = btrfs_previous_item(root, path, 0, key.type);
4007 if (ret < 0)
4008 goto done;
4009 if (ret) {
4010 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004011 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004012 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004013 }
4014
4015 l = path->nodes[0];
4016 slot = path->slots[0];
4017 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4018
Josef Bacikba1bf482009-09-11 16:11:19 -04004019 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004020 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004021 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004022 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004023
4024 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4025 length = btrfs_dev_extent_length(l, dev_extent);
4026
Josef Bacikba1bf482009-09-11 16:11:19 -04004027 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004028 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004029 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004030 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004031
4032 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
4033 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
4034 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004035 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004036
4037 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
4038 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04004039 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004040 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004041 if (ret == -ENOSPC)
4042 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004043 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004044
4045 if (failed && !retried) {
4046 failed = 0;
4047 retried = true;
4048 goto again;
4049 } else if (failed && retried) {
4050 ret = -ENOSPC;
4051 lock_chunks(root);
4052
Miao Xie7cc8e582014-09-03 21:35:38 +08004053 btrfs_device_set_total_bytes(device, old_size);
Josef Bacikba1bf482009-09-11 16:11:19 -04004054 if (device->writeable)
4055 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04004056 spin_lock(&root->fs_info->free_chunk_lock);
4057 root->fs_info->free_chunk_space += diff;
4058 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04004059 unlock_chunks(root);
4060 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004061 }
4062
Chris Balld6397ba2009-04-27 07:29:03 -04004063 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004064 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004065 if (IS_ERR(trans)) {
4066 ret = PTR_ERR(trans);
4067 goto done;
4068 }
4069
Chris Balld6397ba2009-04-27 07:29:03 -04004070 lock_chunks(root);
Miao Xie7cc8e582014-09-03 21:35:38 +08004071 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004072 if (list_empty(&device->resized_list))
4073 list_add_tail(&device->resized_list,
4074 &root->fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004075
Chris Balld6397ba2009-04-27 07:29:03 -04004076 WARN_ON(diff > old_total);
4077 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4078 unlock_chunks(root);
Miao Xie2196d6e2014-09-03 21:35:41 +08004079
4080 /* Now btrfs_update_device() will change the on-disk size. */
4081 ret = btrfs_update_device(trans, device);
Chris Balld6397ba2009-04-27 07:29:03 -04004082 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004083done:
4084 btrfs_free_path(path);
4085 return ret;
4086}
4087
Li Zefan125ccb02011-12-08 15:07:24 +08004088static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004089 struct btrfs_key *key,
4090 struct btrfs_chunk *chunk, int item_size)
4091{
David Sterba6c417612011-04-13 15:41:04 +02004092 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004093 struct btrfs_disk_key disk_key;
4094 u32 array_size;
4095 u8 *ptr;
4096
Miao Xiefe48a5c2014-09-03 21:35:39 +08004097 lock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004098 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004099 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004100 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4101 unlock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004102 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004103 }
Chris Mason0b86a832008-03-24 15:01:56 -04004104
4105 ptr = super_copy->sys_chunk_array + array_size;
4106 btrfs_cpu_key_to_disk(&disk_key, key);
4107 memcpy(ptr, &disk_key, sizeof(disk_key));
4108 ptr += sizeof(disk_key);
4109 memcpy(ptr, chunk, item_size);
4110 item_size += sizeof(disk_key);
4111 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004112 unlock_chunks(root);
4113
Chris Mason0b86a832008-03-24 15:01:56 -04004114 return 0;
4115}
4116
Miao Xieb2117a32011-01-05 10:07:28 +00004117/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004118 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004119 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004120static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004121{
Arne Jansen73c5de02011-04-12 12:07:57 +02004122 const struct btrfs_device_info *di_a = a;
4123 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004124
Arne Jansen73c5de02011-04-12 12:07:57 +02004125 if (di_a->max_avail > di_b->max_avail)
4126 return -1;
4127 if (di_a->max_avail < di_b->max_avail)
4128 return 1;
4129 if (di_a->total_avail > di_b->total_avail)
4130 return -1;
4131 if (di_a->total_avail < di_b->total_avail)
4132 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004133 return 0;
4134}
4135
Eric Sandeen48a3b632013-04-25 20:41:01 +00004136static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00004137 [BTRFS_RAID_RAID10] = {
4138 .sub_stripes = 2,
4139 .dev_stripes = 1,
4140 .devs_max = 0, /* 0 == as many as possible */
4141 .devs_min = 4,
4142 .devs_increment = 2,
4143 .ncopies = 2,
4144 },
4145 [BTRFS_RAID_RAID1] = {
4146 .sub_stripes = 1,
4147 .dev_stripes = 1,
4148 .devs_max = 2,
4149 .devs_min = 2,
4150 .devs_increment = 2,
4151 .ncopies = 2,
4152 },
4153 [BTRFS_RAID_DUP] = {
4154 .sub_stripes = 1,
4155 .dev_stripes = 2,
4156 .devs_max = 1,
4157 .devs_min = 1,
4158 .devs_increment = 1,
4159 .ncopies = 2,
4160 },
4161 [BTRFS_RAID_RAID0] = {
4162 .sub_stripes = 1,
4163 .dev_stripes = 1,
4164 .devs_max = 0,
4165 .devs_min = 2,
4166 .devs_increment = 1,
4167 .ncopies = 1,
4168 },
4169 [BTRFS_RAID_SINGLE] = {
4170 .sub_stripes = 1,
4171 .dev_stripes = 1,
4172 .devs_max = 1,
4173 .devs_min = 1,
4174 .devs_increment = 1,
4175 .ncopies = 1,
4176 },
Chris Masone942f882013-02-20 14:06:05 -05004177 [BTRFS_RAID_RAID5] = {
4178 .sub_stripes = 1,
4179 .dev_stripes = 1,
4180 .devs_max = 0,
4181 .devs_min = 2,
4182 .devs_increment = 1,
4183 .ncopies = 2,
4184 },
4185 [BTRFS_RAID_RAID6] = {
4186 .sub_stripes = 1,
4187 .dev_stripes = 1,
4188 .devs_max = 0,
4189 .devs_min = 3,
4190 .devs_increment = 1,
4191 .ncopies = 3,
4192 },
Liu Bo31e50222012-11-21 14:18:10 +00004193};
4194
David Woodhouse53b381b2013-01-29 18:40:14 -05004195static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4196{
4197 /* TODO allow them to set a preferred stripe size */
4198 return 64 * 1024;
4199}
4200
4201static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4202{
David Woodhouse53b381b2013-01-29 18:40:14 -05004203 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
4204 return;
4205
Miao Xieceda0862013-04-11 10:30:16 +00004206 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004207}
4208
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004209#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4210 - sizeof(struct btrfs_item) \
4211 - sizeof(struct btrfs_chunk)) \
4212 / sizeof(struct btrfs_stripe) + 1)
4213
4214#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4215 - 2 * sizeof(struct btrfs_disk_key) \
4216 - 2 * sizeof(struct btrfs_chunk)) \
4217 / sizeof(struct btrfs_stripe) + 1)
4218
Miao Xieb2117a32011-01-05 10:07:28 +00004219static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004220 struct btrfs_root *extent_root, u64 start,
4221 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004222{
4223 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004224 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4225 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004226 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004227 struct extent_map_tree *em_tree;
4228 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004229 struct btrfs_device_info *devices_info = NULL;
4230 u64 total_avail;
4231 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004232 int data_stripes; /* number of stripes that count for
4233 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004234 int sub_stripes; /* sub_stripes info for map */
4235 int dev_stripes; /* stripes per dev */
4236 int devs_max; /* max devs to use */
4237 int devs_min; /* min devs needed */
4238 int devs_increment; /* ndevs has to be a multiple of this */
4239 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004240 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004241 u64 max_stripe_size;
4242 u64 max_chunk_size;
4243 u64 stripe_size;
4244 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004245 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004246 int ndevs;
4247 int i;
4248 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004249 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004250
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004251 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004252
Miao Xieb2117a32011-01-05 10:07:28 +00004253 if (list_empty(&fs_devices->alloc_list))
4254 return -ENOSPC;
4255
Liu Bo31e50222012-11-21 14:18:10 +00004256 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004257
Liu Bo31e50222012-11-21 14:18:10 +00004258 sub_stripes = btrfs_raid_array[index].sub_stripes;
4259 dev_stripes = btrfs_raid_array[index].dev_stripes;
4260 devs_max = btrfs_raid_array[index].devs_max;
4261 devs_min = btrfs_raid_array[index].devs_min;
4262 devs_increment = btrfs_raid_array[index].devs_increment;
4263 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004264
4265 if (type & BTRFS_BLOCK_GROUP_DATA) {
4266 max_stripe_size = 1024 * 1024 * 1024;
4267 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004268 if (!devs_max)
4269 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004270 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004271 /* for larger filesystems, use larger metadata chunks */
4272 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4273 max_stripe_size = 1024 * 1024 * 1024;
4274 else
4275 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004276 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004277 if (!devs_max)
4278 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004279 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004280 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004281 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004282 if (!devs_max)
4283 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004284 } else {
David Sterba351fd352014-05-15 16:48:20 +02004285 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004286 type);
4287 BUG_ON(1);
4288 }
4289
4290 /* we don't want a chunk larger than 10% of writeable space */
4291 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4292 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004293
4294 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
4295 GFP_NOFS);
4296 if (!devices_info)
4297 return -ENOMEM;
4298
Arne Jansen73c5de02011-04-12 12:07:57 +02004299 cur = fs_devices->alloc_list.next;
4300
4301 /*
4302 * in the first pass through the devices list, we gather information
4303 * about the available holes on each device.
4304 */
4305 ndevs = 0;
4306 while (cur != &fs_devices->alloc_list) {
4307 struct btrfs_device *device;
4308 u64 max_avail;
4309 u64 dev_offset;
4310
4311 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4312
4313 cur = cur->next;
4314
4315 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004316 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004317 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004318 continue;
4319 }
4320
Stefan Behrens63a212a2012-11-05 18:29:28 +01004321 if (!device->in_fs_metadata ||
4322 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004323 continue;
4324
4325 if (device->total_bytes > device->bytes_used)
4326 total_avail = device->total_bytes - device->bytes_used;
4327 else
4328 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004329
4330 /* If there is no space on this device, skip it. */
4331 if (total_avail == 0)
4332 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004333
Josef Bacik6df9a952013-06-27 13:22:46 -04004334 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004335 max_stripe_size * dev_stripes,
4336 &dev_offset, &max_avail);
4337 if (ret && ret != -ENOSPC)
4338 goto error;
4339
4340 if (ret == 0)
4341 max_avail = max_stripe_size * dev_stripes;
4342
4343 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4344 continue;
4345
Eric Sandeen063d0062013-01-31 00:55:01 +00004346 if (ndevs == fs_devices->rw_devices) {
4347 WARN(1, "%s: found more than %llu devices\n",
4348 __func__, fs_devices->rw_devices);
4349 break;
4350 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004351 devices_info[ndevs].dev_offset = dev_offset;
4352 devices_info[ndevs].max_avail = max_avail;
4353 devices_info[ndevs].total_avail = total_avail;
4354 devices_info[ndevs].dev = device;
4355 ++ndevs;
4356 }
4357
4358 /*
4359 * now sort the devices by hole size / available space
4360 */
4361 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4362 btrfs_cmp_device_info, NULL);
4363
4364 /* round down to number of usable stripes */
4365 ndevs -= ndevs % devs_increment;
4366
4367 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4368 ret = -ENOSPC;
4369 goto error;
4370 }
4371
4372 if (devs_max && ndevs > devs_max)
4373 ndevs = devs_max;
4374 /*
4375 * the primary goal is to maximize the number of stripes, so use as many
4376 * devices as possible, even if the stripes are not maximum sized.
4377 */
4378 stripe_size = devices_info[ndevs-1].max_avail;
4379 num_stripes = ndevs * dev_stripes;
4380
David Woodhouse53b381b2013-01-29 18:40:14 -05004381 /*
4382 * this will have to be fixed for RAID1 and RAID10 over
4383 * more drives
4384 */
4385 data_stripes = num_stripes / ncopies;
4386
David Woodhouse53b381b2013-01-29 18:40:14 -05004387 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4388 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4389 btrfs_super_stripesize(info->super_copy));
4390 data_stripes = num_stripes - 1;
4391 }
4392 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4393 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4394 btrfs_super_stripesize(info->super_copy));
4395 data_stripes = num_stripes - 2;
4396 }
Chris Mason86db2572013-02-20 16:23:40 -05004397
4398 /*
4399 * Use the number of data stripes to figure out how big this chunk
4400 * is really going to be in terms of logical address space,
4401 * and compare that answer with the max chunk size
4402 */
4403 if (stripe_size * data_stripes > max_chunk_size) {
4404 u64 mask = (1ULL << 24) - 1;
4405 stripe_size = max_chunk_size;
4406 do_div(stripe_size, data_stripes);
4407
4408 /* bump the answer up to a 16MB boundary */
4409 stripe_size = (stripe_size + mask) & ~mask;
4410
4411 /* but don't go higher than the limits we found
4412 * while searching for free extents
4413 */
4414 if (stripe_size > devices_info[ndevs-1].max_avail)
4415 stripe_size = devices_info[ndevs-1].max_avail;
4416 }
4417
Arne Jansen73c5de02011-04-12 12:07:57 +02004418 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004419
4420 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05004421 do_div(stripe_size, raid_stripe_len);
4422 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004423
Miao Xieb2117a32011-01-05 10:07:28 +00004424 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4425 if (!map) {
4426 ret = -ENOMEM;
4427 goto error;
4428 }
4429 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004430
Arne Jansen73c5de02011-04-12 12:07:57 +02004431 for (i = 0; i < ndevs; ++i) {
4432 for (j = 0; j < dev_stripes; ++j) {
4433 int s = i * dev_stripes + j;
4434 map->stripes[s].dev = devices_info[i].dev;
4435 map->stripes[s].physical = devices_info[i].dev_offset +
4436 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004437 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004438 }
Chris Mason593060d2008-03-25 16:50:33 -04004439 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004440 map->stripe_len = raid_stripe_len;
4441 map->io_align = raid_stripe_len;
4442 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004443 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004444 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004445
David Woodhouse53b381b2013-01-29 18:40:14 -05004446 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004447
Arne Jansen73c5de02011-04-12 12:07:57 +02004448 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004449
David Sterba172ddd62011-04-21 00:48:27 +02004450 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004451 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004452 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004453 ret = -ENOMEM;
4454 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004455 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004456 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004457 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004458 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004459 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004460 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004461 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004462 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004463
Chris Mason0b86a832008-03-24 15:01:56 -04004464 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004465 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004466 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004467 if (!ret) {
4468 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4469 atomic_inc(&em->refs);
4470 }
Chris Mason890871b2009-09-02 16:24:52 -04004471 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004472 if (ret) {
4473 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004474 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004475 }
Yan Zheng2b820322008-11-17 21:11:30 -05004476
Josef Bacik04487482013-01-29 15:03:37 -05004477 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4478 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4479 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004480 if (ret)
4481 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004482
Miao Xie7cc8e582014-09-03 21:35:38 +08004483 for (i = 0; i < map->num_stripes; i++) {
4484 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4485 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4486 }
Miao Xie43530c42014-09-03 21:35:36 +08004487
Miao Xie1c116182014-09-03 21:35:37 +08004488 spin_lock(&extent_root->fs_info->free_chunk_lock);
4489 extent_root->fs_info->free_chunk_space -= (stripe_size *
4490 map->num_stripes);
4491 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4492
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004493 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004494 check_raid56_incompat_flag(extent_root->fs_info, type);
4495
Miao Xieb2117a32011-01-05 10:07:28 +00004496 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004497 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004498
Josef Bacik6df9a952013-06-27 13:22:46 -04004499error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004500 write_lock(&em_tree->lock);
4501 remove_extent_mapping(em_tree, em);
4502 write_unlock(&em_tree->lock);
4503
4504 /* One for our allocation */
4505 free_extent_map(em);
4506 /* One for the tree reference */
4507 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004508error:
Miao Xieb2117a32011-01-05 10:07:28 +00004509 kfree(devices_info);
4510 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004511}
4512
Josef Bacik6df9a952013-06-27 13:22:46 -04004513int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004514 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004515 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004516{
Yan Zheng2b820322008-11-17 21:11:30 -05004517 struct btrfs_key key;
4518 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4519 struct btrfs_device *device;
4520 struct btrfs_chunk *chunk;
4521 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004522 struct extent_map_tree *em_tree;
4523 struct extent_map *em;
4524 struct map_lookup *map;
4525 size_t item_size;
4526 u64 dev_offset;
4527 u64 stripe_size;
4528 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004529 int ret;
4530
Josef Bacik6df9a952013-06-27 13:22:46 -04004531 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4532 read_lock(&em_tree->lock);
4533 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4534 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004535
Josef Bacik6df9a952013-06-27 13:22:46 -04004536 if (!em) {
4537 btrfs_crit(extent_root->fs_info, "unable to find logical "
4538 "%Lu len %Lu", chunk_offset, chunk_size);
4539 return -EINVAL;
4540 }
4541
4542 if (em->start != chunk_offset || em->len != chunk_size) {
4543 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004544 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004545 chunk_size, em->start, em->len);
4546 free_extent_map(em);
4547 return -EINVAL;
4548 }
4549
4550 map = (struct map_lookup *)em->bdev;
4551 item_size = btrfs_chunk_item_size(map->num_stripes);
4552 stripe_size = em->orig_block_len;
4553
4554 chunk = kzalloc(item_size, GFP_NOFS);
4555 if (!chunk) {
4556 ret = -ENOMEM;
4557 goto out;
4558 }
4559
4560 for (i = 0; i < map->num_stripes; i++) {
4561 device = map->stripes[i].dev;
4562 dev_offset = map->stripes[i].physical;
4563
Yan Zheng2b820322008-11-17 21:11:30 -05004564 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004565 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004566 goto out;
4567 ret = btrfs_alloc_dev_extent(trans, device,
4568 chunk_root->root_key.objectid,
4569 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4570 chunk_offset, dev_offset,
4571 stripe_size);
4572 if (ret)
4573 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004574 }
4575
Yan Zheng2b820322008-11-17 21:11:30 -05004576 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004577 for (i = 0; i < map->num_stripes; i++) {
4578 device = map->stripes[i].dev;
4579 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004580
4581 btrfs_set_stack_stripe_devid(stripe, device->devid);
4582 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4583 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4584 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004585 }
4586
4587 btrfs_set_stack_chunk_length(chunk, chunk_size);
4588 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4589 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4590 btrfs_set_stack_chunk_type(chunk, map->type);
4591 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4592 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4593 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4594 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4595 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4596
4597 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4598 key.type = BTRFS_CHUNK_ITEM_KEY;
4599 key.offset = chunk_offset;
4600
4601 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004602 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4603 /*
4604 * TODO: Cleanup of inserted chunk root in case of
4605 * failure.
4606 */
Li Zefan125ccb02011-12-08 15:07:24 +08004607 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004608 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004609 }
liubo1abe9b82011-03-24 11:18:59 +00004610
Josef Bacik6df9a952013-06-27 13:22:46 -04004611out:
Yan Zheng2b820322008-11-17 21:11:30 -05004612 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004613 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004614 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004615}
4616
4617/*
4618 * Chunk allocation falls into two parts. The first part does works
4619 * that make the new allocated chunk useable, but not do any operation
4620 * that modifies the chunk tree. The second part does the works that
4621 * require modifying the chunk tree. This division is important for the
4622 * bootstrap process of adding storage to a seed btrfs.
4623 */
4624int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4625 struct btrfs_root *extent_root, u64 type)
4626{
4627 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004628
Josef Bacik6df9a952013-06-27 13:22:46 -04004629 chunk_offset = find_next_chunk(extent_root->fs_info);
4630 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004631}
4632
Chris Masond3977122009-01-05 21:25:51 -05004633static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004634 struct btrfs_root *root,
4635 struct btrfs_device *device)
4636{
4637 u64 chunk_offset;
4638 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004639 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004640 struct btrfs_fs_info *fs_info = root->fs_info;
4641 struct btrfs_root *extent_root = fs_info->extent_root;
4642 int ret;
4643
Josef Bacik6df9a952013-06-27 13:22:46 -04004644 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004645 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004646 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4647 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004648 if (ret)
4649 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004650
Josef Bacik6df9a952013-06-27 13:22:46 -04004651 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004652 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004653 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4654 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004655 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004656}
4657
Miao Xied20983b2014-07-03 18:22:13 +08004658static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4659{
4660 int max_errors;
4661
4662 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4663 BTRFS_BLOCK_GROUP_RAID10 |
4664 BTRFS_BLOCK_GROUP_RAID5 |
4665 BTRFS_BLOCK_GROUP_DUP)) {
4666 max_errors = 1;
4667 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4668 max_errors = 2;
4669 } else {
4670 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004671 }
4672
Miao Xied20983b2014-07-03 18:22:13 +08004673 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05004674}
4675
4676int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4677{
4678 struct extent_map *em;
4679 struct map_lookup *map;
4680 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4681 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004682 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004683 int i;
4684
Chris Mason890871b2009-09-02 16:24:52 -04004685 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004686 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004687 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004688 if (!em)
4689 return 1;
4690
4691 map = (struct map_lookup *)em->bdev;
4692 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004693 if (map->stripes[i].dev->missing) {
4694 miss_ndevs++;
4695 continue;
4696 }
4697
Yan Zheng2b820322008-11-17 21:11:30 -05004698 if (!map->stripes[i].dev->writeable) {
4699 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004700 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004701 }
4702 }
Miao Xied20983b2014-07-03 18:22:13 +08004703
4704 /*
4705 * If the number of missing devices is larger than max errors,
4706 * we can not write the data into that chunk successfully, so
4707 * set it readonly.
4708 */
4709 if (miss_ndevs > btrfs_chunk_max_errors(map))
4710 readonly = 1;
4711end:
Yan Zheng2b820322008-11-17 21:11:30 -05004712 free_extent_map(em);
4713 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004714}
4715
4716void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4717{
David Sterbaa8067e02011-04-21 00:34:43 +02004718 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004719}
4720
4721void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4722{
4723 struct extent_map *em;
4724
Chris Masond3977122009-01-05 21:25:51 -05004725 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004726 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004727 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4728 if (em)
4729 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004730 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004731 if (!em)
4732 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004733 /* once for us */
4734 free_extent_map(em);
4735 /* once for the tree */
4736 free_extent_map(em);
4737 }
4738}
4739
Stefan Behrens5d964052012-11-05 14:59:07 +01004740int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004741{
Stefan Behrens5d964052012-11-05 14:59:07 +01004742 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004743 struct extent_map *em;
4744 struct map_lookup *map;
4745 struct extent_map_tree *em_tree = &map_tree->map_tree;
4746 int ret;
4747
Chris Mason890871b2009-09-02 16:24:52 -04004748 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004749 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004750 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004751
Josef Bacikfb7669b2013-04-23 10:53:18 -04004752 /*
4753 * We could return errors for these cases, but that could get ugly and
4754 * we'd probably do the same thing which is just not do anything else
4755 * and exit, so return 1 so the callers don't try to use other copies.
4756 */
4757 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004758 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004759 logical+len);
4760 return 1;
4761 }
4762
4763 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004764 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004765 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004766 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004767 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004768 return 1;
4769 }
4770
Chris Masonf1885912008-04-09 16:28:12 -04004771 map = (struct map_lookup *)em->bdev;
4772 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4773 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004774 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4775 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004776 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4777 ret = 2;
4778 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4779 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004780 else
4781 ret = 1;
4782 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004783
4784 btrfs_dev_replace_lock(&fs_info->dev_replace);
4785 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4786 ret++;
4787 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4788
Chris Masonf1885912008-04-09 16:28:12 -04004789 return ret;
4790}
4791
David Woodhouse53b381b2013-01-29 18:40:14 -05004792unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4793 struct btrfs_mapping_tree *map_tree,
4794 u64 logical)
4795{
4796 struct extent_map *em;
4797 struct map_lookup *map;
4798 struct extent_map_tree *em_tree = &map_tree->map_tree;
4799 unsigned long len = root->sectorsize;
4800
4801 read_lock(&em_tree->lock);
4802 em = lookup_extent_mapping(em_tree, logical, len);
4803 read_unlock(&em_tree->lock);
4804 BUG_ON(!em);
4805
4806 BUG_ON(em->start > logical || em->start + em->len < logical);
4807 map = (struct map_lookup *)em->bdev;
4808 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4809 BTRFS_BLOCK_GROUP_RAID6)) {
4810 len = map->stripe_len * nr_data_stripes(map);
4811 }
4812 free_extent_map(em);
4813 return len;
4814}
4815
4816int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4817 u64 logical, u64 len, int mirror_num)
4818{
4819 struct extent_map *em;
4820 struct map_lookup *map;
4821 struct extent_map_tree *em_tree = &map_tree->map_tree;
4822 int ret = 0;
4823
4824 read_lock(&em_tree->lock);
4825 em = lookup_extent_mapping(em_tree, logical, len);
4826 read_unlock(&em_tree->lock);
4827 BUG_ON(!em);
4828
4829 BUG_ON(em->start > logical || em->start + em->len < logical);
4830 map = (struct map_lookup *)em->bdev;
4831 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4832 BTRFS_BLOCK_GROUP_RAID6))
4833 ret = 1;
4834 free_extent_map(em);
4835 return ret;
4836}
4837
Stefan Behrens30d98612012-11-06 14:52:18 +01004838static int find_live_mirror(struct btrfs_fs_info *fs_info,
4839 struct map_lookup *map, int first, int num,
4840 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004841{
4842 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004843 int tolerance;
4844 struct btrfs_device *srcdev;
4845
4846 if (dev_replace_is_ongoing &&
4847 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4848 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4849 srcdev = fs_info->dev_replace.srcdev;
4850 else
4851 srcdev = NULL;
4852
4853 /*
4854 * try to avoid the drive that is the source drive for a
4855 * dev-replace procedure, only choose it if no other non-missing
4856 * mirror is available
4857 */
4858 for (tolerance = 0; tolerance < 2; tolerance++) {
4859 if (map->stripes[optimal].dev->bdev &&
4860 (tolerance || map->stripes[optimal].dev != srcdev))
4861 return optimal;
4862 for (i = first; i < first + num; i++) {
4863 if (map->stripes[i].dev->bdev &&
4864 (tolerance || map->stripes[i].dev != srcdev))
4865 return i;
4866 }
Chris Masondfe25022008-05-13 13:46:40 -04004867 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004868
Chris Masondfe25022008-05-13 13:46:40 -04004869 /* we couldn't find one that doesn't fail. Just return something
4870 * and the io error handling code will clean up eventually
4871 */
4872 return optimal;
4873}
4874
David Woodhouse53b381b2013-01-29 18:40:14 -05004875static inline int parity_smaller(u64 a, u64 b)
4876{
4877 return a > b;
4878}
4879
4880/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4881static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4882{
4883 struct btrfs_bio_stripe s;
4884 int i;
4885 u64 l;
4886 int again = 1;
4887
4888 while (again) {
4889 again = 0;
4890 for (i = 0; i < bbio->num_stripes - 1; i++) {
4891 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4892 s = bbio->stripes[i];
4893 l = raid_map[i];
4894 bbio->stripes[i] = bbio->stripes[i+1];
4895 raid_map[i] = raid_map[i+1];
4896 bbio->stripes[i+1] = s;
4897 raid_map[i+1] = l;
4898 again = 1;
4899 }
4900 }
4901 }
4902}
4903
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004904static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004905 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004906 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004907 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004908{
4909 struct extent_map *em;
4910 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004911 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004912 struct extent_map_tree *em_tree = &map_tree->map_tree;
4913 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004914 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004915 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004916 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004917 u64 stripe_nr_orig;
4918 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004919 u64 stripe_len;
4920 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004921 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004922 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004923 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004924 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004925 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004926 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004927 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4928 int dev_replace_is_ongoing = 0;
4929 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004930 int patch_the_first_stripe_for_dev_replace = 0;
4931 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004932 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004933
Chris Mason890871b2009-09-02 16:24:52 -04004934 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004935 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004936 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004937
Chris Mason3b951512008-04-17 11:29:12 -04004938 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004939 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004940 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004941 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004942 }
Chris Mason0b86a832008-03-24 15:01:56 -04004943
Josef Bacik9bb91872013-04-19 23:45:33 -04004944 if (em->start > logical || em->start + em->len < logical) {
4945 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02004946 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04004947 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004948 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04004949 return -EINVAL;
4950 }
4951
Chris Mason0b86a832008-03-24 15:01:56 -04004952 map = (struct map_lookup *)em->bdev;
4953 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004954
David Woodhouse53b381b2013-01-29 18:40:14 -05004955 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004956 stripe_nr = offset;
4957 /*
4958 * stripe_nr counts the total number of stripes we have to stride
4959 * to get to this block
4960 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004961 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004962
David Woodhouse53b381b2013-01-29 18:40:14 -05004963 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004964 BUG_ON(offset < stripe_offset);
4965
4966 /* stripe_offset is the offset of this block in its stripe*/
4967 stripe_offset = offset - stripe_offset;
4968
David Woodhouse53b381b2013-01-29 18:40:14 -05004969 /* if we're here for raid56, we need to know the stripe aligned start */
4970 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4971 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4972 raid56_full_stripe_start = offset;
4973
4974 /* allow a write of a full stripe, but make sure we don't
4975 * allow straddling of stripes
4976 */
4977 do_div(raid56_full_stripe_start, full_stripe_len);
4978 raid56_full_stripe_start *= full_stripe_len;
4979 }
4980
4981 if (rw & REQ_DISCARD) {
4982 /* we don't discard raid56 yet */
4983 if (map->type &
4984 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4985 ret = -EOPNOTSUPP;
4986 goto out;
4987 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004988 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004989 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4990 u64 max_len;
4991 /* For writes to RAID[56], allow a full stripeset across all disks.
4992 For other RAID types and for RAID[56] reads, just allow a single
4993 stripe (on a single disk). */
4994 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4995 (rw & REQ_WRITE)) {
4996 max_len = stripe_len * nr_data_stripes(map) -
4997 (offset - raid56_full_stripe_start);
4998 } else {
4999 /* we limit the length of each bio to what fits in a stripe */
5000 max_len = stripe_len - stripe_offset;
5001 }
5002 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005003 } else {
5004 *length = em->len - offset;
5005 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005006
David Woodhouse53b381b2013-01-29 18:40:14 -05005007 /* This is for when we're called from btrfs_merge_bio_hook() and all
5008 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005009 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005010 goto out;
5011
Stefan Behrens472262f2012-11-06 14:43:46 +01005012 btrfs_dev_replace_lock(dev_replace);
5013 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5014 if (!dev_replace_is_ongoing)
5015 btrfs_dev_replace_unlock(dev_replace);
5016
Stefan Behrensad6d6202012-11-06 15:06:47 +01005017 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
5018 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
5019 dev_replace->tgtdev != NULL) {
5020 /*
5021 * in dev-replace case, for repair case (that's the only
5022 * case where the mirror is selected explicitly when
5023 * calling btrfs_map_block), blocks left of the left cursor
5024 * can also be read from the target drive.
5025 * For REQ_GET_READ_MIRRORS, the target drive is added as
5026 * the last one to the array of stripes. For READ, it also
5027 * needs to be supported using the same mirror number.
5028 * If the requested block is not left of the left cursor,
5029 * EIO is returned. This can happen because btrfs_num_copies()
5030 * returns one more in the dev-replace case.
5031 */
5032 u64 tmp_length = *length;
5033 struct btrfs_bio *tmp_bbio = NULL;
5034 int tmp_num_stripes;
5035 u64 srcdev_devid = dev_replace->srcdev->devid;
5036 int index_srcdev = 0;
5037 int found = 0;
5038 u64 physical_of_found = 0;
5039
5040 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05005041 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005042 if (ret) {
5043 WARN_ON(tmp_bbio != NULL);
5044 goto out;
5045 }
5046
5047 tmp_num_stripes = tmp_bbio->num_stripes;
5048 if (mirror_num > tmp_num_stripes) {
5049 /*
5050 * REQ_GET_READ_MIRRORS does not contain this
5051 * mirror, that means that the requested area
5052 * is not left of the left cursor
5053 */
5054 ret = -EIO;
5055 kfree(tmp_bbio);
5056 goto out;
5057 }
5058
5059 /*
5060 * process the rest of the function using the mirror_num
5061 * of the source drive. Therefore look it up first.
5062 * At the end, patch the device pointer to the one of the
5063 * target drive.
5064 */
5065 for (i = 0; i < tmp_num_stripes; i++) {
5066 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
5067 /*
5068 * In case of DUP, in order to keep it
5069 * simple, only add the mirror with the
5070 * lowest physical address
5071 */
5072 if (found &&
5073 physical_of_found <=
5074 tmp_bbio->stripes[i].physical)
5075 continue;
5076 index_srcdev = i;
5077 found = 1;
5078 physical_of_found =
5079 tmp_bbio->stripes[i].physical;
5080 }
5081 }
5082
5083 if (found) {
5084 mirror_num = index_srcdev + 1;
5085 patch_the_first_stripe_for_dev_replace = 1;
5086 physical_to_patch_in_first_stripe = physical_of_found;
5087 } else {
5088 WARN_ON(1);
5089 ret = -EIO;
5090 kfree(tmp_bbio);
5091 goto out;
5092 }
5093
5094 kfree(tmp_bbio);
5095 } else if (mirror_num > map->num_stripes) {
5096 mirror_num = 0;
5097 }
5098
Chris Masonf2d8d742008-04-21 10:03:05 -04005099 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005100 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005101 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005102 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005103 do_div(stripe_nr_end, map->stripe_len);
5104 stripe_end_offset = stripe_nr_end * map->stripe_len -
5105 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005106
Li Dongyangfce3bb92011-03-24 10:24:26 +00005107 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5108 if (rw & REQ_DISCARD)
5109 num_stripes = min_t(u64, map->num_stripes,
5110 stripe_nr_end - stripe_nr_orig);
5111 stripe_index = do_div(stripe_nr, map->num_stripes);
Miao Xie28e1cc72014-09-12 18:44:02 +08005112 if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)))
5113 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005114 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005115 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005116 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005117 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005118 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005119 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005120 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005121 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005122 current->pid % map->num_stripes,
5123 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005124 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005125 }
Chris Mason2fff7342008-04-29 14:12:09 -04005126
Chris Mason611f0e02008-04-03 16:29:03 -04005127 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005128 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005129 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005130 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005131 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005132 } else {
5133 mirror_num = 1;
5134 }
Chris Mason2fff7342008-04-29 14:12:09 -04005135
Chris Mason321aecc2008-04-16 10:49:51 -04005136 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5137 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005138
5139 stripe_index = do_div(stripe_nr, factor);
5140 stripe_index *= map->sub_stripes;
5141
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005142 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005143 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005144 else if (rw & REQ_DISCARD)
5145 num_stripes = min_t(u64, map->sub_stripes *
5146 (stripe_nr_end - stripe_nr_orig),
5147 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005148 else if (mirror_num)
5149 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005150 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005151 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005152 stripe_index = find_live_mirror(fs_info, map,
5153 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005154 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005155 current->pid % map->sub_stripes,
5156 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005157 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005158 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005159
5160 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5161 BTRFS_BLOCK_GROUP_RAID6)) {
5162 u64 tmp;
5163
5164 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
5165 && raid_map_ret) {
5166 int i, rot;
5167
5168 /* push stripe_nr back to the start of the full stripe */
5169 stripe_nr = raid56_full_stripe_start;
5170 do_div(stripe_nr, stripe_len);
5171
5172 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5173
5174 /* RAID[56] write or recovery. Return all stripes */
5175 num_stripes = map->num_stripes;
5176 max_errors = nr_parity_stripes(map);
5177
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05305178 raid_map = kmalloc_array(num_stripes, sizeof(u64),
David Woodhouse53b381b2013-01-29 18:40:14 -05005179 GFP_NOFS);
5180 if (!raid_map) {
5181 ret = -ENOMEM;
5182 goto out;
5183 }
5184
5185 /* Work out the disk rotation on this stripe-set */
5186 tmp = stripe_nr;
5187 rot = do_div(tmp, num_stripes);
5188
5189 /* Fill in the logical address of each stripe */
5190 tmp = stripe_nr * nr_data_stripes(map);
5191 for (i = 0; i < nr_data_stripes(map); i++)
5192 raid_map[(i+rot) % num_stripes] =
5193 em->start + (tmp + i) * map->stripe_len;
5194
5195 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5196 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5197 raid_map[(i+rot+1) % num_stripes] =
5198 RAID6_Q_STRIPE;
5199
5200 *length = map->stripe_len;
5201 stripe_index = 0;
5202 stripe_offset = 0;
5203 } else {
5204 /*
5205 * Mirror #0 or #1 means the original data block.
5206 * Mirror #2 is RAID5 parity block.
5207 * Mirror #3 is RAID6 Q block.
5208 */
5209 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5210 if (mirror_num > 1)
5211 stripe_index = nr_data_stripes(map) +
5212 mirror_num - 2;
5213
5214 /* We distribute the parity blocks across stripes */
5215 tmp = stripe_nr + stripe_index;
5216 stripe_index = do_div(tmp, map->num_stripes);
Miao Xie28e1cc72014-09-12 18:44:02 +08005217 if (!(rw & (REQ_WRITE | REQ_DISCARD |
5218 REQ_GET_READ_MIRRORS)) && mirror_num <= 1)
5219 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005220 }
Chris Mason8790d502008-04-03 16:29:03 -04005221 } else {
5222 /*
5223 * after this do_div call, stripe_nr is the number of stripes
5224 * on this device we have to walk to find the data, and
5225 * stripe_index is the number of our device in the stripe array
5226 */
5227 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005228 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005229 }
Chris Mason593060d2008-03-25 16:50:33 -04005230 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005231
Stefan Behrens472262f2012-11-06 14:43:46 +01005232 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005233 if (dev_replace_is_ongoing) {
5234 if (rw & (REQ_WRITE | REQ_DISCARD))
5235 num_alloc_stripes <<= 1;
5236 if (rw & REQ_GET_READ_MIRRORS)
5237 num_alloc_stripes++;
5238 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005239 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08005240 if (!bbio) {
Dave Joneseb2067f2013-07-30 13:42:17 -04005241 kfree(raid_map);
Li Zefande11cc12011-12-01 12:55:47 +08005242 ret = -ENOMEM;
5243 goto out;
5244 }
5245 atomic_set(&bbio->error, 0);
5246
Li Dongyangfce3bb92011-03-24 10:24:26 +00005247 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08005248 int factor = 0;
5249 int sub_stripes = 0;
5250 u64 stripes_per_dev = 0;
5251 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005252 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005253
5254 if (map->type &
5255 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5256 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5257 sub_stripes = 1;
5258 else
5259 sub_stripes = map->sub_stripes;
5260
5261 factor = map->num_stripes / sub_stripes;
5262 stripes_per_dev = div_u64_rem(stripe_nr_end -
5263 stripe_nr_orig,
5264 factor,
5265 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005266 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5267 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005268 }
5269
Li Dongyangfce3bb92011-03-24 10:24:26 +00005270 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005271 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005272 map->stripes[stripe_index].physical +
5273 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005274 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005275
Li Zefanec9ef7a2011-12-01 14:06:42 +08005276 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5277 BTRFS_BLOCK_GROUP_RAID10)) {
5278 bbio->stripes[i].length = stripes_per_dev *
5279 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005280
Li Zefanec9ef7a2011-12-01 14:06:42 +08005281 if (i / sub_stripes < remaining_stripes)
5282 bbio->stripes[i].length +=
5283 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005284
5285 /*
5286 * Special for the first stripe and
5287 * the last stripe:
5288 *
5289 * |-------|...|-------|
5290 * |----------|
5291 * off end_off
5292 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005293 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005294 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005295 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005296
5297 if (stripe_index >= last_stripe &&
5298 stripe_index <= (last_stripe +
5299 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005300 bbio->stripes[i].length -=
5301 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005302
Li Zefanec9ef7a2011-12-01 14:06:42 +08005303 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005304 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005305 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005306 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005307
5308 stripe_index++;
5309 if (stripe_index == map->num_stripes) {
5310 /* This could only happen for RAID0/10 */
5311 stripe_index = 0;
5312 stripe_nr++;
5313 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005314 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005315 } else {
5316 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005317 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005318 map->stripes[stripe_index].physical +
5319 stripe_offset +
5320 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005321 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005322 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005323 stripe_index++;
5324 }
Chris Mason593060d2008-03-25 16:50:33 -04005325 }
Li Zefande11cc12011-12-01 12:55:47 +08005326
Miao Xied20983b2014-07-03 18:22:13 +08005327 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5328 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005329
Stefan Behrens472262f2012-11-06 14:43:46 +01005330 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5331 dev_replace->tgtdev != NULL) {
5332 int index_where_to_add;
5333 u64 srcdev_devid = dev_replace->srcdev->devid;
5334
5335 /*
5336 * duplicate the write operations while the dev replace
5337 * procedure is running. Since the copying of the old disk
5338 * to the new disk takes place at run time while the
5339 * filesystem is mounted writable, the regular write
5340 * operations to the old disk have to be duplicated to go
5341 * to the new disk as well.
5342 * Note that device->missing is handled by the caller, and
5343 * that the write to the old disk is already set up in the
5344 * stripes array.
5345 */
5346 index_where_to_add = num_stripes;
5347 for (i = 0; i < num_stripes; i++) {
5348 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5349 /* write to new disk, too */
5350 struct btrfs_bio_stripe *new =
5351 bbio->stripes + index_where_to_add;
5352 struct btrfs_bio_stripe *old =
5353 bbio->stripes + i;
5354
5355 new->physical = old->physical;
5356 new->length = old->length;
5357 new->dev = dev_replace->tgtdev;
5358 index_where_to_add++;
5359 max_errors++;
5360 }
5361 }
5362 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005363 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5364 dev_replace->tgtdev != NULL) {
5365 u64 srcdev_devid = dev_replace->srcdev->devid;
5366 int index_srcdev = 0;
5367 int found = 0;
5368 u64 physical_of_found = 0;
5369
5370 /*
5371 * During the dev-replace procedure, the target drive can
5372 * also be used to read data in case it is needed to repair
5373 * a corrupt block elsewhere. This is possible if the
5374 * requested area is left of the left cursor. In this area,
5375 * the target drive is a full copy of the source drive.
5376 */
5377 for (i = 0; i < num_stripes; i++) {
5378 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5379 /*
5380 * In case of DUP, in order to keep it
5381 * simple, only add the mirror with the
5382 * lowest physical address
5383 */
5384 if (found &&
5385 physical_of_found <=
5386 bbio->stripes[i].physical)
5387 continue;
5388 index_srcdev = i;
5389 found = 1;
5390 physical_of_found = bbio->stripes[i].physical;
5391 }
5392 }
5393 if (found) {
5394 u64 length = map->stripe_len;
5395
5396 if (physical_of_found + length <=
5397 dev_replace->cursor_left) {
5398 struct btrfs_bio_stripe *tgtdev_stripe =
5399 bbio->stripes + num_stripes;
5400
5401 tgtdev_stripe->physical = physical_of_found;
5402 tgtdev_stripe->length =
5403 bbio->stripes[index_srcdev].length;
5404 tgtdev_stripe->dev = dev_replace->tgtdev;
5405
5406 num_stripes++;
5407 }
5408 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005409 }
5410
Li Zefande11cc12011-12-01 12:55:47 +08005411 *bbio_ret = bbio;
5412 bbio->num_stripes = num_stripes;
5413 bbio->max_errors = max_errors;
5414 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005415
5416 /*
5417 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5418 * mirror_num == num_stripes + 1 && dev_replace target drive is
5419 * available as a mirror
5420 */
5421 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5422 WARN_ON(num_stripes > 1);
5423 bbio->stripes[0].dev = dev_replace->tgtdev;
5424 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5425 bbio->mirror_num = map->num_stripes + 1;
5426 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005427 if (raid_map) {
5428 sort_parity_stripes(bbio, raid_map);
5429 *raid_map_ret = raid_map;
5430 }
Chris Masoncea9e442008-04-09 16:28:12 -04005431out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005432 if (dev_replace_is_ongoing)
5433 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005434 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005435 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005436}
5437
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005438int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005439 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005440 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005441{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005442 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05005443 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04005444}
5445
Yan Zhenga512bbf2008-12-08 16:46:26 -05005446int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5447 u64 chunk_start, u64 physical, u64 devid,
5448 u64 **logical, int *naddrs, int *stripe_len)
5449{
5450 struct extent_map_tree *em_tree = &map_tree->map_tree;
5451 struct extent_map *em;
5452 struct map_lookup *map;
5453 u64 *buf;
5454 u64 bytenr;
5455 u64 length;
5456 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005457 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005458 int i, j, nr = 0;
5459
Chris Mason890871b2009-09-02 16:24:52 -04005460 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005461 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005462 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005463
Josef Bacik835d9742013-03-19 12:13:25 -04005464 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005465 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005466 chunk_start);
5467 return -EIO;
5468 }
5469
5470 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005471 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005472 em->start, chunk_start);
5473 free_extent_map(em);
5474 return -EIO;
5475 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005476 map = (struct map_lookup *)em->bdev;
5477
5478 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005479 rmap_len = map->stripe_len;
5480
Yan Zhenga512bbf2008-12-08 16:46:26 -05005481 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5482 do_div(length, map->num_stripes / map->sub_stripes);
5483 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5484 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05005485 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5486 BTRFS_BLOCK_GROUP_RAID6)) {
5487 do_div(length, nr_data_stripes(map));
5488 rmap_len = map->stripe_len * nr_data_stripes(map);
5489 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005490
5491 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005492 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005493
5494 for (i = 0; i < map->num_stripes; i++) {
5495 if (devid && map->stripes[i].dev->devid != devid)
5496 continue;
5497 if (map->stripes[i].physical > physical ||
5498 map->stripes[i].physical + length <= physical)
5499 continue;
5500
5501 stripe_nr = physical - map->stripes[i].physical;
5502 do_div(stripe_nr, map->stripe_len);
5503
5504 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5505 stripe_nr = stripe_nr * map->num_stripes + i;
5506 do_div(stripe_nr, map->sub_stripes);
5507 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5508 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005509 } /* else if RAID[56], multiply by nr_data_stripes().
5510 * Alternatively, just use rmap_len below instead of
5511 * map->stripe_len */
5512
5513 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005514 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005515 for (j = 0; j < nr; j++) {
5516 if (buf[j] == bytenr)
5517 break;
5518 }
Chris Mason934d3752008-12-08 16:43:10 -05005519 if (j == nr) {
5520 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005521 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005522 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005523 }
5524
Yan Zhenga512bbf2008-12-08 16:46:26 -05005525 *logical = buf;
5526 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005527 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005528
5529 free_extent_map(em);
5530 return 0;
5531}
5532
Miao Xie8408c712014-06-19 10:42:55 +08005533static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5534{
5535 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5536 bio_endio_nodec(bio, err);
5537 else
5538 bio_endio(bio, err);
5539 kfree(bbio);
5540}
5541
Jan Schmidta1d3c472011-08-04 17:15:33 +02005542static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005543{
Chris Mason9be33952013-05-17 18:30:14 -04005544 struct btrfs_bio *bbio = bio->bi_private;
Miao Xiec404e0d2014-01-30 16:46:55 +08005545 struct btrfs_device *dev = bbio->stripes[0].dev;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005546 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005547
Stefan Behrens442a4f62012-05-25 16:06:08 +02005548 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005549 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005550 if (err == -EIO || err == -EREMOTEIO) {
5551 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005552 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005553
5554 BUG_ON(stripe_index >= bbio->num_stripes);
5555 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005556 if (dev->bdev) {
5557 if (bio->bi_rw & WRITE)
5558 btrfs_dev_stat_inc(dev,
5559 BTRFS_DEV_STAT_WRITE_ERRS);
5560 else
5561 btrfs_dev_stat_inc(dev,
5562 BTRFS_DEV_STAT_READ_ERRS);
5563 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5564 btrfs_dev_stat_inc(dev,
5565 BTRFS_DEV_STAT_FLUSH_ERRS);
5566 btrfs_dev_stat_print_on_error(dev);
5567 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005568 }
5569 }
Chris Mason8790d502008-04-03 16:29:03 -04005570
Jan Schmidta1d3c472011-08-04 17:15:33 +02005571 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005572 is_orig_bio = 1;
5573
Miao Xiec404e0d2014-01-30 16:46:55 +08005574 btrfs_bio_counter_dec(bbio->fs_info);
5575
Jan Schmidta1d3c472011-08-04 17:15:33 +02005576 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005577 if (!is_orig_bio) {
5578 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005579 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005580 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005581
Jan Schmidta1d3c472011-08-04 17:15:33 +02005582 bio->bi_private = bbio->private;
5583 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005584 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005585 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005586 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005587 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005588 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005589 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005590 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005591 /*
5592 * this bio is actually up to date, we didn't
5593 * go over the max number of errors
5594 */
5595 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005596 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005597 }
Miao Xiec55f1392014-06-19 10:42:54 +08005598
Miao Xie8408c712014-06-19 10:42:55 +08005599 btrfs_end_bbio(bbio, bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005600 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005601 bio_put(bio);
5602 }
Chris Mason8790d502008-04-03 16:29:03 -04005603}
5604
Chris Mason8b712842008-06-11 16:50:36 -04005605/*
5606 * see run_scheduled_bios for a description of why bios are collected for
5607 * async submit.
5608 *
5609 * This will add one bio to the pending list for a device and make sure
5610 * the work struct is scheduled.
5611 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005612static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5613 struct btrfs_device *device,
5614 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005615{
5616 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005617 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005618
David Woodhouse53b381b2013-01-29 18:40:14 -05005619 if (device->missing || !device->bdev) {
5620 bio_endio(bio, -EIO);
5621 return;
5622 }
5623
Chris Mason8b712842008-06-11 16:50:36 -04005624 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005625 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005626 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005627 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005628 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005629 return;
Chris Mason8b712842008-06-11 16:50:36 -04005630 }
5631
5632 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005633 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005634 * higher layers. Otherwise, the async bio makes it appear we have
5635 * made progress against dirty pages when we've really just put it
5636 * on a queue for later
5637 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005638 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005639 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005640 bio->bi_next = NULL;
5641 bio->bi_rw |= rw;
5642
5643 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005644 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005645 pending_bios = &device->pending_sync_bios;
5646 else
5647 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005648
Chris Masonffbd5172009-04-20 15:50:09 -04005649 if (pending_bios->tail)
5650 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005651
Chris Masonffbd5172009-04-20 15:50:09 -04005652 pending_bios->tail = bio;
5653 if (!pending_bios->head)
5654 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005655 if (device->running_pending)
5656 should_queue = 0;
5657
5658 spin_unlock(&device->io_lock);
5659
5660 if (should_queue)
Qu Wenruoa8c93d42014-02-28 10:46:08 +08005661 btrfs_queue_work(root->fs_info->submit_workers,
5662 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005663}
5664
Josef Bacikde1ee922012-10-19 16:50:56 -04005665static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5666 sector_t sector)
5667{
5668 struct bio_vec *prev;
5669 struct request_queue *q = bdev_get_queue(bdev);
Akinobu Mita475bf362013-11-18 22:13:18 +09005670 unsigned int max_sectors = queue_max_sectors(q);
Josef Bacikde1ee922012-10-19 16:50:56 -04005671 struct bvec_merge_data bvm = {
5672 .bi_bdev = bdev,
5673 .bi_sector = sector,
5674 .bi_rw = bio->bi_rw,
5675 };
5676
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305677 if (WARN_ON(bio->bi_vcnt == 0))
Josef Bacikde1ee922012-10-19 16:50:56 -04005678 return 1;
Josef Bacikde1ee922012-10-19 16:50:56 -04005679
5680 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005681 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005682 return 0;
5683
5684 if (!q->merge_bvec_fn)
5685 return 1;
5686
Kent Overstreet4f024f32013-10-11 15:44:27 -07005687 bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len;
Josef Bacikde1ee922012-10-19 16:50:56 -04005688 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5689 return 0;
5690 return 1;
5691}
5692
5693static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5694 struct bio *bio, u64 physical, int dev_nr,
5695 int rw, int async)
5696{
5697 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5698
5699 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005700 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005701 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005702 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005703#ifdef DEBUG
5704 {
5705 struct rcu_string *name;
5706
5707 rcu_read_lock();
5708 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005709 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005710 "(%s id %llu), size=%u\n", rw,
Fabian Frederick1b6e4462014-09-24 20:23:05 +02005711 (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev,
5712 name->str, dev->devid, bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04005713 rcu_read_unlock();
5714 }
5715#endif
5716 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005717
5718 btrfs_bio_counter_inc_noblocked(root->fs_info);
5719
Josef Bacikde1ee922012-10-19 16:50:56 -04005720 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005721 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005722 else
5723 btrfsic_submit_bio(rw, bio);
5724}
5725
5726static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5727 struct bio *first_bio, struct btrfs_device *dev,
5728 int dev_nr, int rw, int async)
5729{
5730 struct bio_vec *bvec = first_bio->bi_io_vec;
5731 struct bio *bio;
5732 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5733 u64 physical = bbio->stripes[dev_nr].physical;
5734
5735again:
5736 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5737 if (!bio)
5738 return -ENOMEM;
5739
5740 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5741 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5742 bvec->bv_offset) < bvec->bv_len) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07005743 u64 len = bio->bi_iter.bi_size;
Josef Bacikde1ee922012-10-19 16:50:56 -04005744
5745 atomic_inc(&bbio->stripes_pending);
5746 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5747 rw, async);
5748 physical += len;
5749 goto again;
5750 }
5751 bvec++;
5752 }
5753
5754 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5755 return 0;
5756}
5757
5758static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5759{
5760 atomic_inc(&bbio->error);
5761 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005762 /* Shoud be the original bio. */
5763 WARN_ON(bio != bbio->orig_bio);
5764
Josef Bacikde1ee922012-10-19 16:50:56 -04005765 bio->bi_private = bbio->private;
5766 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005767 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005768 bio->bi_iter.bi_sector = logical >> 9;
Miao Xie8408c712014-06-19 10:42:55 +08005769
5770 btrfs_end_bbio(bbio, bio, -EIO);
Josef Bacikde1ee922012-10-19 16:50:56 -04005771 }
5772}
5773
Chris Masonf1885912008-04-09 16:28:12 -04005774int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005775 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005776{
Chris Mason0b86a832008-03-24 15:01:56 -04005777 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005778 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005779 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005780 u64 length = 0;
5781 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005782 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005783 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005784 int dev_nr = 0;
5785 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005786 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005787
Kent Overstreet4f024f32013-10-11 15:44:27 -07005788 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005789 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005790
Miao Xiec404e0d2014-01-30 16:46:55 +08005791 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005792 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5793 mirror_num, &raid_map);
Miao Xiec404e0d2014-01-30 16:46:55 +08005794 if (ret) {
5795 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005796 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005797 }
Chris Masoncea9e442008-04-09 16:28:12 -04005798
Jan Schmidta1d3c472011-08-04 17:15:33 +02005799 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005800 bbio->orig_bio = first_bio;
5801 bbio->private = first_bio->bi_private;
5802 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005803 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005804 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5805
5806 if (raid_map) {
5807 /* In this case, map_length has been set to the length of
5808 a single stripe; not the whole write */
5809 if (rw & WRITE) {
Miao Xiec404e0d2014-01-30 16:46:55 +08005810 ret = raid56_parity_write(root, bio, bbio,
5811 raid_map, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005812 } else {
Miao Xiec404e0d2014-01-30 16:46:55 +08005813 ret = raid56_parity_recover(root, bio, bbio,
5814 raid_map, map_length,
5815 mirror_num);
David Woodhouse53b381b2013-01-29 18:40:14 -05005816 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005817 /*
5818 * FIXME, replace dosen't support raid56 yet, please fix
5819 * it in the future.
5820 */
5821 btrfs_bio_counter_dec(root->fs_info);
5822 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005823 }
5824
Chris Masoncea9e442008-04-09 16:28:12 -04005825 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005826 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005827 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005828 BUG();
5829 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005830
Chris Masond3977122009-01-05 21:25:51 -05005831 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005832 dev = bbio->stripes[dev_nr].dev;
5833 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5834 bbio_error(bbio, first_bio, logical);
5835 dev_nr++;
5836 continue;
5837 }
5838
5839 /*
5840 * Check and see if we're ok with this bio based on it's size
5841 * and offset with the given device.
5842 */
5843 if (!bio_size_ok(dev->bdev, first_bio,
5844 bbio->stripes[dev_nr].physical >> 9)) {
5845 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5846 dev_nr, rw, async_submit);
5847 BUG_ON(ret);
5848 dev_nr++;
5849 continue;
5850 }
5851
Jan Schmidta1d3c472011-08-04 17:15:33 +02005852 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005853 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005854 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005855 } else {
5856 bio = first_bio;
Miao Xiec55f1392014-06-19 10:42:54 +08005857 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
Chris Mason8790d502008-04-03 16:29:03 -04005858 }
Josef Bacik606686e2012-06-04 14:03:51 -04005859
Josef Bacikde1ee922012-10-19 16:50:56 -04005860 submit_stripe_bio(root, bbio, bio,
5861 bbio->stripes[dev_nr].physical, dev_nr, rw,
5862 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005863 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005864 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005865 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04005866 return 0;
5867}
5868
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005869struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005870 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005871{
Yan Zheng2b820322008-11-17 21:11:30 -05005872 struct btrfs_device *device;
5873 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005874
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005875 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005876 while (cur_devices) {
5877 if (!fsid ||
5878 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5879 device = __find_device(&cur_devices->devices,
5880 devid, uuid);
5881 if (device)
5882 return device;
5883 }
5884 cur_devices = cur_devices->seed;
5885 }
5886 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005887}
5888
Chris Masondfe25022008-05-13 13:46:40 -04005889static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
Miao Xie5f375832014-09-03 21:35:46 +08005890 struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04005891 u64 devid, u8 *dev_uuid)
5892{
5893 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04005894
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005895 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5896 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005897 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005898
5899 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005900 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04005901 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005902
5903 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05005904 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005905
Chris Masondfe25022008-05-13 13:46:40 -04005906 return device;
5907}
5908
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005909/**
5910 * btrfs_alloc_device - allocate struct btrfs_device
5911 * @fs_info: used only for generating a new devid, can be NULL if
5912 * devid is provided (i.e. @devid != NULL).
5913 * @devid: a pointer to devid for this device. If NULL a new devid
5914 * is generated.
5915 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5916 * is generated.
5917 *
5918 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5919 * on error. Returned struct is not linked onto any lists and can be
5920 * destroyed with kfree() right away.
5921 */
5922struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5923 const u64 *devid,
5924 const u8 *uuid)
5925{
5926 struct btrfs_device *dev;
5927 u64 tmp;
5928
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305929 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005930 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005931
5932 dev = __alloc_device();
5933 if (IS_ERR(dev))
5934 return dev;
5935
5936 if (devid)
5937 tmp = *devid;
5938 else {
5939 int ret;
5940
5941 ret = find_next_devid(fs_info, &tmp);
5942 if (ret) {
5943 kfree(dev);
5944 return ERR_PTR(ret);
5945 }
5946 }
5947 dev->devid = tmp;
5948
5949 if (uuid)
5950 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5951 else
5952 generate_random_uuid(dev->uuid);
5953
Liu Bo9e0af232014-08-15 23:36:53 +08005954 btrfs_init_work(&dev->work, btrfs_submit_helper,
5955 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005956
5957 return dev;
5958}
5959
Chris Mason0b86a832008-03-24 15:01:56 -04005960static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5961 struct extent_buffer *leaf,
5962 struct btrfs_chunk *chunk)
5963{
5964 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5965 struct map_lookup *map;
5966 struct extent_map *em;
5967 u64 logical;
5968 u64 length;
5969 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005970 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005971 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005972 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005973 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005974
Chris Masone17cade2008-04-15 15:41:47 -04005975 logical = key->offset;
5976 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005977
Chris Mason890871b2009-09-02 16:24:52 -04005978 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005979 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005980 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005981
5982 /* already mapped? */
5983 if (em && em->start <= logical && em->start + em->len > logical) {
5984 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005985 return 0;
5986 } else if (em) {
5987 free_extent_map(em);
5988 }
Chris Mason0b86a832008-03-24 15:01:56 -04005989
David Sterba172ddd62011-04-21 00:48:27 +02005990 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005991 if (!em)
5992 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005993 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5994 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005995 if (!map) {
5996 free_extent_map(em);
5997 return -ENOMEM;
5998 }
5999
Wang Shilong298a8f92014-06-19 10:42:52 +08006000 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04006001 em->bdev = (struct block_device *)map;
6002 em->start = logical;
6003 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006004 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006005 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006006 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006007
Chris Mason593060d2008-03-25 16:50:33 -04006008 map->num_stripes = num_stripes;
6009 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6010 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6011 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
6012 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6013 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006014 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006015 for (i = 0; i < num_stripes; i++) {
6016 map->stripes[i].physical =
6017 btrfs_stripe_offset_nr(leaf, chunk, i);
6018 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006019 read_extent_buffer(leaf, uuid, (unsigned long)
6020 btrfs_stripe_dev_uuid_nr(chunk, i),
6021 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006022 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
6023 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04006024 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006025 free_extent_map(em);
6026 return -EIO;
6027 }
Chris Masondfe25022008-05-13 13:46:40 -04006028 if (!map->stripes[i].dev) {
6029 map->stripes[i].dev =
Miao Xie5f375832014-09-03 21:35:46 +08006030 add_missing_dev(root, root->fs_info->fs_devices,
6031 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006032 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04006033 free_extent_map(em);
6034 return -EIO;
6035 }
6036 }
6037 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04006038 }
6039
Chris Mason890871b2009-09-02 16:24:52 -04006040 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006041 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006042 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006043 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006044 free_extent_map(em);
6045
6046 return 0;
6047}
6048
Jeff Mahoney143bede2012-03-01 14:56:26 +01006049static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006050 struct btrfs_dev_item *dev_item,
6051 struct btrfs_device *device)
6052{
6053 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006054
6055 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006056 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6057 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006058 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006059 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006060 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006061 device->type = btrfs_device_type(leaf, dev_item);
6062 device->io_align = btrfs_device_io_align(leaf, dev_item);
6063 device->io_width = btrfs_device_io_width(leaf, dev_item);
6064 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006065 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006066 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006067
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006068 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006069 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006070}
6071
Miao Xie5f375832014-09-03 21:35:46 +08006072static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6073 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006074{
6075 struct btrfs_fs_devices *fs_devices;
6076 int ret;
6077
Li Zefanb367e472011-12-07 11:38:24 +08006078 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05006079
6080 fs_devices = root->fs_info->fs_devices->seed;
6081 while (fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006082 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE))
6083 return fs_devices;
6084
Yan Zheng2b820322008-11-17 21:11:30 -05006085 fs_devices = fs_devices->seed;
6086 }
6087
6088 fs_devices = find_fsid(fsid);
6089 if (!fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006090 if (!btrfs_test_opt(root, DEGRADED))
6091 return ERR_PTR(-ENOENT);
6092
6093 fs_devices = alloc_fs_devices(fsid);
6094 if (IS_ERR(fs_devices))
6095 return fs_devices;
6096
6097 fs_devices->seeding = 1;
6098 fs_devices->opened = 1;
6099 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006100 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006101
6102 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006103 if (IS_ERR(fs_devices))
6104 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006105
Christoph Hellwig97288f22008-12-02 06:36:09 -05006106 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006107 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006108 if (ret) {
6109 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006110 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006111 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006112 }
Yan Zheng2b820322008-11-17 21:11:30 -05006113
6114 if (!fs_devices->seeding) {
6115 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006116 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006117 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006118 goto out;
6119 }
6120
6121 fs_devices->seed = root->fs_info->fs_devices->seed;
6122 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006123out:
Miao Xie5f375832014-09-03 21:35:46 +08006124 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006125}
6126
Chris Mason0d81ba52008-03-24 15:02:07 -04006127static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006128 struct extent_buffer *leaf,
6129 struct btrfs_dev_item *dev_item)
6130{
Miao Xie5f375832014-09-03 21:35:46 +08006131 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006132 struct btrfs_device *device;
6133 u64 devid;
6134 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006135 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006136 u8 dev_uuid[BTRFS_UUID_SIZE];
6137
Chris Mason0b86a832008-03-24 15:01:56 -04006138 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006139 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006140 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006141 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006142 BTRFS_UUID_SIZE);
6143
6144 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
Miao Xie5f375832014-09-03 21:35:46 +08006145 fs_devices = open_seed_devices(root, fs_uuid);
6146 if (IS_ERR(fs_devices))
6147 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006148 }
6149
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006150 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006151 if (!device) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006152 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006153 return -EIO;
6154
Miao Xie5f375832014-09-03 21:35:46 +08006155 btrfs_warn(root->fs_info, "devid %llu missing", devid);
6156 device = add_missing_dev(root, fs_devices, devid, dev_uuid);
6157 if (!device)
6158 return -ENOMEM;
6159 } else {
6160 if (!device->bdev && !btrfs_test_opt(root, DEGRADED))
6161 return -EIO;
6162
6163 if(!device->bdev && !device->missing) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006164 /*
6165 * this happens when a device that was properly setup
6166 * in the device info lists suddenly goes bad.
6167 * device->bdev is NULL, and so we have to set
6168 * device->missing to one here
6169 */
Miao Xie5f375832014-09-03 21:35:46 +08006170 device->fs_devices->missing_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05006171 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006172 }
Miao Xie5f375832014-09-03 21:35:46 +08006173
6174 /* Move the device to its own fs_devices */
6175 if (device->fs_devices != fs_devices) {
6176 ASSERT(device->missing);
6177
6178 list_move(&device->dev_list, &fs_devices->devices);
6179 device->fs_devices->num_devices--;
6180 fs_devices->num_devices++;
6181
6182 device->fs_devices->missing_devices--;
6183 fs_devices->missing_devices++;
6184
6185 device->fs_devices = fs_devices;
6186 }
Yan Zheng2b820322008-11-17 21:11:30 -05006187 }
6188
6189 if (device->fs_devices != root->fs_info->fs_devices) {
6190 BUG_ON(device->writeable);
6191 if (device->generation !=
6192 btrfs_device_generation(leaf, dev_item))
6193 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006194 }
Chris Mason0b86a832008-03-24 15:01:56 -04006195
6196 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006197 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006198 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006199 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006200 spin_lock(&root->fs_info->free_chunk_lock);
6201 root->fs_info->free_chunk_space += device->total_bytes -
6202 device->bytes_used;
6203 spin_unlock(&root->fs_info->free_chunk_lock);
6204 }
Chris Mason0b86a832008-03-24 15:01:56 -04006205 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006206 return ret;
6207}
6208
Yan Zhenge4404d62008-12-12 10:03:26 -05006209int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006210{
David Sterba6c417612011-04-13 15:41:04 +02006211 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006212 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006213 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006214 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04006215 u8 *ptr;
6216 unsigned long sb_ptr;
6217 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006218 u32 num_stripes;
6219 u32 array_size;
6220 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006221 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04006222 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006223
Yan Zhenge4404d62008-12-12 10:03:26 -05006224 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04006225 BTRFS_SUPER_INFO_SIZE);
6226 if (!sb)
6227 return -ENOMEM;
6228 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006229 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006230 /*
6231 * The sb extent buffer is artifical and just used to read the system array.
6232 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6233 * pages up-to-date when the page is larger: extent does not cover the
6234 * whole page and consequently check_page_uptodate does not find all
6235 * the page's extents up-to-date (the hole beyond sb),
6236 * write_extent_buffer then triggers a WARN_ON.
6237 *
6238 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6239 * but sb spans only this function. Add an explicit SetPageUptodate call
6240 * to silence the warning eg. on PowerPC 64.
6241 */
6242 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006243 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006244
Chris Masona061fc82008-05-07 11:43:44 -04006245 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006246 array_size = btrfs_super_sys_array_size(super_copy);
6247
Chris Mason0b86a832008-03-24 15:01:56 -04006248 ptr = super_copy->sys_chunk_array;
6249 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
6250 cur = 0;
6251
6252 while (cur < array_size) {
6253 disk_key = (struct btrfs_disk_key *)ptr;
6254 btrfs_disk_key_to_cpu(&key, disk_key);
6255
Chris Masona061fc82008-05-07 11:43:44 -04006256 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006257 sb_ptr += len;
6258 cur += len;
6259
Chris Mason0d81ba52008-03-24 15:02:07 -04006260 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04006261 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04006262 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006263 if (ret)
6264 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006265 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6266 len = btrfs_chunk_item_size(num_stripes);
6267 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006268 ret = -EIO;
6269 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006270 }
6271 ptr += len;
6272 sb_ptr += len;
6273 cur += len;
6274 }
Chris Masona061fc82008-05-07 11:43:44 -04006275 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006276 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006277}
6278
6279int btrfs_read_chunk_tree(struct btrfs_root *root)
6280{
6281 struct btrfs_path *path;
6282 struct extent_buffer *leaf;
6283 struct btrfs_key key;
6284 struct btrfs_key found_key;
6285 int ret;
6286 int slot;
6287
6288 root = root->fs_info->chunk_root;
6289
6290 path = btrfs_alloc_path();
6291 if (!path)
6292 return -ENOMEM;
6293
Li Zefanb367e472011-12-07 11:38:24 +08006294 mutex_lock(&uuid_mutex);
6295 lock_chunks(root);
6296
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006297 /*
6298 * Read all device items, and then all the chunk items. All
6299 * device items are found before any chunk item (their object id
6300 * is smaller than the lowest possible object id for a chunk
6301 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006302 */
6303 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6304 key.offset = 0;
6305 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006306 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006307 if (ret < 0)
6308 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006309 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006310 leaf = path->nodes[0];
6311 slot = path->slots[0];
6312 if (slot >= btrfs_header_nritems(leaf)) {
6313 ret = btrfs_next_leaf(root, path);
6314 if (ret == 0)
6315 continue;
6316 if (ret < 0)
6317 goto error;
6318 break;
6319 }
6320 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006321 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6322 struct btrfs_dev_item *dev_item;
6323 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006324 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006325 ret = read_one_dev(root, leaf, dev_item);
6326 if (ret)
6327 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006328 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6329 struct btrfs_chunk *chunk;
6330 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6331 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006332 if (ret)
6333 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006334 }
6335 path->slots[0]++;
6336 }
Chris Mason0b86a832008-03-24 15:01:56 -04006337 ret = 0;
6338error:
Li Zefanb367e472011-12-07 11:38:24 +08006339 unlock_chunks(root);
6340 mutex_unlock(&uuid_mutex);
6341
Yan Zheng2b820322008-11-17 21:11:30 -05006342 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006343 return ret;
6344}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006345
Miao Xiecb517ea2013-05-15 07:48:19 +00006346void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6347{
6348 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6349 struct btrfs_device *device;
6350
Liu Bo29cc83f2014-05-11 23:14:59 +08006351 while (fs_devices) {
6352 mutex_lock(&fs_devices->device_list_mutex);
6353 list_for_each_entry(device, &fs_devices->devices, dev_list)
6354 device->dev_root = fs_info->dev_root;
6355 mutex_unlock(&fs_devices->device_list_mutex);
6356
6357 fs_devices = fs_devices->seed;
6358 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006359}
6360
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006361static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6362{
6363 int i;
6364
6365 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6366 btrfs_dev_stat_reset(dev, i);
6367}
6368
6369int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6370{
6371 struct btrfs_key key;
6372 struct btrfs_key found_key;
6373 struct btrfs_root *dev_root = fs_info->dev_root;
6374 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6375 struct extent_buffer *eb;
6376 int slot;
6377 int ret = 0;
6378 struct btrfs_device *device;
6379 struct btrfs_path *path = NULL;
6380 int i;
6381
6382 path = btrfs_alloc_path();
6383 if (!path) {
6384 ret = -ENOMEM;
6385 goto out;
6386 }
6387
6388 mutex_lock(&fs_devices->device_list_mutex);
6389 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6390 int item_size;
6391 struct btrfs_dev_stats_item *ptr;
6392
6393 key.objectid = 0;
6394 key.type = BTRFS_DEV_STATS_KEY;
6395 key.offset = device->devid;
6396 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6397 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006398 __btrfs_reset_dev_stats(device);
6399 device->dev_stats_valid = 1;
6400 btrfs_release_path(path);
6401 continue;
6402 }
6403 slot = path->slots[0];
6404 eb = path->nodes[0];
6405 btrfs_item_key_to_cpu(eb, &found_key, slot);
6406 item_size = btrfs_item_size_nr(eb, slot);
6407
6408 ptr = btrfs_item_ptr(eb, slot,
6409 struct btrfs_dev_stats_item);
6410
6411 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6412 if (item_size >= (1 + i) * sizeof(__le64))
6413 btrfs_dev_stat_set(device, i,
6414 btrfs_dev_stats_value(eb, ptr, i));
6415 else
6416 btrfs_dev_stat_reset(device, i);
6417 }
6418
6419 device->dev_stats_valid = 1;
6420 btrfs_dev_stat_print_on_load(device);
6421 btrfs_release_path(path);
6422 }
6423 mutex_unlock(&fs_devices->device_list_mutex);
6424
6425out:
6426 btrfs_free_path(path);
6427 return ret < 0 ? ret : 0;
6428}
6429
6430static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6431 struct btrfs_root *dev_root,
6432 struct btrfs_device *device)
6433{
6434 struct btrfs_path *path;
6435 struct btrfs_key key;
6436 struct extent_buffer *eb;
6437 struct btrfs_dev_stats_item *ptr;
6438 int ret;
6439 int i;
6440
6441 key.objectid = 0;
6442 key.type = BTRFS_DEV_STATS_KEY;
6443 key.offset = device->devid;
6444
6445 path = btrfs_alloc_path();
6446 BUG_ON(!path);
6447 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6448 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006449 printk_in_rcu(KERN_WARNING "BTRFS: "
6450 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006451 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006452 goto out;
6453 }
6454
6455 if (ret == 0 &&
6456 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6457 /* need to delete old one and insert a new one */
6458 ret = btrfs_del_item(trans, dev_root, path);
6459 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006460 printk_in_rcu(KERN_WARNING "BTRFS: "
6461 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006462 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006463 goto out;
6464 }
6465 ret = 1;
6466 }
6467
6468 if (ret == 1) {
6469 /* need to insert a new item */
6470 btrfs_release_path(path);
6471 ret = btrfs_insert_empty_item(trans, dev_root, path,
6472 &key, sizeof(*ptr));
6473 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006474 printk_in_rcu(KERN_WARNING "BTRFS: "
6475 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006476 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006477 goto out;
6478 }
6479 }
6480
6481 eb = path->nodes[0];
6482 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6483 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6484 btrfs_set_dev_stats_value(eb, ptr, i,
6485 btrfs_dev_stat_read(device, i));
6486 btrfs_mark_buffer_dirty(eb);
6487
6488out:
6489 btrfs_free_path(path);
6490 return ret;
6491}
6492
6493/*
6494 * called from commit_transaction. Writes all changed device stats to disk.
6495 */
6496int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6497 struct btrfs_fs_info *fs_info)
6498{
6499 struct btrfs_root *dev_root = fs_info->dev_root;
6500 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6501 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006502 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006503 int ret = 0;
6504
6505 mutex_lock(&fs_devices->device_list_mutex);
6506 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006507 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006508 continue;
6509
Miao Xieaddc3fa2014-07-24 11:37:11 +08006510 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006511 ret = update_dev_stat_item(trans, dev_root, device);
6512 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006513 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006514 }
6515 mutex_unlock(&fs_devices->device_list_mutex);
6516
6517 return ret;
6518}
6519
Stefan Behrens442a4f62012-05-25 16:06:08 +02006520void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6521{
6522 btrfs_dev_stat_inc(dev, index);
6523 btrfs_dev_stat_print_on_error(dev);
6524}
6525
Eric Sandeen48a3b632013-04-25 20:41:01 +00006526static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006527{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006528 if (!dev->dev_stats_valid)
6529 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006530 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6531 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006532 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006533 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6534 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6535 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006536 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6537 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006538}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006539
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006540static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6541{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006542 int i;
6543
6544 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6545 if (btrfs_dev_stat_read(dev, i) != 0)
6546 break;
6547 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6548 return; /* all values == 0, suppress message */
6549
Frank Holtonefe120a2013-12-20 11:37:06 -05006550 printk_in_rcu(KERN_INFO "BTRFS: "
6551 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006552 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006553 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6554 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6555 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6556 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6557 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6558}
6559
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006560int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006561 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006562{
6563 struct btrfs_device *dev;
6564 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6565 int i;
6566
6567 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006568 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006569 mutex_unlock(&fs_devices->device_list_mutex);
6570
6571 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006572 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006573 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006574 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006575 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006576 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006577 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006578 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6579 if (stats->nr_items > i)
6580 stats->values[i] =
6581 btrfs_dev_stat_read_and_reset(dev, i);
6582 else
6583 btrfs_dev_stat_reset(dev, i);
6584 }
6585 } else {
6586 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6587 if (stats->nr_items > i)
6588 stats->values[i] = btrfs_dev_stat_read(dev, i);
6589 }
6590 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6591 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6592 return 0;
6593}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006594
6595int btrfs_scratch_superblock(struct btrfs_device *device)
6596{
6597 struct buffer_head *bh;
6598 struct btrfs_super_block *disk_super;
6599
6600 bh = btrfs_read_dev_super(device->bdev);
6601 if (!bh)
6602 return -EINVAL;
6603 disk_super = (struct btrfs_super_block *)bh->b_data;
6604
6605 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6606 set_buffer_dirty(bh);
6607 sync_dirty_buffer(bh);
6608 brelse(bh);
6609
6610 return 0;
6611}
Miao Xie935e5cc2014-09-03 21:35:33 +08006612
6613/*
6614 * Update the size of all devices, which is used for writing out the
6615 * super blocks.
6616 */
6617void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
6618{
6619 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6620 struct btrfs_device *curr, *next;
6621
6622 if (list_empty(&fs_devices->resized_devices))
6623 return;
6624
6625 mutex_lock(&fs_devices->device_list_mutex);
6626 lock_chunks(fs_info->dev_root);
6627 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
6628 resized_list) {
6629 list_del_init(&curr->resized_list);
6630 curr->commit_total_bytes = curr->disk_total_bytes;
6631 }
6632 unlock_chunks(fs_info->dev_root);
6633 mutex_unlock(&fs_devices->device_list_mutex);
6634}
Miao Xiece7213c2014-09-03 21:35:34 +08006635
6636/* Must be invoked during the transaction commit */
6637void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
6638 struct btrfs_transaction *transaction)
6639{
6640 struct extent_map *em;
6641 struct map_lookup *map;
6642 struct btrfs_device *dev;
6643 int i;
6644
6645 if (list_empty(&transaction->pending_chunks))
6646 return;
6647
6648 /* In order to kick the device replace finish process */
6649 lock_chunks(root);
6650 list_for_each_entry(em, &transaction->pending_chunks, list) {
6651 map = (struct map_lookup *)em->bdev;
6652
6653 for (i = 0; i < map->num_stripes; i++) {
6654 dev = map->stripes[i].dev;
6655 dev->commit_bytes_used = dev->bytes_used;
6656 }
6657 }
6658 unlock_chunks(root);
6659}