blob: 9f22398d465fbd909537df11d10a6d3f0ff31b22 [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040021#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040022#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040023#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040024#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000025#include <linux/capability.h>
Stefan Behrens442a4f62012-05-25 16:06:08 +020026#include <linux/ratelimit.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020027#include <linux/kthread.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050028#include <linux/raid/pq.h>
Stefan Behrens803b2f52013-08-15 17:11:21 +020029#include <linux/semaphore.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050030#include <asm/div64.h>
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "ctree.h"
32#include "extent_map.h"
33#include "disk-io.h"
34#include "transaction.h"
35#include "print-tree.h"
36#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050037#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040038#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010039#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040040#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060041#include "math.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010042#include "dev-replace.h"
Anand Jain99994cd2014-06-03 11:36:00 +080043#include "sysfs.h"
Chris Mason0b86a832008-03-24 15:01:56 -040044
Yan Zheng2b820322008-11-17 21:11:30 -050045static int init_first_rw_device(struct btrfs_trans_handle *trans,
46 struct btrfs_root *root,
47 struct btrfs_device *device);
48static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020049static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +000050static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020051static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Yan Zheng2b820322008-11-17 21:11:30 -050052
Chris Mason8a4b83c2008-03-24 15:02:07 -040053static DEFINE_MUTEX(uuid_mutex);
54static LIST_HEAD(fs_uuids);
55
Chris Mason7d9eb122008-07-08 14:19:17 -040056static void lock_chunks(struct btrfs_root *root)
57{
Chris Mason7d9eb122008-07-08 14:19:17 -040058 mutex_lock(&root->fs_info->chunk_mutex);
59}
60
61static void unlock_chunks(struct btrfs_root *root)
62{
Chris Mason7d9eb122008-07-08 14:19:17 -040063 mutex_unlock(&root->fs_info->chunk_mutex);
64}
65
Ilya Dryomov2208a372013-08-12 14:33:03 +030066static struct btrfs_fs_devices *__alloc_fs_devices(void)
67{
68 struct btrfs_fs_devices *fs_devs;
69
70 fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS);
71 if (!fs_devs)
72 return ERR_PTR(-ENOMEM);
73
74 mutex_init(&fs_devs->device_list_mutex);
75
76 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +080077 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +030078 INIT_LIST_HEAD(&fs_devs->alloc_list);
79 INIT_LIST_HEAD(&fs_devs->list);
80
81 return fs_devs;
82}
83
84/**
85 * alloc_fs_devices - allocate struct btrfs_fs_devices
86 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
87 * generated.
88 *
89 * Return: a pointer to a new &struct btrfs_fs_devices on success;
90 * ERR_PTR() on error. Returned struct is not linked onto any lists and
91 * can be destroyed with kfree() right away.
92 */
93static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
94{
95 struct btrfs_fs_devices *fs_devs;
96
97 fs_devs = __alloc_fs_devices();
98 if (IS_ERR(fs_devs))
99 return fs_devs;
100
101 if (fsid)
102 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
103 else
104 generate_random_uuid(fs_devs->fsid);
105
106 return fs_devs;
107}
108
Yan Zhenge4404d62008-12-12 10:03:26 -0500109static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
110{
111 struct btrfs_device *device;
112 WARN_ON(fs_devices->opened);
113 while (!list_empty(&fs_devices->devices)) {
114 device = list_entry(fs_devices->devices.next,
115 struct btrfs_device, dev_list);
116 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -0400117 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500118 kfree(device);
119 }
120 kfree(fs_devices);
121}
122
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000123static void btrfs_kobject_uevent(struct block_device *bdev,
124 enum kobject_action action)
125{
126 int ret;
127
128 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
129 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500130 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000131 action,
132 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
133 &disk_to_dev(bdev->bd_disk)->kobj);
134}
135
Jeff Mahoney143bede2012-03-01 14:56:26 +0100136void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400137{
138 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400139
Yan Zheng2b820322008-11-17 21:11:30 -0500140 while (!list_empty(&fs_uuids)) {
141 fs_devices = list_entry(fs_uuids.next,
142 struct btrfs_fs_devices, list);
143 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500144 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400145 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400146}
147
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300148static struct btrfs_device *__alloc_device(void)
149{
150 struct btrfs_device *dev;
151
152 dev = kzalloc(sizeof(*dev), GFP_NOFS);
153 if (!dev)
154 return ERR_PTR(-ENOMEM);
155
156 INIT_LIST_HEAD(&dev->dev_list);
157 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800158 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300159
160 spin_lock_init(&dev->io_lock);
161
162 spin_lock_init(&dev->reada_lock);
163 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800164 atomic_set(&dev->dev_stats_ccnt, 0);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300165 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT);
166 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT);
167
168 return dev;
169}
170
Chris Masona1b32a52008-09-05 16:09:51 -0400171static noinline struct btrfs_device *__find_device(struct list_head *head,
172 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400173{
174 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400175
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500176 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400177 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400178 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400179 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400180 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400181 }
182 return NULL;
183}
184
Chris Masona1b32a52008-09-05 16:09:51 -0400185static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400186{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400187 struct btrfs_fs_devices *fs_devices;
188
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500189 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400190 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
191 return fs_devices;
192 }
193 return NULL;
194}
195
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100196static int
197btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
198 int flush, struct block_device **bdev,
199 struct buffer_head **bh)
200{
201 int ret;
202
203 *bdev = blkdev_get_by_path(device_path, flags, holder);
204
205 if (IS_ERR(*bdev)) {
206 ret = PTR_ERR(*bdev);
Frank Holtonefe120a2013-12-20 11:37:06 -0500207 printk(KERN_INFO "BTRFS: open %s failed\n", device_path);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100208 goto error;
209 }
210
211 if (flush)
212 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
213 ret = set_blocksize(*bdev, 4096);
214 if (ret) {
215 blkdev_put(*bdev, flags);
216 goto error;
217 }
218 invalidate_bdev(*bdev);
219 *bh = btrfs_read_dev_super(*bdev);
220 if (!*bh) {
221 ret = -EINVAL;
222 blkdev_put(*bdev, flags);
223 goto error;
224 }
225
226 return 0;
227
228error:
229 *bdev = NULL;
230 *bh = NULL;
231 return ret;
232}
233
Chris Masonffbd5172009-04-20 15:50:09 -0400234static void requeue_list(struct btrfs_pending_bios *pending_bios,
235 struct bio *head, struct bio *tail)
236{
237
238 struct bio *old_head;
239
240 old_head = pending_bios->head;
241 pending_bios->head = head;
242 if (pending_bios->tail)
243 tail->bi_next = old_head;
244 else
245 pending_bios->tail = tail;
246}
247
Chris Mason8b712842008-06-11 16:50:36 -0400248/*
249 * we try to collect pending bios for a device so we don't get a large
250 * number of procs sending bios down to the same device. This greatly
251 * improves the schedulers ability to collect and merge the bios.
252 *
253 * But, it also turns into a long list of bios to process and that is sure
254 * to eventually make the worker thread block. The solution here is to
255 * make some progress and then put this work struct back at the end of
256 * the list if the block device is congested. This way, multiple devices
257 * can make progress from a single worker thread.
258 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100259static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400260{
261 struct bio *pending;
262 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400263 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400264 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400265 struct bio *tail;
266 struct bio *cur;
267 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400268 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400269 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400270 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400271 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400272 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000273 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400274 struct blk_plug plug;
275
276 /*
277 * this function runs all the bios we've collected for
278 * a particular device. We don't want to wander off to
279 * another device without first sending all of these down.
280 * So, setup a plug here and finish it off before we return
281 */
282 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400283
Chris Masonbedf7622009-04-03 10:32:58 -0400284 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400285 fs_info = device->dev_root->fs_info;
286 limit = btrfs_async_submit_limit(fs_info);
287 limit = limit * 2 / 3;
288
Chris Mason8b712842008-06-11 16:50:36 -0400289loop:
290 spin_lock(&device->io_lock);
291
Chris Masona6837052009-02-04 09:19:41 -0500292loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400293 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400294
Chris Mason8b712842008-06-11 16:50:36 -0400295 /* take all the bios off the list at once and process them
296 * later on (without the lock held). But, remember the
297 * tail and other pointers so the bios can be properly reinserted
298 * into the list if we hit congestion
299 */
Chris Masond84275c2009-06-09 15:39:08 -0400300 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400301 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400302 force_reg = 1;
303 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400304 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400305 force_reg = 0;
306 }
Chris Masonffbd5172009-04-20 15:50:09 -0400307
308 pending = pending_bios->head;
309 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400310 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400311
312 /*
313 * if pending was null this time around, no bios need processing
314 * at all and we can stop. Otherwise it'll loop back up again
315 * and do an additional check so no bios are missed.
316 *
317 * device->running_pending is used to synchronize with the
318 * schedule_bio code.
319 */
Chris Masonffbd5172009-04-20 15:50:09 -0400320 if (device->pending_sync_bios.head == NULL &&
321 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400322 again = 0;
323 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400324 } else {
325 again = 1;
326 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400327 }
Chris Masonffbd5172009-04-20 15:50:09 -0400328
329 pending_bios->head = NULL;
330 pending_bios->tail = NULL;
331
Chris Mason8b712842008-06-11 16:50:36 -0400332 spin_unlock(&device->io_lock);
333
Chris Masond3977122009-01-05 21:25:51 -0500334 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400335
336 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400337 /* we want to work on both lists, but do more bios on the
338 * sync list than the regular list
339 */
340 if ((num_run > 32 &&
341 pending_bios != &device->pending_sync_bios &&
342 device->pending_sync_bios.head) ||
343 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
344 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400345 spin_lock(&device->io_lock);
346 requeue_list(pending_bios, pending, tail);
347 goto loop_lock;
348 }
349
Chris Mason8b712842008-06-11 16:50:36 -0400350 cur = pending;
351 pending = pending->bi_next;
352 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400353
Josef Bacik66657b32012-08-01 15:36:24 -0400354 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400355 waitqueue_active(&fs_info->async_submit_wait))
356 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400357
358 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400359
Chris Mason2ab1ba62011-08-04 14:28:36 -0400360 /*
361 * if we're doing the sync list, record that our
362 * plug has some sync requests on it
363 *
364 * If we're doing the regular list and there are
365 * sync requests sitting around, unplug before
366 * we add more
367 */
368 if (pending_bios == &device->pending_sync_bios) {
369 sync_pending = 1;
370 } else if (sync_pending) {
371 blk_finish_plug(&plug);
372 blk_start_plug(&plug);
373 sync_pending = 0;
374 }
375
Stefan Behrens21adbd52011-11-09 13:44:05 +0100376 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400377 num_run++;
378 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100379 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400380 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400381
382 /*
383 * we made progress, there is more work to do and the bdi
384 * is now congested. Back off and let other work structs
385 * run instead
386 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400387 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500388 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400389 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400390
Chris Masonb765ead2009-04-03 10:27:10 -0400391 ioc = current->io_context;
392
393 /*
394 * the main goal here is that we don't want to
395 * block if we're going to be able to submit
396 * more requests without blocking.
397 *
398 * This code does two great things, it pokes into
399 * the elevator code from a filesystem _and_
400 * it makes assumptions about how batching works.
401 */
402 if (ioc && ioc->nr_batch_requests > 0 &&
403 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
404 (last_waited == 0 ||
405 ioc->last_waited == last_waited)) {
406 /*
407 * we want to go through our batch of
408 * requests and stop. So, we copy out
409 * the ioc->last_waited time and test
410 * against it before looping
411 */
412 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100413 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400414 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400415 continue;
416 }
Chris Mason8b712842008-06-11 16:50:36 -0400417 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400418 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500419 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400420
421 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800422 btrfs_queue_work(fs_info->submit_workers,
423 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400424 goto done;
425 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500426 /* unplug every 64 requests just for good measure */
427 if (batch_run % 64 == 0) {
428 blk_finish_plug(&plug);
429 blk_start_plug(&plug);
430 sync_pending = 0;
431 }
Chris Mason8b712842008-06-11 16:50:36 -0400432 }
Chris Masonffbd5172009-04-20 15:50:09 -0400433
Chris Mason51684082010-03-10 15:33:32 -0500434 cond_resched();
435 if (again)
436 goto loop;
437
438 spin_lock(&device->io_lock);
439 if (device->pending_bios.head || device->pending_sync_bios.head)
440 goto loop_lock;
441 spin_unlock(&device->io_lock);
442
Chris Mason8b712842008-06-11 16:50:36 -0400443done:
Chris Mason211588a2011-04-19 20:12:40 -0400444 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400445}
446
Christoph Hellwigb2950862008-12-02 09:54:17 -0500447static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400448{
449 struct btrfs_device *device;
450
451 device = container_of(work, struct btrfs_device, work);
452 run_scheduled_bios(device);
453}
454
David Sterba60999ca2014-03-26 18:26:36 +0100455/*
456 * Add new device to list of registered devices
457 *
458 * Returns:
459 * 1 - first time device is seen
460 * 0 - device already known
461 * < 0 - error
462 */
Chris Masona1b32a52008-09-05 16:09:51 -0400463static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400464 struct btrfs_super_block *disk_super,
465 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
466{
467 struct btrfs_device *device;
468 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400469 struct rcu_string *name;
David Sterba60999ca2014-03-26 18:26:36 +0100470 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400471 u64 found_transid = btrfs_super_generation(disk_super);
472
473 fs_devices = find_fsid(disk_super->fsid);
474 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300475 fs_devices = alloc_fs_devices(disk_super->fsid);
476 if (IS_ERR(fs_devices))
477 return PTR_ERR(fs_devices);
478
Chris Mason8a4b83c2008-03-24 15:02:07 -0400479 list_add(&fs_devices->list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300480
Chris Mason8a4b83c2008-03-24 15:02:07 -0400481 device = NULL;
482 } else {
Chris Masona4437552008-04-18 10:29:38 -0400483 device = __find_device(&fs_devices->devices, devid,
484 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400485 }
Miao Xie443f24f2014-07-24 11:37:15 +0800486
Chris Mason8a4b83c2008-03-24 15:02:07 -0400487 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500488 if (fs_devices->opened)
489 return -EBUSY;
490
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300491 device = btrfs_alloc_device(NULL, &devid,
492 disk_super->dev_item.uuid);
493 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400494 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300495 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400496 }
Josef Bacik606686e2012-06-04 14:03:51 -0400497
498 name = rcu_string_strdup(path, GFP_NOFS);
499 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400500 kfree(device);
501 return -ENOMEM;
502 }
Josef Bacik606686e2012-06-04 14:03:51 -0400503 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200504
Chris Masone5e9a522009-06-10 15:17:02 -0400505 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000506 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100507 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400508 mutex_unlock(&fs_devices->device_list_mutex);
509
David Sterba60999ca2014-03-26 18:26:36 +0100510 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500511 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400512 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800513 /*
514 * When FS is already mounted.
515 * 1. If you are here and if the device->name is NULL that
516 * means this device was missing at time of FS mount.
517 * 2. If you are here and if the device->name is different
518 * from 'path' that means either
519 * a. The same device disappeared and reappeared with
520 * different name. or
521 * b. The missing-disk-which-was-replaced, has
522 * reappeared now.
523 *
524 * We must allow 1 and 2a above. But 2b would be a spurious
525 * and unintentional.
526 *
527 * Further in case of 1 and 2a above, the disk at 'path'
528 * would have missed some transaction when it was away and
529 * in case of 2a the stale bdev has to be updated as well.
530 * 2b must not be allowed at all time.
531 */
532
533 /*
534 * As of now don't allow update to btrfs_fs_device through
535 * the btrfs dev scan cli, after FS has been mounted.
536 */
Anand Jain77bdae42014-07-03 18:22:06 +0800537 if (fs_devices->opened) {
Anand Jainb96de002014-07-03 18:22:05 +0800538 return -EBUSY;
Anand Jain77bdae42014-07-03 18:22:06 +0800539 } else {
540 /*
541 * That is if the FS is _not_ mounted and if you
542 * are here, that means there is more than one
543 * disk with same uuid and devid.We keep the one
544 * with larger generation number or the last-in if
545 * generation are equal.
546 */
547 if (found_transid < device->generation)
548 return -EEXIST;
549 }
Anand Jainb96de002014-07-03 18:22:05 +0800550
Josef Bacik606686e2012-06-04 14:03:51 -0400551 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000552 if (!name)
553 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400554 rcu_string_free(device->name);
555 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500556 if (device->missing) {
557 fs_devices->missing_devices--;
558 device->missing = 0;
559 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400560 }
561
Anand Jain77bdae42014-07-03 18:22:06 +0800562 /*
563 * Unmount does not free the btrfs_device struct but would zero
564 * generation along with most of the other members. So just update
565 * it back. We need it to pick the disk with largest generation
566 * (as above).
567 */
568 if (!fs_devices->opened)
569 device->generation = found_transid;
570
Chris Mason8a4b83c2008-03-24 15:02:07 -0400571 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100572
573 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400574}
575
Yan Zhenge4404d62008-12-12 10:03:26 -0500576static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
577{
578 struct btrfs_fs_devices *fs_devices;
579 struct btrfs_device *device;
580 struct btrfs_device *orig_dev;
581
Ilya Dryomov2208a372013-08-12 14:33:03 +0300582 fs_devices = alloc_fs_devices(orig->fsid);
583 if (IS_ERR(fs_devices))
584 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500585
Josef Bacik02db0842012-06-21 16:03:58 -0400586 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500587
Xiao Guangrong46224702011-04-20 10:08:47 +0000588 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500589 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400590 struct rcu_string *name;
591
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300592 device = btrfs_alloc_device(NULL, &orig_dev->devid,
593 orig_dev->uuid);
594 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500595 goto error;
596
Josef Bacik606686e2012-06-04 14:03:51 -0400597 /*
598 * This is ok to do without rcu read locked because we hold the
599 * uuid mutex so nothing we touch in here is going to disappear.
600 */
Anand Jaine755f782014-06-30 17:12:47 +0800601 if (orig_dev->name) {
602 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
603 if (!name) {
604 kfree(device);
605 goto error;
606 }
607 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400608 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500609
Yan Zhenge4404d62008-12-12 10:03:26 -0500610 list_add(&device->dev_list, &fs_devices->devices);
611 device->fs_devices = fs_devices;
612 fs_devices->num_devices++;
613 }
614 return fs_devices;
615error:
616 free_fs_devices(fs_devices);
617 return ERR_PTR(-ENOMEM);
618}
619
Stefan Behrens8dabb742012-11-06 13:15:27 +0100620void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
621 struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400622{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500623 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800624 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500625
Chris Masondfe25022008-05-13 13:46:40 -0400626 mutex_lock(&uuid_mutex);
627again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000628 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500629 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500630 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100631 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800632 (!latest_dev ||
633 device->generation > latest_dev->generation)) {
634 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500635 }
Yan Zheng2b820322008-11-17 21:11:30 -0500636 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500637 }
Yan Zheng2b820322008-11-17 21:11:30 -0500638
Stefan Behrens8dabb742012-11-06 13:15:27 +0100639 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
640 /*
641 * In the first step, keep the device which has
642 * the correct fsid and the devid that is used
643 * for the dev_replace procedure.
644 * In the second step, the dev_replace state is
645 * read from the device tree and it is known
646 * whether the procedure is really active or
647 * not, which means whether this device is
648 * used or whether it should be removed.
649 */
650 if (step == 0 || device->is_tgtdev_for_dev_replace) {
651 continue;
652 }
653 }
Yan Zheng2b820322008-11-17 21:11:30 -0500654 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100655 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500656 device->bdev = NULL;
657 fs_devices->open_devices--;
658 }
659 if (device->writeable) {
660 list_del_init(&device->dev_alloc_list);
661 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100662 if (!device->is_tgtdev_for_dev_replace)
663 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500664 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500665 list_del_init(&device->dev_list);
666 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400667 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500668 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400669 }
Yan Zheng2b820322008-11-17 21:11:30 -0500670
671 if (fs_devices->seed) {
672 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500673 goto again;
674 }
675
Miao Xie443f24f2014-07-24 11:37:15 +0800676 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500677
Chris Masondfe25022008-05-13 13:46:40 -0400678 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400679}
Chris Masona0af4692008-05-13 16:03:06 -0400680
Xiao Guangrong1f781602011-04-20 10:09:16 +0000681static void __free_device(struct work_struct *work)
682{
683 struct btrfs_device *device;
684
685 device = container_of(work, struct btrfs_device, rcu_work);
686
687 if (device->bdev)
688 blkdev_put(device->bdev, device->mode);
689
Josef Bacik606686e2012-06-04 14:03:51 -0400690 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000691 kfree(device);
692}
693
694static void free_device(struct rcu_head *head)
695{
696 struct btrfs_device *device;
697
698 device = container_of(head, struct btrfs_device, rcu);
699
700 INIT_WORK(&device->rcu_work, __free_device);
701 schedule_work(&device->rcu_work);
702}
703
Yan Zheng2b820322008-11-17 21:11:30 -0500704static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400705{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400706 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500707
Yan Zheng2b820322008-11-17 21:11:30 -0500708 if (--fs_devices->opened > 0)
709 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400710
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000711 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500712 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000713 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400714 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000715
716 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400717 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000718
Ilya Dryomovf747cab2013-10-10 20:37:29 +0300719 if (device->writeable &&
720 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500721 list_del_init(&device->dev_alloc_list);
722 fs_devices->rw_devices--;
723 }
724
Josef Bacik726551e2013-08-26 13:45:53 -0400725 if (device->missing)
726 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000727
Ilya Dryomova1e87802013-08-12 14:33:04 +0300728 new_device = btrfs_alloc_device(NULL, &device->devid,
729 device->uuid);
730 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400731
732 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400733 if (device->name) {
734 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300735 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400736 rcu_assign_pointer(new_device->name, name);
737 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300738
Xiao Guangrong1f781602011-04-20 10:09:16 +0000739 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300740 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000741
742 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400743 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000744 mutex_unlock(&fs_devices->device_list_mutex);
745
Yan Zhenge4404d62008-12-12 10:03:26 -0500746 WARN_ON(fs_devices->open_devices);
747 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500748 fs_devices->opened = 0;
749 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500750
Chris Mason8a4b83c2008-03-24 15:02:07 -0400751 return 0;
752}
753
Yan Zheng2b820322008-11-17 21:11:30 -0500754int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
755{
Yan Zhenge4404d62008-12-12 10:03:26 -0500756 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500757 int ret;
758
759 mutex_lock(&uuid_mutex);
760 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500761 if (!fs_devices->opened) {
762 seed_devices = fs_devices->seed;
763 fs_devices->seed = NULL;
764 }
Yan Zheng2b820322008-11-17 21:11:30 -0500765 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500766
767 while (seed_devices) {
768 fs_devices = seed_devices;
769 seed_devices = fs_devices->seed;
770 __btrfs_close_devices(fs_devices);
771 free_fs_devices(fs_devices);
772 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000773 /*
774 * Wait for rcu kworkers under __btrfs_close_devices
775 * to finish all blkdev_puts so device is really
776 * free when umount is done.
777 */
778 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500779 return ret;
780}
781
Yan Zhenge4404d62008-12-12 10:03:26 -0500782static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
783 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400784{
Josef Bacikd5e20032011-08-04 14:52:27 +0000785 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400786 struct block_device *bdev;
787 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400788 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +0800789 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -0400790 struct buffer_head *bh;
791 struct btrfs_super_block *disk_super;
Chris Masona0af4692008-05-13 16:03:06 -0400792 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500793 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400794 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400795
Tejun Heod4d77622010-11-13 11:55:18 +0100796 flags |= FMODE_EXCL;
797
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500798 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400799 if (device->bdev)
800 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400801 if (!device->name)
802 continue;
803
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000804 /* Just open everything we can; ignore failures here */
805 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
806 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100807 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400808
809 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000810 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400811 if (devid != device->devid)
812 goto error_brelse;
813
Yan Zheng2b820322008-11-17 21:11:30 -0500814 if (memcmp(device->uuid, disk_super->dev_item.uuid,
815 BTRFS_UUID_SIZE))
816 goto error_brelse;
817
818 device->generation = btrfs_super_generation(disk_super);
Miao Xie443f24f2014-07-24 11:37:15 +0800819 if (!latest_dev ||
820 device->generation > latest_dev->generation)
821 latest_dev = device;
Chris Masona0af4692008-05-13 16:03:06 -0400822
Yan Zheng2b820322008-11-17 21:11:30 -0500823 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
824 device->writeable = 0;
825 } else {
826 device->writeable = !bdev_read_only(bdev);
827 seeding = 0;
828 }
829
Josef Bacikd5e20032011-08-04 14:52:27 +0000830 q = bdev_get_queue(bdev);
Miao Xie90180da2014-09-03 21:35:30 +0800831 if (blk_queue_discard(q))
Josef Bacikd5e20032011-08-04 14:52:27 +0000832 device->can_discard = 1;
Josef Bacikd5e20032011-08-04 14:52:27 +0000833
Chris Mason8a4b83c2008-03-24 15:02:07 -0400834 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400835 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500836 device->mode = flags;
837
Chris Masonc2898112009-06-10 09:51:32 -0400838 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
839 fs_devices->rotating = 1;
840
Chris Masona0af4692008-05-13 16:03:06 -0400841 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300842 if (device->writeable &&
843 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500844 fs_devices->rw_devices++;
845 list_add(&device->dev_alloc_list,
846 &fs_devices->alloc_list);
847 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000848 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400849 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400850
Chris Masona0af4692008-05-13 16:03:06 -0400851error_brelse:
852 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100853 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400854 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400855 }
Chris Masona0af4692008-05-13 16:03:06 -0400856 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300857 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400858 goto out;
859 }
Yan Zheng2b820322008-11-17 21:11:30 -0500860 fs_devices->seeding = seeding;
861 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +0800862 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -0500863 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400864out:
Yan Zheng2b820322008-11-17 21:11:30 -0500865 return ret;
866}
867
868int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500869 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500870{
871 int ret;
872
873 mutex_lock(&uuid_mutex);
874 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500875 fs_devices->opened++;
876 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500877 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500878 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500879 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400880 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400881 return ret;
882}
883
David Sterba6f60cbd2013-02-15 11:31:02 -0700884/*
885 * Look for a btrfs signature on a device. This may be called out of the mount path
886 * and we are not allowed to call set_blocksize during the scan. The superblock
887 * is read via pagecache
888 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500889int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400890 struct btrfs_fs_devices **fs_devices_ret)
891{
892 struct btrfs_super_block *disk_super;
893 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700894 struct page *page;
895 void *p;
896 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400897 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400898 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400899 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700900 u64 bytenr;
901 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400902
David Sterba6f60cbd2013-02-15 11:31:02 -0700903 /*
904 * we would like to check all the supers, but that would make
905 * a btrfs mount succeed after a mkfs from a different FS.
906 * So, we need to add a special mount option to scan for
907 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
908 */
909 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100910 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500911 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700912
913 bdev = blkdev_get_by_path(path, flags, holder);
914
915 if (IS_ERR(bdev)) {
916 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100917 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700918 }
919
920 /* make sure our super fits in the device */
921 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
922 goto error_bdev_put;
923
924 /* make sure our super fits in the page */
925 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
926 goto error_bdev_put;
927
928 /* make sure our super doesn't straddle pages on disk */
929 index = bytenr >> PAGE_CACHE_SHIFT;
930 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
931 goto error_bdev_put;
932
933 /* pull in the page with our super */
934 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
935 index, GFP_NOFS);
936
937 if (IS_ERR_OR_NULL(page))
938 goto error_bdev_put;
939
940 p = kmap(page);
941
942 /* align our pointer to the offset of the super block */
943 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
944
945 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +0800946 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -0700947 goto error_unmap;
948
Xiao Guangronga3438322010-01-06 11:48:18 +0000949 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400950 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400951 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700952
Chris Mason8a4b83c2008-03-24 15:02:07 -0400953 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +0100954 if (ret > 0) {
955 if (disk_super->label[0]) {
956 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
957 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
958 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
959 } else {
960 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
961 }
962
963 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
964 ret = 0;
965 }
Josef Bacik02db0842012-06-21 16:03:58 -0400966 if (!ret && fs_devices_ret)
967 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700968
969error_unmap:
970 kunmap(page);
971 page_cache_release(page);
972
973error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100974 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400975error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100976 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400977 return ret;
978}
Chris Mason0b86a832008-03-24 15:01:56 -0400979
Miao Xie6d07bce2011-01-05 10:07:31 +0000980/* helper to account the used device space in the range */
981int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
982 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400983{
984 struct btrfs_key key;
985 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000986 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500987 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000988 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400989 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000990 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400991 struct extent_buffer *l;
992
Miao Xie6d07bce2011-01-05 10:07:31 +0000993 *length = 0;
994
Stefan Behrens63a212a2012-11-05 18:29:28 +0100995 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000996 return 0;
997
Yan Zheng2b820322008-11-17 21:11:30 -0500998 path = btrfs_alloc_path();
999 if (!path)
1000 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001001 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -04001002
Chris Mason0b86a832008-03-24 15:01:56 -04001003 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001004 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001005 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001006
1007 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001008 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001009 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001010 if (ret > 0) {
1011 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1012 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001013 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001014 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001015
Chris Mason0b86a832008-03-24 15:01:56 -04001016 while (1) {
1017 l = path->nodes[0];
1018 slot = path->slots[0];
1019 if (slot >= btrfs_header_nritems(l)) {
1020 ret = btrfs_next_leaf(root, path);
1021 if (ret == 0)
1022 continue;
1023 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001024 goto out;
1025
1026 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001027 }
1028 btrfs_item_key_to_cpu(l, &key, slot);
1029
1030 if (key.objectid < device->devid)
1031 goto next;
1032
1033 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001034 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001035
David Sterba962a2982014-06-04 18:41:45 +02001036 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001037 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001038
Chris Mason0b86a832008-03-24 15:01:56 -04001039 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001040 extent_end = key.offset + btrfs_dev_extent_length(l,
1041 dev_extent);
1042 if (key.offset <= start && extent_end > end) {
1043 *length = end - start + 1;
1044 break;
1045 } else if (key.offset <= start && extent_end > start)
1046 *length += extent_end - start;
1047 else if (key.offset > start && extent_end <= end)
1048 *length += extent_end - key.offset;
1049 else if (key.offset > start && key.offset <= end) {
1050 *length += end - key.offset + 1;
1051 break;
1052 } else if (key.offset > end)
1053 break;
1054
1055next:
1056 path->slots[0]++;
1057 }
1058 ret = 0;
1059out:
1060 btrfs_free_path(path);
1061 return ret;
1062}
1063
Josef Bacik6df9a952013-06-27 13:22:46 -04001064static int contains_pending_extent(struct btrfs_trans_handle *trans,
1065 struct btrfs_device *device,
1066 u64 *start, u64 len)
1067{
1068 struct extent_map *em;
1069 int ret = 0;
1070
1071 list_for_each_entry(em, &trans->transaction->pending_chunks, list) {
1072 struct map_lookup *map;
1073 int i;
1074
1075 map = (struct map_lookup *)em->bdev;
1076 for (i = 0; i < map->num_stripes; i++) {
1077 if (map->stripes[i].dev != device)
1078 continue;
1079 if (map->stripes[i].physical >= *start + len ||
1080 map->stripes[i].physical + em->orig_block_len <=
1081 *start)
1082 continue;
1083 *start = map->stripes[i].physical +
1084 em->orig_block_len;
1085 ret = 1;
1086 }
1087 }
1088
1089 return ret;
1090}
1091
1092
Chris Mason0b86a832008-03-24 15:01:56 -04001093/*
Miao Xie7bfc8372011-01-05 10:07:26 +00001094 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +00001095 * @device: the device which we search the free space in
1096 * @num_bytes: the size of the free space that we need
1097 * @start: store the start of the free space.
1098 * @len: the size of the free space. that we find, or the size of the max
1099 * free space if we don't find suitable free space
1100 *
Chris Mason0b86a832008-03-24 15:01:56 -04001101 * this uses a pretty simple search, the expectation is that it is
1102 * called very infrequently and that a given device has a small number
1103 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001104 *
1105 * @start is used to store the start of the free space if we find. But if we
1106 * don't find suitable free space, it will be used to store the start position
1107 * of the max free space.
1108 *
1109 * @len is used to store the size of the free space that we find.
1110 * But if we don't find suitable free space, it is used to store the size of
1111 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001112 */
Josef Bacik6df9a952013-06-27 13:22:46 -04001113int find_free_dev_extent(struct btrfs_trans_handle *trans,
1114 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001115 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001116{
1117 struct btrfs_key key;
1118 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001119 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001120 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001121 u64 hole_size;
1122 u64 max_hole_start;
1123 u64 max_hole_size;
1124 u64 extent_end;
1125 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001126 u64 search_end = device->total_bytes;
1127 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001128 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001129 struct extent_buffer *l;
1130
Chris Mason0b86a832008-03-24 15:01:56 -04001131 /* FIXME use last free of some kind */
1132
1133 /* we don't want to overwrite the superblock on the drive,
1134 * so we make sure to start at an offset of at least 1MB
1135 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001136 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001137
Josef Bacik6df9a952013-06-27 13:22:46 -04001138 path = btrfs_alloc_path();
1139 if (!path)
1140 return -ENOMEM;
1141again:
Miao Xie7bfc8372011-01-05 10:07:26 +00001142 max_hole_start = search_start;
1143 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +00001144 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +00001145
Stefan Behrens63a212a2012-11-05 18:29:28 +01001146 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001147 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001148 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001149 }
1150
Miao Xie7bfc8372011-01-05 10:07:26 +00001151 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001152 path->search_commit_root = 1;
1153 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001154
Chris Mason0b86a832008-03-24 15:01:56 -04001155 key.objectid = device->devid;
1156 key.offset = search_start;
1157 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001158
Li Zefan125ccb02011-12-08 15:07:24 +08001159 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001160 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001161 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001162 if (ret > 0) {
1163 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1164 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001165 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001166 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001167
Chris Mason0b86a832008-03-24 15:01:56 -04001168 while (1) {
1169 l = path->nodes[0];
1170 slot = path->slots[0];
1171 if (slot >= btrfs_header_nritems(l)) {
1172 ret = btrfs_next_leaf(root, path);
1173 if (ret == 0)
1174 continue;
1175 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001176 goto out;
1177
1178 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001179 }
1180 btrfs_item_key_to_cpu(l, &key, slot);
1181
1182 if (key.objectid < device->devid)
1183 goto next;
1184
1185 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001186 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001187
David Sterba962a2982014-06-04 18:41:45 +02001188 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001189 goto next;
1190
Miao Xie7bfc8372011-01-05 10:07:26 +00001191 if (key.offset > search_start) {
1192 hole_size = key.offset - search_start;
1193
Josef Bacik6df9a952013-06-27 13:22:46 -04001194 /*
1195 * Have to check before we set max_hole_start, otherwise
1196 * we could end up sending back this offset anyway.
1197 */
1198 if (contains_pending_extent(trans, device,
1199 &search_start,
1200 hole_size))
1201 hole_size = 0;
1202
Miao Xie7bfc8372011-01-05 10:07:26 +00001203 if (hole_size > max_hole_size) {
1204 max_hole_start = search_start;
1205 max_hole_size = hole_size;
1206 }
1207
1208 /*
1209 * If this free space is greater than which we need,
1210 * it must be the max free space that we have found
1211 * until now, so max_hole_start must point to the start
1212 * of this free space and the length of this free space
1213 * is stored in max_hole_size. Thus, we return
1214 * max_hole_start and max_hole_size and go back to the
1215 * caller.
1216 */
1217 if (hole_size >= num_bytes) {
1218 ret = 0;
1219 goto out;
1220 }
1221 }
1222
Chris Mason0b86a832008-03-24 15:01:56 -04001223 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001224 extent_end = key.offset + btrfs_dev_extent_length(l,
1225 dev_extent);
1226 if (extent_end > search_start)
1227 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001228next:
1229 path->slots[0]++;
1230 cond_resched();
1231 }
Chris Mason0b86a832008-03-24 15:01:56 -04001232
liubo38c01b92011-08-02 02:39:03 +00001233 /*
1234 * At this point, search_start should be the end of
1235 * allocated dev extents, and when shrinking the device,
1236 * search_end may be smaller than search_start.
1237 */
1238 if (search_end > search_start)
1239 hole_size = search_end - search_start;
1240
Miao Xie7bfc8372011-01-05 10:07:26 +00001241 if (hole_size > max_hole_size) {
1242 max_hole_start = search_start;
1243 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001244 }
Chris Mason0b86a832008-03-24 15:01:56 -04001245
Josef Bacik6df9a952013-06-27 13:22:46 -04001246 if (contains_pending_extent(trans, device, &search_start, hole_size)) {
1247 btrfs_release_path(path);
1248 goto again;
1249 }
1250
Miao Xie7bfc8372011-01-05 10:07:26 +00001251 /* See above. */
1252 if (hole_size < num_bytes)
1253 ret = -ENOSPC;
1254 else
1255 ret = 0;
1256
1257out:
Yan Zheng2b820322008-11-17 21:11:30 -05001258 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001259 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001260 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001261 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001262 return ret;
1263}
1264
Christoph Hellwigb2950862008-12-02 09:54:17 -05001265static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001266 struct btrfs_device *device,
1267 u64 start)
1268{
1269 int ret;
1270 struct btrfs_path *path;
1271 struct btrfs_root *root = device->dev_root;
1272 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001273 struct btrfs_key found_key;
1274 struct extent_buffer *leaf = NULL;
1275 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001276
1277 path = btrfs_alloc_path();
1278 if (!path)
1279 return -ENOMEM;
1280
1281 key.objectid = device->devid;
1282 key.offset = start;
1283 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001284again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001285 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001286 if (ret > 0) {
1287 ret = btrfs_previous_item(root, path, key.objectid,
1288 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001289 if (ret)
1290 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001291 leaf = path->nodes[0];
1292 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1293 extent = btrfs_item_ptr(leaf, path->slots[0],
1294 struct btrfs_dev_extent);
1295 BUG_ON(found_key.offset > start || found_key.offset +
1296 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001297 key = found_key;
1298 btrfs_release_path(path);
1299 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001300 } else if (ret == 0) {
1301 leaf = path->nodes[0];
1302 extent = btrfs_item_ptr(leaf, path->slots[0],
1303 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001304 } else {
1305 btrfs_error(root->fs_info, ret, "Slot search failed");
1306 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001307 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001308
Josef Bacik2bf64752011-09-26 17:12:22 -04001309 if (device->bytes_used > 0) {
1310 u64 len = btrfs_dev_extent_length(leaf, extent);
Miao Xie7cc8e582014-09-03 21:35:38 +08001311 btrfs_device_set_bytes_used(device, device->bytes_used - len);
Josef Bacik2bf64752011-09-26 17:12:22 -04001312 spin_lock(&root->fs_info->free_chunk_lock);
1313 root->fs_info->free_chunk_space += len;
1314 spin_unlock(&root->fs_info->free_chunk_lock);
1315 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001316 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001317 if (ret) {
1318 btrfs_error(root->fs_info, ret,
1319 "Failed to remove dev extent item");
1320 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001321out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001322 btrfs_free_path(path);
1323 return ret;
1324}
1325
Eric Sandeen48a3b632013-04-25 20:41:01 +00001326static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1327 struct btrfs_device *device,
1328 u64 chunk_tree, u64 chunk_objectid,
1329 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001330{
1331 int ret;
1332 struct btrfs_path *path;
1333 struct btrfs_root *root = device->dev_root;
1334 struct btrfs_dev_extent *extent;
1335 struct extent_buffer *leaf;
1336 struct btrfs_key key;
1337
Chris Masondfe25022008-05-13 13:46:40 -04001338 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001339 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001340 path = btrfs_alloc_path();
1341 if (!path)
1342 return -ENOMEM;
1343
Chris Mason0b86a832008-03-24 15:01:56 -04001344 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001345 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001346 key.type = BTRFS_DEV_EXTENT_KEY;
1347 ret = btrfs_insert_empty_item(trans, root, path, &key,
1348 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001349 if (ret)
1350 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001351
1352 leaf = path->nodes[0];
1353 extent = btrfs_item_ptr(leaf, path->slots[0],
1354 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001355 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1356 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1357 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1358
1359 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001360 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001361
Chris Mason0b86a832008-03-24 15:01:56 -04001362 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1363 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001364out:
Chris Mason0b86a832008-03-24 15:01:56 -04001365 btrfs_free_path(path);
1366 return ret;
1367}
1368
Josef Bacik6df9a952013-06-27 13:22:46 -04001369static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001370{
Josef Bacik6df9a952013-06-27 13:22:46 -04001371 struct extent_map_tree *em_tree;
1372 struct extent_map *em;
1373 struct rb_node *n;
1374 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001375
Josef Bacik6df9a952013-06-27 13:22:46 -04001376 em_tree = &fs_info->mapping_tree.map_tree;
1377 read_lock(&em_tree->lock);
1378 n = rb_last(&em_tree->map);
1379 if (n) {
1380 em = rb_entry(n, struct extent_map, rb_node);
1381 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001382 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001383 read_unlock(&em_tree->lock);
1384
Chris Mason0b86a832008-03-24 15:01:56 -04001385 return ret;
1386}
1387
Ilya Dryomov53f10652013-08-12 14:33:01 +03001388static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1389 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001390{
1391 int ret;
1392 struct btrfs_key key;
1393 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001394 struct btrfs_path *path;
1395
Yan Zheng2b820322008-11-17 21:11:30 -05001396 path = btrfs_alloc_path();
1397 if (!path)
1398 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001399
1400 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1401 key.type = BTRFS_DEV_ITEM_KEY;
1402 key.offset = (u64)-1;
1403
Ilya Dryomov53f10652013-08-12 14:33:01 +03001404 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001405 if (ret < 0)
1406 goto error;
1407
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001408 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001409
Ilya Dryomov53f10652013-08-12 14:33:01 +03001410 ret = btrfs_previous_item(fs_info->chunk_root, path,
1411 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001412 BTRFS_DEV_ITEM_KEY);
1413 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001414 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001415 } else {
1416 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1417 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001418 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001419 }
1420 ret = 0;
1421error:
Yan Zheng2b820322008-11-17 21:11:30 -05001422 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001423 return ret;
1424}
1425
1426/*
1427 * the device information is stored in the chunk root
1428 * the btrfs_device struct should be fully filled in
1429 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001430static int btrfs_add_device(struct btrfs_trans_handle *trans,
1431 struct btrfs_root *root,
1432 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001433{
1434 int ret;
1435 struct btrfs_path *path;
1436 struct btrfs_dev_item *dev_item;
1437 struct extent_buffer *leaf;
1438 struct btrfs_key key;
1439 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001440
1441 root = root->fs_info->chunk_root;
1442
1443 path = btrfs_alloc_path();
1444 if (!path)
1445 return -ENOMEM;
1446
Chris Mason0b86a832008-03-24 15:01:56 -04001447 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1448 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001449 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001450
1451 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001452 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001453 if (ret)
1454 goto out;
1455
1456 leaf = path->nodes[0];
1457 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1458
1459 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001460 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001461 btrfs_set_device_type(leaf, dev_item, device->type);
1462 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1463 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1464 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001465 btrfs_set_device_total_bytes(leaf, dev_item,
1466 btrfs_device_get_disk_total_bytes(device));
1467 btrfs_set_device_bytes_used(leaf, dev_item,
1468 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001469 btrfs_set_device_group(leaf, dev_item, 0);
1470 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1471 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001472 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001473
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001474 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001475 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001476 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001477 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001478 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001479
Yan Zheng2b820322008-11-17 21:11:30 -05001480 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001481out:
1482 btrfs_free_path(path);
1483 return ret;
1484}
Chris Mason8f18cf12008-04-25 16:53:30 -04001485
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001486/*
1487 * Function to update ctime/mtime for a given device path.
1488 * Mainly used for ctime/mtime based probe like libblkid.
1489 */
1490static void update_dev_time(char *path_name)
1491{
1492 struct file *filp;
1493
1494 filp = filp_open(path_name, O_RDWR, 0);
1495 if (!filp)
1496 return;
1497 file_update_time(filp);
1498 filp_close(filp, NULL);
1499 return;
1500}
1501
Chris Masona061fc82008-05-07 11:43:44 -04001502static int btrfs_rm_dev_item(struct btrfs_root *root,
1503 struct btrfs_device *device)
1504{
1505 int ret;
1506 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001507 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001508 struct btrfs_trans_handle *trans;
1509
1510 root = root->fs_info->chunk_root;
1511
1512 path = btrfs_alloc_path();
1513 if (!path)
1514 return -ENOMEM;
1515
Yan, Zhenga22285a2010-05-16 10:48:46 -04001516 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001517 if (IS_ERR(trans)) {
1518 btrfs_free_path(path);
1519 return PTR_ERR(trans);
1520 }
Chris Masona061fc82008-05-07 11:43:44 -04001521 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1522 key.type = BTRFS_DEV_ITEM_KEY;
1523 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001524 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001525
1526 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1527 if (ret < 0)
1528 goto out;
1529
1530 if (ret > 0) {
1531 ret = -ENOENT;
1532 goto out;
1533 }
1534
1535 ret = btrfs_del_item(trans, root, path);
1536 if (ret)
1537 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001538out:
1539 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001540 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001541 btrfs_commit_transaction(trans, root);
1542 return ret;
1543}
1544
1545int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1546{
1547 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001548 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001549 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001550 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001551 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001552 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001553 u64 all_avail;
1554 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001555 u64 num_devices;
1556 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001557 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001558 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001559 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001560
Chris Masona061fc82008-05-07 11:43:44 -04001561 mutex_lock(&uuid_mutex);
1562
Miao Xiede98ced2013-01-29 10:13:12 +00001563 do {
1564 seq = read_seqbegin(&root->fs_info->profiles_lock);
1565
1566 all_avail = root->fs_info->avail_data_alloc_bits |
1567 root->fs_info->avail_system_alloc_bits |
1568 root->fs_info->avail_metadata_alloc_bits;
1569 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001570
Stefan Behrens8dabb742012-11-06 13:15:27 +01001571 num_devices = root->fs_info->fs_devices->num_devices;
1572 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1573 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1574 WARN_ON(num_devices < 1);
1575 num_devices--;
1576 }
1577 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1578
1579 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001580 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001581 goto out;
1582 }
1583
Stefan Behrens8dabb742012-11-06 13:15:27 +01001584 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001585 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001586 goto out;
1587 }
1588
David Woodhouse53b381b2013-01-29 18:40:14 -05001589 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1590 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001591 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001592 goto out;
1593 }
1594 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1595 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001596 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001597 goto out;
1598 }
1599
Chris Masondfe25022008-05-13 13:46:40 -04001600 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001601 struct list_head *devices;
1602 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001603
Chris Masondfe25022008-05-13 13:46:40 -04001604 device = NULL;
1605 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001606 /*
1607 * It is safe to read the devices since the volume_mutex
1608 * is held.
1609 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001610 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001611 if (tmp->in_fs_metadata &&
1612 !tmp->is_tgtdev_for_dev_replace &&
1613 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001614 device = tmp;
1615 break;
1616 }
1617 }
1618 bdev = NULL;
1619 bh = NULL;
1620 disk_super = NULL;
1621 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001622 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001623 goto out;
1624 }
Chris Masondfe25022008-05-13 13:46:40 -04001625 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001626 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001627 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001628 root->fs_info->bdev_holder, 0,
1629 &bdev, &bh);
1630 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001631 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001632 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001633 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001634 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001635 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001636 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001637 if (!device) {
1638 ret = -ENOENT;
1639 goto error_brelse;
1640 }
Chris Masondfe25022008-05-13 13:46:40 -04001641 }
Yan Zheng2b820322008-11-17 21:11:30 -05001642
Stefan Behrens63a212a2012-11-05 18:29:28 +01001643 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001644 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001645 goto error_brelse;
1646 }
1647
Yan Zheng2b820322008-11-17 21:11:30 -05001648 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001649 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001650 goto error_brelse;
1651 }
1652
1653 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001654 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001655 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001656 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001657 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001658 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001659 }
Chris Masona061fc82008-05-07 11:43:44 -04001660
Carey Underwoodd7901552013-03-04 16:37:06 -06001661 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001662 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001663 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001664 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001665 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001666
Stefan Behrens63a212a2012-11-05 18:29:28 +01001667 /*
1668 * TODO: the superblock still includes this device in its num_devices
1669 * counter although write_all_supers() is not locked out. This
1670 * could give a filesystem state which requires a degraded mount.
1671 */
Chris Masona061fc82008-05-07 11:43:44 -04001672 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1673 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001674 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001675
Yan Zheng2b820322008-11-17 21:11:30 -05001676 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001677 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001678
1679 /*
1680 * the device list mutex makes sure that we don't change
1681 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001682 * the device supers. Whoever is writing all supers, should
1683 * lock the device list mutex before getting the number of
1684 * devices in the super block (super_copy). Conversely,
1685 * whoever updates the number of devices in the super block
1686 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001687 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001688
1689 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001690 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001691 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001692
Yan Zhenge4404d62008-12-12 10:03:26 -05001693 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001694 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001695
Chris Masoncd02dca2010-12-13 14:56:23 -05001696 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001697 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001698
Yan Zheng2b820322008-11-17 21:11:30 -05001699 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1700 struct btrfs_device, dev_list);
1701 if (device->bdev == root->fs_info->sb->s_bdev)
1702 root->fs_info->sb->s_bdev = next_device->bdev;
1703 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1704 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1705
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001706 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001707 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001708 /* remove sysfs entry */
1709 btrfs_kobj_rm_device(root->fs_info, device);
1710 }
Anand Jain99994cd2014-06-03 11:36:00 +08001711
Xiao Guangrong1f781602011-04-20 10:09:16 +00001712 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001713
David Sterba6c417612011-04-13 15:41:04 +02001714 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1715 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001716 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001717
Xiao Guangrong1f781602011-04-20 10:09:16 +00001718 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001719 struct btrfs_fs_devices *fs_devices;
1720 fs_devices = root->fs_info->fs_devices;
1721 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001722 if (fs_devices->seed == cur_devices) {
1723 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001724 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001725 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001726 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001727 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001728 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001729 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001730 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001731 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001732 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001733 }
1734
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001735 root->fs_info->num_tolerated_disk_barrier_failures =
1736 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1737
Yan Zheng2b820322008-11-17 21:11:30 -05001738 /*
1739 * at this point, the device is zero sized. We want to
1740 * remove it from the devices list and zero out the old super
1741 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001742 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001743 u64 bytenr;
1744 int i;
1745
Chris Masondfe25022008-05-13 13:46:40 -04001746 /* make sure this device isn't detected as part of
1747 * the FS anymore
1748 */
1749 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1750 set_buffer_dirty(bh);
1751 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001752
1753 /* clear the mirror copies of super block on the disk
1754 * being removed, 0th copy is been taken care above and
1755 * the below would take of the rest
1756 */
1757 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1758 bytenr = btrfs_sb_offset(i);
1759 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1760 i_size_read(bdev->bd_inode))
1761 break;
1762
1763 brelse(bh);
1764 bh = __bread(bdev, bytenr / 4096,
1765 BTRFS_SUPER_INFO_SIZE);
1766 if (!bh)
1767 continue;
1768
1769 disk_super = (struct btrfs_super_block *)bh->b_data;
1770
1771 if (btrfs_super_bytenr(disk_super) != bytenr ||
1772 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1773 continue;
1774 }
1775 memset(&disk_super->magic, 0,
1776 sizeof(disk_super->magic));
1777 set_buffer_dirty(bh);
1778 sync_dirty_buffer(bh);
1779 }
Chris Masondfe25022008-05-13 13:46:40 -04001780 }
Chris Masona061fc82008-05-07 11:43:44 -04001781
Chris Masona061fc82008-05-07 11:43:44 -04001782 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001783
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001784 if (bdev) {
1785 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001786 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001787
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001788 /* Update ctime/mtime for device path for libblkid */
1789 update_dev_time(device_path);
1790 }
1791
Chris Masona061fc82008-05-07 11:43:44 -04001792error_brelse:
1793 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001794 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001795 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001796out:
1797 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001798 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001799error_undo:
1800 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001801 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001802 list_add(&device->dev_alloc_list,
1803 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001804 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001805 root->fs_info->fs_devices->rw_devices++;
1806 }
1807 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001808}
1809
Stefan Behrense93c89c2012-11-05 17:33:06 +01001810void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1811 struct btrfs_device *srcdev)
1812{
Anand Jaind51908c2014-08-13 14:24:19 +08001813 struct btrfs_fs_devices *fs_devices;
1814
Stefan Behrense93c89c2012-11-05 17:33:06 +01001815 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001816
Anand Jain25e8e912014-08-20 10:56:56 +08001817 /*
1818 * in case of fs with no seed, srcdev->fs_devices will point
1819 * to fs_devices of fs_info. However when the dev being replaced is
1820 * a seed dev it will point to the seed's local fs_devices. In short
1821 * srcdev will have its correct fs_devices in both the cases.
1822 */
1823 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08001824
Stefan Behrense93c89c2012-11-05 17:33:06 +01001825 list_del_rcu(&srcdev->dev_list);
1826 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08001827 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001828 if (srcdev->missing) {
Anand Jaind51908c2014-08-13 14:24:19 +08001829 fs_devices->missing_devices--;
Anand Jainb2efedc2014-08-13 14:24:24 +08001830 if (!fs_devices->seeding)
1831 fs_devices->rw_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001832 }
Miao Xie90180da2014-09-03 21:35:30 +08001833
Ilya Dryomov13572722013-10-02 20:41:01 +03001834 if (srcdev->bdev) {
Anand Jaind51908c2014-08-13 14:24:19 +08001835 fs_devices->open_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001836
Miao Xieff61d172014-07-24 11:37:06 +08001837 /*
1838 * zero out the old super if it is not writable
1839 * (e.g. seed device)
1840 */
1841 if (srcdev->writeable)
1842 btrfs_scratch_superblock(srcdev);
Ilya Dryomov13572722013-10-02 20:41:01 +03001843 }
1844
Stefan Behrense93c89c2012-11-05 17:33:06 +01001845 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001846
1847 /*
1848 * unless fs_devices is seed fs, num_devices shouldn't go
1849 * zero
1850 */
1851 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
1852
1853 /* if this is no devs we rather delete the fs_devices */
1854 if (!fs_devices->num_devices) {
1855 struct btrfs_fs_devices *tmp_fs_devices;
1856
1857 tmp_fs_devices = fs_info->fs_devices;
1858 while (tmp_fs_devices) {
1859 if (tmp_fs_devices->seed == fs_devices) {
1860 tmp_fs_devices->seed = fs_devices->seed;
1861 break;
1862 }
1863 tmp_fs_devices = tmp_fs_devices->seed;
1864 }
1865 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08001866 __btrfs_close_devices(fs_devices);
1867 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001868 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01001869}
1870
1871void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1872 struct btrfs_device *tgtdev)
1873{
1874 struct btrfs_device *next_device;
1875
1876 WARN_ON(!tgtdev);
1877 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1878 if (tgtdev->bdev) {
1879 btrfs_scratch_superblock(tgtdev);
1880 fs_info->fs_devices->open_devices--;
1881 }
1882 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001883
1884 next_device = list_entry(fs_info->fs_devices->devices.next,
1885 struct btrfs_device, dev_list);
1886 if (tgtdev->bdev == fs_info->sb->s_bdev)
1887 fs_info->sb->s_bdev = next_device->bdev;
1888 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1889 fs_info->fs_devices->latest_bdev = next_device->bdev;
1890 list_del_rcu(&tgtdev->dev_list);
1891
1892 call_rcu(&tgtdev->rcu, free_device);
1893
1894 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1895}
1896
Eric Sandeen48a3b632013-04-25 20:41:01 +00001897static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1898 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001899{
1900 int ret = 0;
1901 struct btrfs_super_block *disk_super;
1902 u64 devid;
1903 u8 *dev_uuid;
1904 struct block_device *bdev;
1905 struct buffer_head *bh;
1906
1907 *device = NULL;
1908 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1909 root->fs_info->bdev_holder, 0, &bdev, &bh);
1910 if (ret)
1911 return ret;
1912 disk_super = (struct btrfs_super_block *)bh->b_data;
1913 devid = btrfs_stack_device_id(&disk_super->dev_item);
1914 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001915 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001916 disk_super->fsid);
1917 brelse(bh);
1918 if (!*device)
1919 ret = -ENOENT;
1920 blkdev_put(bdev, FMODE_READ);
1921 return ret;
1922}
1923
1924int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1925 char *device_path,
1926 struct btrfs_device **device)
1927{
1928 *device = NULL;
1929 if (strcmp(device_path, "missing") == 0) {
1930 struct list_head *devices;
1931 struct btrfs_device *tmp;
1932
1933 devices = &root->fs_info->fs_devices->devices;
1934 /*
1935 * It is safe to read the devices since the volume_mutex
1936 * is held by the caller.
1937 */
1938 list_for_each_entry(tmp, devices, dev_list) {
1939 if (tmp->in_fs_metadata && !tmp->bdev) {
1940 *device = tmp;
1941 break;
1942 }
1943 }
1944
1945 if (!*device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001946 btrfs_err(root->fs_info, "no missing device found");
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001947 return -ENOENT;
1948 }
1949
1950 return 0;
1951 } else {
1952 return btrfs_find_device_by_path(root, device_path, device);
1953 }
1954}
1955
Yan Zheng2b820322008-11-17 21:11:30 -05001956/*
1957 * does all the dirty work required for changing file system's UUID.
1958 */
Li Zefan125ccb02011-12-08 15:07:24 +08001959static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001960{
1961 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1962 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001963 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001964 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001965 struct btrfs_device *device;
1966 u64 super_flags;
1967
1968 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001969 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001970 return -EINVAL;
1971
Ilya Dryomov2208a372013-08-12 14:33:03 +03001972 seed_devices = __alloc_fs_devices();
1973 if (IS_ERR(seed_devices))
1974 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001975
Yan Zhenge4404d62008-12-12 10:03:26 -05001976 old_devices = clone_fs_devices(fs_devices);
1977 if (IS_ERR(old_devices)) {
1978 kfree(seed_devices);
1979 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001980 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001981
Yan Zheng2b820322008-11-17 21:11:30 -05001982 list_add(&old_devices->list, &fs_uuids);
1983
Yan Zhenge4404d62008-12-12 10:03:26 -05001984 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1985 seed_devices->opened = 1;
1986 INIT_LIST_HEAD(&seed_devices->devices);
1987 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001988 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001989
1990 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001991 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1992 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001993
Yan Zhenge4404d62008-12-12 10:03:26 -05001994 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1995 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1996 device->fs_devices = seed_devices;
1997 }
1998
Yan Zheng2b820322008-11-17 21:11:30 -05001999 fs_devices->seeding = 0;
2000 fs_devices->num_devices = 0;
2001 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002002 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002003 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002004 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002005
2006 generate_random_uuid(fs_devices->fsid);
2007 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2008 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002009 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2010
Yan Zheng2b820322008-11-17 21:11:30 -05002011 super_flags = btrfs_super_flags(disk_super) &
2012 ~BTRFS_SUPER_FLAG_SEEDING;
2013 btrfs_set_super_flags(disk_super, super_flags);
2014
2015 return 0;
2016}
2017
2018/*
2019 * strore the expected generation for seed devices in device items.
2020 */
2021static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2022 struct btrfs_root *root)
2023{
2024 struct btrfs_path *path;
2025 struct extent_buffer *leaf;
2026 struct btrfs_dev_item *dev_item;
2027 struct btrfs_device *device;
2028 struct btrfs_key key;
2029 u8 fs_uuid[BTRFS_UUID_SIZE];
2030 u8 dev_uuid[BTRFS_UUID_SIZE];
2031 u64 devid;
2032 int ret;
2033
2034 path = btrfs_alloc_path();
2035 if (!path)
2036 return -ENOMEM;
2037
2038 root = root->fs_info->chunk_root;
2039 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2040 key.offset = 0;
2041 key.type = BTRFS_DEV_ITEM_KEY;
2042
2043 while (1) {
2044 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2045 if (ret < 0)
2046 goto error;
2047
2048 leaf = path->nodes[0];
2049next_slot:
2050 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2051 ret = btrfs_next_leaf(root, path);
2052 if (ret > 0)
2053 break;
2054 if (ret < 0)
2055 goto error;
2056 leaf = path->nodes[0];
2057 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002058 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002059 continue;
2060 }
2061
2062 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2063 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2064 key.type != BTRFS_DEV_ITEM_KEY)
2065 break;
2066
2067 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2068 struct btrfs_dev_item);
2069 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002070 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002071 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002072 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002073 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002074 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2075 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002076 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002077
2078 if (device->fs_devices->seeding) {
2079 btrfs_set_device_generation(leaf, dev_item,
2080 device->generation);
2081 btrfs_mark_buffer_dirty(leaf);
2082 }
2083
2084 path->slots[0]++;
2085 goto next_slot;
2086 }
2087 ret = 0;
2088error:
2089 btrfs_free_path(path);
2090 return ret;
2091}
2092
Chris Mason788f20e2008-04-28 15:29:42 -04002093int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2094{
Josef Bacikd5e20032011-08-04 14:52:27 +00002095 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002096 struct btrfs_trans_handle *trans;
2097 struct btrfs_device *device;
2098 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002099 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002100 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002101 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002102 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002103 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002104 int ret = 0;
2105
Yan Zheng2b820322008-11-17 21:11:30 -05002106 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002107 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002108
Li Zefana5d163332011-12-07 20:08:40 -05002109 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002110 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002111 if (IS_ERR(bdev))
2112 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002113
Yan Zheng2b820322008-11-17 21:11:30 -05002114 if (root->fs_info->fs_devices->seeding) {
2115 seeding_dev = 1;
2116 down_write(&sb->s_umount);
2117 mutex_lock(&uuid_mutex);
2118 }
2119
Chris Mason8c8bee12008-09-29 11:19:10 -04002120 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002121
Chris Mason788f20e2008-04-28 15:29:42 -04002122 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002123
2124 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002125 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002126 if (device->bdev == bdev) {
2127 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002128 mutex_unlock(
2129 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002130 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002131 }
2132 }
Liu Bod25628b2012-11-14 14:35:30 +00002133 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002134
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002135 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2136 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002137 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002138 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002139 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002140 }
2141
Josef Bacik606686e2012-06-04 14:03:51 -04002142 name = rcu_string_strdup(device_path, GFP_NOFS);
2143 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002144 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002145 ret = -ENOMEM;
2146 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002147 }
Josef Bacik606686e2012-06-04 14:03:51 -04002148 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002149
Yan, Zhenga22285a2010-05-16 10:48:46 -04002150 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002151 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002152 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002153 kfree(device);
2154 ret = PTR_ERR(trans);
2155 goto error;
2156 }
2157
Yan Zheng2b820322008-11-17 21:11:30 -05002158 lock_chunks(root);
2159
Josef Bacikd5e20032011-08-04 14:52:27 +00002160 q = bdev_get_queue(bdev);
2161 if (blk_queue_discard(q))
2162 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002163 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002164 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002165 device->io_width = root->sectorsize;
2166 device->io_align = root->sectorsize;
2167 device->sector_size = root->sectorsize;
2168 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002169 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002170 device->commit_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002171 device->dev_root = root->fs_info->dev_root;
2172 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002173 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002174 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002175 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002176 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002177 set_blocksize(device->bdev, 4096);
2178
Yan Zheng2b820322008-11-17 21:11:30 -05002179 if (seeding_dev) {
2180 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002181 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002182 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002183 }
2184
2185 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002186
Chris Masone5e9a522009-06-10 15:17:02 -04002187 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002188 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002189 list_add(&device->dev_alloc_list,
2190 &root->fs_info->fs_devices->alloc_list);
2191 root->fs_info->fs_devices->num_devices++;
2192 root->fs_info->fs_devices->open_devices++;
2193 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002194 root->fs_info->fs_devices->total_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -05002195 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2196
Josef Bacik2bf64752011-09-26 17:12:22 -04002197 spin_lock(&root->fs_info->free_chunk_lock);
2198 root->fs_info->free_chunk_space += device->total_bytes;
2199 spin_unlock(&root->fs_info->free_chunk_lock);
2200
Chris Masonc2898112009-06-10 09:51:32 -04002201 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2202 root->fs_info->fs_devices->rotating = 1;
2203
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002204 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002205 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002206 tmp + device->total_bytes);
Chris Mason788f20e2008-04-28 15:29:42 -04002207
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002208 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002209 btrfs_set_super_num_devices(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002210 tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002211
2212 /* add sysfs device entry */
2213 btrfs_kobj_add_device(root->fs_info, device);
2214
Chris Masone5e9a522009-06-10 15:17:02 -04002215 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002216
Yan Zheng2b820322008-11-17 21:11:30 -05002217 if (seeding_dev) {
Anand Jainb2373f22014-06-03 11:36:03 +08002218 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002219 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002220 if (ret) {
2221 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002222 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002223 }
Yan Zheng2b820322008-11-17 21:11:30 -05002224 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002225 if (ret) {
2226 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002227 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002228 }
Anand Jainb2373f22014-06-03 11:36:03 +08002229
2230 /* Sprouting would change fsid of the mounted root,
2231 * so rename the fsid on the sysfs
2232 */
2233 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2234 root->fs_info->fsid);
2235 if (kobject_rename(&root->fs_info->super_kobj, fsid_buf))
2236 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05002237 } else {
2238 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002239 if (ret) {
2240 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002241 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002242 }
Yan Zheng2b820322008-11-17 21:11:30 -05002243 }
2244
Chris Mason913d9522009-03-10 13:17:18 -04002245 /*
2246 * we've got more storage, clear any full flags on the space
2247 * infos
2248 */
2249 btrfs_clear_space_info_full(root->fs_info);
2250
Chris Mason7d9eb122008-07-08 14:19:17 -04002251 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002252 root->fs_info->num_tolerated_disk_barrier_failures =
2253 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002254 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002255
2256 if (seeding_dev) {
2257 mutex_unlock(&uuid_mutex);
2258 up_write(&sb->s_umount);
2259
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002260 if (ret) /* transaction commit */
2261 return ret;
2262
Yan Zheng2b820322008-11-17 21:11:30 -05002263 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002264 if (ret < 0)
2265 btrfs_error(root->fs_info, ret,
2266 "Failed to relocate sys chunks after "
2267 "device initialization. This can be fixed "
2268 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002269 trans = btrfs_attach_transaction(root);
2270 if (IS_ERR(trans)) {
2271 if (PTR_ERR(trans) == -ENOENT)
2272 return 0;
2273 return PTR_ERR(trans);
2274 }
2275 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002276 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002277
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002278 /* Update ctime/mtime for libblkid */
2279 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002280 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002281
2282error_trans:
2283 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002284 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002285 rcu_string_free(device->name);
Anand Jain0d393762014-06-03 11:36:01 +08002286 btrfs_kobj_rm_device(root->fs_info, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002287 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002288error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002289 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002290 if (seeding_dev) {
2291 mutex_unlock(&uuid_mutex);
2292 up_write(&sb->s_umount);
2293 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002294 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002295}
2296
Stefan Behrense93c89c2012-11-05 17:33:06 +01002297int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002298 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002299 struct btrfs_device **device_out)
2300{
2301 struct request_queue *q;
2302 struct btrfs_device *device;
2303 struct block_device *bdev;
2304 struct btrfs_fs_info *fs_info = root->fs_info;
2305 struct list_head *devices;
2306 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002307 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002308 int ret = 0;
2309
2310 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002311 if (fs_info->fs_devices->seeding) {
2312 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002313 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002314 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002315
2316 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2317 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002318 if (IS_ERR(bdev)) {
2319 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002320 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002321 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002322
2323 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2324
2325 devices = &fs_info->fs_devices->devices;
2326 list_for_each_entry(device, devices, dev_list) {
2327 if (device->bdev == bdev) {
Miao Xie1c433662014-09-03 21:35:32 +08002328 btrfs_err(fs_info, "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002329 ret = -EEXIST;
2330 goto error;
2331 }
2332 }
2333
Miao Xie1c433662014-09-03 21:35:32 +08002334
Miao Xie7cc8e582014-09-03 21:35:38 +08002335 if (i_size_read(bdev->bd_inode) <
2336 btrfs_device_get_total_bytes(srcdev)) {
Miao Xie1c433662014-09-03 21:35:32 +08002337 btrfs_err(fs_info, "target device is smaller than source device!");
2338 ret = -EINVAL;
2339 goto error;
2340 }
2341
2342
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002343 device = btrfs_alloc_device(NULL, &devid, NULL);
2344 if (IS_ERR(device)) {
2345 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002346 goto error;
2347 }
2348
2349 name = rcu_string_strdup(device_path, GFP_NOFS);
2350 if (!name) {
2351 kfree(device);
2352 ret = -ENOMEM;
2353 goto error;
2354 }
2355 rcu_assign_pointer(device->name, name);
2356
2357 q = bdev_get_queue(bdev);
2358 if (blk_queue_discard(q))
2359 device->can_discard = 1;
2360 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2361 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002362 device->generation = 0;
2363 device->io_width = root->sectorsize;
2364 device->io_align = root->sectorsize;
2365 device->sector_size = root->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002366 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2367 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2368 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002369 ASSERT(list_empty(&srcdev->resized_list));
2370 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002371 device->commit_bytes_used = device->bytes_used;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002372 device->dev_root = fs_info->dev_root;
2373 device->bdev = bdev;
2374 device->in_fs_metadata = 1;
2375 device->is_tgtdev_for_dev_replace = 1;
2376 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002377 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002378 set_blocksize(device->bdev, 4096);
2379 device->fs_devices = fs_info->fs_devices;
2380 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2381 fs_info->fs_devices->num_devices++;
2382 fs_info->fs_devices->open_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002383 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2384
2385 *device_out = device;
2386 return ret;
2387
2388error:
2389 blkdev_put(bdev, FMODE_EXCL);
2390 return ret;
2391}
2392
2393void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2394 struct btrfs_device *tgtdev)
2395{
2396 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2397 tgtdev->io_width = fs_info->dev_root->sectorsize;
2398 tgtdev->io_align = fs_info->dev_root->sectorsize;
2399 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2400 tgtdev->dev_root = fs_info->dev_root;
2401 tgtdev->in_fs_metadata = 1;
2402}
2403
Chris Masond3977122009-01-05 21:25:51 -05002404static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2405 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002406{
2407 int ret;
2408 struct btrfs_path *path;
2409 struct btrfs_root *root;
2410 struct btrfs_dev_item *dev_item;
2411 struct extent_buffer *leaf;
2412 struct btrfs_key key;
2413
2414 root = device->dev_root->fs_info->chunk_root;
2415
2416 path = btrfs_alloc_path();
2417 if (!path)
2418 return -ENOMEM;
2419
2420 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2421 key.type = BTRFS_DEV_ITEM_KEY;
2422 key.offset = device->devid;
2423
2424 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2425 if (ret < 0)
2426 goto out;
2427
2428 if (ret > 0) {
2429 ret = -ENOENT;
2430 goto out;
2431 }
2432
2433 leaf = path->nodes[0];
2434 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2435
2436 btrfs_set_device_id(leaf, dev_item, device->devid);
2437 btrfs_set_device_type(leaf, dev_item, device->type);
2438 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2439 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2440 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002441 btrfs_set_device_total_bytes(leaf, dev_item,
2442 btrfs_device_get_disk_total_bytes(device));
2443 btrfs_set_device_bytes_used(leaf, dev_item,
2444 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002445 btrfs_mark_buffer_dirty(leaf);
2446
2447out:
2448 btrfs_free_path(path);
2449 return ret;
2450}
2451
Chris Mason7d9eb122008-07-08 14:19:17 -04002452static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002453 struct btrfs_device *device, u64 new_size)
2454{
2455 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002456 device->dev_root->fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002457 struct btrfs_fs_devices *fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002458 u64 old_total = btrfs_super_total_bytes(super_copy);
2459 u64 diff = new_size - device->total_bytes;
2460
Yan Zheng2b820322008-11-17 21:11:30 -05002461 if (!device->writeable)
2462 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002463 if (new_size <= device->total_bytes ||
2464 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002465 return -EINVAL;
2466
Miao Xie935e5cc2014-09-03 21:35:33 +08002467 fs_devices = device->dev_root->fs_info->fs_devices;
2468
Chris Mason8f18cf12008-04-25 16:53:30 -04002469 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002470 device->fs_devices->total_rw_bytes += diff;
2471
Miao Xie7cc8e582014-09-03 21:35:38 +08002472 btrfs_device_set_total_bytes(device, new_size);
2473 btrfs_device_set_disk_total_bytes(device, new_size);
Chris Mason4184ea72009-03-10 12:39:20 -04002474 btrfs_clear_space_info_full(device->dev_root->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002475 if (list_empty(&device->resized_list))
2476 list_add_tail(&device->resized_list,
2477 &fs_devices->resized_devices);
Chris Mason4184ea72009-03-10 12:39:20 -04002478
Chris Mason8f18cf12008-04-25 16:53:30 -04002479 return btrfs_update_device(trans, device);
2480}
2481
Chris Mason7d9eb122008-07-08 14:19:17 -04002482int btrfs_grow_device(struct btrfs_trans_handle *trans,
2483 struct btrfs_device *device, u64 new_size)
2484{
2485 int ret;
2486 lock_chunks(device->dev_root);
2487 ret = __btrfs_grow_device(trans, device, new_size);
2488 unlock_chunks(device->dev_root);
2489 return ret;
2490}
2491
Chris Mason8f18cf12008-04-25 16:53:30 -04002492static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2493 struct btrfs_root *root,
2494 u64 chunk_tree, u64 chunk_objectid,
2495 u64 chunk_offset)
2496{
2497 int ret;
2498 struct btrfs_path *path;
2499 struct btrfs_key key;
2500
2501 root = root->fs_info->chunk_root;
2502 path = btrfs_alloc_path();
2503 if (!path)
2504 return -ENOMEM;
2505
2506 key.objectid = chunk_objectid;
2507 key.offset = chunk_offset;
2508 key.type = BTRFS_CHUNK_ITEM_KEY;
2509
2510 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002511 if (ret < 0)
2512 goto out;
2513 else if (ret > 0) { /* Logic error or corruption */
2514 btrfs_error(root->fs_info, -ENOENT,
2515 "Failed lookup while freeing chunk.");
2516 ret = -ENOENT;
2517 goto out;
2518 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002519
2520 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002521 if (ret < 0)
2522 btrfs_error(root->fs_info, ret,
2523 "Failed to delete chunk item.");
2524out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002525 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002526 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002527}
2528
Christoph Hellwigb2950862008-12-02 09:54:17 -05002529static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002530 chunk_offset)
2531{
David Sterba6c417612011-04-13 15:41:04 +02002532 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002533 struct btrfs_disk_key *disk_key;
2534 struct btrfs_chunk *chunk;
2535 u8 *ptr;
2536 int ret = 0;
2537 u32 num_stripes;
2538 u32 array_size;
2539 u32 len = 0;
2540 u32 cur;
2541 struct btrfs_key key;
2542
2543 array_size = btrfs_super_sys_array_size(super_copy);
2544
2545 ptr = super_copy->sys_chunk_array;
2546 cur = 0;
2547
2548 while (cur < array_size) {
2549 disk_key = (struct btrfs_disk_key *)ptr;
2550 btrfs_disk_key_to_cpu(&key, disk_key);
2551
2552 len = sizeof(*disk_key);
2553
2554 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2555 chunk = (struct btrfs_chunk *)(ptr + len);
2556 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2557 len += btrfs_chunk_item_size(num_stripes);
2558 } else {
2559 ret = -EIO;
2560 break;
2561 }
2562 if (key.objectid == chunk_objectid &&
2563 key.offset == chunk_offset) {
2564 memmove(ptr, ptr + len, array_size - (cur + len));
2565 array_size -= len;
2566 btrfs_set_super_sys_array_size(super_copy, array_size);
2567 } else {
2568 ptr += len;
2569 cur += len;
2570 }
2571 }
2572 return ret;
2573}
2574
Christoph Hellwigb2950862008-12-02 09:54:17 -05002575static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002576 u64 chunk_tree, u64 chunk_objectid,
2577 u64 chunk_offset)
2578{
2579 struct extent_map_tree *em_tree;
2580 struct btrfs_root *extent_root;
2581 struct btrfs_trans_handle *trans;
2582 struct extent_map *em;
2583 struct map_lookup *map;
2584 int ret;
2585 int i;
2586
2587 root = root->fs_info->chunk_root;
2588 extent_root = root->fs_info->extent_root;
2589 em_tree = &root->fs_info->mapping_tree.map_tree;
2590
Josef Bacikba1bf482009-09-11 16:11:19 -04002591 ret = btrfs_can_relocate(extent_root, chunk_offset);
2592 if (ret)
2593 return -ENOSPC;
2594
Chris Mason8f18cf12008-04-25 16:53:30 -04002595 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002596 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002597 if (ret)
2598 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002599
Yan, Zhenga22285a2010-05-16 10:48:46 -04002600 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002601 if (IS_ERR(trans)) {
2602 ret = PTR_ERR(trans);
2603 btrfs_std_error(root->fs_info, ret);
2604 return ret;
2605 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002606
Chris Mason7d9eb122008-07-08 14:19:17 -04002607 lock_chunks(root);
2608
Chris Mason8f18cf12008-04-25 16:53:30 -04002609 /*
2610 * step two, delete the device extents and the
2611 * chunk tree entries
2612 */
Chris Mason890871b2009-09-02 16:24:52 -04002613 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002614 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002615 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002616
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002617 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002618 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002619 map = (struct map_lookup *)em->bdev;
2620
2621 for (i = 0; i < map->num_stripes; i++) {
2622 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2623 map->stripes[i].physical);
2624 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002625
Chris Masondfe25022008-05-13 13:46:40 -04002626 if (map->stripes[i].dev) {
2627 ret = btrfs_update_device(trans, map->stripes[i].dev);
2628 BUG_ON(ret);
2629 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002630 }
2631 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2632 chunk_offset);
2633
2634 BUG_ON(ret);
2635
liubo1abe9b82011-03-24 11:18:59 +00002636 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2637
Chris Mason8f18cf12008-04-25 16:53:30 -04002638 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2639 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2640 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002641 }
2642
Zheng Yan1a40e232008-09-26 10:09:34 -04002643 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2644 BUG_ON(ret);
2645
Chris Mason890871b2009-09-02 16:24:52 -04002646 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002647 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002648 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002649
Chris Mason8f18cf12008-04-25 16:53:30 -04002650 /* once for the tree */
2651 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002652 /* once for us */
2653 free_extent_map(em);
2654
Chris Mason7d9eb122008-07-08 14:19:17 -04002655 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002656 btrfs_end_transaction(trans, root);
2657 return 0;
2658}
2659
Yan Zheng2b820322008-11-17 21:11:30 -05002660static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2661{
2662 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2663 struct btrfs_path *path;
2664 struct extent_buffer *leaf;
2665 struct btrfs_chunk *chunk;
2666 struct btrfs_key key;
2667 struct btrfs_key found_key;
2668 u64 chunk_tree = chunk_root->root_key.objectid;
2669 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002670 bool retried = false;
2671 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002672 int ret;
2673
2674 path = btrfs_alloc_path();
2675 if (!path)
2676 return -ENOMEM;
2677
Josef Bacikba1bf482009-09-11 16:11:19 -04002678again:
Yan Zheng2b820322008-11-17 21:11:30 -05002679 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2680 key.offset = (u64)-1;
2681 key.type = BTRFS_CHUNK_ITEM_KEY;
2682
2683 while (1) {
2684 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2685 if (ret < 0)
2686 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002687 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002688
2689 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2690 key.type);
2691 if (ret < 0)
2692 goto error;
2693 if (ret > 0)
2694 break;
2695
2696 leaf = path->nodes[0];
2697 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2698
2699 chunk = btrfs_item_ptr(leaf, path->slots[0],
2700 struct btrfs_chunk);
2701 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002702 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002703
2704 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2705 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2706 found_key.objectid,
2707 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002708 if (ret == -ENOSPC)
2709 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302710 else
2711 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002712 }
2713
2714 if (found_key.offset == 0)
2715 break;
2716 key.offset = found_key.offset - 1;
2717 }
2718 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002719 if (failed && !retried) {
2720 failed = 0;
2721 retried = true;
2722 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302723 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002724 ret = -ENOSPC;
2725 }
Yan Zheng2b820322008-11-17 21:11:30 -05002726error:
2727 btrfs_free_path(path);
2728 return ret;
2729}
2730
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002731static int insert_balance_item(struct btrfs_root *root,
2732 struct btrfs_balance_control *bctl)
2733{
2734 struct btrfs_trans_handle *trans;
2735 struct btrfs_balance_item *item;
2736 struct btrfs_disk_balance_args disk_bargs;
2737 struct btrfs_path *path;
2738 struct extent_buffer *leaf;
2739 struct btrfs_key key;
2740 int ret, err;
2741
2742 path = btrfs_alloc_path();
2743 if (!path)
2744 return -ENOMEM;
2745
2746 trans = btrfs_start_transaction(root, 0);
2747 if (IS_ERR(trans)) {
2748 btrfs_free_path(path);
2749 return PTR_ERR(trans);
2750 }
2751
2752 key.objectid = BTRFS_BALANCE_OBJECTID;
2753 key.type = BTRFS_BALANCE_ITEM_KEY;
2754 key.offset = 0;
2755
2756 ret = btrfs_insert_empty_item(trans, root, path, &key,
2757 sizeof(*item));
2758 if (ret)
2759 goto out;
2760
2761 leaf = path->nodes[0];
2762 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2763
2764 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2765
2766 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2767 btrfs_set_balance_data(leaf, item, &disk_bargs);
2768 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2769 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2770 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2771 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2772
2773 btrfs_set_balance_flags(leaf, item, bctl->flags);
2774
2775 btrfs_mark_buffer_dirty(leaf);
2776out:
2777 btrfs_free_path(path);
2778 err = btrfs_commit_transaction(trans, root);
2779 if (err && !ret)
2780 ret = err;
2781 return ret;
2782}
2783
2784static int del_balance_item(struct btrfs_root *root)
2785{
2786 struct btrfs_trans_handle *trans;
2787 struct btrfs_path *path;
2788 struct btrfs_key key;
2789 int ret, err;
2790
2791 path = btrfs_alloc_path();
2792 if (!path)
2793 return -ENOMEM;
2794
2795 trans = btrfs_start_transaction(root, 0);
2796 if (IS_ERR(trans)) {
2797 btrfs_free_path(path);
2798 return PTR_ERR(trans);
2799 }
2800
2801 key.objectid = BTRFS_BALANCE_OBJECTID;
2802 key.type = BTRFS_BALANCE_ITEM_KEY;
2803 key.offset = 0;
2804
2805 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2806 if (ret < 0)
2807 goto out;
2808 if (ret > 0) {
2809 ret = -ENOENT;
2810 goto out;
2811 }
2812
2813 ret = btrfs_del_item(trans, root, path);
2814out:
2815 btrfs_free_path(path);
2816 err = btrfs_commit_transaction(trans, root);
2817 if (err && !ret)
2818 ret = err;
2819 return ret;
2820}
2821
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002822/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002823 * This is a heuristic used to reduce the number of chunks balanced on
2824 * resume after balance was interrupted.
2825 */
2826static void update_balance_args(struct btrfs_balance_control *bctl)
2827{
2828 /*
2829 * Turn on soft mode for chunk types that were being converted.
2830 */
2831 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2832 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2833 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2834 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2835 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2836 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2837
2838 /*
2839 * Turn on usage filter if is not already used. The idea is
2840 * that chunks that we have already balanced should be
2841 * reasonably full. Don't do it for chunks that are being
2842 * converted - that will keep us from relocating unconverted
2843 * (albeit full) chunks.
2844 */
2845 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2846 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2847 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2848 bctl->data.usage = 90;
2849 }
2850 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2851 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2852 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2853 bctl->sys.usage = 90;
2854 }
2855 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2856 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2857 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2858 bctl->meta.usage = 90;
2859 }
2860}
2861
2862/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002863 * Should be called with both balance and volume mutexes held to
2864 * serialize other volume operations (add_dev/rm_dev/resize) with
2865 * restriper. Same goes for unset_balance_control.
2866 */
2867static void set_balance_control(struct btrfs_balance_control *bctl)
2868{
2869 struct btrfs_fs_info *fs_info = bctl->fs_info;
2870
2871 BUG_ON(fs_info->balance_ctl);
2872
2873 spin_lock(&fs_info->balance_lock);
2874 fs_info->balance_ctl = bctl;
2875 spin_unlock(&fs_info->balance_lock);
2876}
2877
2878static void unset_balance_control(struct btrfs_fs_info *fs_info)
2879{
2880 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2881
2882 BUG_ON(!fs_info->balance_ctl);
2883
2884 spin_lock(&fs_info->balance_lock);
2885 fs_info->balance_ctl = NULL;
2886 spin_unlock(&fs_info->balance_lock);
2887
2888 kfree(bctl);
2889}
2890
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002891/*
2892 * Balance filters. Return 1 if chunk should be filtered out
2893 * (should not be balanced).
2894 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002895static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002896 struct btrfs_balance_args *bargs)
2897{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002898 chunk_type = chunk_to_extended(chunk_type) &
2899 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002900
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002901 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002902 return 0;
2903
2904 return 1;
2905}
2906
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002907static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2908 struct btrfs_balance_args *bargs)
2909{
2910 struct btrfs_block_group_cache *cache;
2911 u64 chunk_used, user_thresh;
2912 int ret = 1;
2913
2914 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2915 chunk_used = btrfs_block_group_used(&cache->item);
2916
Ilya Dryomova105bb82013-01-21 15:15:56 +02002917 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002918 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002919 else if (bargs->usage > 100)
2920 user_thresh = cache->key.offset;
2921 else
2922 user_thresh = div_factor_fine(cache->key.offset,
2923 bargs->usage);
2924
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002925 if (chunk_used < user_thresh)
2926 ret = 0;
2927
2928 btrfs_put_block_group(cache);
2929 return ret;
2930}
2931
Ilya Dryomov409d4042012-01-16 22:04:47 +02002932static int chunk_devid_filter(struct extent_buffer *leaf,
2933 struct btrfs_chunk *chunk,
2934 struct btrfs_balance_args *bargs)
2935{
2936 struct btrfs_stripe *stripe;
2937 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2938 int i;
2939
2940 for (i = 0; i < num_stripes; i++) {
2941 stripe = btrfs_stripe_nr(chunk, i);
2942 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2943 return 0;
2944 }
2945
2946 return 1;
2947}
2948
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002949/* [pstart, pend) */
2950static int chunk_drange_filter(struct extent_buffer *leaf,
2951 struct btrfs_chunk *chunk,
2952 u64 chunk_offset,
2953 struct btrfs_balance_args *bargs)
2954{
2955 struct btrfs_stripe *stripe;
2956 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2957 u64 stripe_offset;
2958 u64 stripe_length;
2959 int factor;
2960 int i;
2961
2962 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2963 return 0;
2964
2965 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002966 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2967 factor = num_stripes / 2;
2968 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2969 factor = num_stripes - 1;
2970 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2971 factor = num_stripes - 2;
2972 } else {
2973 factor = num_stripes;
2974 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002975
2976 for (i = 0; i < num_stripes; i++) {
2977 stripe = btrfs_stripe_nr(chunk, i);
2978 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2979 continue;
2980
2981 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2982 stripe_length = btrfs_chunk_length(leaf, chunk);
2983 do_div(stripe_length, factor);
2984
2985 if (stripe_offset < bargs->pend &&
2986 stripe_offset + stripe_length > bargs->pstart)
2987 return 0;
2988 }
2989
2990 return 1;
2991}
2992
Ilya Dryomovea671762012-01-16 22:04:48 +02002993/* [vstart, vend) */
2994static int chunk_vrange_filter(struct extent_buffer *leaf,
2995 struct btrfs_chunk *chunk,
2996 u64 chunk_offset,
2997 struct btrfs_balance_args *bargs)
2998{
2999 if (chunk_offset < bargs->vend &&
3000 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3001 /* at least part of the chunk is inside this vrange */
3002 return 0;
3003
3004 return 1;
3005}
3006
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003007static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003008 struct btrfs_balance_args *bargs)
3009{
3010 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3011 return 0;
3012
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003013 chunk_type = chunk_to_extended(chunk_type) &
3014 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003015
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003016 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003017 return 1;
3018
3019 return 0;
3020}
3021
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003022static int should_balance_chunk(struct btrfs_root *root,
3023 struct extent_buffer *leaf,
3024 struct btrfs_chunk *chunk, u64 chunk_offset)
3025{
3026 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3027 struct btrfs_balance_args *bargs = NULL;
3028 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3029
3030 /* type filter */
3031 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3032 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3033 return 0;
3034 }
3035
3036 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3037 bargs = &bctl->data;
3038 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3039 bargs = &bctl->sys;
3040 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3041 bargs = &bctl->meta;
3042
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003043 /* profiles filter */
3044 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3045 chunk_profiles_filter(chunk_type, bargs)) {
3046 return 0;
3047 }
3048
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003049 /* usage filter */
3050 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3051 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3052 return 0;
3053 }
3054
Ilya Dryomov409d4042012-01-16 22:04:47 +02003055 /* devid filter */
3056 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3057 chunk_devid_filter(leaf, chunk, bargs)) {
3058 return 0;
3059 }
3060
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003061 /* drange filter, makes sense only with devid filter */
3062 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3063 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3064 return 0;
3065 }
3066
Ilya Dryomovea671762012-01-16 22:04:48 +02003067 /* vrange filter */
3068 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3069 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3070 return 0;
3071 }
3072
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003073 /* soft profile changing mode */
3074 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3075 chunk_soft_convert_filter(chunk_type, bargs)) {
3076 return 0;
3077 }
3078
David Sterba7d824b62014-05-07 17:37:51 +02003079 /*
3080 * limited by count, must be the last filter
3081 */
3082 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3083 if (bargs->limit == 0)
3084 return 0;
3085 else
3086 bargs->limit--;
3087 }
3088
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003089 return 1;
3090}
3091
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003092static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003093{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003094 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003095 struct btrfs_root *chunk_root = fs_info->chunk_root;
3096 struct btrfs_root *dev_root = fs_info->dev_root;
3097 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003098 struct btrfs_device *device;
3099 u64 old_size;
3100 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003101 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003102 struct btrfs_path *path;
3103 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003104 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003105 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003106 struct extent_buffer *leaf;
3107 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003108 int ret;
3109 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003110 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003111 u64 limit_data = bctl->data.limit;
3112 u64 limit_meta = bctl->meta.limit;
3113 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003114
Chris Masonec44a352008-04-28 15:29:52 -04003115 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003116 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003117 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003118 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003119 size_to_free = div_factor(old_size, 1);
3120 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003121 if (!device->writeable ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003122 btrfs_device_get_total_bytes(device) -
3123 btrfs_device_get_bytes_used(device) > size_to_free ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003124 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003125 continue;
3126
3127 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003128 if (ret == -ENOSPC)
3129 break;
Chris Masonec44a352008-04-28 15:29:52 -04003130 BUG_ON(ret);
3131
Yan, Zhenga22285a2010-05-16 10:48:46 -04003132 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003133 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003134
3135 ret = btrfs_grow_device(trans, device, old_size);
3136 BUG_ON(ret);
3137
3138 btrfs_end_transaction(trans, dev_root);
3139 }
3140
3141 /* step two, relocate all the chunks */
3142 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003143 if (!path) {
3144 ret = -ENOMEM;
3145 goto error;
3146 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003147
3148 /* zero out stat counters */
3149 spin_lock(&fs_info->balance_lock);
3150 memset(&bctl->stat, 0, sizeof(bctl->stat));
3151 spin_unlock(&fs_info->balance_lock);
3152again:
David Sterba7d824b62014-05-07 17:37:51 +02003153 if (!counting) {
3154 bctl->data.limit = limit_data;
3155 bctl->meta.limit = limit_meta;
3156 bctl->sys.limit = limit_sys;
3157 }
Chris Masonec44a352008-04-28 15:29:52 -04003158 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3159 key.offset = (u64)-1;
3160 key.type = BTRFS_CHUNK_ITEM_KEY;
3161
Chris Masond3977122009-01-05 21:25:51 -05003162 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003163 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003164 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003165 ret = -ECANCELED;
3166 goto error;
3167 }
3168
Chris Masonec44a352008-04-28 15:29:52 -04003169 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3170 if (ret < 0)
3171 goto error;
3172
3173 /*
3174 * this shouldn't happen, it means the last relocate
3175 * failed
3176 */
3177 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003178 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003179
3180 ret = btrfs_previous_item(chunk_root, path, 0,
3181 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003182 if (ret) {
3183 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003184 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003185 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003186
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003187 leaf = path->nodes[0];
3188 slot = path->slots[0];
3189 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3190
Chris Masonec44a352008-04-28 15:29:52 -04003191 if (found_key.objectid != key.objectid)
3192 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04003193
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003194 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3195
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003196 if (!counting) {
3197 spin_lock(&fs_info->balance_lock);
3198 bctl->stat.considered++;
3199 spin_unlock(&fs_info->balance_lock);
3200 }
3201
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003202 ret = should_balance_chunk(chunk_root, leaf, chunk,
3203 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003204 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003205 if (!ret)
3206 goto loop;
3207
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003208 if (counting) {
3209 spin_lock(&fs_info->balance_lock);
3210 bctl->stat.expected++;
3211 spin_unlock(&fs_info->balance_lock);
3212 goto loop;
3213 }
3214
Chris Masonec44a352008-04-28 15:29:52 -04003215 ret = btrfs_relocate_chunk(chunk_root,
3216 chunk_root->root_key.objectid,
3217 found_key.objectid,
3218 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003219 if (ret && ret != -ENOSPC)
3220 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003221 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003222 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003223 } else {
3224 spin_lock(&fs_info->balance_lock);
3225 bctl->stat.completed++;
3226 spin_unlock(&fs_info->balance_lock);
3227 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003228loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003229 if (found_key.offset == 0)
3230 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003231 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003232 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003233
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003234 if (counting) {
3235 btrfs_release_path(path);
3236 counting = false;
3237 goto again;
3238 }
Chris Masonec44a352008-04-28 15:29:52 -04003239error:
3240 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003241 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003242 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003243 enospc_errors);
3244 if (!ret)
3245 ret = -ENOSPC;
3246 }
3247
Chris Masonec44a352008-04-28 15:29:52 -04003248 return ret;
3249}
3250
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003251/**
3252 * alloc_profile_is_valid - see if a given profile is valid and reduced
3253 * @flags: profile to validate
3254 * @extended: if true @flags is treated as an extended profile
3255 */
3256static int alloc_profile_is_valid(u64 flags, int extended)
3257{
3258 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3259 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3260
3261 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3262
3263 /* 1) check that all other bits are zeroed */
3264 if (flags & ~mask)
3265 return 0;
3266
3267 /* 2) see if profile is reduced */
3268 if (flags == 0)
3269 return !extended; /* "0" is valid for usual profiles */
3270
3271 /* true if exactly one bit set */
3272 return (flags & (flags - 1)) == 0;
3273}
3274
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003275static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3276{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003277 /* cancel requested || normal exit path */
3278 return atomic_read(&fs_info->balance_cancel_req) ||
3279 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3280 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003281}
3282
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003283static void __cancel_balance(struct btrfs_fs_info *fs_info)
3284{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003285 int ret;
3286
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003287 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003288 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003289 if (ret)
3290 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003291
3292 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003293}
3294
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003295/*
3296 * Should be called with both balance and volume mutexes held
3297 */
3298int btrfs_balance(struct btrfs_balance_control *bctl,
3299 struct btrfs_ioctl_balance_args *bargs)
3300{
3301 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003302 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003303 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003304 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003305 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003306 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003307
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003308 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003309 atomic_read(&fs_info->balance_pause_req) ||
3310 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003311 ret = -EINVAL;
3312 goto out;
3313 }
3314
Ilya Dryomove4837f82012-03-27 17:09:17 +03003315 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3316 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3317 mixed = 1;
3318
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003319 /*
3320 * In case of mixed groups both data and meta should be picked,
3321 * and identical options should be given for both of them.
3322 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003323 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3324 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003325 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3326 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3327 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003328 btrfs_err(fs_info, "with mixed groups data and "
3329 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003330 ret = -EINVAL;
3331 goto out;
3332 }
3333 }
3334
Stefan Behrens8dabb742012-11-06 13:15:27 +01003335 num_devices = fs_info->fs_devices->num_devices;
3336 btrfs_dev_replace_lock(&fs_info->dev_replace);
3337 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3338 BUG_ON(num_devices < 1);
3339 num_devices--;
3340 }
3341 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003342 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003343 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003344 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003345 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003346 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003347 if (num_devices > 2)
3348 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3349 if (num_devices > 3)
3350 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3351 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003352 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3353 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3354 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003355 btrfs_err(fs_info, "unable to start balance with target "
3356 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003357 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003358 ret = -EINVAL;
3359 goto out;
3360 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003361 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3362 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3363 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003364 btrfs_err(fs_info,
3365 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003366 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003367 ret = -EINVAL;
3368 goto out;
3369 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003370 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3371 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3372 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003373 btrfs_err(fs_info,
3374 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003375 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003376 ret = -EINVAL;
3377 goto out;
3378 }
3379
Ilya Dryomove4837f82012-03-27 17:09:17 +03003380 /* allow dup'ed data chunks only in mixed mode */
3381 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003382 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003383 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003384 ret = -EINVAL;
3385 goto out;
3386 }
3387
3388 /* allow to reduce meta or sys integrity only if force set */
3389 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003390 BTRFS_BLOCK_GROUP_RAID10 |
3391 BTRFS_BLOCK_GROUP_RAID5 |
3392 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003393 do {
3394 seq = read_seqbegin(&fs_info->profiles_lock);
3395
3396 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3397 (fs_info->avail_system_alloc_bits & allowed) &&
3398 !(bctl->sys.target & allowed)) ||
3399 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3400 (fs_info->avail_metadata_alloc_bits & allowed) &&
3401 !(bctl->meta.target & allowed))) {
3402 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003403 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003404 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003405 btrfs_err(fs_info, "balance will reduce metadata "
3406 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003407 ret = -EINVAL;
3408 goto out;
3409 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003410 }
Miao Xiede98ced2013-01-29 10:13:12 +00003411 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003412
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003413 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3414 int num_tolerated_disk_barrier_failures;
3415 u64 target = bctl->sys.target;
3416
3417 num_tolerated_disk_barrier_failures =
3418 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3419 if (num_tolerated_disk_barrier_failures > 0 &&
3420 (target &
3421 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3422 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3423 num_tolerated_disk_barrier_failures = 0;
3424 else if (num_tolerated_disk_barrier_failures > 1 &&
3425 (target &
3426 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3427 num_tolerated_disk_barrier_failures = 1;
3428
3429 fs_info->num_tolerated_disk_barrier_failures =
3430 num_tolerated_disk_barrier_failures;
3431 }
3432
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003433 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003434 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003435 goto out;
3436
Ilya Dryomov59641012012-01-16 22:04:48 +02003437 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3438 BUG_ON(ret == -EEXIST);
3439 set_balance_control(bctl);
3440 } else {
3441 BUG_ON(ret != -EEXIST);
3442 spin_lock(&fs_info->balance_lock);
3443 update_balance_args(bctl);
3444 spin_unlock(&fs_info->balance_lock);
3445 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003446
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003447 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003448 mutex_unlock(&fs_info->balance_mutex);
3449
3450 ret = __btrfs_balance(fs_info);
3451
3452 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003453 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003454
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003455 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3456 fs_info->num_tolerated_disk_barrier_failures =
3457 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3458 }
3459
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003460 if (bargs) {
3461 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003462 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003463 }
3464
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003465 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3466 balance_need_close(fs_info)) {
3467 __cancel_balance(fs_info);
3468 }
3469
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003470 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003471
3472 return ret;
3473out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003474 if (bctl->flags & BTRFS_BALANCE_RESUME)
3475 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003476 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003477 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003478 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3479 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003480 return ret;
3481}
3482
3483static int balance_kthread(void *data)
3484{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003485 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003486 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003487
3488 mutex_lock(&fs_info->volume_mutex);
3489 mutex_lock(&fs_info->balance_mutex);
3490
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003491 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003492 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003493 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003494 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003495
3496 mutex_unlock(&fs_info->balance_mutex);
3497 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003498
Ilya Dryomov59641012012-01-16 22:04:48 +02003499 return ret;
3500}
3501
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003502int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3503{
3504 struct task_struct *tsk;
3505
3506 spin_lock(&fs_info->balance_lock);
3507 if (!fs_info->balance_ctl) {
3508 spin_unlock(&fs_info->balance_lock);
3509 return 0;
3510 }
3511 spin_unlock(&fs_info->balance_lock);
3512
3513 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003514 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003515 return 0;
3516 }
3517
3518 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303519 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003520}
3521
Ilya Dryomov68310a52012-06-22 12:24:12 -06003522int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003523{
Ilya Dryomov59641012012-01-16 22:04:48 +02003524 struct btrfs_balance_control *bctl;
3525 struct btrfs_balance_item *item;
3526 struct btrfs_disk_balance_args disk_bargs;
3527 struct btrfs_path *path;
3528 struct extent_buffer *leaf;
3529 struct btrfs_key key;
3530 int ret;
3531
3532 path = btrfs_alloc_path();
3533 if (!path)
3534 return -ENOMEM;
3535
Ilya Dryomov68310a52012-06-22 12:24:12 -06003536 key.objectid = BTRFS_BALANCE_OBJECTID;
3537 key.type = BTRFS_BALANCE_ITEM_KEY;
3538 key.offset = 0;
3539
3540 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3541 if (ret < 0)
3542 goto out;
3543 if (ret > 0) { /* ret = -ENOENT; */
3544 ret = 0;
3545 goto out;
3546 }
3547
Ilya Dryomov59641012012-01-16 22:04:48 +02003548 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3549 if (!bctl) {
3550 ret = -ENOMEM;
3551 goto out;
3552 }
3553
Ilya Dryomov59641012012-01-16 22:04:48 +02003554 leaf = path->nodes[0];
3555 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3556
Ilya Dryomov68310a52012-06-22 12:24:12 -06003557 bctl->fs_info = fs_info;
3558 bctl->flags = btrfs_balance_flags(leaf, item);
3559 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003560
3561 btrfs_balance_data(leaf, item, &disk_bargs);
3562 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3563 btrfs_balance_meta(leaf, item, &disk_bargs);
3564 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3565 btrfs_balance_sys(leaf, item, &disk_bargs);
3566 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3567
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003568 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3569
Ilya Dryomov68310a52012-06-22 12:24:12 -06003570 mutex_lock(&fs_info->volume_mutex);
3571 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003572
Ilya Dryomov68310a52012-06-22 12:24:12 -06003573 set_balance_control(bctl);
3574
3575 mutex_unlock(&fs_info->balance_mutex);
3576 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003577out:
3578 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003579 return ret;
3580}
3581
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003582int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3583{
3584 int ret = 0;
3585
3586 mutex_lock(&fs_info->balance_mutex);
3587 if (!fs_info->balance_ctl) {
3588 mutex_unlock(&fs_info->balance_mutex);
3589 return -ENOTCONN;
3590 }
3591
3592 if (atomic_read(&fs_info->balance_running)) {
3593 atomic_inc(&fs_info->balance_pause_req);
3594 mutex_unlock(&fs_info->balance_mutex);
3595
3596 wait_event(fs_info->balance_wait_q,
3597 atomic_read(&fs_info->balance_running) == 0);
3598
3599 mutex_lock(&fs_info->balance_mutex);
3600 /* we are good with balance_ctl ripped off from under us */
3601 BUG_ON(atomic_read(&fs_info->balance_running));
3602 atomic_dec(&fs_info->balance_pause_req);
3603 } else {
3604 ret = -ENOTCONN;
3605 }
3606
3607 mutex_unlock(&fs_info->balance_mutex);
3608 return ret;
3609}
3610
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003611int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3612{
Ilya Dryomove649e582013-10-10 20:40:21 +03003613 if (fs_info->sb->s_flags & MS_RDONLY)
3614 return -EROFS;
3615
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003616 mutex_lock(&fs_info->balance_mutex);
3617 if (!fs_info->balance_ctl) {
3618 mutex_unlock(&fs_info->balance_mutex);
3619 return -ENOTCONN;
3620 }
3621
3622 atomic_inc(&fs_info->balance_cancel_req);
3623 /*
3624 * if we are running just wait and return, balance item is
3625 * deleted in btrfs_balance in this case
3626 */
3627 if (atomic_read(&fs_info->balance_running)) {
3628 mutex_unlock(&fs_info->balance_mutex);
3629 wait_event(fs_info->balance_wait_q,
3630 atomic_read(&fs_info->balance_running) == 0);
3631 mutex_lock(&fs_info->balance_mutex);
3632 } else {
3633 /* __cancel_balance needs volume_mutex */
3634 mutex_unlock(&fs_info->balance_mutex);
3635 mutex_lock(&fs_info->volume_mutex);
3636 mutex_lock(&fs_info->balance_mutex);
3637
3638 if (fs_info->balance_ctl)
3639 __cancel_balance(fs_info);
3640
3641 mutex_unlock(&fs_info->volume_mutex);
3642 }
3643
3644 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3645 atomic_dec(&fs_info->balance_cancel_req);
3646 mutex_unlock(&fs_info->balance_mutex);
3647 return 0;
3648}
3649
Stefan Behrens803b2f52013-08-15 17:11:21 +02003650static int btrfs_uuid_scan_kthread(void *data)
3651{
3652 struct btrfs_fs_info *fs_info = data;
3653 struct btrfs_root *root = fs_info->tree_root;
3654 struct btrfs_key key;
3655 struct btrfs_key max_key;
3656 struct btrfs_path *path = NULL;
3657 int ret = 0;
3658 struct extent_buffer *eb;
3659 int slot;
3660 struct btrfs_root_item root_item;
3661 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003662 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003663
3664 path = btrfs_alloc_path();
3665 if (!path) {
3666 ret = -ENOMEM;
3667 goto out;
3668 }
3669
3670 key.objectid = 0;
3671 key.type = BTRFS_ROOT_ITEM_KEY;
3672 key.offset = 0;
3673
3674 max_key.objectid = (u64)-1;
3675 max_key.type = BTRFS_ROOT_ITEM_KEY;
3676 max_key.offset = (u64)-1;
3677
Stefan Behrens803b2f52013-08-15 17:11:21 +02003678 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003679 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003680 if (ret) {
3681 if (ret > 0)
3682 ret = 0;
3683 break;
3684 }
3685
3686 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3687 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3688 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3689 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3690 goto skip;
3691
3692 eb = path->nodes[0];
3693 slot = path->slots[0];
3694 item_size = btrfs_item_size_nr(eb, slot);
3695 if (item_size < sizeof(root_item))
3696 goto skip;
3697
Stefan Behrens803b2f52013-08-15 17:11:21 +02003698 read_extent_buffer(eb, &root_item,
3699 btrfs_item_ptr_offset(eb, slot),
3700 (int)sizeof(root_item));
3701 if (btrfs_root_refs(&root_item) == 0)
3702 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003703
3704 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3705 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3706 if (trans)
3707 goto update_tree;
3708
3709 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003710 /*
3711 * 1 - subvol uuid item
3712 * 1 - received_subvol uuid item
3713 */
3714 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3715 if (IS_ERR(trans)) {
3716 ret = PTR_ERR(trans);
3717 break;
3718 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003719 continue;
3720 } else {
3721 goto skip;
3722 }
3723update_tree:
3724 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003725 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3726 root_item.uuid,
3727 BTRFS_UUID_KEY_SUBVOL,
3728 key.objectid);
3729 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003730 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003731 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003732 break;
3733 }
3734 }
3735
3736 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003737 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3738 root_item.received_uuid,
3739 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3740 key.objectid);
3741 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003742 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003743 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003744 break;
3745 }
3746 }
3747
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003748skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003749 if (trans) {
3750 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003751 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003752 if (ret)
3753 break;
3754 }
3755
Stefan Behrens803b2f52013-08-15 17:11:21 +02003756 btrfs_release_path(path);
3757 if (key.offset < (u64)-1) {
3758 key.offset++;
3759 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3760 key.offset = 0;
3761 key.type = BTRFS_ROOT_ITEM_KEY;
3762 } else if (key.objectid < (u64)-1) {
3763 key.offset = 0;
3764 key.type = BTRFS_ROOT_ITEM_KEY;
3765 key.objectid++;
3766 } else {
3767 break;
3768 }
3769 cond_resched();
3770 }
3771
3772out:
3773 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003774 if (trans && !IS_ERR(trans))
3775 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003776 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05003777 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003778 else
3779 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003780 up(&fs_info->uuid_tree_rescan_sem);
3781 return 0;
3782}
3783
Stefan Behrens70f80172013-08-15 17:11:23 +02003784/*
3785 * Callback for btrfs_uuid_tree_iterate().
3786 * returns:
3787 * 0 check succeeded, the entry is not outdated.
3788 * < 0 if an error occured.
3789 * > 0 if the check failed, which means the caller shall remove the entry.
3790 */
3791static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3792 u8 *uuid, u8 type, u64 subid)
3793{
3794 struct btrfs_key key;
3795 int ret = 0;
3796 struct btrfs_root *subvol_root;
3797
3798 if (type != BTRFS_UUID_KEY_SUBVOL &&
3799 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3800 goto out;
3801
3802 key.objectid = subid;
3803 key.type = BTRFS_ROOT_ITEM_KEY;
3804 key.offset = (u64)-1;
3805 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3806 if (IS_ERR(subvol_root)) {
3807 ret = PTR_ERR(subvol_root);
3808 if (ret == -ENOENT)
3809 ret = 1;
3810 goto out;
3811 }
3812
3813 switch (type) {
3814 case BTRFS_UUID_KEY_SUBVOL:
3815 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3816 ret = 1;
3817 break;
3818 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3819 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3820 BTRFS_UUID_SIZE))
3821 ret = 1;
3822 break;
3823 }
3824
3825out:
3826 return ret;
3827}
3828
3829static int btrfs_uuid_rescan_kthread(void *data)
3830{
3831 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3832 int ret;
3833
3834 /*
3835 * 1st step is to iterate through the existing UUID tree and
3836 * to delete all entries that contain outdated data.
3837 * 2nd step is to add all missing entries to the UUID tree.
3838 */
3839 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3840 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003841 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003842 up(&fs_info->uuid_tree_rescan_sem);
3843 return ret;
3844 }
3845 return btrfs_uuid_scan_kthread(data);
3846}
3847
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003848int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3849{
3850 struct btrfs_trans_handle *trans;
3851 struct btrfs_root *tree_root = fs_info->tree_root;
3852 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003853 struct task_struct *task;
3854 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003855
3856 /*
3857 * 1 - root node
3858 * 1 - root item
3859 */
3860 trans = btrfs_start_transaction(tree_root, 2);
3861 if (IS_ERR(trans))
3862 return PTR_ERR(trans);
3863
3864 uuid_root = btrfs_create_tree(trans, fs_info,
3865 BTRFS_UUID_TREE_OBJECTID);
3866 if (IS_ERR(uuid_root)) {
3867 btrfs_abort_transaction(trans, tree_root,
3868 PTR_ERR(uuid_root));
3869 return PTR_ERR(uuid_root);
3870 }
3871
3872 fs_info->uuid_root = uuid_root;
3873
Stefan Behrens803b2f52013-08-15 17:11:21 +02003874 ret = btrfs_commit_transaction(trans, tree_root);
3875 if (ret)
3876 return ret;
3877
3878 down(&fs_info->uuid_tree_rescan_sem);
3879 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3880 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02003881 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003882 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02003883 up(&fs_info->uuid_tree_rescan_sem);
3884 return PTR_ERR(task);
3885 }
3886
3887 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003888}
Stefan Behrens803b2f52013-08-15 17:11:21 +02003889
Stefan Behrens70f80172013-08-15 17:11:23 +02003890int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3891{
3892 struct task_struct *task;
3893
3894 down(&fs_info->uuid_tree_rescan_sem);
3895 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3896 if (IS_ERR(task)) {
3897 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003898 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02003899 up(&fs_info->uuid_tree_rescan_sem);
3900 return PTR_ERR(task);
3901 }
3902
3903 return 0;
3904}
3905
Chris Mason8f18cf12008-04-25 16:53:30 -04003906/*
3907 * shrinking a device means finding all of the device extents past
3908 * the new size, and then following the back refs to the chunks.
3909 * The chunk relocation code actually frees the device extent
3910 */
3911int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3912{
3913 struct btrfs_trans_handle *trans;
3914 struct btrfs_root *root = device->dev_root;
3915 struct btrfs_dev_extent *dev_extent = NULL;
3916 struct btrfs_path *path;
3917 u64 length;
3918 u64 chunk_tree;
3919 u64 chunk_objectid;
3920 u64 chunk_offset;
3921 int ret;
3922 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003923 int failed = 0;
3924 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003925 struct extent_buffer *l;
3926 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003927 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003928 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08003929 u64 old_size = btrfs_device_get_total_bytes(device);
3930 u64 diff = old_size - new_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04003931
Stefan Behrens63a212a2012-11-05 18:29:28 +01003932 if (device->is_tgtdev_for_dev_replace)
3933 return -EINVAL;
3934
Chris Mason8f18cf12008-04-25 16:53:30 -04003935 path = btrfs_alloc_path();
3936 if (!path)
3937 return -ENOMEM;
3938
Chris Mason8f18cf12008-04-25 16:53:30 -04003939 path->reada = 2;
3940
Chris Mason7d9eb122008-07-08 14:19:17 -04003941 lock_chunks(root);
3942
Miao Xie7cc8e582014-09-03 21:35:38 +08003943 btrfs_device_set_total_bytes(device, new_size);
Josef Bacik2bf64752011-09-26 17:12:22 -04003944 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003945 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003946 spin_lock(&root->fs_info->free_chunk_lock);
3947 root->fs_info->free_chunk_space -= diff;
3948 spin_unlock(&root->fs_info->free_chunk_lock);
3949 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003950 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003951
Josef Bacikba1bf482009-09-11 16:11:19 -04003952again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003953 key.objectid = device->devid;
3954 key.offset = (u64)-1;
3955 key.type = BTRFS_DEV_EXTENT_KEY;
3956
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003957 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003958 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3959 if (ret < 0)
3960 goto done;
3961
3962 ret = btrfs_previous_item(root, path, 0, key.type);
3963 if (ret < 0)
3964 goto done;
3965 if (ret) {
3966 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003967 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003968 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003969 }
3970
3971 l = path->nodes[0];
3972 slot = path->slots[0];
3973 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3974
Josef Bacikba1bf482009-09-11 16:11:19 -04003975 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003976 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003977 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003978 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003979
3980 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3981 length = btrfs_dev_extent_length(l, dev_extent);
3982
Josef Bacikba1bf482009-09-11 16:11:19 -04003983 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003984 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003985 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003986 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003987
3988 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3989 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3990 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003991 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003992
3993 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3994 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003995 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003996 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003997 if (ret == -ENOSPC)
3998 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003999 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004000
4001 if (failed && !retried) {
4002 failed = 0;
4003 retried = true;
4004 goto again;
4005 } else if (failed && retried) {
4006 ret = -ENOSPC;
4007 lock_chunks(root);
4008
Miao Xie7cc8e582014-09-03 21:35:38 +08004009 btrfs_device_set_total_bytes(device, old_size);
Josef Bacikba1bf482009-09-11 16:11:19 -04004010 if (device->writeable)
4011 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04004012 spin_lock(&root->fs_info->free_chunk_lock);
4013 root->fs_info->free_chunk_space += diff;
4014 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04004015 unlock_chunks(root);
4016 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004017 }
4018
Chris Balld6397ba2009-04-27 07:29:03 -04004019 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004020 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004021 if (IS_ERR(trans)) {
4022 ret = PTR_ERR(trans);
4023 goto done;
4024 }
4025
Chris Balld6397ba2009-04-27 07:29:03 -04004026 lock_chunks(root);
Miao Xie7cc8e582014-09-03 21:35:38 +08004027 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004028 if (list_empty(&device->resized_list))
4029 list_add_tail(&device->resized_list,
4030 &root->fs_info->fs_devices->resized_devices);
4031
Chris Balld6397ba2009-04-27 07:29:03 -04004032 /* Now btrfs_update_device() will change the on-disk size. */
4033 ret = btrfs_update_device(trans, device);
4034 if (ret) {
4035 unlock_chunks(root);
4036 btrfs_end_transaction(trans, root);
4037 goto done;
4038 }
4039 WARN_ON(diff > old_total);
4040 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4041 unlock_chunks(root);
4042 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004043done:
4044 btrfs_free_path(path);
4045 return ret;
4046}
4047
Li Zefan125ccb02011-12-08 15:07:24 +08004048static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004049 struct btrfs_key *key,
4050 struct btrfs_chunk *chunk, int item_size)
4051{
David Sterba6c417612011-04-13 15:41:04 +02004052 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004053 struct btrfs_disk_key disk_key;
4054 u32 array_size;
4055 u8 *ptr;
4056
Miao Xiefe48a5c2014-09-03 21:35:39 +08004057 lock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004058 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004059 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004060 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4061 unlock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004062 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004063 }
Chris Mason0b86a832008-03-24 15:01:56 -04004064
4065 ptr = super_copy->sys_chunk_array + array_size;
4066 btrfs_cpu_key_to_disk(&disk_key, key);
4067 memcpy(ptr, &disk_key, sizeof(disk_key));
4068 ptr += sizeof(disk_key);
4069 memcpy(ptr, chunk, item_size);
4070 item_size += sizeof(disk_key);
4071 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004072 unlock_chunks(root);
4073
Chris Mason0b86a832008-03-24 15:01:56 -04004074 return 0;
4075}
4076
Miao Xieb2117a32011-01-05 10:07:28 +00004077/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004078 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004079 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004080static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004081{
Arne Jansen73c5de02011-04-12 12:07:57 +02004082 const struct btrfs_device_info *di_a = a;
4083 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004084
Arne Jansen73c5de02011-04-12 12:07:57 +02004085 if (di_a->max_avail > di_b->max_avail)
4086 return -1;
4087 if (di_a->max_avail < di_b->max_avail)
4088 return 1;
4089 if (di_a->total_avail > di_b->total_avail)
4090 return -1;
4091 if (di_a->total_avail < di_b->total_avail)
4092 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004093 return 0;
4094}
4095
Eric Sandeen48a3b632013-04-25 20:41:01 +00004096static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00004097 [BTRFS_RAID_RAID10] = {
4098 .sub_stripes = 2,
4099 .dev_stripes = 1,
4100 .devs_max = 0, /* 0 == as many as possible */
4101 .devs_min = 4,
4102 .devs_increment = 2,
4103 .ncopies = 2,
4104 },
4105 [BTRFS_RAID_RAID1] = {
4106 .sub_stripes = 1,
4107 .dev_stripes = 1,
4108 .devs_max = 2,
4109 .devs_min = 2,
4110 .devs_increment = 2,
4111 .ncopies = 2,
4112 },
4113 [BTRFS_RAID_DUP] = {
4114 .sub_stripes = 1,
4115 .dev_stripes = 2,
4116 .devs_max = 1,
4117 .devs_min = 1,
4118 .devs_increment = 1,
4119 .ncopies = 2,
4120 },
4121 [BTRFS_RAID_RAID0] = {
4122 .sub_stripes = 1,
4123 .dev_stripes = 1,
4124 .devs_max = 0,
4125 .devs_min = 2,
4126 .devs_increment = 1,
4127 .ncopies = 1,
4128 },
4129 [BTRFS_RAID_SINGLE] = {
4130 .sub_stripes = 1,
4131 .dev_stripes = 1,
4132 .devs_max = 1,
4133 .devs_min = 1,
4134 .devs_increment = 1,
4135 .ncopies = 1,
4136 },
Chris Masone942f882013-02-20 14:06:05 -05004137 [BTRFS_RAID_RAID5] = {
4138 .sub_stripes = 1,
4139 .dev_stripes = 1,
4140 .devs_max = 0,
4141 .devs_min = 2,
4142 .devs_increment = 1,
4143 .ncopies = 2,
4144 },
4145 [BTRFS_RAID_RAID6] = {
4146 .sub_stripes = 1,
4147 .dev_stripes = 1,
4148 .devs_max = 0,
4149 .devs_min = 3,
4150 .devs_increment = 1,
4151 .ncopies = 3,
4152 },
Liu Bo31e50222012-11-21 14:18:10 +00004153};
4154
David Woodhouse53b381b2013-01-29 18:40:14 -05004155static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4156{
4157 /* TODO allow them to set a preferred stripe size */
4158 return 64 * 1024;
4159}
4160
4161static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4162{
David Woodhouse53b381b2013-01-29 18:40:14 -05004163 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
4164 return;
4165
Miao Xieceda0862013-04-11 10:30:16 +00004166 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004167}
4168
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004169#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4170 - sizeof(struct btrfs_item) \
4171 - sizeof(struct btrfs_chunk)) \
4172 / sizeof(struct btrfs_stripe) + 1)
4173
4174#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4175 - 2 * sizeof(struct btrfs_disk_key) \
4176 - 2 * sizeof(struct btrfs_chunk)) \
4177 / sizeof(struct btrfs_stripe) + 1)
4178
Miao Xieb2117a32011-01-05 10:07:28 +00004179static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004180 struct btrfs_root *extent_root, u64 start,
4181 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004182{
4183 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004184 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4185 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004186 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004187 struct extent_map_tree *em_tree;
4188 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004189 struct btrfs_device_info *devices_info = NULL;
4190 u64 total_avail;
4191 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004192 int data_stripes; /* number of stripes that count for
4193 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004194 int sub_stripes; /* sub_stripes info for map */
4195 int dev_stripes; /* stripes per dev */
4196 int devs_max; /* max devs to use */
4197 int devs_min; /* min devs needed */
4198 int devs_increment; /* ndevs has to be a multiple of this */
4199 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004200 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004201 u64 max_stripe_size;
4202 u64 max_chunk_size;
4203 u64 stripe_size;
4204 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004205 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004206 int ndevs;
4207 int i;
4208 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004209 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004210
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004211 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004212
Miao Xieb2117a32011-01-05 10:07:28 +00004213 if (list_empty(&fs_devices->alloc_list))
4214 return -ENOSPC;
4215
Liu Bo31e50222012-11-21 14:18:10 +00004216 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004217
Liu Bo31e50222012-11-21 14:18:10 +00004218 sub_stripes = btrfs_raid_array[index].sub_stripes;
4219 dev_stripes = btrfs_raid_array[index].dev_stripes;
4220 devs_max = btrfs_raid_array[index].devs_max;
4221 devs_min = btrfs_raid_array[index].devs_min;
4222 devs_increment = btrfs_raid_array[index].devs_increment;
4223 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004224
4225 if (type & BTRFS_BLOCK_GROUP_DATA) {
4226 max_stripe_size = 1024 * 1024 * 1024;
4227 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004228 if (!devs_max)
4229 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004230 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004231 /* for larger filesystems, use larger metadata chunks */
4232 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4233 max_stripe_size = 1024 * 1024 * 1024;
4234 else
4235 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004236 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004237 if (!devs_max)
4238 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004239 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004240 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004241 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004242 if (!devs_max)
4243 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004244 } else {
David Sterba351fd352014-05-15 16:48:20 +02004245 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004246 type);
4247 BUG_ON(1);
4248 }
4249
4250 /* we don't want a chunk larger than 10% of writeable space */
4251 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4252 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004253
4254 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
4255 GFP_NOFS);
4256 if (!devices_info)
4257 return -ENOMEM;
4258
Arne Jansen73c5de02011-04-12 12:07:57 +02004259 cur = fs_devices->alloc_list.next;
4260
4261 /*
4262 * in the first pass through the devices list, we gather information
4263 * about the available holes on each device.
4264 */
4265 ndevs = 0;
4266 while (cur != &fs_devices->alloc_list) {
4267 struct btrfs_device *device;
4268 u64 max_avail;
4269 u64 dev_offset;
4270
4271 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4272
4273 cur = cur->next;
4274
4275 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004276 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004277 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004278 continue;
4279 }
4280
Stefan Behrens63a212a2012-11-05 18:29:28 +01004281 if (!device->in_fs_metadata ||
4282 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004283 continue;
4284
4285 if (device->total_bytes > device->bytes_used)
4286 total_avail = device->total_bytes - device->bytes_used;
4287 else
4288 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004289
4290 /* If there is no space on this device, skip it. */
4291 if (total_avail == 0)
4292 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004293
Josef Bacik6df9a952013-06-27 13:22:46 -04004294 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004295 max_stripe_size * dev_stripes,
4296 &dev_offset, &max_avail);
4297 if (ret && ret != -ENOSPC)
4298 goto error;
4299
4300 if (ret == 0)
4301 max_avail = max_stripe_size * dev_stripes;
4302
4303 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4304 continue;
4305
Eric Sandeen063d0062013-01-31 00:55:01 +00004306 if (ndevs == fs_devices->rw_devices) {
4307 WARN(1, "%s: found more than %llu devices\n",
4308 __func__, fs_devices->rw_devices);
4309 break;
4310 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004311 devices_info[ndevs].dev_offset = dev_offset;
4312 devices_info[ndevs].max_avail = max_avail;
4313 devices_info[ndevs].total_avail = total_avail;
4314 devices_info[ndevs].dev = device;
4315 ++ndevs;
4316 }
4317
4318 /*
4319 * now sort the devices by hole size / available space
4320 */
4321 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4322 btrfs_cmp_device_info, NULL);
4323
4324 /* round down to number of usable stripes */
4325 ndevs -= ndevs % devs_increment;
4326
4327 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4328 ret = -ENOSPC;
4329 goto error;
4330 }
4331
4332 if (devs_max && ndevs > devs_max)
4333 ndevs = devs_max;
4334 /*
4335 * the primary goal is to maximize the number of stripes, so use as many
4336 * devices as possible, even if the stripes are not maximum sized.
4337 */
4338 stripe_size = devices_info[ndevs-1].max_avail;
4339 num_stripes = ndevs * dev_stripes;
4340
David Woodhouse53b381b2013-01-29 18:40:14 -05004341 /*
4342 * this will have to be fixed for RAID1 and RAID10 over
4343 * more drives
4344 */
4345 data_stripes = num_stripes / ncopies;
4346
David Woodhouse53b381b2013-01-29 18:40:14 -05004347 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4348 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4349 btrfs_super_stripesize(info->super_copy));
4350 data_stripes = num_stripes - 1;
4351 }
4352 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4353 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4354 btrfs_super_stripesize(info->super_copy));
4355 data_stripes = num_stripes - 2;
4356 }
Chris Mason86db2572013-02-20 16:23:40 -05004357
4358 /*
4359 * Use the number of data stripes to figure out how big this chunk
4360 * is really going to be in terms of logical address space,
4361 * and compare that answer with the max chunk size
4362 */
4363 if (stripe_size * data_stripes > max_chunk_size) {
4364 u64 mask = (1ULL << 24) - 1;
4365 stripe_size = max_chunk_size;
4366 do_div(stripe_size, data_stripes);
4367
4368 /* bump the answer up to a 16MB boundary */
4369 stripe_size = (stripe_size + mask) & ~mask;
4370
4371 /* but don't go higher than the limits we found
4372 * while searching for free extents
4373 */
4374 if (stripe_size > devices_info[ndevs-1].max_avail)
4375 stripe_size = devices_info[ndevs-1].max_avail;
4376 }
4377
Arne Jansen73c5de02011-04-12 12:07:57 +02004378 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004379
4380 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05004381 do_div(stripe_size, raid_stripe_len);
4382 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004383
Miao Xieb2117a32011-01-05 10:07:28 +00004384 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4385 if (!map) {
4386 ret = -ENOMEM;
4387 goto error;
4388 }
4389 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004390
Arne Jansen73c5de02011-04-12 12:07:57 +02004391 for (i = 0; i < ndevs; ++i) {
4392 for (j = 0; j < dev_stripes; ++j) {
4393 int s = i * dev_stripes + j;
4394 map->stripes[s].dev = devices_info[i].dev;
4395 map->stripes[s].physical = devices_info[i].dev_offset +
4396 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004397 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004398 }
Chris Mason593060d2008-03-25 16:50:33 -04004399 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004400 map->stripe_len = raid_stripe_len;
4401 map->io_align = raid_stripe_len;
4402 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004403 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004404 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004405
David Woodhouse53b381b2013-01-29 18:40:14 -05004406 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004407
Arne Jansen73c5de02011-04-12 12:07:57 +02004408 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004409
David Sterba172ddd62011-04-21 00:48:27 +02004410 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004411 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004412 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004413 ret = -ENOMEM;
4414 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004415 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004416 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004417 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004418 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004419 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004420 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004421 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004422 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004423
Chris Mason0b86a832008-03-24 15:01:56 -04004424 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004425 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004426 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004427 if (!ret) {
4428 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4429 atomic_inc(&em->refs);
4430 }
Chris Mason890871b2009-09-02 16:24:52 -04004431 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004432 if (ret) {
4433 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004434 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004435 }
Yan Zheng2b820322008-11-17 21:11:30 -05004436
Josef Bacik04487482013-01-29 15:03:37 -05004437 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4438 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4439 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004440 if (ret)
4441 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004442
Miao Xie7cc8e582014-09-03 21:35:38 +08004443 for (i = 0; i < map->num_stripes; i++) {
4444 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4445 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4446 }
Miao Xie43530c42014-09-03 21:35:36 +08004447
Miao Xie1c116182014-09-03 21:35:37 +08004448 spin_lock(&extent_root->fs_info->free_chunk_lock);
4449 extent_root->fs_info->free_chunk_space -= (stripe_size *
4450 map->num_stripes);
4451 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4452
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004453 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004454 check_raid56_incompat_flag(extent_root->fs_info, type);
4455
Miao Xieb2117a32011-01-05 10:07:28 +00004456 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004457 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004458
Josef Bacik6df9a952013-06-27 13:22:46 -04004459error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004460 write_lock(&em_tree->lock);
4461 remove_extent_mapping(em_tree, em);
4462 write_unlock(&em_tree->lock);
4463
4464 /* One for our allocation */
4465 free_extent_map(em);
4466 /* One for the tree reference */
4467 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004468error:
Miao Xieb2117a32011-01-05 10:07:28 +00004469 kfree(devices_info);
4470 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004471}
4472
Josef Bacik6df9a952013-06-27 13:22:46 -04004473int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004474 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004475 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004476{
Yan Zheng2b820322008-11-17 21:11:30 -05004477 struct btrfs_key key;
4478 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4479 struct btrfs_device *device;
4480 struct btrfs_chunk *chunk;
4481 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004482 struct extent_map_tree *em_tree;
4483 struct extent_map *em;
4484 struct map_lookup *map;
4485 size_t item_size;
4486 u64 dev_offset;
4487 u64 stripe_size;
4488 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004489 int ret;
4490
Josef Bacik6df9a952013-06-27 13:22:46 -04004491 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4492 read_lock(&em_tree->lock);
4493 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4494 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004495
Josef Bacik6df9a952013-06-27 13:22:46 -04004496 if (!em) {
4497 btrfs_crit(extent_root->fs_info, "unable to find logical "
4498 "%Lu len %Lu", chunk_offset, chunk_size);
4499 return -EINVAL;
4500 }
4501
4502 if (em->start != chunk_offset || em->len != chunk_size) {
4503 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004504 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004505 chunk_size, em->start, em->len);
4506 free_extent_map(em);
4507 return -EINVAL;
4508 }
4509
4510 map = (struct map_lookup *)em->bdev;
4511 item_size = btrfs_chunk_item_size(map->num_stripes);
4512 stripe_size = em->orig_block_len;
4513
4514 chunk = kzalloc(item_size, GFP_NOFS);
4515 if (!chunk) {
4516 ret = -ENOMEM;
4517 goto out;
4518 }
4519
4520 for (i = 0; i < map->num_stripes; i++) {
4521 device = map->stripes[i].dev;
4522 dev_offset = map->stripes[i].physical;
4523
Yan Zheng2b820322008-11-17 21:11:30 -05004524 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004525 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004526 goto out;
4527 ret = btrfs_alloc_dev_extent(trans, device,
4528 chunk_root->root_key.objectid,
4529 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4530 chunk_offset, dev_offset,
4531 stripe_size);
4532 if (ret)
4533 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004534 }
4535
Yan Zheng2b820322008-11-17 21:11:30 -05004536 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004537 for (i = 0; i < map->num_stripes; i++) {
4538 device = map->stripes[i].dev;
4539 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004540
4541 btrfs_set_stack_stripe_devid(stripe, device->devid);
4542 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4543 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4544 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004545 }
4546
4547 btrfs_set_stack_chunk_length(chunk, chunk_size);
4548 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4549 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4550 btrfs_set_stack_chunk_type(chunk, map->type);
4551 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4552 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4553 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4554 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4555 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4556
4557 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4558 key.type = BTRFS_CHUNK_ITEM_KEY;
4559 key.offset = chunk_offset;
4560
4561 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004562 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4563 /*
4564 * TODO: Cleanup of inserted chunk root in case of
4565 * failure.
4566 */
Li Zefan125ccb02011-12-08 15:07:24 +08004567 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004568 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004569 }
liubo1abe9b82011-03-24 11:18:59 +00004570
Josef Bacik6df9a952013-06-27 13:22:46 -04004571out:
Yan Zheng2b820322008-11-17 21:11:30 -05004572 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004573 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004574 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004575}
4576
4577/*
4578 * Chunk allocation falls into two parts. The first part does works
4579 * that make the new allocated chunk useable, but not do any operation
4580 * that modifies the chunk tree. The second part does the works that
4581 * require modifying the chunk tree. This division is important for the
4582 * bootstrap process of adding storage to a seed btrfs.
4583 */
4584int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4585 struct btrfs_root *extent_root, u64 type)
4586{
4587 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004588
Josef Bacik6df9a952013-06-27 13:22:46 -04004589 chunk_offset = find_next_chunk(extent_root->fs_info);
4590 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004591}
4592
Chris Masond3977122009-01-05 21:25:51 -05004593static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004594 struct btrfs_root *root,
4595 struct btrfs_device *device)
4596{
4597 u64 chunk_offset;
4598 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004599 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004600 struct btrfs_fs_info *fs_info = root->fs_info;
4601 struct btrfs_root *extent_root = fs_info->extent_root;
4602 int ret;
4603
Josef Bacik6df9a952013-06-27 13:22:46 -04004604 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004605 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004606 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4607 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004608 if (ret)
4609 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004610
Josef Bacik6df9a952013-06-27 13:22:46 -04004611 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004612 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004613 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4614 alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06004615 if (ret) {
4616 btrfs_abort_transaction(trans, root, ret);
4617 goto out;
4618 }
Yan Zheng2b820322008-11-17 21:11:30 -05004619
4620 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004621 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004622 btrfs_abort_transaction(trans, root, ret);
David Sterba005d6422012-09-18 07:52:32 -06004623out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004624 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004625}
4626
Miao Xied20983b2014-07-03 18:22:13 +08004627static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4628{
4629 int max_errors;
4630
4631 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4632 BTRFS_BLOCK_GROUP_RAID10 |
4633 BTRFS_BLOCK_GROUP_RAID5 |
4634 BTRFS_BLOCK_GROUP_DUP)) {
4635 max_errors = 1;
4636 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4637 max_errors = 2;
4638 } else {
4639 max_errors = 0;
4640 }
4641
4642 return max_errors;
4643}
4644
Yan Zheng2b820322008-11-17 21:11:30 -05004645int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4646{
4647 struct extent_map *em;
4648 struct map_lookup *map;
4649 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4650 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004651 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004652 int i;
4653
Chris Mason890871b2009-09-02 16:24:52 -04004654 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004655 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004656 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004657 if (!em)
4658 return 1;
4659
4660 map = (struct map_lookup *)em->bdev;
4661 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004662 if (map->stripes[i].dev->missing) {
4663 miss_ndevs++;
4664 continue;
4665 }
4666
Yan Zheng2b820322008-11-17 21:11:30 -05004667 if (!map->stripes[i].dev->writeable) {
4668 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004669 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004670 }
4671 }
Miao Xied20983b2014-07-03 18:22:13 +08004672
4673 /*
4674 * If the number of missing devices is larger than max errors,
4675 * we can not write the data into that chunk successfully, so
4676 * set it readonly.
4677 */
4678 if (miss_ndevs > btrfs_chunk_max_errors(map))
4679 readonly = 1;
4680end:
Yan Zheng2b820322008-11-17 21:11:30 -05004681 free_extent_map(em);
4682 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004683}
4684
4685void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4686{
David Sterbaa8067e02011-04-21 00:34:43 +02004687 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004688}
4689
4690void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4691{
4692 struct extent_map *em;
4693
Chris Masond3977122009-01-05 21:25:51 -05004694 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004695 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004696 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4697 if (em)
4698 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004699 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004700 if (!em)
4701 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004702 /* once for us */
4703 free_extent_map(em);
4704 /* once for the tree */
4705 free_extent_map(em);
4706 }
4707}
4708
Stefan Behrens5d964052012-11-05 14:59:07 +01004709int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004710{
Stefan Behrens5d964052012-11-05 14:59:07 +01004711 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004712 struct extent_map *em;
4713 struct map_lookup *map;
4714 struct extent_map_tree *em_tree = &map_tree->map_tree;
4715 int ret;
4716
Chris Mason890871b2009-09-02 16:24:52 -04004717 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004718 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004719 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004720
Josef Bacikfb7669b2013-04-23 10:53:18 -04004721 /*
4722 * We could return errors for these cases, but that could get ugly and
4723 * we'd probably do the same thing which is just not do anything else
4724 * and exit, so return 1 so the callers don't try to use other copies.
4725 */
4726 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004727 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004728 logical+len);
4729 return 1;
4730 }
4731
4732 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004733 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004734 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004735 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004736 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004737 return 1;
4738 }
4739
Chris Masonf1885912008-04-09 16:28:12 -04004740 map = (struct map_lookup *)em->bdev;
4741 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4742 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004743 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4744 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004745 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4746 ret = 2;
4747 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4748 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004749 else
4750 ret = 1;
4751 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004752
4753 btrfs_dev_replace_lock(&fs_info->dev_replace);
4754 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4755 ret++;
4756 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4757
Chris Masonf1885912008-04-09 16:28:12 -04004758 return ret;
4759}
4760
David Woodhouse53b381b2013-01-29 18:40:14 -05004761unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4762 struct btrfs_mapping_tree *map_tree,
4763 u64 logical)
4764{
4765 struct extent_map *em;
4766 struct map_lookup *map;
4767 struct extent_map_tree *em_tree = &map_tree->map_tree;
4768 unsigned long len = root->sectorsize;
4769
4770 read_lock(&em_tree->lock);
4771 em = lookup_extent_mapping(em_tree, logical, len);
4772 read_unlock(&em_tree->lock);
4773 BUG_ON(!em);
4774
4775 BUG_ON(em->start > logical || em->start + em->len < logical);
4776 map = (struct map_lookup *)em->bdev;
4777 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4778 BTRFS_BLOCK_GROUP_RAID6)) {
4779 len = map->stripe_len * nr_data_stripes(map);
4780 }
4781 free_extent_map(em);
4782 return len;
4783}
4784
4785int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4786 u64 logical, u64 len, int mirror_num)
4787{
4788 struct extent_map *em;
4789 struct map_lookup *map;
4790 struct extent_map_tree *em_tree = &map_tree->map_tree;
4791 int ret = 0;
4792
4793 read_lock(&em_tree->lock);
4794 em = lookup_extent_mapping(em_tree, logical, len);
4795 read_unlock(&em_tree->lock);
4796 BUG_ON(!em);
4797
4798 BUG_ON(em->start > logical || em->start + em->len < logical);
4799 map = (struct map_lookup *)em->bdev;
4800 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4801 BTRFS_BLOCK_GROUP_RAID6))
4802 ret = 1;
4803 free_extent_map(em);
4804 return ret;
4805}
4806
Stefan Behrens30d98612012-11-06 14:52:18 +01004807static int find_live_mirror(struct btrfs_fs_info *fs_info,
4808 struct map_lookup *map, int first, int num,
4809 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004810{
4811 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004812 int tolerance;
4813 struct btrfs_device *srcdev;
4814
4815 if (dev_replace_is_ongoing &&
4816 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4817 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4818 srcdev = fs_info->dev_replace.srcdev;
4819 else
4820 srcdev = NULL;
4821
4822 /*
4823 * try to avoid the drive that is the source drive for a
4824 * dev-replace procedure, only choose it if no other non-missing
4825 * mirror is available
4826 */
4827 for (tolerance = 0; tolerance < 2; tolerance++) {
4828 if (map->stripes[optimal].dev->bdev &&
4829 (tolerance || map->stripes[optimal].dev != srcdev))
4830 return optimal;
4831 for (i = first; i < first + num; i++) {
4832 if (map->stripes[i].dev->bdev &&
4833 (tolerance || map->stripes[i].dev != srcdev))
4834 return i;
4835 }
Chris Masondfe25022008-05-13 13:46:40 -04004836 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004837
Chris Masondfe25022008-05-13 13:46:40 -04004838 /* we couldn't find one that doesn't fail. Just return something
4839 * and the io error handling code will clean up eventually
4840 */
4841 return optimal;
4842}
4843
David Woodhouse53b381b2013-01-29 18:40:14 -05004844static inline int parity_smaller(u64 a, u64 b)
4845{
4846 return a > b;
4847}
4848
4849/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4850static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4851{
4852 struct btrfs_bio_stripe s;
4853 int i;
4854 u64 l;
4855 int again = 1;
4856
4857 while (again) {
4858 again = 0;
4859 for (i = 0; i < bbio->num_stripes - 1; i++) {
4860 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4861 s = bbio->stripes[i];
4862 l = raid_map[i];
4863 bbio->stripes[i] = bbio->stripes[i+1];
4864 raid_map[i] = raid_map[i+1];
4865 bbio->stripes[i+1] = s;
4866 raid_map[i+1] = l;
4867 again = 1;
4868 }
4869 }
4870 }
4871}
4872
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004873static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004874 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004875 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004876 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004877{
4878 struct extent_map *em;
4879 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004880 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004881 struct extent_map_tree *em_tree = &map_tree->map_tree;
4882 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004883 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004884 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004885 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004886 u64 stripe_nr_orig;
4887 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004888 u64 stripe_len;
4889 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004890 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004891 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004892 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004893 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004894 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004895 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004896 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4897 int dev_replace_is_ongoing = 0;
4898 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004899 int patch_the_first_stripe_for_dev_replace = 0;
4900 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004901 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004902
Chris Mason890871b2009-09-02 16:24:52 -04004903 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004904 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004905 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004906
Chris Mason3b951512008-04-17 11:29:12 -04004907 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004908 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004909 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004910 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004911 }
Chris Mason0b86a832008-03-24 15:01:56 -04004912
Josef Bacik9bb91872013-04-19 23:45:33 -04004913 if (em->start > logical || em->start + em->len < logical) {
4914 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02004915 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04004916 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004917 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04004918 return -EINVAL;
4919 }
4920
Chris Mason0b86a832008-03-24 15:01:56 -04004921 map = (struct map_lookup *)em->bdev;
4922 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004923
David Woodhouse53b381b2013-01-29 18:40:14 -05004924 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004925 stripe_nr = offset;
4926 /*
4927 * stripe_nr counts the total number of stripes we have to stride
4928 * to get to this block
4929 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004930 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004931
David Woodhouse53b381b2013-01-29 18:40:14 -05004932 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004933 BUG_ON(offset < stripe_offset);
4934
4935 /* stripe_offset is the offset of this block in its stripe*/
4936 stripe_offset = offset - stripe_offset;
4937
David Woodhouse53b381b2013-01-29 18:40:14 -05004938 /* if we're here for raid56, we need to know the stripe aligned start */
4939 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4940 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4941 raid56_full_stripe_start = offset;
4942
4943 /* allow a write of a full stripe, but make sure we don't
4944 * allow straddling of stripes
4945 */
4946 do_div(raid56_full_stripe_start, full_stripe_len);
4947 raid56_full_stripe_start *= full_stripe_len;
4948 }
4949
4950 if (rw & REQ_DISCARD) {
4951 /* we don't discard raid56 yet */
4952 if (map->type &
4953 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4954 ret = -EOPNOTSUPP;
4955 goto out;
4956 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004957 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004958 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4959 u64 max_len;
4960 /* For writes to RAID[56], allow a full stripeset across all disks.
4961 For other RAID types and for RAID[56] reads, just allow a single
4962 stripe (on a single disk). */
4963 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4964 (rw & REQ_WRITE)) {
4965 max_len = stripe_len * nr_data_stripes(map) -
4966 (offset - raid56_full_stripe_start);
4967 } else {
4968 /* we limit the length of each bio to what fits in a stripe */
4969 max_len = stripe_len - stripe_offset;
4970 }
4971 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004972 } else {
4973 *length = em->len - offset;
4974 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004975
David Woodhouse53b381b2013-01-29 18:40:14 -05004976 /* This is for when we're called from btrfs_merge_bio_hook() and all
4977 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004978 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004979 goto out;
4980
Stefan Behrens472262f2012-11-06 14:43:46 +01004981 btrfs_dev_replace_lock(dev_replace);
4982 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4983 if (!dev_replace_is_ongoing)
4984 btrfs_dev_replace_unlock(dev_replace);
4985
Stefan Behrensad6d6202012-11-06 15:06:47 +01004986 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4987 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4988 dev_replace->tgtdev != NULL) {
4989 /*
4990 * in dev-replace case, for repair case (that's the only
4991 * case where the mirror is selected explicitly when
4992 * calling btrfs_map_block), blocks left of the left cursor
4993 * can also be read from the target drive.
4994 * For REQ_GET_READ_MIRRORS, the target drive is added as
4995 * the last one to the array of stripes. For READ, it also
4996 * needs to be supported using the same mirror number.
4997 * If the requested block is not left of the left cursor,
4998 * EIO is returned. This can happen because btrfs_num_copies()
4999 * returns one more in the dev-replace case.
5000 */
5001 u64 tmp_length = *length;
5002 struct btrfs_bio *tmp_bbio = NULL;
5003 int tmp_num_stripes;
5004 u64 srcdev_devid = dev_replace->srcdev->devid;
5005 int index_srcdev = 0;
5006 int found = 0;
5007 u64 physical_of_found = 0;
5008
5009 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05005010 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005011 if (ret) {
5012 WARN_ON(tmp_bbio != NULL);
5013 goto out;
5014 }
5015
5016 tmp_num_stripes = tmp_bbio->num_stripes;
5017 if (mirror_num > tmp_num_stripes) {
5018 /*
5019 * REQ_GET_READ_MIRRORS does not contain this
5020 * mirror, that means that the requested area
5021 * is not left of the left cursor
5022 */
5023 ret = -EIO;
5024 kfree(tmp_bbio);
5025 goto out;
5026 }
5027
5028 /*
5029 * process the rest of the function using the mirror_num
5030 * of the source drive. Therefore look it up first.
5031 * At the end, patch the device pointer to the one of the
5032 * target drive.
5033 */
5034 for (i = 0; i < tmp_num_stripes; i++) {
5035 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
5036 /*
5037 * In case of DUP, in order to keep it
5038 * simple, only add the mirror with the
5039 * lowest physical address
5040 */
5041 if (found &&
5042 physical_of_found <=
5043 tmp_bbio->stripes[i].physical)
5044 continue;
5045 index_srcdev = i;
5046 found = 1;
5047 physical_of_found =
5048 tmp_bbio->stripes[i].physical;
5049 }
5050 }
5051
5052 if (found) {
5053 mirror_num = index_srcdev + 1;
5054 patch_the_first_stripe_for_dev_replace = 1;
5055 physical_to_patch_in_first_stripe = physical_of_found;
5056 } else {
5057 WARN_ON(1);
5058 ret = -EIO;
5059 kfree(tmp_bbio);
5060 goto out;
5061 }
5062
5063 kfree(tmp_bbio);
5064 } else if (mirror_num > map->num_stripes) {
5065 mirror_num = 0;
5066 }
5067
Chris Masonf2d8d742008-04-21 10:03:05 -04005068 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005069 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005070 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005071 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005072 do_div(stripe_nr_end, map->stripe_len);
5073 stripe_end_offset = stripe_nr_end * map->stripe_len -
5074 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005075
Li Dongyangfce3bb92011-03-24 10:24:26 +00005076 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5077 if (rw & REQ_DISCARD)
5078 num_stripes = min_t(u64, map->num_stripes,
5079 stripe_nr_end - stripe_nr_orig);
5080 stripe_index = do_div(stripe_nr, map->num_stripes);
5081 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005082 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005083 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005084 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005085 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005086 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005087 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005088 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005089 current->pid % map->num_stripes,
5090 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005091 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005092 }
Chris Mason2fff7342008-04-29 14:12:09 -04005093
Chris Mason611f0e02008-04-03 16:29:03 -04005094 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005095 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005096 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005097 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005098 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005099 } else {
5100 mirror_num = 1;
5101 }
Chris Mason2fff7342008-04-29 14:12:09 -04005102
Chris Mason321aecc2008-04-16 10:49:51 -04005103 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5104 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005105
5106 stripe_index = do_div(stripe_nr, factor);
5107 stripe_index *= map->sub_stripes;
5108
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005109 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005110 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005111 else if (rw & REQ_DISCARD)
5112 num_stripes = min_t(u64, map->sub_stripes *
5113 (stripe_nr_end - stripe_nr_orig),
5114 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005115 else if (mirror_num)
5116 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005117 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005118 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005119 stripe_index = find_live_mirror(fs_info, map,
5120 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005121 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005122 current->pid % map->sub_stripes,
5123 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005124 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005125 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005126
5127 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5128 BTRFS_BLOCK_GROUP_RAID6)) {
5129 u64 tmp;
5130
5131 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
5132 && raid_map_ret) {
5133 int i, rot;
5134
5135 /* push stripe_nr back to the start of the full stripe */
5136 stripe_nr = raid56_full_stripe_start;
5137 do_div(stripe_nr, stripe_len);
5138
5139 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5140
5141 /* RAID[56] write or recovery. Return all stripes */
5142 num_stripes = map->num_stripes;
5143 max_errors = nr_parity_stripes(map);
5144
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05305145 raid_map = kmalloc_array(num_stripes, sizeof(u64),
David Woodhouse53b381b2013-01-29 18:40:14 -05005146 GFP_NOFS);
5147 if (!raid_map) {
5148 ret = -ENOMEM;
5149 goto out;
5150 }
5151
5152 /* Work out the disk rotation on this stripe-set */
5153 tmp = stripe_nr;
5154 rot = do_div(tmp, num_stripes);
5155
5156 /* Fill in the logical address of each stripe */
5157 tmp = stripe_nr * nr_data_stripes(map);
5158 for (i = 0; i < nr_data_stripes(map); i++)
5159 raid_map[(i+rot) % num_stripes] =
5160 em->start + (tmp + i) * map->stripe_len;
5161
5162 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5163 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5164 raid_map[(i+rot+1) % num_stripes] =
5165 RAID6_Q_STRIPE;
5166
5167 *length = map->stripe_len;
5168 stripe_index = 0;
5169 stripe_offset = 0;
5170 } else {
5171 /*
5172 * Mirror #0 or #1 means the original data block.
5173 * Mirror #2 is RAID5 parity block.
5174 * Mirror #3 is RAID6 Q block.
5175 */
5176 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5177 if (mirror_num > 1)
5178 stripe_index = nr_data_stripes(map) +
5179 mirror_num - 2;
5180
5181 /* We distribute the parity blocks across stripes */
5182 tmp = stripe_nr + stripe_index;
5183 stripe_index = do_div(tmp, map->num_stripes);
5184 }
Chris Mason8790d502008-04-03 16:29:03 -04005185 } else {
5186 /*
5187 * after this do_div call, stripe_nr is the number of stripes
5188 * on this device we have to walk to find the data, and
5189 * stripe_index is the number of our device in the stripe array
5190 */
5191 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005192 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005193 }
Chris Mason593060d2008-03-25 16:50:33 -04005194 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005195
Stefan Behrens472262f2012-11-06 14:43:46 +01005196 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005197 if (dev_replace_is_ongoing) {
5198 if (rw & (REQ_WRITE | REQ_DISCARD))
5199 num_alloc_stripes <<= 1;
5200 if (rw & REQ_GET_READ_MIRRORS)
5201 num_alloc_stripes++;
5202 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005203 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08005204 if (!bbio) {
Dave Joneseb2067f2013-07-30 13:42:17 -04005205 kfree(raid_map);
Li Zefande11cc12011-12-01 12:55:47 +08005206 ret = -ENOMEM;
5207 goto out;
5208 }
5209 atomic_set(&bbio->error, 0);
5210
Li Dongyangfce3bb92011-03-24 10:24:26 +00005211 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08005212 int factor = 0;
5213 int sub_stripes = 0;
5214 u64 stripes_per_dev = 0;
5215 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005216 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005217
5218 if (map->type &
5219 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5220 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5221 sub_stripes = 1;
5222 else
5223 sub_stripes = map->sub_stripes;
5224
5225 factor = map->num_stripes / sub_stripes;
5226 stripes_per_dev = div_u64_rem(stripe_nr_end -
5227 stripe_nr_orig,
5228 factor,
5229 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005230 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5231 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005232 }
5233
Li Dongyangfce3bb92011-03-24 10:24:26 +00005234 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005235 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005236 map->stripes[stripe_index].physical +
5237 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005238 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005239
Li Zefanec9ef7a2011-12-01 14:06:42 +08005240 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5241 BTRFS_BLOCK_GROUP_RAID10)) {
5242 bbio->stripes[i].length = stripes_per_dev *
5243 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005244
Li Zefanec9ef7a2011-12-01 14:06:42 +08005245 if (i / sub_stripes < remaining_stripes)
5246 bbio->stripes[i].length +=
5247 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005248
5249 /*
5250 * Special for the first stripe and
5251 * the last stripe:
5252 *
5253 * |-------|...|-------|
5254 * |----------|
5255 * off end_off
5256 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005257 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005258 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005259 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005260
5261 if (stripe_index >= last_stripe &&
5262 stripe_index <= (last_stripe +
5263 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005264 bbio->stripes[i].length -=
5265 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005266
Li Zefanec9ef7a2011-12-01 14:06:42 +08005267 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005268 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005269 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005270 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005271
5272 stripe_index++;
5273 if (stripe_index == map->num_stripes) {
5274 /* This could only happen for RAID0/10 */
5275 stripe_index = 0;
5276 stripe_nr++;
5277 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005278 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005279 } else {
5280 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005281 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005282 map->stripes[stripe_index].physical +
5283 stripe_offset +
5284 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005285 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005286 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005287 stripe_index++;
5288 }
Chris Mason593060d2008-03-25 16:50:33 -04005289 }
Li Zefande11cc12011-12-01 12:55:47 +08005290
Miao Xied20983b2014-07-03 18:22:13 +08005291 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5292 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005293
Stefan Behrens472262f2012-11-06 14:43:46 +01005294 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5295 dev_replace->tgtdev != NULL) {
5296 int index_where_to_add;
5297 u64 srcdev_devid = dev_replace->srcdev->devid;
5298
5299 /*
5300 * duplicate the write operations while the dev replace
5301 * procedure is running. Since the copying of the old disk
5302 * to the new disk takes place at run time while the
5303 * filesystem is mounted writable, the regular write
5304 * operations to the old disk have to be duplicated to go
5305 * to the new disk as well.
5306 * Note that device->missing is handled by the caller, and
5307 * that the write to the old disk is already set up in the
5308 * stripes array.
5309 */
5310 index_where_to_add = num_stripes;
5311 for (i = 0; i < num_stripes; i++) {
5312 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5313 /* write to new disk, too */
5314 struct btrfs_bio_stripe *new =
5315 bbio->stripes + index_where_to_add;
5316 struct btrfs_bio_stripe *old =
5317 bbio->stripes + i;
5318
5319 new->physical = old->physical;
5320 new->length = old->length;
5321 new->dev = dev_replace->tgtdev;
5322 index_where_to_add++;
5323 max_errors++;
5324 }
5325 }
5326 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005327 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5328 dev_replace->tgtdev != NULL) {
5329 u64 srcdev_devid = dev_replace->srcdev->devid;
5330 int index_srcdev = 0;
5331 int found = 0;
5332 u64 physical_of_found = 0;
5333
5334 /*
5335 * During the dev-replace procedure, the target drive can
5336 * also be used to read data in case it is needed to repair
5337 * a corrupt block elsewhere. This is possible if the
5338 * requested area is left of the left cursor. In this area,
5339 * the target drive is a full copy of the source drive.
5340 */
5341 for (i = 0; i < num_stripes; i++) {
5342 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5343 /*
5344 * In case of DUP, in order to keep it
5345 * simple, only add the mirror with the
5346 * lowest physical address
5347 */
5348 if (found &&
5349 physical_of_found <=
5350 bbio->stripes[i].physical)
5351 continue;
5352 index_srcdev = i;
5353 found = 1;
5354 physical_of_found = bbio->stripes[i].physical;
5355 }
5356 }
5357 if (found) {
5358 u64 length = map->stripe_len;
5359
5360 if (physical_of_found + length <=
5361 dev_replace->cursor_left) {
5362 struct btrfs_bio_stripe *tgtdev_stripe =
5363 bbio->stripes + num_stripes;
5364
5365 tgtdev_stripe->physical = physical_of_found;
5366 tgtdev_stripe->length =
5367 bbio->stripes[index_srcdev].length;
5368 tgtdev_stripe->dev = dev_replace->tgtdev;
5369
5370 num_stripes++;
5371 }
5372 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005373 }
5374
Li Zefande11cc12011-12-01 12:55:47 +08005375 *bbio_ret = bbio;
5376 bbio->num_stripes = num_stripes;
5377 bbio->max_errors = max_errors;
5378 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005379
5380 /*
5381 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5382 * mirror_num == num_stripes + 1 && dev_replace target drive is
5383 * available as a mirror
5384 */
5385 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5386 WARN_ON(num_stripes > 1);
5387 bbio->stripes[0].dev = dev_replace->tgtdev;
5388 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5389 bbio->mirror_num = map->num_stripes + 1;
5390 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005391 if (raid_map) {
5392 sort_parity_stripes(bbio, raid_map);
5393 *raid_map_ret = raid_map;
5394 }
Chris Masoncea9e442008-04-09 16:28:12 -04005395out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005396 if (dev_replace_is_ongoing)
5397 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005398 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005399 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005400}
5401
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005402int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005403 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005404 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005405{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005406 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05005407 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04005408}
5409
Yan Zhenga512bbf2008-12-08 16:46:26 -05005410int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5411 u64 chunk_start, u64 physical, u64 devid,
5412 u64 **logical, int *naddrs, int *stripe_len)
5413{
5414 struct extent_map_tree *em_tree = &map_tree->map_tree;
5415 struct extent_map *em;
5416 struct map_lookup *map;
5417 u64 *buf;
5418 u64 bytenr;
5419 u64 length;
5420 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005421 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005422 int i, j, nr = 0;
5423
Chris Mason890871b2009-09-02 16:24:52 -04005424 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005425 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005426 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005427
Josef Bacik835d9742013-03-19 12:13:25 -04005428 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005429 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005430 chunk_start);
5431 return -EIO;
5432 }
5433
5434 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005435 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005436 em->start, chunk_start);
5437 free_extent_map(em);
5438 return -EIO;
5439 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005440 map = (struct map_lookup *)em->bdev;
5441
5442 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005443 rmap_len = map->stripe_len;
5444
Yan Zhenga512bbf2008-12-08 16:46:26 -05005445 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5446 do_div(length, map->num_stripes / map->sub_stripes);
5447 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5448 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05005449 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5450 BTRFS_BLOCK_GROUP_RAID6)) {
5451 do_div(length, nr_data_stripes(map));
5452 rmap_len = map->stripe_len * nr_data_stripes(map);
5453 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005454
5455 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005456 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005457
5458 for (i = 0; i < map->num_stripes; i++) {
5459 if (devid && map->stripes[i].dev->devid != devid)
5460 continue;
5461 if (map->stripes[i].physical > physical ||
5462 map->stripes[i].physical + length <= physical)
5463 continue;
5464
5465 stripe_nr = physical - map->stripes[i].physical;
5466 do_div(stripe_nr, map->stripe_len);
5467
5468 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5469 stripe_nr = stripe_nr * map->num_stripes + i;
5470 do_div(stripe_nr, map->sub_stripes);
5471 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5472 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005473 } /* else if RAID[56], multiply by nr_data_stripes().
5474 * Alternatively, just use rmap_len below instead of
5475 * map->stripe_len */
5476
5477 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005478 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005479 for (j = 0; j < nr; j++) {
5480 if (buf[j] == bytenr)
5481 break;
5482 }
Chris Mason934d3752008-12-08 16:43:10 -05005483 if (j == nr) {
5484 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005485 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005486 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005487 }
5488
Yan Zhenga512bbf2008-12-08 16:46:26 -05005489 *logical = buf;
5490 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005491 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005492
5493 free_extent_map(em);
5494 return 0;
5495}
5496
Miao Xie8408c712014-06-19 10:42:55 +08005497static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5498{
5499 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5500 bio_endio_nodec(bio, err);
5501 else
5502 bio_endio(bio, err);
5503 kfree(bbio);
5504}
5505
Jan Schmidta1d3c472011-08-04 17:15:33 +02005506static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005507{
Chris Mason9be33952013-05-17 18:30:14 -04005508 struct btrfs_bio *bbio = bio->bi_private;
Miao Xiec404e0d2014-01-30 16:46:55 +08005509 struct btrfs_device *dev = bbio->stripes[0].dev;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005510 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005511
Stefan Behrens442a4f62012-05-25 16:06:08 +02005512 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005513 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005514 if (err == -EIO || err == -EREMOTEIO) {
5515 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005516 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005517
5518 BUG_ON(stripe_index >= bbio->num_stripes);
5519 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005520 if (dev->bdev) {
5521 if (bio->bi_rw & WRITE)
5522 btrfs_dev_stat_inc(dev,
5523 BTRFS_DEV_STAT_WRITE_ERRS);
5524 else
5525 btrfs_dev_stat_inc(dev,
5526 BTRFS_DEV_STAT_READ_ERRS);
5527 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5528 btrfs_dev_stat_inc(dev,
5529 BTRFS_DEV_STAT_FLUSH_ERRS);
5530 btrfs_dev_stat_print_on_error(dev);
5531 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005532 }
5533 }
Chris Mason8790d502008-04-03 16:29:03 -04005534
Jan Schmidta1d3c472011-08-04 17:15:33 +02005535 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005536 is_orig_bio = 1;
5537
Miao Xiec404e0d2014-01-30 16:46:55 +08005538 btrfs_bio_counter_dec(bbio->fs_info);
5539
Jan Schmidta1d3c472011-08-04 17:15:33 +02005540 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005541 if (!is_orig_bio) {
5542 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005543 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005544 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005545
Jan Schmidta1d3c472011-08-04 17:15:33 +02005546 bio->bi_private = bbio->private;
5547 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005548 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005549 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005550 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005551 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005552 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005553 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005554 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005555 /*
5556 * this bio is actually up to date, we didn't
5557 * go over the max number of errors
5558 */
5559 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005560 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005561 }
Miao Xiec55f1392014-06-19 10:42:54 +08005562
Miao Xie8408c712014-06-19 10:42:55 +08005563 btrfs_end_bbio(bbio, bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005564 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005565 bio_put(bio);
5566 }
Chris Mason8790d502008-04-03 16:29:03 -04005567}
5568
Chris Mason8b712842008-06-11 16:50:36 -04005569/*
5570 * see run_scheduled_bios for a description of why bios are collected for
5571 * async submit.
5572 *
5573 * This will add one bio to the pending list for a device and make sure
5574 * the work struct is scheduled.
5575 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005576static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5577 struct btrfs_device *device,
5578 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005579{
5580 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005581 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005582
David Woodhouse53b381b2013-01-29 18:40:14 -05005583 if (device->missing || !device->bdev) {
5584 bio_endio(bio, -EIO);
5585 return;
5586 }
5587
Chris Mason8b712842008-06-11 16:50:36 -04005588 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005589 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005590 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005591 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005592 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005593 return;
Chris Mason8b712842008-06-11 16:50:36 -04005594 }
5595
5596 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005597 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005598 * higher layers. Otherwise, the async bio makes it appear we have
5599 * made progress against dirty pages when we've really just put it
5600 * on a queue for later
5601 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005602 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005603 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005604 bio->bi_next = NULL;
5605 bio->bi_rw |= rw;
5606
5607 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005608 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005609 pending_bios = &device->pending_sync_bios;
5610 else
5611 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005612
Chris Masonffbd5172009-04-20 15:50:09 -04005613 if (pending_bios->tail)
5614 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005615
Chris Masonffbd5172009-04-20 15:50:09 -04005616 pending_bios->tail = bio;
5617 if (!pending_bios->head)
5618 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005619 if (device->running_pending)
5620 should_queue = 0;
5621
5622 spin_unlock(&device->io_lock);
5623
5624 if (should_queue)
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +08005625 btrfs_queue_work(root->fs_info->submit_workers,
5626 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005627}
5628
Josef Bacikde1ee922012-10-19 16:50:56 -04005629static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5630 sector_t sector)
5631{
5632 struct bio_vec *prev;
5633 struct request_queue *q = bdev_get_queue(bdev);
Akinobu Mita475bf362013-11-18 22:13:18 +09005634 unsigned int max_sectors = queue_max_sectors(q);
Josef Bacikde1ee922012-10-19 16:50:56 -04005635 struct bvec_merge_data bvm = {
5636 .bi_bdev = bdev,
5637 .bi_sector = sector,
5638 .bi_rw = bio->bi_rw,
5639 };
5640
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305641 if (WARN_ON(bio->bi_vcnt == 0))
Josef Bacikde1ee922012-10-19 16:50:56 -04005642 return 1;
Josef Bacikde1ee922012-10-19 16:50:56 -04005643
5644 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005645 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005646 return 0;
5647
5648 if (!q->merge_bvec_fn)
5649 return 1;
5650
Kent Overstreet4f024f32013-10-11 15:44:27 -07005651 bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len;
Josef Bacikde1ee922012-10-19 16:50:56 -04005652 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5653 return 0;
5654 return 1;
5655}
5656
5657static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5658 struct bio *bio, u64 physical, int dev_nr,
5659 int rw, int async)
5660{
5661 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5662
5663 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005664 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005665 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005666 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005667#ifdef DEBUG
5668 {
5669 struct rcu_string *name;
5670
5671 rcu_read_lock();
5672 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005673 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005674 "(%s id %llu), size=%u\n", rw,
5675 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5676 name->str, dev->devid, bio->bi_size);
5677 rcu_read_unlock();
5678 }
5679#endif
5680 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005681
5682 btrfs_bio_counter_inc_noblocked(root->fs_info);
5683
Josef Bacikde1ee922012-10-19 16:50:56 -04005684 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005685 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005686 else
5687 btrfsic_submit_bio(rw, bio);
5688}
5689
5690static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5691 struct bio *first_bio, struct btrfs_device *dev,
5692 int dev_nr, int rw, int async)
5693{
5694 struct bio_vec *bvec = first_bio->bi_io_vec;
5695 struct bio *bio;
5696 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5697 u64 physical = bbio->stripes[dev_nr].physical;
5698
5699again:
5700 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5701 if (!bio)
5702 return -ENOMEM;
5703
5704 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5705 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5706 bvec->bv_offset) < bvec->bv_len) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07005707 u64 len = bio->bi_iter.bi_size;
Josef Bacikde1ee922012-10-19 16:50:56 -04005708
5709 atomic_inc(&bbio->stripes_pending);
5710 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5711 rw, async);
5712 physical += len;
5713 goto again;
5714 }
5715 bvec++;
5716 }
5717
5718 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5719 return 0;
5720}
5721
5722static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5723{
5724 atomic_inc(&bbio->error);
5725 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005726 /* Shoud be the original bio. */
5727 WARN_ON(bio != bbio->orig_bio);
5728
Josef Bacikde1ee922012-10-19 16:50:56 -04005729 bio->bi_private = bbio->private;
5730 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005731 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005732 bio->bi_iter.bi_sector = logical >> 9;
Miao Xie8408c712014-06-19 10:42:55 +08005733
5734 btrfs_end_bbio(bbio, bio, -EIO);
Josef Bacikde1ee922012-10-19 16:50:56 -04005735 }
5736}
5737
Chris Masonf1885912008-04-09 16:28:12 -04005738int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005739 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005740{
Chris Mason0b86a832008-03-24 15:01:56 -04005741 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005742 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005743 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005744 u64 length = 0;
5745 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005746 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005747 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005748 int dev_nr = 0;
5749 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005750 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005751
Kent Overstreet4f024f32013-10-11 15:44:27 -07005752 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005753 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005754
Miao Xiec404e0d2014-01-30 16:46:55 +08005755 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005756 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5757 mirror_num, &raid_map);
Miao Xiec404e0d2014-01-30 16:46:55 +08005758 if (ret) {
5759 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005760 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005761 }
Chris Masoncea9e442008-04-09 16:28:12 -04005762
Jan Schmidta1d3c472011-08-04 17:15:33 +02005763 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005764 bbio->orig_bio = first_bio;
5765 bbio->private = first_bio->bi_private;
5766 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005767 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005768 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5769
5770 if (raid_map) {
5771 /* In this case, map_length has been set to the length of
5772 a single stripe; not the whole write */
5773 if (rw & WRITE) {
Miao Xiec404e0d2014-01-30 16:46:55 +08005774 ret = raid56_parity_write(root, bio, bbio,
5775 raid_map, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005776 } else {
Miao Xiec404e0d2014-01-30 16:46:55 +08005777 ret = raid56_parity_recover(root, bio, bbio,
5778 raid_map, map_length,
5779 mirror_num);
David Woodhouse53b381b2013-01-29 18:40:14 -05005780 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005781 /*
5782 * FIXME, replace dosen't support raid56 yet, please fix
5783 * it in the future.
5784 */
5785 btrfs_bio_counter_dec(root->fs_info);
5786 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005787 }
5788
Chris Masoncea9e442008-04-09 16:28:12 -04005789 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005790 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005791 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005792 BUG();
5793 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005794
Chris Masond3977122009-01-05 21:25:51 -05005795 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005796 dev = bbio->stripes[dev_nr].dev;
5797 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5798 bbio_error(bbio, first_bio, logical);
5799 dev_nr++;
5800 continue;
5801 }
5802
5803 /*
5804 * Check and see if we're ok with this bio based on it's size
5805 * and offset with the given device.
5806 */
5807 if (!bio_size_ok(dev->bdev, first_bio,
5808 bbio->stripes[dev_nr].physical >> 9)) {
5809 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5810 dev_nr, rw, async_submit);
5811 BUG_ON(ret);
5812 dev_nr++;
5813 continue;
5814 }
5815
Jan Schmidta1d3c472011-08-04 17:15:33 +02005816 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005817 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005818 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005819 } else {
5820 bio = first_bio;
Miao Xiec55f1392014-06-19 10:42:54 +08005821 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
Chris Mason8790d502008-04-03 16:29:03 -04005822 }
Josef Bacik606686e2012-06-04 14:03:51 -04005823
Josef Bacikde1ee922012-10-19 16:50:56 -04005824 submit_stripe_bio(root, bbio, bio,
5825 bbio->stripes[dev_nr].physical, dev_nr, rw,
5826 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005827 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005828 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005829 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04005830 return 0;
5831}
5832
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005833struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005834 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005835{
Yan Zheng2b820322008-11-17 21:11:30 -05005836 struct btrfs_device *device;
5837 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005838
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005839 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005840 while (cur_devices) {
5841 if (!fsid ||
5842 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5843 device = __find_device(&cur_devices->devices,
5844 devid, uuid);
5845 if (device)
5846 return device;
5847 }
5848 cur_devices = cur_devices->seed;
5849 }
5850 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005851}
5852
Chris Masondfe25022008-05-13 13:46:40 -04005853static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5854 u64 devid, u8 *dev_uuid)
5855{
5856 struct btrfs_device *device;
5857 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5858
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005859 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5860 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005861 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005862
5863 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005864 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04005865 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005866
5867 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05005868 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005869
Chris Masondfe25022008-05-13 13:46:40 -04005870 return device;
5871}
5872
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005873/**
5874 * btrfs_alloc_device - allocate struct btrfs_device
5875 * @fs_info: used only for generating a new devid, can be NULL if
5876 * devid is provided (i.e. @devid != NULL).
5877 * @devid: a pointer to devid for this device. If NULL a new devid
5878 * is generated.
5879 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5880 * is generated.
5881 *
5882 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5883 * on error. Returned struct is not linked onto any lists and can be
5884 * destroyed with kfree() right away.
5885 */
5886struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5887 const u64 *devid,
5888 const u8 *uuid)
5889{
5890 struct btrfs_device *dev;
5891 u64 tmp;
5892
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305893 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005894 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005895
5896 dev = __alloc_device();
5897 if (IS_ERR(dev))
5898 return dev;
5899
5900 if (devid)
5901 tmp = *devid;
5902 else {
5903 int ret;
5904
5905 ret = find_next_devid(fs_info, &tmp);
5906 if (ret) {
5907 kfree(dev);
5908 return ERR_PTR(ret);
5909 }
5910 }
5911 dev->devid = tmp;
5912
5913 if (uuid)
5914 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5915 else
5916 generate_random_uuid(dev->uuid);
5917
Liu Bo9e0af232014-08-15 23:36:53 +08005918 btrfs_init_work(&dev->work, btrfs_submit_helper,
5919 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005920
5921 return dev;
5922}
5923
Chris Mason0b86a832008-03-24 15:01:56 -04005924static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5925 struct extent_buffer *leaf,
5926 struct btrfs_chunk *chunk)
5927{
5928 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5929 struct map_lookup *map;
5930 struct extent_map *em;
5931 u64 logical;
5932 u64 length;
5933 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005934 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005935 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005936 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005937 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005938
Chris Masone17cade2008-04-15 15:41:47 -04005939 logical = key->offset;
5940 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005941
Chris Mason890871b2009-09-02 16:24:52 -04005942 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005943 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005944 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005945
5946 /* already mapped? */
5947 if (em && em->start <= logical && em->start + em->len > logical) {
5948 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005949 return 0;
5950 } else if (em) {
5951 free_extent_map(em);
5952 }
Chris Mason0b86a832008-03-24 15:01:56 -04005953
David Sterba172ddd62011-04-21 00:48:27 +02005954 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005955 if (!em)
5956 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005957 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5958 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005959 if (!map) {
5960 free_extent_map(em);
5961 return -ENOMEM;
5962 }
5963
Wang Shilong298a8f92014-06-19 10:42:52 +08005964 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04005965 em->bdev = (struct block_device *)map;
5966 em->start = logical;
5967 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005968 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005969 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005970 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005971
Chris Mason593060d2008-03-25 16:50:33 -04005972 map->num_stripes = num_stripes;
5973 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5974 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5975 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5976 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5977 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005978 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005979 for (i = 0; i < num_stripes; i++) {
5980 map->stripes[i].physical =
5981 btrfs_stripe_offset_nr(leaf, chunk, i);
5982 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005983 read_extent_buffer(leaf, uuid, (unsigned long)
5984 btrfs_stripe_dev_uuid_nr(chunk, i),
5985 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005986 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5987 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005988 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005989 free_extent_map(em);
5990 return -EIO;
5991 }
Chris Masondfe25022008-05-13 13:46:40 -04005992 if (!map->stripes[i].dev) {
5993 map->stripes[i].dev =
5994 add_missing_dev(root, devid, uuid);
5995 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04005996 free_extent_map(em);
5997 return -EIO;
5998 }
5999 }
6000 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04006001 }
6002
Chris Mason890871b2009-09-02 16:24:52 -04006003 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006004 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006005 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006006 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006007 free_extent_map(em);
6008
6009 return 0;
6010}
6011
Jeff Mahoney143bede2012-03-01 14:56:26 +01006012static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006013 struct btrfs_dev_item *dev_item,
6014 struct btrfs_device *device)
6015{
6016 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006017
6018 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006019 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6020 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006021 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006022 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006023 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006024 device->type = btrfs_device_type(leaf, dev_item);
6025 device->io_align = btrfs_device_io_align(leaf, dev_item);
6026 device->io_width = btrfs_device_io_width(leaf, dev_item);
6027 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006028 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006029 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006030
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006031 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006032 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006033}
6034
Yan Zheng2b820322008-11-17 21:11:30 -05006035static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
6036{
6037 struct btrfs_fs_devices *fs_devices;
6038 int ret;
6039
Li Zefanb367e472011-12-07 11:38:24 +08006040 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05006041
6042 fs_devices = root->fs_info->fs_devices->seed;
6043 while (fs_devices) {
6044 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
6045 ret = 0;
6046 goto out;
6047 }
6048 fs_devices = fs_devices->seed;
6049 }
6050
6051 fs_devices = find_fsid(fsid);
6052 if (!fs_devices) {
6053 ret = -ENOENT;
6054 goto out;
6055 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006056
6057 fs_devices = clone_fs_devices(fs_devices);
6058 if (IS_ERR(fs_devices)) {
6059 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006060 goto out;
6061 }
6062
Christoph Hellwig97288f22008-12-02 06:36:09 -05006063 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006064 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006065 if (ret) {
6066 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006067 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006068 }
Yan Zheng2b820322008-11-17 21:11:30 -05006069
6070 if (!fs_devices->seeding) {
6071 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006072 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006073 ret = -EINVAL;
6074 goto out;
6075 }
6076
6077 fs_devices->seed = root->fs_info->fs_devices->seed;
6078 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006079out:
Yan Zheng2b820322008-11-17 21:11:30 -05006080 return ret;
6081}
6082
Chris Mason0d81ba52008-03-24 15:02:07 -04006083static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006084 struct extent_buffer *leaf,
6085 struct btrfs_dev_item *dev_item)
6086{
6087 struct btrfs_device *device;
6088 u64 devid;
6089 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006090 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006091 u8 dev_uuid[BTRFS_UUID_SIZE];
6092
Chris Mason0b86a832008-03-24 15:01:56 -04006093 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006094 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006095 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006096 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006097 BTRFS_UUID_SIZE);
6098
6099 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
6100 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05006101 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006102 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006103 }
6104
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006105 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006106 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006107 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006108 return -EIO;
6109
6110 if (!device) {
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006111 btrfs_warn(root->fs_info, "devid %llu missing", devid);
Yan Zheng2b820322008-11-17 21:11:30 -05006112 device = add_missing_dev(root, devid, dev_uuid);
6113 if (!device)
6114 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05006115 } else if (!device->missing) {
6116 /*
6117 * this happens when a device that was properly setup
6118 * in the device info lists suddenly goes bad.
6119 * device->bdev is NULL, and so we have to set
6120 * device->missing to one here
6121 */
6122 root->fs_info->fs_devices->missing_devices++;
6123 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006124 }
6125 }
6126
6127 if (device->fs_devices != root->fs_info->fs_devices) {
6128 BUG_ON(device->writeable);
6129 if (device->generation !=
6130 btrfs_device_generation(leaf, dev_item))
6131 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006132 }
Chris Mason0b86a832008-03-24 15:01:56 -04006133
6134 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006135 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006136 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006137 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006138 spin_lock(&root->fs_info->free_chunk_lock);
6139 root->fs_info->free_chunk_space += device->total_bytes -
6140 device->bytes_used;
6141 spin_unlock(&root->fs_info->free_chunk_lock);
6142 }
Chris Mason0b86a832008-03-24 15:01:56 -04006143 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006144 return ret;
6145}
6146
Yan Zhenge4404d62008-12-12 10:03:26 -05006147int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006148{
David Sterba6c417612011-04-13 15:41:04 +02006149 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006150 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006151 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006152 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04006153 u8 *ptr;
6154 unsigned long sb_ptr;
6155 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006156 u32 num_stripes;
6157 u32 array_size;
6158 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006159 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04006160 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006161
Yan Zhenge4404d62008-12-12 10:03:26 -05006162 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04006163 BTRFS_SUPER_INFO_SIZE);
6164 if (!sb)
6165 return -ENOMEM;
6166 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006167 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006168 /*
6169 * The sb extent buffer is artifical and just used to read the system array.
6170 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6171 * pages up-to-date when the page is larger: extent does not cover the
6172 * whole page and consequently check_page_uptodate does not find all
6173 * the page's extents up-to-date (the hole beyond sb),
6174 * write_extent_buffer then triggers a WARN_ON.
6175 *
6176 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6177 * but sb spans only this function. Add an explicit SetPageUptodate call
6178 * to silence the warning eg. on PowerPC 64.
6179 */
6180 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006181 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006182
Chris Masona061fc82008-05-07 11:43:44 -04006183 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006184 array_size = btrfs_super_sys_array_size(super_copy);
6185
Chris Mason0b86a832008-03-24 15:01:56 -04006186 ptr = super_copy->sys_chunk_array;
6187 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
6188 cur = 0;
6189
6190 while (cur < array_size) {
6191 disk_key = (struct btrfs_disk_key *)ptr;
6192 btrfs_disk_key_to_cpu(&key, disk_key);
6193
Chris Masona061fc82008-05-07 11:43:44 -04006194 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006195 sb_ptr += len;
6196 cur += len;
6197
Chris Mason0d81ba52008-03-24 15:02:07 -04006198 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04006199 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04006200 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006201 if (ret)
6202 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006203 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6204 len = btrfs_chunk_item_size(num_stripes);
6205 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006206 ret = -EIO;
6207 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006208 }
6209 ptr += len;
6210 sb_ptr += len;
6211 cur += len;
6212 }
Chris Masona061fc82008-05-07 11:43:44 -04006213 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006214 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006215}
6216
6217int btrfs_read_chunk_tree(struct btrfs_root *root)
6218{
6219 struct btrfs_path *path;
6220 struct extent_buffer *leaf;
6221 struct btrfs_key key;
6222 struct btrfs_key found_key;
6223 int ret;
6224 int slot;
6225
6226 root = root->fs_info->chunk_root;
6227
6228 path = btrfs_alloc_path();
6229 if (!path)
6230 return -ENOMEM;
6231
Li Zefanb367e472011-12-07 11:38:24 +08006232 mutex_lock(&uuid_mutex);
6233 lock_chunks(root);
6234
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006235 /*
6236 * Read all device items, and then all the chunk items. All
6237 * device items are found before any chunk item (their object id
6238 * is smaller than the lowest possible object id for a chunk
6239 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006240 */
6241 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6242 key.offset = 0;
6243 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006244 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006245 if (ret < 0)
6246 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006247 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006248 leaf = path->nodes[0];
6249 slot = path->slots[0];
6250 if (slot >= btrfs_header_nritems(leaf)) {
6251 ret = btrfs_next_leaf(root, path);
6252 if (ret == 0)
6253 continue;
6254 if (ret < 0)
6255 goto error;
6256 break;
6257 }
6258 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006259 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6260 struct btrfs_dev_item *dev_item;
6261 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006262 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006263 ret = read_one_dev(root, leaf, dev_item);
6264 if (ret)
6265 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006266 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6267 struct btrfs_chunk *chunk;
6268 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6269 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006270 if (ret)
6271 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006272 }
6273 path->slots[0]++;
6274 }
Chris Mason0b86a832008-03-24 15:01:56 -04006275 ret = 0;
6276error:
Li Zefanb367e472011-12-07 11:38:24 +08006277 unlock_chunks(root);
6278 mutex_unlock(&uuid_mutex);
6279
Yan Zheng2b820322008-11-17 21:11:30 -05006280 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006281 return ret;
6282}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006283
Miao Xiecb517ea2013-05-15 07:48:19 +00006284void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6285{
6286 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6287 struct btrfs_device *device;
6288
Liu Bo29cc83f2014-05-11 23:14:59 +08006289 while (fs_devices) {
6290 mutex_lock(&fs_devices->device_list_mutex);
6291 list_for_each_entry(device, &fs_devices->devices, dev_list)
6292 device->dev_root = fs_info->dev_root;
6293 mutex_unlock(&fs_devices->device_list_mutex);
6294
6295 fs_devices = fs_devices->seed;
6296 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006297}
6298
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006299static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6300{
6301 int i;
6302
6303 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6304 btrfs_dev_stat_reset(dev, i);
6305}
6306
6307int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6308{
6309 struct btrfs_key key;
6310 struct btrfs_key found_key;
6311 struct btrfs_root *dev_root = fs_info->dev_root;
6312 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6313 struct extent_buffer *eb;
6314 int slot;
6315 int ret = 0;
6316 struct btrfs_device *device;
6317 struct btrfs_path *path = NULL;
6318 int i;
6319
6320 path = btrfs_alloc_path();
6321 if (!path) {
6322 ret = -ENOMEM;
6323 goto out;
6324 }
6325
6326 mutex_lock(&fs_devices->device_list_mutex);
6327 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6328 int item_size;
6329 struct btrfs_dev_stats_item *ptr;
6330
6331 key.objectid = 0;
6332 key.type = BTRFS_DEV_STATS_KEY;
6333 key.offset = device->devid;
6334 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6335 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006336 __btrfs_reset_dev_stats(device);
6337 device->dev_stats_valid = 1;
6338 btrfs_release_path(path);
6339 continue;
6340 }
6341 slot = path->slots[0];
6342 eb = path->nodes[0];
6343 btrfs_item_key_to_cpu(eb, &found_key, slot);
6344 item_size = btrfs_item_size_nr(eb, slot);
6345
6346 ptr = btrfs_item_ptr(eb, slot,
6347 struct btrfs_dev_stats_item);
6348
6349 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6350 if (item_size >= (1 + i) * sizeof(__le64))
6351 btrfs_dev_stat_set(device, i,
6352 btrfs_dev_stats_value(eb, ptr, i));
6353 else
6354 btrfs_dev_stat_reset(device, i);
6355 }
6356
6357 device->dev_stats_valid = 1;
6358 btrfs_dev_stat_print_on_load(device);
6359 btrfs_release_path(path);
6360 }
6361 mutex_unlock(&fs_devices->device_list_mutex);
6362
6363out:
6364 btrfs_free_path(path);
6365 return ret < 0 ? ret : 0;
6366}
6367
6368static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6369 struct btrfs_root *dev_root,
6370 struct btrfs_device *device)
6371{
6372 struct btrfs_path *path;
6373 struct btrfs_key key;
6374 struct extent_buffer *eb;
6375 struct btrfs_dev_stats_item *ptr;
6376 int ret;
6377 int i;
6378
6379 key.objectid = 0;
6380 key.type = BTRFS_DEV_STATS_KEY;
6381 key.offset = device->devid;
6382
6383 path = btrfs_alloc_path();
6384 BUG_ON(!path);
6385 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6386 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006387 printk_in_rcu(KERN_WARNING "BTRFS: "
6388 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006389 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006390 goto out;
6391 }
6392
6393 if (ret == 0 &&
6394 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6395 /* need to delete old one and insert a new one */
6396 ret = btrfs_del_item(trans, dev_root, path);
6397 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006398 printk_in_rcu(KERN_WARNING "BTRFS: "
6399 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006400 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006401 goto out;
6402 }
6403 ret = 1;
6404 }
6405
6406 if (ret == 1) {
6407 /* need to insert a new item */
6408 btrfs_release_path(path);
6409 ret = btrfs_insert_empty_item(trans, dev_root, path,
6410 &key, sizeof(*ptr));
6411 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006412 printk_in_rcu(KERN_WARNING "BTRFS: "
6413 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006414 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006415 goto out;
6416 }
6417 }
6418
6419 eb = path->nodes[0];
6420 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6421 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6422 btrfs_set_dev_stats_value(eb, ptr, i,
6423 btrfs_dev_stat_read(device, i));
6424 btrfs_mark_buffer_dirty(eb);
6425
6426out:
6427 btrfs_free_path(path);
6428 return ret;
6429}
6430
6431/*
6432 * called from commit_transaction. Writes all changed device stats to disk.
6433 */
6434int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6435 struct btrfs_fs_info *fs_info)
6436{
6437 struct btrfs_root *dev_root = fs_info->dev_root;
6438 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6439 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006440 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006441 int ret = 0;
6442
6443 mutex_lock(&fs_devices->device_list_mutex);
6444 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006445 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006446 continue;
6447
Miao Xieaddc3fa2014-07-24 11:37:11 +08006448 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006449 ret = update_dev_stat_item(trans, dev_root, device);
6450 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006451 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006452 }
6453 mutex_unlock(&fs_devices->device_list_mutex);
6454
6455 return ret;
6456}
6457
Stefan Behrens442a4f62012-05-25 16:06:08 +02006458void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6459{
6460 btrfs_dev_stat_inc(dev, index);
6461 btrfs_dev_stat_print_on_error(dev);
6462}
6463
Eric Sandeen48a3b632013-04-25 20:41:01 +00006464static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006465{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006466 if (!dev->dev_stats_valid)
6467 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006468 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6469 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006470 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006471 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6472 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6473 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006474 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6475 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006476}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006477
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006478static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6479{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006480 int i;
6481
6482 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6483 if (btrfs_dev_stat_read(dev, i) != 0)
6484 break;
6485 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6486 return; /* all values == 0, suppress message */
6487
Frank Holtonefe120a2013-12-20 11:37:06 -05006488 printk_in_rcu(KERN_INFO "BTRFS: "
6489 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006490 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006491 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6492 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6493 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6494 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6495 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6496}
6497
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006498int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006499 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006500{
6501 struct btrfs_device *dev;
6502 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6503 int i;
6504
6505 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006506 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006507 mutex_unlock(&fs_devices->device_list_mutex);
6508
6509 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006510 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006511 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006512 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006513 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006514 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006515 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006516 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6517 if (stats->nr_items > i)
6518 stats->values[i] =
6519 btrfs_dev_stat_read_and_reset(dev, i);
6520 else
6521 btrfs_dev_stat_reset(dev, i);
6522 }
6523 } else {
6524 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6525 if (stats->nr_items > i)
6526 stats->values[i] = btrfs_dev_stat_read(dev, i);
6527 }
6528 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6529 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6530 return 0;
6531}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006532
6533int btrfs_scratch_superblock(struct btrfs_device *device)
6534{
6535 struct buffer_head *bh;
6536 struct btrfs_super_block *disk_super;
6537
6538 bh = btrfs_read_dev_super(device->bdev);
6539 if (!bh)
6540 return -EINVAL;
6541 disk_super = (struct btrfs_super_block *)bh->b_data;
6542
6543 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6544 set_buffer_dirty(bh);
6545 sync_dirty_buffer(bh);
6546 brelse(bh);
6547
6548 return 0;
6549}
Miao Xie935e5cc2014-09-03 21:35:33 +08006550
6551/*
6552 * Update the size of all devices, which is used for writing out the
6553 * super blocks.
6554 */
6555void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
6556{
6557 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6558 struct btrfs_device *curr, *next;
6559
6560 if (list_empty(&fs_devices->resized_devices))
6561 return;
6562
6563 mutex_lock(&fs_devices->device_list_mutex);
6564 lock_chunks(fs_info->dev_root);
6565 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
6566 resized_list) {
6567 list_del_init(&curr->resized_list);
6568 curr->commit_total_bytes = curr->disk_total_bytes;
6569 }
6570 unlock_chunks(fs_info->dev_root);
6571 mutex_unlock(&fs_devices->device_list_mutex);
6572}
Miao Xiece7213c2014-09-03 21:35:34 +08006573
6574/* Must be invoked during the transaction commit */
6575void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
6576 struct btrfs_transaction *transaction)
6577{
6578 struct extent_map *em;
6579 struct map_lookup *map;
6580 struct btrfs_device *dev;
6581 int i;
6582
6583 if (list_empty(&transaction->pending_chunks))
6584 return;
6585
6586 /* In order to kick the device replace finish process */
6587 lock_chunks(root);
6588 list_for_each_entry(em, &transaction->pending_chunks, list) {
6589 map = (struct map_lookup *)em->bdev;
6590
6591 for (i = 0; i < map->num_stripes; i++) {
6592 dev = map->stripes[i].dev;
6593 dev->commit_bytes_used = dev->bytes_used;
6594 }
6595 }
6596 unlock_chunks(root);
6597}