blob: 6d4fb246eb7e8d3ea557be3db7f8dacb5ae06d11 [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040021#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040022#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040023#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040024#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000025#include <linux/capability.h>
Stefan Behrens442a4f62012-05-25 16:06:08 +020026#include <linux/ratelimit.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020027#include <linux/kthread.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050028#include <linux/raid/pq.h>
Stefan Behrens803b2f52013-08-15 17:11:21 +020029#include <linux/semaphore.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050030#include <asm/div64.h>
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "ctree.h"
32#include "extent_map.h"
33#include "disk-io.h"
34#include "transaction.h"
35#include "print-tree.h"
36#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050037#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040038#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010039#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040040#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060041#include "math.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010042#include "dev-replace.h"
Anand Jain99994cd2014-06-03 11:36:00 +080043#include "sysfs.h"
Chris Mason0b86a832008-03-24 15:01:56 -040044
Yan Zheng2b820322008-11-17 21:11:30 -050045static int init_first_rw_device(struct btrfs_trans_handle *trans,
46 struct btrfs_root *root,
47 struct btrfs_device *device);
48static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020049static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +000050static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020051static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Yan Zheng2b820322008-11-17 21:11:30 -050052
Miao Xie67a2c452014-09-03 21:35:43 +080053DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -040054static LIST_HEAD(fs_uuids);
55
Ilya Dryomov2208a372013-08-12 14:33:03 +030056static struct btrfs_fs_devices *__alloc_fs_devices(void)
57{
58 struct btrfs_fs_devices *fs_devs;
59
60 fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS);
61 if (!fs_devs)
62 return ERR_PTR(-ENOMEM);
63
64 mutex_init(&fs_devs->device_list_mutex);
65
66 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +080067 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +030068 INIT_LIST_HEAD(&fs_devs->alloc_list);
69 INIT_LIST_HEAD(&fs_devs->list);
70
71 return fs_devs;
72}
73
74/**
75 * alloc_fs_devices - allocate struct btrfs_fs_devices
76 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
77 * generated.
78 *
79 * Return: a pointer to a new &struct btrfs_fs_devices on success;
80 * ERR_PTR() on error. Returned struct is not linked onto any lists and
81 * can be destroyed with kfree() right away.
82 */
83static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
84{
85 struct btrfs_fs_devices *fs_devs;
86
87 fs_devs = __alloc_fs_devices();
88 if (IS_ERR(fs_devs))
89 return fs_devs;
90
91 if (fsid)
92 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
93 else
94 generate_random_uuid(fs_devs->fsid);
95
96 return fs_devs;
97}
98
Yan Zhenge4404d62008-12-12 10:03:26 -050099static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
100{
101 struct btrfs_device *device;
102 WARN_ON(fs_devices->opened);
103 while (!list_empty(&fs_devices->devices)) {
104 device = list_entry(fs_devices->devices.next,
105 struct btrfs_device, dev_list);
106 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -0400107 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500108 kfree(device);
109 }
110 kfree(fs_devices);
111}
112
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000113static void btrfs_kobject_uevent(struct block_device *bdev,
114 enum kobject_action action)
115{
116 int ret;
117
118 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
119 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500120 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000121 action,
122 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
123 &disk_to_dev(bdev->bd_disk)->kobj);
124}
125
Jeff Mahoney143bede2012-03-01 14:56:26 +0100126void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400127{
128 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400129
Yan Zheng2b820322008-11-17 21:11:30 -0500130 while (!list_empty(&fs_uuids)) {
131 fs_devices = list_entry(fs_uuids.next,
132 struct btrfs_fs_devices, list);
133 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500134 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400135 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400136}
137
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300138static struct btrfs_device *__alloc_device(void)
139{
140 struct btrfs_device *dev;
141
142 dev = kzalloc(sizeof(*dev), GFP_NOFS);
143 if (!dev)
144 return ERR_PTR(-ENOMEM);
145
146 INIT_LIST_HEAD(&dev->dev_list);
147 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800148 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300149
150 spin_lock_init(&dev->io_lock);
151
152 spin_lock_init(&dev->reada_lock);
153 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800154 atomic_set(&dev->dev_stats_ccnt, 0);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300155 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT);
156 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT);
157
158 return dev;
159}
160
Chris Masona1b32a52008-09-05 16:09:51 -0400161static noinline struct btrfs_device *__find_device(struct list_head *head,
162 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400163{
164 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400165
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500166 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400167 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400168 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400169 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400170 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400171 }
172 return NULL;
173}
174
Chris Masona1b32a52008-09-05 16:09:51 -0400175static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400176{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400177 struct btrfs_fs_devices *fs_devices;
178
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500179 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400180 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
181 return fs_devices;
182 }
183 return NULL;
184}
185
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100186static int
187btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
188 int flush, struct block_device **bdev,
189 struct buffer_head **bh)
190{
191 int ret;
192
193 *bdev = blkdev_get_by_path(device_path, flags, holder);
194
195 if (IS_ERR(*bdev)) {
196 ret = PTR_ERR(*bdev);
Frank Holtonefe120a2013-12-20 11:37:06 -0500197 printk(KERN_INFO "BTRFS: open %s failed\n", device_path);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100198 goto error;
199 }
200
201 if (flush)
202 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
203 ret = set_blocksize(*bdev, 4096);
204 if (ret) {
205 blkdev_put(*bdev, flags);
206 goto error;
207 }
208 invalidate_bdev(*bdev);
209 *bh = btrfs_read_dev_super(*bdev);
210 if (!*bh) {
211 ret = -EINVAL;
212 blkdev_put(*bdev, flags);
213 goto error;
214 }
215
216 return 0;
217
218error:
219 *bdev = NULL;
220 *bh = NULL;
221 return ret;
222}
223
Chris Masonffbd5172009-04-20 15:50:09 -0400224static void requeue_list(struct btrfs_pending_bios *pending_bios,
225 struct bio *head, struct bio *tail)
226{
227
228 struct bio *old_head;
229
230 old_head = pending_bios->head;
231 pending_bios->head = head;
232 if (pending_bios->tail)
233 tail->bi_next = old_head;
234 else
235 pending_bios->tail = tail;
236}
237
Chris Mason8b712842008-06-11 16:50:36 -0400238/*
239 * we try to collect pending bios for a device so we don't get a large
240 * number of procs sending bios down to the same device. This greatly
241 * improves the schedulers ability to collect and merge the bios.
242 *
243 * But, it also turns into a long list of bios to process and that is sure
244 * to eventually make the worker thread block. The solution here is to
245 * make some progress and then put this work struct back at the end of
246 * the list if the block device is congested. This way, multiple devices
247 * can make progress from a single worker thread.
248 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100249static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400250{
251 struct bio *pending;
252 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400253 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400254 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400255 struct bio *tail;
256 struct bio *cur;
257 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400258 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400259 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400260 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400261 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400262 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000263 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400264 struct blk_plug plug;
265
266 /*
267 * this function runs all the bios we've collected for
268 * a particular device. We don't want to wander off to
269 * another device without first sending all of these down.
270 * So, setup a plug here and finish it off before we return
271 */
272 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400273
Chris Masonbedf7622009-04-03 10:32:58 -0400274 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400275 fs_info = device->dev_root->fs_info;
276 limit = btrfs_async_submit_limit(fs_info);
277 limit = limit * 2 / 3;
278
Chris Mason8b712842008-06-11 16:50:36 -0400279loop:
280 spin_lock(&device->io_lock);
281
Chris Masona6837052009-02-04 09:19:41 -0500282loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400283 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400284
Chris Mason8b712842008-06-11 16:50:36 -0400285 /* take all the bios off the list at once and process them
286 * later on (without the lock held). But, remember the
287 * tail and other pointers so the bios can be properly reinserted
288 * into the list if we hit congestion
289 */
Chris Masond84275c2009-06-09 15:39:08 -0400290 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400291 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400292 force_reg = 1;
293 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400294 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400295 force_reg = 0;
296 }
Chris Masonffbd5172009-04-20 15:50:09 -0400297
298 pending = pending_bios->head;
299 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400300 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400301
302 /*
303 * if pending was null this time around, no bios need processing
304 * at all and we can stop. Otherwise it'll loop back up again
305 * and do an additional check so no bios are missed.
306 *
307 * device->running_pending is used to synchronize with the
308 * schedule_bio code.
309 */
Chris Masonffbd5172009-04-20 15:50:09 -0400310 if (device->pending_sync_bios.head == NULL &&
311 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400312 again = 0;
313 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400314 } else {
315 again = 1;
316 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400317 }
Chris Masonffbd5172009-04-20 15:50:09 -0400318
319 pending_bios->head = NULL;
320 pending_bios->tail = NULL;
321
Chris Mason8b712842008-06-11 16:50:36 -0400322 spin_unlock(&device->io_lock);
323
Chris Masond3977122009-01-05 21:25:51 -0500324 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400325
326 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400327 /* we want to work on both lists, but do more bios on the
328 * sync list than the regular list
329 */
330 if ((num_run > 32 &&
331 pending_bios != &device->pending_sync_bios &&
332 device->pending_sync_bios.head) ||
333 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
334 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400335 spin_lock(&device->io_lock);
336 requeue_list(pending_bios, pending, tail);
337 goto loop_lock;
338 }
339
Chris Mason8b712842008-06-11 16:50:36 -0400340 cur = pending;
341 pending = pending->bi_next;
342 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400343
Josef Bacik66657b32012-08-01 15:36:24 -0400344 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400345 waitqueue_active(&fs_info->async_submit_wait))
346 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400347
348 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400349
Chris Mason2ab1ba62011-08-04 14:28:36 -0400350 /*
351 * if we're doing the sync list, record that our
352 * plug has some sync requests on it
353 *
354 * If we're doing the regular list and there are
355 * sync requests sitting around, unplug before
356 * we add more
357 */
358 if (pending_bios == &device->pending_sync_bios) {
359 sync_pending = 1;
360 } else if (sync_pending) {
361 blk_finish_plug(&plug);
362 blk_start_plug(&plug);
363 sync_pending = 0;
364 }
365
Stefan Behrens21adbd52011-11-09 13:44:05 +0100366 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400367 num_run++;
368 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100369 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400370 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400371
372 /*
373 * we made progress, there is more work to do and the bdi
374 * is now congested. Back off and let other work structs
375 * run instead
376 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400377 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500378 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400379 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400380
Chris Masonb765ead2009-04-03 10:27:10 -0400381 ioc = current->io_context;
382
383 /*
384 * the main goal here is that we don't want to
385 * block if we're going to be able to submit
386 * more requests without blocking.
387 *
388 * This code does two great things, it pokes into
389 * the elevator code from a filesystem _and_
390 * it makes assumptions about how batching works.
391 */
392 if (ioc && ioc->nr_batch_requests > 0 &&
393 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
394 (last_waited == 0 ||
395 ioc->last_waited == last_waited)) {
396 /*
397 * we want to go through our batch of
398 * requests and stop. So, we copy out
399 * the ioc->last_waited time and test
400 * against it before looping
401 */
402 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100403 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400404 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400405 continue;
406 }
Chris Mason8b712842008-06-11 16:50:36 -0400407 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400408 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500409 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400410
411 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800412 btrfs_queue_work(fs_info->submit_workers,
413 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400414 goto done;
415 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500416 /* unplug every 64 requests just for good measure */
417 if (batch_run % 64 == 0) {
418 blk_finish_plug(&plug);
419 blk_start_plug(&plug);
420 sync_pending = 0;
421 }
Chris Mason8b712842008-06-11 16:50:36 -0400422 }
Chris Masonffbd5172009-04-20 15:50:09 -0400423
Chris Mason51684082010-03-10 15:33:32 -0500424 cond_resched();
425 if (again)
426 goto loop;
427
428 spin_lock(&device->io_lock);
429 if (device->pending_bios.head || device->pending_sync_bios.head)
430 goto loop_lock;
431 spin_unlock(&device->io_lock);
432
Chris Mason8b712842008-06-11 16:50:36 -0400433done:
Chris Mason211588a2011-04-19 20:12:40 -0400434 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400435}
436
Christoph Hellwigb2950862008-12-02 09:54:17 -0500437static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400438{
439 struct btrfs_device *device;
440
441 device = container_of(work, struct btrfs_device, work);
442 run_scheduled_bios(device);
443}
444
David Sterba60999ca2014-03-26 18:26:36 +0100445/*
446 * Add new device to list of registered devices
447 *
448 * Returns:
449 * 1 - first time device is seen
450 * 0 - device already known
451 * < 0 - error
452 */
Chris Masona1b32a52008-09-05 16:09:51 -0400453static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400454 struct btrfs_super_block *disk_super,
455 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
456{
457 struct btrfs_device *device;
458 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400459 struct rcu_string *name;
David Sterba60999ca2014-03-26 18:26:36 +0100460 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400461 u64 found_transid = btrfs_super_generation(disk_super);
462
463 fs_devices = find_fsid(disk_super->fsid);
464 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300465 fs_devices = alloc_fs_devices(disk_super->fsid);
466 if (IS_ERR(fs_devices))
467 return PTR_ERR(fs_devices);
468
Chris Mason8a4b83c2008-03-24 15:02:07 -0400469 list_add(&fs_devices->list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300470
Chris Mason8a4b83c2008-03-24 15:02:07 -0400471 device = NULL;
472 } else {
Chris Masona4437552008-04-18 10:29:38 -0400473 device = __find_device(&fs_devices->devices, devid,
474 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400475 }
Miao Xie443f24f2014-07-24 11:37:15 +0800476
Chris Mason8a4b83c2008-03-24 15:02:07 -0400477 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500478 if (fs_devices->opened)
479 return -EBUSY;
480
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300481 device = btrfs_alloc_device(NULL, &devid,
482 disk_super->dev_item.uuid);
483 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400484 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300485 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400486 }
Josef Bacik606686e2012-06-04 14:03:51 -0400487
488 name = rcu_string_strdup(path, GFP_NOFS);
489 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400490 kfree(device);
491 return -ENOMEM;
492 }
Josef Bacik606686e2012-06-04 14:03:51 -0400493 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200494
Chris Masone5e9a522009-06-10 15:17:02 -0400495 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000496 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100497 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400498 mutex_unlock(&fs_devices->device_list_mutex);
499
David Sterba60999ca2014-03-26 18:26:36 +0100500 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500501 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400502 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800503 /*
504 * When FS is already mounted.
505 * 1. If you are here and if the device->name is NULL that
506 * means this device was missing at time of FS mount.
507 * 2. If you are here and if the device->name is different
508 * from 'path' that means either
509 * a. The same device disappeared and reappeared with
510 * different name. or
511 * b. The missing-disk-which-was-replaced, has
512 * reappeared now.
513 *
514 * We must allow 1 and 2a above. But 2b would be a spurious
515 * and unintentional.
516 *
517 * Further in case of 1 and 2a above, the disk at 'path'
518 * would have missed some transaction when it was away and
519 * in case of 2a the stale bdev has to be updated as well.
520 * 2b must not be allowed at all time.
521 */
522
523 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700524 * For now, we do allow update to btrfs_fs_device through the
525 * btrfs dev scan cli after FS has been mounted. We're still
526 * tracking a problem where systems fail mount by subvolume id
527 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800528 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700529 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800530 /*
531 * That is if the FS is _not_ mounted and if you
532 * are here, that means there is more than one
533 * disk with same uuid and devid.We keep the one
534 * with larger generation number or the last-in if
535 * generation are equal.
536 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700537 return -EEXIST;
Anand Jain77bdae42014-07-03 18:22:06 +0800538 }
Anand Jainb96de002014-07-03 18:22:05 +0800539
Josef Bacik606686e2012-06-04 14:03:51 -0400540 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000541 if (!name)
542 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400543 rcu_string_free(device->name);
544 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500545 if (device->missing) {
546 fs_devices->missing_devices--;
547 device->missing = 0;
548 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400549 }
550
Anand Jain77bdae42014-07-03 18:22:06 +0800551 /*
552 * Unmount does not free the btrfs_device struct but would zero
553 * generation along with most of the other members. So just update
554 * it back. We need it to pick the disk with largest generation
555 * (as above).
556 */
557 if (!fs_devices->opened)
558 device->generation = found_transid;
559
Chris Mason8a4b83c2008-03-24 15:02:07 -0400560 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100561
562 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400563}
564
Yan Zhenge4404d62008-12-12 10:03:26 -0500565static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
566{
567 struct btrfs_fs_devices *fs_devices;
568 struct btrfs_device *device;
569 struct btrfs_device *orig_dev;
570
Ilya Dryomov2208a372013-08-12 14:33:03 +0300571 fs_devices = alloc_fs_devices(orig->fsid);
572 if (IS_ERR(fs_devices))
573 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500574
Miao Xieadbbb862014-09-03 21:35:42 +0800575 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400576 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500577
Xiao Guangrong46224702011-04-20 10:08:47 +0000578 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500579 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400580 struct rcu_string *name;
581
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300582 device = btrfs_alloc_device(NULL, &orig_dev->devid,
583 orig_dev->uuid);
584 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500585 goto error;
586
Josef Bacik606686e2012-06-04 14:03:51 -0400587 /*
588 * This is ok to do without rcu read locked because we hold the
589 * uuid mutex so nothing we touch in here is going to disappear.
590 */
Anand Jaine755f782014-06-30 17:12:47 +0800591 if (orig_dev->name) {
592 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
593 if (!name) {
594 kfree(device);
595 goto error;
596 }
597 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400598 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500599
Yan Zhenge4404d62008-12-12 10:03:26 -0500600 list_add(&device->dev_list, &fs_devices->devices);
601 device->fs_devices = fs_devices;
602 fs_devices->num_devices++;
603 }
Miao Xieadbbb862014-09-03 21:35:42 +0800604 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500605 return fs_devices;
606error:
Miao Xieadbbb862014-09-03 21:35:42 +0800607 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500608 free_fs_devices(fs_devices);
609 return ERR_PTR(-ENOMEM);
610}
611
Eric Sandeen9eaed212014-08-01 18:12:35 -0500612void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400613{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500614 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800615 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500616
Chris Masondfe25022008-05-13 13:46:40 -0400617 mutex_lock(&uuid_mutex);
618again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000619 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500620 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500621 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100622 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800623 (!latest_dev ||
624 device->generation > latest_dev->generation)) {
625 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500626 }
Yan Zheng2b820322008-11-17 21:11:30 -0500627 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500628 }
Yan Zheng2b820322008-11-17 21:11:30 -0500629
Stefan Behrens8dabb742012-11-06 13:15:27 +0100630 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
631 /*
632 * In the first step, keep the device which has
633 * the correct fsid and the devid that is used
634 * for the dev_replace procedure.
635 * In the second step, the dev_replace state is
636 * read from the device tree and it is known
637 * whether the procedure is really active or
638 * not, which means whether this device is
639 * used or whether it should be removed.
640 */
641 if (step == 0 || device->is_tgtdev_for_dev_replace) {
642 continue;
643 }
644 }
Yan Zheng2b820322008-11-17 21:11:30 -0500645 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100646 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500647 device->bdev = NULL;
648 fs_devices->open_devices--;
649 }
650 if (device->writeable) {
651 list_del_init(&device->dev_alloc_list);
652 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100653 if (!device->is_tgtdev_for_dev_replace)
654 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500655 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500656 list_del_init(&device->dev_list);
657 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400658 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500659 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400660 }
Yan Zheng2b820322008-11-17 21:11:30 -0500661
662 if (fs_devices->seed) {
663 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500664 goto again;
665 }
666
Miao Xie443f24f2014-07-24 11:37:15 +0800667 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500668
Chris Masondfe25022008-05-13 13:46:40 -0400669 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400670}
Chris Masona0af4692008-05-13 16:03:06 -0400671
Xiao Guangrong1f781602011-04-20 10:09:16 +0000672static void __free_device(struct work_struct *work)
673{
674 struct btrfs_device *device;
675
676 device = container_of(work, struct btrfs_device, rcu_work);
677
678 if (device->bdev)
679 blkdev_put(device->bdev, device->mode);
680
Josef Bacik606686e2012-06-04 14:03:51 -0400681 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000682 kfree(device);
683}
684
685static void free_device(struct rcu_head *head)
686{
687 struct btrfs_device *device;
688
689 device = container_of(head, struct btrfs_device, rcu);
690
691 INIT_WORK(&device->rcu_work, __free_device);
692 schedule_work(&device->rcu_work);
693}
694
Yan Zheng2b820322008-11-17 21:11:30 -0500695static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400696{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400697 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500698
Yan Zheng2b820322008-11-17 21:11:30 -0500699 if (--fs_devices->opened > 0)
700 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400701
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000702 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500703 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000704 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400705 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000706
707 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400708 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000709
Ilya Dryomovf747cab2013-10-10 20:37:29 +0300710 if (device->writeable &&
711 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500712 list_del_init(&device->dev_alloc_list);
713 fs_devices->rw_devices--;
714 }
715
Josef Bacik726551e2013-08-26 13:45:53 -0400716 if (device->missing)
717 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000718
Ilya Dryomova1e87802013-08-12 14:33:04 +0300719 new_device = btrfs_alloc_device(NULL, &device->devid,
720 device->uuid);
721 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400722
723 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400724 if (device->name) {
725 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300726 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400727 rcu_assign_pointer(new_device->name, name);
728 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300729
Xiao Guangrong1f781602011-04-20 10:09:16 +0000730 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300731 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000732
733 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400734 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000735 mutex_unlock(&fs_devices->device_list_mutex);
736
Yan Zhenge4404d62008-12-12 10:03:26 -0500737 WARN_ON(fs_devices->open_devices);
738 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500739 fs_devices->opened = 0;
740 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500741
Chris Mason8a4b83c2008-03-24 15:02:07 -0400742 return 0;
743}
744
Yan Zheng2b820322008-11-17 21:11:30 -0500745int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
746{
Yan Zhenge4404d62008-12-12 10:03:26 -0500747 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500748 int ret;
749
750 mutex_lock(&uuid_mutex);
751 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500752 if (!fs_devices->opened) {
753 seed_devices = fs_devices->seed;
754 fs_devices->seed = NULL;
755 }
Yan Zheng2b820322008-11-17 21:11:30 -0500756 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500757
758 while (seed_devices) {
759 fs_devices = seed_devices;
760 seed_devices = fs_devices->seed;
761 __btrfs_close_devices(fs_devices);
762 free_fs_devices(fs_devices);
763 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000764 /*
765 * Wait for rcu kworkers under __btrfs_close_devices
766 * to finish all blkdev_puts so device is really
767 * free when umount is done.
768 */
769 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500770 return ret;
771}
772
Yan Zhenge4404d62008-12-12 10:03:26 -0500773static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
774 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400775{
Josef Bacikd5e20032011-08-04 14:52:27 +0000776 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400777 struct block_device *bdev;
778 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400779 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +0800780 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -0400781 struct buffer_head *bh;
782 struct btrfs_super_block *disk_super;
Chris Masona0af4692008-05-13 16:03:06 -0400783 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500784 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400785 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400786
Tejun Heod4d77622010-11-13 11:55:18 +0100787 flags |= FMODE_EXCL;
788
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500789 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400790 if (device->bdev)
791 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400792 if (!device->name)
793 continue;
794
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000795 /* Just open everything we can; ignore failures here */
796 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
797 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100798 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400799
800 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000801 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400802 if (devid != device->devid)
803 goto error_brelse;
804
Yan Zheng2b820322008-11-17 21:11:30 -0500805 if (memcmp(device->uuid, disk_super->dev_item.uuid,
806 BTRFS_UUID_SIZE))
807 goto error_brelse;
808
809 device->generation = btrfs_super_generation(disk_super);
Miao Xie443f24f2014-07-24 11:37:15 +0800810 if (!latest_dev ||
811 device->generation > latest_dev->generation)
812 latest_dev = device;
Chris Masona0af4692008-05-13 16:03:06 -0400813
Yan Zheng2b820322008-11-17 21:11:30 -0500814 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
815 device->writeable = 0;
816 } else {
817 device->writeable = !bdev_read_only(bdev);
818 seeding = 0;
819 }
820
Josef Bacikd5e20032011-08-04 14:52:27 +0000821 q = bdev_get_queue(bdev);
Miao Xie90180da2014-09-03 21:35:30 +0800822 if (blk_queue_discard(q))
Josef Bacikd5e20032011-08-04 14:52:27 +0000823 device->can_discard = 1;
Josef Bacikd5e20032011-08-04 14:52:27 +0000824
Chris Mason8a4b83c2008-03-24 15:02:07 -0400825 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400826 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500827 device->mode = flags;
828
Chris Masonc2898112009-06-10 09:51:32 -0400829 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
830 fs_devices->rotating = 1;
831
Chris Masona0af4692008-05-13 16:03:06 -0400832 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300833 if (device->writeable &&
834 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500835 fs_devices->rw_devices++;
836 list_add(&device->dev_alloc_list,
837 &fs_devices->alloc_list);
838 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000839 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400840 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400841
Chris Masona0af4692008-05-13 16:03:06 -0400842error_brelse:
843 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100844 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400845 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400846 }
Chris Masona0af4692008-05-13 16:03:06 -0400847 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300848 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400849 goto out;
850 }
Yan Zheng2b820322008-11-17 21:11:30 -0500851 fs_devices->seeding = seeding;
852 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +0800853 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -0500854 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400855out:
Yan Zheng2b820322008-11-17 21:11:30 -0500856 return ret;
857}
858
859int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500860 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500861{
862 int ret;
863
864 mutex_lock(&uuid_mutex);
865 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500866 fs_devices->opened++;
867 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500868 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500869 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500870 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400871 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400872 return ret;
873}
874
David Sterba6f60cbd2013-02-15 11:31:02 -0700875/*
876 * Look for a btrfs signature on a device. This may be called out of the mount path
877 * and we are not allowed to call set_blocksize during the scan. The superblock
878 * is read via pagecache
879 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500880int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400881 struct btrfs_fs_devices **fs_devices_ret)
882{
883 struct btrfs_super_block *disk_super;
884 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700885 struct page *page;
886 void *p;
887 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400888 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400889 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400890 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700891 u64 bytenr;
892 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400893
David Sterba6f60cbd2013-02-15 11:31:02 -0700894 /*
895 * we would like to check all the supers, but that would make
896 * a btrfs mount succeed after a mkfs from a different FS.
897 * So, we need to add a special mount option to scan for
898 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
899 */
900 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100901 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500902 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700903
904 bdev = blkdev_get_by_path(path, flags, holder);
905
906 if (IS_ERR(bdev)) {
907 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100908 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700909 }
910
911 /* make sure our super fits in the device */
912 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
913 goto error_bdev_put;
914
915 /* make sure our super fits in the page */
916 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
917 goto error_bdev_put;
918
919 /* make sure our super doesn't straddle pages on disk */
920 index = bytenr >> PAGE_CACHE_SHIFT;
921 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
922 goto error_bdev_put;
923
924 /* pull in the page with our super */
925 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
926 index, GFP_NOFS);
927
928 if (IS_ERR_OR_NULL(page))
929 goto error_bdev_put;
930
931 p = kmap(page);
932
933 /* align our pointer to the offset of the super block */
934 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
935
936 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +0800937 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -0700938 goto error_unmap;
939
Xiao Guangronga3438322010-01-06 11:48:18 +0000940 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400941 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400942 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700943
Chris Mason8a4b83c2008-03-24 15:02:07 -0400944 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +0100945 if (ret > 0) {
946 if (disk_super->label[0]) {
947 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
948 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
949 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
950 } else {
951 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
952 }
953
954 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
955 ret = 0;
956 }
Josef Bacik02db0842012-06-21 16:03:58 -0400957 if (!ret && fs_devices_ret)
958 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700959
960error_unmap:
961 kunmap(page);
962 page_cache_release(page);
963
964error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100965 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400966error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100967 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400968 return ret;
969}
Chris Mason0b86a832008-03-24 15:01:56 -0400970
Miao Xie6d07bce2011-01-05 10:07:31 +0000971/* helper to account the used device space in the range */
972int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
973 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400974{
975 struct btrfs_key key;
976 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000977 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500978 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000979 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400980 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000981 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400982 struct extent_buffer *l;
983
Miao Xie6d07bce2011-01-05 10:07:31 +0000984 *length = 0;
985
Stefan Behrens63a212a2012-11-05 18:29:28 +0100986 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000987 return 0;
988
Yan Zheng2b820322008-11-17 21:11:30 -0500989 path = btrfs_alloc_path();
990 if (!path)
991 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400992 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400993
Chris Mason0b86a832008-03-24 15:01:56 -0400994 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000995 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400996 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000997
998 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400999 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001000 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001001 if (ret > 0) {
1002 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1003 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001004 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001005 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001006
Chris Mason0b86a832008-03-24 15:01:56 -04001007 while (1) {
1008 l = path->nodes[0];
1009 slot = path->slots[0];
1010 if (slot >= btrfs_header_nritems(l)) {
1011 ret = btrfs_next_leaf(root, path);
1012 if (ret == 0)
1013 continue;
1014 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001015 goto out;
1016
1017 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001018 }
1019 btrfs_item_key_to_cpu(l, &key, slot);
1020
1021 if (key.objectid < device->devid)
1022 goto next;
1023
1024 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001025 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001026
David Sterba962a2982014-06-04 18:41:45 +02001027 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001028 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001029
Chris Mason0b86a832008-03-24 15:01:56 -04001030 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001031 extent_end = key.offset + btrfs_dev_extent_length(l,
1032 dev_extent);
1033 if (key.offset <= start && extent_end > end) {
1034 *length = end - start + 1;
1035 break;
1036 } else if (key.offset <= start && extent_end > start)
1037 *length += extent_end - start;
1038 else if (key.offset > start && extent_end <= end)
1039 *length += extent_end - key.offset;
1040 else if (key.offset > start && key.offset <= end) {
1041 *length += end - key.offset + 1;
1042 break;
1043 } else if (key.offset > end)
1044 break;
1045
1046next:
1047 path->slots[0]++;
1048 }
1049 ret = 0;
1050out:
1051 btrfs_free_path(path);
1052 return ret;
1053}
1054
Josef Bacik6df9a952013-06-27 13:22:46 -04001055static int contains_pending_extent(struct btrfs_trans_handle *trans,
1056 struct btrfs_device *device,
1057 u64 *start, u64 len)
1058{
1059 struct extent_map *em;
Filipe Manana04216822014-11-27 21:14:15 +00001060 struct list_head *search_list = &trans->transaction->pending_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001061 int ret = 0;
1062
Filipe Manana04216822014-11-27 21:14:15 +00001063again:
1064 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001065 struct map_lookup *map;
1066 int i;
1067
1068 map = (struct map_lookup *)em->bdev;
1069 for (i = 0; i < map->num_stripes; i++) {
1070 if (map->stripes[i].dev != device)
1071 continue;
1072 if (map->stripes[i].physical >= *start + len ||
1073 map->stripes[i].physical + em->orig_block_len <=
1074 *start)
1075 continue;
1076 *start = map->stripes[i].physical +
1077 em->orig_block_len;
1078 ret = 1;
1079 }
1080 }
Filipe Manana04216822014-11-27 21:14:15 +00001081 if (search_list == &trans->transaction->pending_chunks) {
1082 search_list = &trans->root->fs_info->pinned_chunks;
1083 goto again;
1084 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001085
1086 return ret;
1087}
1088
1089
Chris Mason0b86a832008-03-24 15:01:56 -04001090/*
Miao Xie7bfc8372011-01-05 10:07:26 +00001091 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +00001092 * @device: the device which we search the free space in
1093 * @num_bytes: the size of the free space that we need
1094 * @start: store the start of the free space.
1095 * @len: the size of the free space. that we find, or the size of the max
1096 * free space if we don't find suitable free space
1097 *
Chris Mason0b86a832008-03-24 15:01:56 -04001098 * this uses a pretty simple search, the expectation is that it is
1099 * called very infrequently and that a given device has a small number
1100 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001101 *
1102 * @start is used to store the start of the free space if we find. But if we
1103 * don't find suitable free space, it will be used to store the start position
1104 * of the max free space.
1105 *
1106 * @len is used to store the size of the free space that we find.
1107 * But if we don't find suitable free space, it is used to store the size of
1108 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001109 */
Josef Bacik6df9a952013-06-27 13:22:46 -04001110int find_free_dev_extent(struct btrfs_trans_handle *trans,
1111 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001112 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001113{
1114 struct btrfs_key key;
1115 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001116 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001117 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001118 u64 hole_size;
1119 u64 max_hole_start;
1120 u64 max_hole_size;
1121 u64 extent_end;
1122 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001123 u64 search_end = device->total_bytes;
1124 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001125 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001126 struct extent_buffer *l;
1127
Chris Mason0b86a832008-03-24 15:01:56 -04001128 /* FIXME use last free of some kind */
1129
1130 /* we don't want to overwrite the superblock on the drive,
1131 * so we make sure to start at an offset of at least 1MB
1132 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001133 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001134
Josef Bacik6df9a952013-06-27 13:22:46 -04001135 path = btrfs_alloc_path();
1136 if (!path)
1137 return -ENOMEM;
1138again:
Miao Xie7bfc8372011-01-05 10:07:26 +00001139 max_hole_start = search_start;
1140 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +00001141 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +00001142
Stefan Behrens63a212a2012-11-05 18:29:28 +01001143 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001144 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001145 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001146 }
1147
Miao Xie7bfc8372011-01-05 10:07:26 +00001148 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001149 path->search_commit_root = 1;
1150 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001151
Chris Mason0b86a832008-03-24 15:01:56 -04001152 key.objectid = device->devid;
1153 key.offset = search_start;
1154 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001155
Li Zefan125ccb02011-12-08 15:07:24 +08001156 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001157 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001158 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001159 if (ret > 0) {
1160 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1161 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001162 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001163 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001164
Chris Mason0b86a832008-03-24 15:01:56 -04001165 while (1) {
1166 l = path->nodes[0];
1167 slot = path->slots[0];
1168 if (slot >= btrfs_header_nritems(l)) {
1169 ret = btrfs_next_leaf(root, path);
1170 if (ret == 0)
1171 continue;
1172 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001173 goto out;
1174
1175 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001176 }
1177 btrfs_item_key_to_cpu(l, &key, slot);
1178
1179 if (key.objectid < device->devid)
1180 goto next;
1181
1182 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001183 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001184
David Sterba962a2982014-06-04 18:41:45 +02001185 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001186 goto next;
1187
Miao Xie7bfc8372011-01-05 10:07:26 +00001188 if (key.offset > search_start) {
1189 hole_size = key.offset - search_start;
1190
Josef Bacik6df9a952013-06-27 13:22:46 -04001191 /*
1192 * Have to check before we set max_hole_start, otherwise
1193 * we could end up sending back this offset anyway.
1194 */
1195 if (contains_pending_extent(trans, device,
1196 &search_start,
1197 hole_size))
1198 hole_size = 0;
1199
Miao Xie7bfc8372011-01-05 10:07:26 +00001200 if (hole_size > max_hole_size) {
1201 max_hole_start = search_start;
1202 max_hole_size = hole_size;
1203 }
1204
1205 /*
1206 * If this free space is greater than which we need,
1207 * it must be the max free space that we have found
1208 * until now, so max_hole_start must point to the start
1209 * of this free space and the length of this free space
1210 * is stored in max_hole_size. Thus, we return
1211 * max_hole_start and max_hole_size and go back to the
1212 * caller.
1213 */
1214 if (hole_size >= num_bytes) {
1215 ret = 0;
1216 goto out;
1217 }
1218 }
1219
Chris Mason0b86a832008-03-24 15:01:56 -04001220 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001221 extent_end = key.offset + btrfs_dev_extent_length(l,
1222 dev_extent);
1223 if (extent_end > search_start)
1224 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001225next:
1226 path->slots[0]++;
1227 cond_resched();
1228 }
Chris Mason0b86a832008-03-24 15:01:56 -04001229
liubo38c01b92011-08-02 02:39:03 +00001230 /*
1231 * At this point, search_start should be the end of
1232 * allocated dev extents, and when shrinking the device,
1233 * search_end may be smaller than search_start.
1234 */
1235 if (search_end > search_start)
1236 hole_size = search_end - search_start;
1237
Miao Xie7bfc8372011-01-05 10:07:26 +00001238 if (hole_size > max_hole_size) {
1239 max_hole_start = search_start;
1240 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001241 }
Chris Mason0b86a832008-03-24 15:01:56 -04001242
Josef Bacik6df9a952013-06-27 13:22:46 -04001243 if (contains_pending_extent(trans, device, &search_start, hole_size)) {
1244 btrfs_release_path(path);
1245 goto again;
1246 }
1247
Miao Xie7bfc8372011-01-05 10:07:26 +00001248 /* See above. */
1249 if (hole_size < num_bytes)
1250 ret = -ENOSPC;
1251 else
1252 ret = 0;
1253
1254out:
Yan Zheng2b820322008-11-17 21:11:30 -05001255 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001256 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001257 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001258 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001259 return ret;
1260}
1261
Christoph Hellwigb2950862008-12-02 09:54:17 -05001262static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001263 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001264 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001265{
1266 int ret;
1267 struct btrfs_path *path;
1268 struct btrfs_root *root = device->dev_root;
1269 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001270 struct btrfs_key found_key;
1271 struct extent_buffer *leaf = NULL;
1272 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001273
1274 path = btrfs_alloc_path();
1275 if (!path)
1276 return -ENOMEM;
1277
1278 key.objectid = device->devid;
1279 key.offset = start;
1280 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001281again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001282 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001283 if (ret > 0) {
1284 ret = btrfs_previous_item(root, path, key.objectid,
1285 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001286 if (ret)
1287 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001288 leaf = path->nodes[0];
1289 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1290 extent = btrfs_item_ptr(leaf, path->slots[0],
1291 struct btrfs_dev_extent);
1292 BUG_ON(found_key.offset > start || found_key.offset +
1293 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001294 key = found_key;
1295 btrfs_release_path(path);
1296 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001297 } else if (ret == 0) {
1298 leaf = path->nodes[0];
1299 extent = btrfs_item_ptr(leaf, path->slots[0],
1300 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001301 } else {
1302 btrfs_error(root->fs_info, ret, "Slot search failed");
1303 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001304 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001305
Miao Xie2196d6e2014-09-03 21:35:41 +08001306 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1307
Chris Mason8f18cf12008-04-25 16:53:30 -04001308 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001309 if (ret) {
1310 btrfs_error(root->fs_info, ret,
1311 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001312 } else {
1313 trans->transaction->have_free_bgs = 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001314 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001315out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001316 btrfs_free_path(path);
1317 return ret;
1318}
1319
Eric Sandeen48a3b632013-04-25 20:41:01 +00001320static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1321 struct btrfs_device *device,
1322 u64 chunk_tree, u64 chunk_objectid,
1323 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001324{
1325 int ret;
1326 struct btrfs_path *path;
1327 struct btrfs_root *root = device->dev_root;
1328 struct btrfs_dev_extent *extent;
1329 struct extent_buffer *leaf;
1330 struct btrfs_key key;
1331
Chris Masondfe25022008-05-13 13:46:40 -04001332 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001333 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001334 path = btrfs_alloc_path();
1335 if (!path)
1336 return -ENOMEM;
1337
Chris Mason0b86a832008-03-24 15:01:56 -04001338 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001339 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001340 key.type = BTRFS_DEV_EXTENT_KEY;
1341 ret = btrfs_insert_empty_item(trans, root, path, &key,
1342 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001343 if (ret)
1344 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001345
1346 leaf = path->nodes[0];
1347 extent = btrfs_item_ptr(leaf, path->slots[0],
1348 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001349 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1350 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1351 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1352
1353 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001354 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001355
Chris Mason0b86a832008-03-24 15:01:56 -04001356 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1357 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001358out:
Chris Mason0b86a832008-03-24 15:01:56 -04001359 btrfs_free_path(path);
1360 return ret;
1361}
1362
Josef Bacik6df9a952013-06-27 13:22:46 -04001363static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001364{
Josef Bacik6df9a952013-06-27 13:22:46 -04001365 struct extent_map_tree *em_tree;
1366 struct extent_map *em;
1367 struct rb_node *n;
1368 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001369
Josef Bacik6df9a952013-06-27 13:22:46 -04001370 em_tree = &fs_info->mapping_tree.map_tree;
1371 read_lock(&em_tree->lock);
1372 n = rb_last(&em_tree->map);
1373 if (n) {
1374 em = rb_entry(n, struct extent_map, rb_node);
1375 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001376 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001377 read_unlock(&em_tree->lock);
1378
Chris Mason0b86a832008-03-24 15:01:56 -04001379 return ret;
1380}
1381
Ilya Dryomov53f10652013-08-12 14:33:01 +03001382static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1383 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001384{
1385 int ret;
1386 struct btrfs_key key;
1387 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001388 struct btrfs_path *path;
1389
Yan Zheng2b820322008-11-17 21:11:30 -05001390 path = btrfs_alloc_path();
1391 if (!path)
1392 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001393
1394 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1395 key.type = BTRFS_DEV_ITEM_KEY;
1396 key.offset = (u64)-1;
1397
Ilya Dryomov53f10652013-08-12 14:33:01 +03001398 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001399 if (ret < 0)
1400 goto error;
1401
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001402 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001403
Ilya Dryomov53f10652013-08-12 14:33:01 +03001404 ret = btrfs_previous_item(fs_info->chunk_root, path,
1405 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001406 BTRFS_DEV_ITEM_KEY);
1407 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001408 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001409 } else {
1410 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1411 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001412 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001413 }
1414 ret = 0;
1415error:
Yan Zheng2b820322008-11-17 21:11:30 -05001416 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001417 return ret;
1418}
1419
1420/*
1421 * the device information is stored in the chunk root
1422 * the btrfs_device struct should be fully filled in
1423 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001424static int btrfs_add_device(struct btrfs_trans_handle *trans,
1425 struct btrfs_root *root,
1426 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001427{
1428 int ret;
1429 struct btrfs_path *path;
1430 struct btrfs_dev_item *dev_item;
1431 struct extent_buffer *leaf;
1432 struct btrfs_key key;
1433 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001434
1435 root = root->fs_info->chunk_root;
1436
1437 path = btrfs_alloc_path();
1438 if (!path)
1439 return -ENOMEM;
1440
Chris Mason0b86a832008-03-24 15:01:56 -04001441 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1442 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001443 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001444
1445 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001446 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001447 if (ret)
1448 goto out;
1449
1450 leaf = path->nodes[0];
1451 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1452
1453 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001454 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001455 btrfs_set_device_type(leaf, dev_item, device->type);
1456 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1457 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1458 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001459 btrfs_set_device_total_bytes(leaf, dev_item,
1460 btrfs_device_get_disk_total_bytes(device));
1461 btrfs_set_device_bytes_used(leaf, dev_item,
1462 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001463 btrfs_set_device_group(leaf, dev_item, 0);
1464 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1465 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001466 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001467
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001468 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001469 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001470 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001471 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001472 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001473
Yan Zheng2b820322008-11-17 21:11:30 -05001474 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001475out:
1476 btrfs_free_path(path);
1477 return ret;
1478}
Chris Mason8f18cf12008-04-25 16:53:30 -04001479
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001480/*
1481 * Function to update ctime/mtime for a given device path.
1482 * Mainly used for ctime/mtime based probe like libblkid.
1483 */
1484static void update_dev_time(char *path_name)
1485{
1486 struct file *filp;
1487
1488 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001489 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001490 return;
1491 file_update_time(filp);
1492 filp_close(filp, NULL);
1493 return;
1494}
1495
Chris Masona061fc82008-05-07 11:43:44 -04001496static int btrfs_rm_dev_item(struct btrfs_root *root,
1497 struct btrfs_device *device)
1498{
1499 int ret;
1500 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001501 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001502 struct btrfs_trans_handle *trans;
1503
1504 root = root->fs_info->chunk_root;
1505
1506 path = btrfs_alloc_path();
1507 if (!path)
1508 return -ENOMEM;
1509
Yan, Zhenga22285a2010-05-16 10:48:46 -04001510 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001511 if (IS_ERR(trans)) {
1512 btrfs_free_path(path);
1513 return PTR_ERR(trans);
1514 }
Chris Masona061fc82008-05-07 11:43:44 -04001515 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1516 key.type = BTRFS_DEV_ITEM_KEY;
1517 key.offset = device->devid;
1518
1519 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1520 if (ret < 0)
1521 goto out;
1522
1523 if (ret > 0) {
1524 ret = -ENOENT;
1525 goto out;
1526 }
1527
1528 ret = btrfs_del_item(trans, root, path);
1529 if (ret)
1530 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001531out:
1532 btrfs_free_path(path);
1533 btrfs_commit_transaction(trans, root);
1534 return ret;
1535}
1536
1537int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1538{
1539 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001540 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001541 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001542 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001543 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001544 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001545 u64 all_avail;
1546 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001547 u64 num_devices;
1548 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001549 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001550 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001551 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001552
Chris Masona061fc82008-05-07 11:43:44 -04001553 mutex_lock(&uuid_mutex);
1554
Miao Xiede98ced2013-01-29 10:13:12 +00001555 do {
1556 seq = read_seqbegin(&root->fs_info->profiles_lock);
1557
1558 all_avail = root->fs_info->avail_data_alloc_bits |
1559 root->fs_info->avail_system_alloc_bits |
1560 root->fs_info->avail_metadata_alloc_bits;
1561 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001562
Stefan Behrens8dabb742012-11-06 13:15:27 +01001563 num_devices = root->fs_info->fs_devices->num_devices;
1564 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1565 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1566 WARN_ON(num_devices < 1);
1567 num_devices--;
1568 }
1569 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1570
1571 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001572 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001573 goto out;
1574 }
1575
Stefan Behrens8dabb742012-11-06 13:15:27 +01001576 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001577 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001578 goto out;
1579 }
1580
David Woodhouse53b381b2013-01-29 18:40:14 -05001581 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1582 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001583 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001584 goto out;
1585 }
1586 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1587 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001588 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001589 goto out;
1590 }
1591
Chris Masondfe25022008-05-13 13:46:40 -04001592 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001593 struct list_head *devices;
1594 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001595
Chris Masondfe25022008-05-13 13:46:40 -04001596 device = NULL;
1597 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001598 /*
1599 * It is safe to read the devices since the volume_mutex
1600 * is held.
1601 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001602 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001603 if (tmp->in_fs_metadata &&
1604 !tmp->is_tgtdev_for_dev_replace &&
1605 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001606 device = tmp;
1607 break;
1608 }
1609 }
1610 bdev = NULL;
1611 bh = NULL;
1612 disk_super = NULL;
1613 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001614 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001615 goto out;
1616 }
Chris Masondfe25022008-05-13 13:46:40 -04001617 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001618 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001619 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001620 root->fs_info->bdev_holder, 0,
1621 &bdev, &bh);
1622 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001623 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001624 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001625 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001626 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001627 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001628 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001629 if (!device) {
1630 ret = -ENOENT;
1631 goto error_brelse;
1632 }
Chris Masondfe25022008-05-13 13:46:40 -04001633 }
Yan Zheng2b820322008-11-17 21:11:30 -05001634
Stefan Behrens63a212a2012-11-05 18:29:28 +01001635 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001636 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001637 goto error_brelse;
1638 }
1639
Yan Zheng2b820322008-11-17 21:11:30 -05001640 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001641 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001642 goto error_brelse;
1643 }
1644
1645 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001646 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001647 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001648 device->fs_devices->rw_devices--;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001649 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001650 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001651 }
Chris Masona061fc82008-05-07 11:43:44 -04001652
Carey Underwoodd7901552013-03-04 16:37:06 -06001653 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001654 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001655 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001656 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001657 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001658
Stefan Behrens63a212a2012-11-05 18:29:28 +01001659 /*
1660 * TODO: the superblock still includes this device in its num_devices
1661 * counter although write_all_supers() is not locked out. This
1662 * could give a filesystem state which requires a degraded mount.
1663 */
Chris Masona061fc82008-05-07 11:43:44 -04001664 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1665 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001666 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001667
Yan Zheng2b820322008-11-17 21:11:30 -05001668 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001669 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001670
1671 /*
1672 * the device list mutex makes sure that we don't change
1673 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001674 * the device supers. Whoever is writing all supers, should
1675 * lock the device list mutex before getting the number of
1676 * devices in the super block (super_copy). Conversely,
1677 * whoever updates the number of devices in the super block
1678 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001679 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001680
1681 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001682 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001683 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001684
Yan Zhenge4404d62008-12-12 10:03:26 -05001685 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001686 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001687
Chris Masoncd02dca2010-12-13 14:56:23 -05001688 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001689 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001690
Yan Zheng2b820322008-11-17 21:11:30 -05001691 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1692 struct btrfs_device, dev_list);
1693 if (device->bdev == root->fs_info->sb->s_bdev)
1694 root->fs_info->sb->s_bdev = next_device->bdev;
1695 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1696 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1697
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001698 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001699 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001700 /* remove sysfs entry */
1701 btrfs_kobj_rm_device(root->fs_info, device);
1702 }
Anand Jain99994cd2014-06-03 11:36:00 +08001703
Xiao Guangrong1f781602011-04-20 10:09:16 +00001704 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001705
David Sterba6c417612011-04-13 15:41:04 +02001706 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1707 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001708 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001709
Xiao Guangrong1f781602011-04-20 10:09:16 +00001710 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001711 struct btrfs_fs_devices *fs_devices;
1712 fs_devices = root->fs_info->fs_devices;
1713 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001714 if (fs_devices->seed == cur_devices) {
1715 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001716 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001717 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001718 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001719 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001720 cur_devices->seed = NULL;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001721 __btrfs_close_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001722 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001723 }
1724
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001725 root->fs_info->num_tolerated_disk_barrier_failures =
1726 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1727
Yan Zheng2b820322008-11-17 21:11:30 -05001728 /*
1729 * at this point, the device is zero sized. We want to
1730 * remove it from the devices list and zero out the old super
1731 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001732 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001733 u64 bytenr;
1734 int i;
1735
Chris Masondfe25022008-05-13 13:46:40 -04001736 /* make sure this device isn't detected as part of
1737 * the FS anymore
1738 */
1739 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1740 set_buffer_dirty(bh);
1741 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001742
1743 /* clear the mirror copies of super block on the disk
1744 * being removed, 0th copy is been taken care above and
1745 * the below would take of the rest
1746 */
1747 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1748 bytenr = btrfs_sb_offset(i);
1749 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1750 i_size_read(bdev->bd_inode))
1751 break;
1752
1753 brelse(bh);
1754 bh = __bread(bdev, bytenr / 4096,
1755 BTRFS_SUPER_INFO_SIZE);
1756 if (!bh)
1757 continue;
1758
1759 disk_super = (struct btrfs_super_block *)bh->b_data;
1760
1761 if (btrfs_super_bytenr(disk_super) != bytenr ||
1762 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1763 continue;
1764 }
1765 memset(&disk_super->magic, 0,
1766 sizeof(disk_super->magic));
1767 set_buffer_dirty(bh);
1768 sync_dirty_buffer(bh);
1769 }
Chris Masondfe25022008-05-13 13:46:40 -04001770 }
Chris Masona061fc82008-05-07 11:43:44 -04001771
Chris Masona061fc82008-05-07 11:43:44 -04001772 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001773
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001774 if (bdev) {
1775 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001776 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001777
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001778 /* Update ctime/mtime for device path for libblkid */
1779 update_dev_time(device_path);
1780 }
1781
Chris Masona061fc82008-05-07 11:43:44 -04001782error_brelse:
1783 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001784 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001785 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001786out:
1787 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001788 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001789error_undo:
1790 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001791 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001792 list_add(&device->dev_alloc_list,
1793 &root->fs_info->fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001794 device->fs_devices->rw_devices++;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001795 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001796 }
1797 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001798}
1799
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001800void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
1801 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01001802{
Anand Jaind51908c2014-08-13 14:24:19 +08001803 struct btrfs_fs_devices *fs_devices;
1804
Stefan Behrense93c89c2012-11-05 17:33:06 +01001805 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001806
Anand Jain25e8e912014-08-20 10:56:56 +08001807 /*
1808 * in case of fs with no seed, srcdev->fs_devices will point
1809 * to fs_devices of fs_info. However when the dev being replaced is
1810 * a seed dev it will point to the seed's local fs_devices. In short
1811 * srcdev will have its correct fs_devices in both the cases.
1812 */
1813 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08001814
Stefan Behrense93c89c2012-11-05 17:33:06 +01001815 list_del_rcu(&srcdev->dev_list);
1816 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08001817 fs_devices->num_devices--;
Miao Xie82372bc2014-09-03 21:35:44 +08001818 if (srcdev->missing)
Anand Jaind51908c2014-08-13 14:24:19 +08001819 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001820
Miao Xie82372bc2014-09-03 21:35:44 +08001821 if (srcdev->writeable) {
1822 fs_devices->rw_devices--;
1823 /* zero out the old super if it is writable */
1824 btrfs_scratch_superblock(srcdev);
Ilya Dryomov13572722013-10-02 20:41:01 +03001825 }
1826
Miao Xie82372bc2014-09-03 21:35:44 +08001827 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08001828 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001829}
1830
1831void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
1832 struct btrfs_device *srcdev)
1833{
1834 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001835
Stefan Behrense93c89c2012-11-05 17:33:06 +01001836 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001837
1838 /*
1839 * unless fs_devices is seed fs, num_devices shouldn't go
1840 * zero
1841 */
1842 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
1843
1844 /* if this is no devs we rather delete the fs_devices */
1845 if (!fs_devices->num_devices) {
1846 struct btrfs_fs_devices *tmp_fs_devices;
1847
1848 tmp_fs_devices = fs_info->fs_devices;
1849 while (tmp_fs_devices) {
1850 if (tmp_fs_devices->seed == fs_devices) {
1851 tmp_fs_devices->seed = fs_devices->seed;
1852 break;
1853 }
1854 tmp_fs_devices = tmp_fs_devices->seed;
1855 }
1856 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08001857 __btrfs_close_devices(fs_devices);
1858 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001859 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01001860}
1861
1862void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1863 struct btrfs_device *tgtdev)
1864{
1865 struct btrfs_device *next_device;
1866
Miao Xie67a2c452014-09-03 21:35:43 +08001867 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001868 WARN_ON(!tgtdev);
1869 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1870 if (tgtdev->bdev) {
1871 btrfs_scratch_superblock(tgtdev);
1872 fs_info->fs_devices->open_devices--;
1873 }
1874 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001875
1876 next_device = list_entry(fs_info->fs_devices->devices.next,
1877 struct btrfs_device, dev_list);
1878 if (tgtdev->bdev == fs_info->sb->s_bdev)
1879 fs_info->sb->s_bdev = next_device->bdev;
1880 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1881 fs_info->fs_devices->latest_bdev = next_device->bdev;
1882 list_del_rcu(&tgtdev->dev_list);
1883
1884 call_rcu(&tgtdev->rcu, free_device);
1885
1886 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08001887 mutex_unlock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001888}
1889
Eric Sandeen48a3b632013-04-25 20:41:01 +00001890static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1891 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001892{
1893 int ret = 0;
1894 struct btrfs_super_block *disk_super;
1895 u64 devid;
1896 u8 *dev_uuid;
1897 struct block_device *bdev;
1898 struct buffer_head *bh;
1899
1900 *device = NULL;
1901 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1902 root->fs_info->bdev_holder, 0, &bdev, &bh);
1903 if (ret)
1904 return ret;
1905 disk_super = (struct btrfs_super_block *)bh->b_data;
1906 devid = btrfs_stack_device_id(&disk_super->dev_item);
1907 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001908 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001909 disk_super->fsid);
1910 brelse(bh);
1911 if (!*device)
1912 ret = -ENOENT;
1913 blkdev_put(bdev, FMODE_READ);
1914 return ret;
1915}
1916
1917int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1918 char *device_path,
1919 struct btrfs_device **device)
1920{
1921 *device = NULL;
1922 if (strcmp(device_path, "missing") == 0) {
1923 struct list_head *devices;
1924 struct btrfs_device *tmp;
1925
1926 devices = &root->fs_info->fs_devices->devices;
1927 /*
1928 * It is safe to read the devices since the volume_mutex
1929 * is held by the caller.
1930 */
1931 list_for_each_entry(tmp, devices, dev_list) {
1932 if (tmp->in_fs_metadata && !tmp->bdev) {
1933 *device = tmp;
1934 break;
1935 }
1936 }
1937
1938 if (!*device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001939 btrfs_err(root->fs_info, "no missing device found");
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001940 return -ENOENT;
1941 }
1942
1943 return 0;
1944 } else {
1945 return btrfs_find_device_by_path(root, device_path, device);
1946 }
1947}
1948
Yan Zheng2b820322008-11-17 21:11:30 -05001949/*
1950 * does all the dirty work required for changing file system's UUID.
1951 */
Li Zefan125ccb02011-12-08 15:07:24 +08001952static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001953{
1954 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1955 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001956 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001957 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001958 struct btrfs_device *device;
1959 u64 super_flags;
1960
1961 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001962 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001963 return -EINVAL;
1964
Ilya Dryomov2208a372013-08-12 14:33:03 +03001965 seed_devices = __alloc_fs_devices();
1966 if (IS_ERR(seed_devices))
1967 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001968
Yan Zhenge4404d62008-12-12 10:03:26 -05001969 old_devices = clone_fs_devices(fs_devices);
1970 if (IS_ERR(old_devices)) {
1971 kfree(seed_devices);
1972 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001973 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001974
Yan Zheng2b820322008-11-17 21:11:30 -05001975 list_add(&old_devices->list, &fs_uuids);
1976
Yan Zhenge4404d62008-12-12 10:03:26 -05001977 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1978 seed_devices->opened = 1;
1979 INIT_LIST_HEAD(&seed_devices->devices);
1980 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001981 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001982
1983 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001984 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1985 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08001986 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05001987 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08001988
1989 lock_chunks(root);
1990 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1991 unlock_chunks(root);
Yan Zhenge4404d62008-12-12 10:03:26 -05001992
Yan Zheng2b820322008-11-17 21:11:30 -05001993 fs_devices->seeding = 0;
1994 fs_devices->num_devices = 0;
1995 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08001996 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08001997 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001998 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001999
2000 generate_random_uuid(fs_devices->fsid);
2001 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2002 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002003 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2004
Yan Zheng2b820322008-11-17 21:11:30 -05002005 super_flags = btrfs_super_flags(disk_super) &
2006 ~BTRFS_SUPER_FLAG_SEEDING;
2007 btrfs_set_super_flags(disk_super, super_flags);
2008
2009 return 0;
2010}
2011
2012/*
2013 * strore the expected generation for seed devices in device items.
2014 */
2015static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2016 struct btrfs_root *root)
2017{
2018 struct btrfs_path *path;
2019 struct extent_buffer *leaf;
2020 struct btrfs_dev_item *dev_item;
2021 struct btrfs_device *device;
2022 struct btrfs_key key;
2023 u8 fs_uuid[BTRFS_UUID_SIZE];
2024 u8 dev_uuid[BTRFS_UUID_SIZE];
2025 u64 devid;
2026 int ret;
2027
2028 path = btrfs_alloc_path();
2029 if (!path)
2030 return -ENOMEM;
2031
2032 root = root->fs_info->chunk_root;
2033 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2034 key.offset = 0;
2035 key.type = BTRFS_DEV_ITEM_KEY;
2036
2037 while (1) {
2038 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2039 if (ret < 0)
2040 goto error;
2041
2042 leaf = path->nodes[0];
2043next_slot:
2044 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2045 ret = btrfs_next_leaf(root, path);
2046 if (ret > 0)
2047 break;
2048 if (ret < 0)
2049 goto error;
2050 leaf = path->nodes[0];
2051 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002052 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002053 continue;
2054 }
2055
2056 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2057 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2058 key.type != BTRFS_DEV_ITEM_KEY)
2059 break;
2060
2061 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2062 struct btrfs_dev_item);
2063 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002064 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002065 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002066 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002067 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002068 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2069 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002070 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002071
2072 if (device->fs_devices->seeding) {
2073 btrfs_set_device_generation(leaf, dev_item,
2074 device->generation);
2075 btrfs_mark_buffer_dirty(leaf);
2076 }
2077
2078 path->slots[0]++;
2079 goto next_slot;
2080 }
2081 ret = 0;
2082error:
2083 btrfs_free_path(path);
2084 return ret;
2085}
2086
Chris Mason788f20e2008-04-28 15:29:42 -04002087int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2088{
Josef Bacikd5e20032011-08-04 14:52:27 +00002089 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002090 struct btrfs_trans_handle *trans;
2091 struct btrfs_device *device;
2092 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002093 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002094 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002095 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002096 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002097 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002098 int ret = 0;
2099
Yan Zheng2b820322008-11-17 21:11:30 -05002100 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002101 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002102
Li Zefana5d163332011-12-07 20:08:40 -05002103 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002104 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002105 if (IS_ERR(bdev))
2106 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002107
Yan Zheng2b820322008-11-17 21:11:30 -05002108 if (root->fs_info->fs_devices->seeding) {
2109 seeding_dev = 1;
2110 down_write(&sb->s_umount);
2111 mutex_lock(&uuid_mutex);
2112 }
2113
Chris Mason8c8bee12008-09-29 11:19:10 -04002114 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002115
Chris Mason788f20e2008-04-28 15:29:42 -04002116 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002117
2118 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002119 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002120 if (device->bdev == bdev) {
2121 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002122 mutex_unlock(
2123 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002124 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002125 }
2126 }
Liu Bod25628b2012-11-14 14:35:30 +00002127 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002128
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002129 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2130 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002131 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002132 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002133 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002134 }
2135
Josef Bacik606686e2012-06-04 14:03:51 -04002136 name = rcu_string_strdup(device_path, GFP_NOFS);
2137 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002138 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002139 ret = -ENOMEM;
2140 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002141 }
Josef Bacik606686e2012-06-04 14:03:51 -04002142 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002143
Yan, Zhenga22285a2010-05-16 10:48:46 -04002144 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002145 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002146 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002147 kfree(device);
2148 ret = PTR_ERR(trans);
2149 goto error;
2150 }
2151
Josef Bacikd5e20032011-08-04 14:52:27 +00002152 q = bdev_get_queue(bdev);
2153 if (blk_queue_discard(q))
2154 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002155 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002156 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002157 device->io_width = root->sectorsize;
2158 device->io_align = root->sectorsize;
2159 device->sector_size = root->sectorsize;
2160 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002161 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002162 device->commit_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002163 device->dev_root = root->fs_info->dev_root;
2164 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002165 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002166 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002167 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002168 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002169 set_blocksize(device->bdev, 4096);
2170
Yan Zheng2b820322008-11-17 21:11:30 -05002171 if (seeding_dev) {
2172 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002173 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002174 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002175 }
2176
2177 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002178
Chris Masone5e9a522009-06-10 15:17:02 -04002179 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002180 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002181 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002182 list_add(&device->dev_alloc_list,
2183 &root->fs_info->fs_devices->alloc_list);
2184 root->fs_info->fs_devices->num_devices++;
2185 root->fs_info->fs_devices->open_devices++;
2186 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002187 root->fs_info->fs_devices->total_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -05002188 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2189
Josef Bacik2bf64752011-09-26 17:12:22 -04002190 spin_lock(&root->fs_info->free_chunk_lock);
2191 root->fs_info->free_chunk_space += device->total_bytes;
2192 spin_unlock(&root->fs_info->free_chunk_lock);
2193
Chris Masonc2898112009-06-10 09:51:32 -04002194 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2195 root->fs_info->fs_devices->rotating = 1;
2196
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002197 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002198 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002199 tmp + device->total_bytes);
Chris Mason788f20e2008-04-28 15:29:42 -04002200
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002201 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002202 btrfs_set_super_num_devices(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002203 tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002204
2205 /* add sysfs device entry */
2206 btrfs_kobj_add_device(root->fs_info, device);
2207
Miao Xie2196d6e2014-09-03 21:35:41 +08002208 /*
2209 * we've got more storage, clear any full flags on the space
2210 * infos
2211 */
2212 btrfs_clear_space_info_full(root->fs_info);
2213
2214 unlock_chunks(root);
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) {
Miao Xie2196d6e2014-09-03 21:35:41 +08002218 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05002219 ret = init_first_rw_device(trans, root, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002220 unlock_chunks(root);
David Sterba005d6422012-09-18 07:52:32 -06002221 if (ret) {
2222 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002223 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002224 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002225 }
2226
2227 ret = btrfs_add_device(trans, root, device);
2228 if (ret) {
2229 btrfs_abort_transaction(trans, root, ret);
2230 goto error_trans;
2231 }
2232
2233 if (seeding_dev) {
2234 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2235
Yan Zheng2b820322008-11-17 21:11:30 -05002236 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002237 if (ret) {
2238 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002239 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002240 }
Anand Jainb2373f22014-06-03 11:36:03 +08002241
2242 /* Sprouting would change fsid of the mounted root,
2243 * so rename the fsid on the sysfs
2244 */
2245 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2246 root->fs_info->fsid);
2247 if (kobject_rename(&root->fs_info->super_kobj, fsid_buf))
2248 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05002249 }
2250
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002251 root->fs_info->num_tolerated_disk_barrier_failures =
2252 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002253 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002254
2255 if (seeding_dev) {
2256 mutex_unlock(&uuid_mutex);
2257 up_write(&sb->s_umount);
2258
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002259 if (ret) /* transaction commit */
2260 return ret;
2261
Yan Zheng2b820322008-11-17 21:11:30 -05002262 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002263 if (ret < 0)
2264 btrfs_error(root->fs_info, ret,
2265 "Failed to relocate sys chunks after "
2266 "device initialization. This can be fixed "
2267 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002268 trans = btrfs_attach_transaction(root);
2269 if (IS_ERR(trans)) {
2270 if (PTR_ERR(trans) == -ENOENT)
2271 return 0;
2272 return PTR_ERR(trans);
2273 }
2274 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002275 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002276
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002277 /* Update ctime/mtime for libblkid */
2278 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002279 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002280
2281error_trans:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002282 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002283 rcu_string_free(device->name);
Anand Jain0d393762014-06-03 11:36:01 +08002284 btrfs_kobj_rm_device(root->fs_info, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002285 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002286error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002287 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002288 if (seeding_dev) {
2289 mutex_unlock(&uuid_mutex);
2290 up_write(&sb->s_umount);
2291 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002292 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002293}
2294
Stefan Behrense93c89c2012-11-05 17:33:06 +01002295int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002296 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002297 struct btrfs_device **device_out)
2298{
2299 struct request_queue *q;
2300 struct btrfs_device *device;
2301 struct block_device *bdev;
2302 struct btrfs_fs_info *fs_info = root->fs_info;
2303 struct list_head *devices;
2304 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002305 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002306 int ret = 0;
2307
2308 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002309 if (fs_info->fs_devices->seeding) {
2310 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002311 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002312 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002313
2314 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2315 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002316 if (IS_ERR(bdev)) {
2317 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002318 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002319 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002320
2321 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2322
2323 devices = &fs_info->fs_devices->devices;
2324 list_for_each_entry(device, devices, dev_list) {
2325 if (device->bdev == bdev) {
Miao Xie1c433662014-09-03 21:35:32 +08002326 btrfs_err(fs_info, "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002327 ret = -EEXIST;
2328 goto error;
2329 }
2330 }
2331
Miao Xie1c433662014-09-03 21:35:32 +08002332
Miao Xie7cc8e582014-09-03 21:35:38 +08002333 if (i_size_read(bdev->bd_inode) <
2334 btrfs_device_get_total_bytes(srcdev)) {
Miao Xie1c433662014-09-03 21:35:32 +08002335 btrfs_err(fs_info, "target device is smaller than source device!");
2336 ret = -EINVAL;
2337 goto error;
2338 }
2339
2340
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002341 device = btrfs_alloc_device(NULL, &devid, NULL);
2342 if (IS_ERR(device)) {
2343 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002344 goto error;
2345 }
2346
2347 name = rcu_string_strdup(device_path, GFP_NOFS);
2348 if (!name) {
2349 kfree(device);
2350 ret = -ENOMEM;
2351 goto error;
2352 }
2353 rcu_assign_pointer(device->name, name);
2354
2355 q = bdev_get_queue(bdev);
2356 if (blk_queue_discard(q))
2357 device->can_discard = 1;
2358 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2359 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002360 device->generation = 0;
2361 device->io_width = root->sectorsize;
2362 device->io_align = root->sectorsize;
2363 device->sector_size = root->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002364 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2365 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2366 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002367 ASSERT(list_empty(&srcdev->resized_list));
2368 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002369 device->commit_bytes_used = device->bytes_used;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002370 device->dev_root = fs_info->dev_root;
2371 device->bdev = bdev;
2372 device->in_fs_metadata = 1;
2373 device->is_tgtdev_for_dev_replace = 1;
2374 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002375 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002376 set_blocksize(device->bdev, 4096);
2377 device->fs_devices = fs_info->fs_devices;
2378 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2379 fs_info->fs_devices->num_devices++;
2380 fs_info->fs_devices->open_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002381 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2382
2383 *device_out = device;
2384 return ret;
2385
2386error:
2387 blkdev_put(bdev, FMODE_EXCL);
2388 return ret;
2389}
2390
2391void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2392 struct btrfs_device *tgtdev)
2393{
2394 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2395 tgtdev->io_width = fs_info->dev_root->sectorsize;
2396 tgtdev->io_align = fs_info->dev_root->sectorsize;
2397 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2398 tgtdev->dev_root = fs_info->dev_root;
2399 tgtdev->in_fs_metadata = 1;
2400}
2401
Chris Masond3977122009-01-05 21:25:51 -05002402static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2403 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002404{
2405 int ret;
2406 struct btrfs_path *path;
2407 struct btrfs_root *root;
2408 struct btrfs_dev_item *dev_item;
2409 struct extent_buffer *leaf;
2410 struct btrfs_key key;
2411
2412 root = device->dev_root->fs_info->chunk_root;
2413
2414 path = btrfs_alloc_path();
2415 if (!path)
2416 return -ENOMEM;
2417
2418 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2419 key.type = BTRFS_DEV_ITEM_KEY;
2420 key.offset = device->devid;
2421
2422 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2423 if (ret < 0)
2424 goto out;
2425
2426 if (ret > 0) {
2427 ret = -ENOENT;
2428 goto out;
2429 }
2430
2431 leaf = path->nodes[0];
2432 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2433
2434 btrfs_set_device_id(leaf, dev_item, device->devid);
2435 btrfs_set_device_type(leaf, dev_item, device->type);
2436 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2437 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2438 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002439 btrfs_set_device_total_bytes(leaf, dev_item,
2440 btrfs_device_get_disk_total_bytes(device));
2441 btrfs_set_device_bytes_used(leaf, dev_item,
2442 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002443 btrfs_mark_buffer_dirty(leaf);
2444
2445out:
2446 btrfs_free_path(path);
2447 return ret;
2448}
2449
Miao Xie2196d6e2014-09-03 21:35:41 +08002450int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002451 struct btrfs_device *device, u64 new_size)
2452{
2453 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002454 device->dev_root->fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002455 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002456 u64 old_total;
2457 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002458
Yan Zheng2b820322008-11-17 21:11:30 -05002459 if (!device->writeable)
2460 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002461
2462 lock_chunks(device->dev_root);
2463 old_total = btrfs_super_total_bytes(super_copy);
2464 diff = new_size - device->total_bytes;
2465
Stefan Behrens63a212a2012-11-05 18:29:28 +01002466 if (new_size <= device->total_bytes ||
Miao Xie2196d6e2014-09-03 21:35:41 +08002467 device->is_tgtdev_for_dev_replace) {
2468 unlock_chunks(device->dev_root);
Yan Zheng2b820322008-11-17 21:11:30 -05002469 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002470 }
Yan Zheng2b820322008-11-17 21:11:30 -05002471
Miao Xie935e5cc2014-09-03 21:35:33 +08002472 fs_devices = device->dev_root->fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002473
2474 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002475 device->fs_devices->total_rw_bytes += diff;
2476
Miao Xie7cc8e582014-09-03 21:35:38 +08002477 btrfs_device_set_total_bytes(device, new_size);
2478 btrfs_device_set_disk_total_bytes(device, new_size);
Chris Mason4184ea72009-03-10 12:39:20 -04002479 btrfs_clear_space_info_full(device->dev_root->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002480 if (list_empty(&device->resized_list))
2481 list_add_tail(&device->resized_list,
2482 &fs_devices->resized_devices);
Miao Xie2196d6e2014-09-03 21:35:41 +08002483 unlock_chunks(device->dev_root);
Chris Mason4184ea72009-03-10 12:39:20 -04002484
Chris Mason8f18cf12008-04-25 16:53:30 -04002485 return btrfs_update_device(trans, device);
2486}
2487
2488static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Zhao Leia688a042015-02-09 20:31:44 +08002489 struct btrfs_root *root, u64 chunk_objectid,
Chris Mason8f18cf12008-04-25 16:53:30 -04002490 u64 chunk_offset)
2491{
2492 int ret;
2493 struct btrfs_path *path;
2494 struct btrfs_key key;
2495
2496 root = root->fs_info->chunk_root;
2497 path = btrfs_alloc_path();
2498 if (!path)
2499 return -ENOMEM;
2500
2501 key.objectid = chunk_objectid;
2502 key.offset = chunk_offset;
2503 key.type = BTRFS_CHUNK_ITEM_KEY;
2504
2505 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002506 if (ret < 0)
2507 goto out;
2508 else if (ret > 0) { /* Logic error or corruption */
2509 btrfs_error(root->fs_info, -ENOENT,
2510 "Failed lookup while freeing chunk.");
2511 ret = -ENOENT;
2512 goto out;
2513 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002514
2515 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002516 if (ret < 0)
2517 btrfs_error(root->fs_info, ret,
2518 "Failed to delete chunk item.");
2519out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002520 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002521 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002522}
2523
Christoph Hellwigb2950862008-12-02 09:54:17 -05002524static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002525 chunk_offset)
2526{
David Sterba6c417612011-04-13 15:41:04 +02002527 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002528 struct btrfs_disk_key *disk_key;
2529 struct btrfs_chunk *chunk;
2530 u8 *ptr;
2531 int ret = 0;
2532 u32 num_stripes;
2533 u32 array_size;
2534 u32 len = 0;
2535 u32 cur;
2536 struct btrfs_key key;
2537
Miao Xie2196d6e2014-09-03 21:35:41 +08002538 lock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002539 array_size = btrfs_super_sys_array_size(super_copy);
2540
2541 ptr = super_copy->sys_chunk_array;
2542 cur = 0;
2543
2544 while (cur < array_size) {
2545 disk_key = (struct btrfs_disk_key *)ptr;
2546 btrfs_disk_key_to_cpu(&key, disk_key);
2547
2548 len = sizeof(*disk_key);
2549
2550 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2551 chunk = (struct btrfs_chunk *)(ptr + len);
2552 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2553 len += btrfs_chunk_item_size(num_stripes);
2554 } else {
2555 ret = -EIO;
2556 break;
2557 }
2558 if (key.objectid == chunk_objectid &&
2559 key.offset == chunk_offset) {
2560 memmove(ptr, ptr + len, array_size - (cur + len));
2561 array_size -= len;
2562 btrfs_set_super_sys_array_size(super_copy, array_size);
2563 } else {
2564 ptr += len;
2565 cur += len;
2566 }
2567 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002568 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002569 return ret;
2570}
2571
Josef Bacik47ab2a62014-09-18 11:20:02 -04002572int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2573 struct btrfs_root *root, u64 chunk_offset)
2574{
2575 struct extent_map_tree *em_tree;
2576 struct extent_map *em;
2577 struct btrfs_root *extent_root = root->fs_info->extent_root;
2578 struct map_lookup *map;
2579 u64 dev_extent_len = 0;
2580 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002581 int i, ret = 0;
2582
2583 /* Just in case */
2584 root = root->fs_info->chunk_root;
2585 em_tree = &root->fs_info->mapping_tree.map_tree;
2586
2587 read_lock(&em_tree->lock);
2588 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2589 read_unlock(&em_tree->lock);
2590
2591 if (!em || em->start > chunk_offset ||
2592 em->start + em->len < chunk_offset) {
2593 /*
2594 * This is a logic error, but we don't want to just rely on the
2595 * user having built with ASSERT enabled, so if ASSERT doens't
2596 * do anything we still error out.
2597 */
2598 ASSERT(0);
2599 if (em)
2600 free_extent_map(em);
2601 return -EINVAL;
2602 }
2603 map = (struct map_lookup *)em->bdev;
2604
2605 for (i = 0; i < map->num_stripes; i++) {
2606 struct btrfs_device *device = map->stripes[i].dev;
2607 ret = btrfs_free_dev_extent(trans, device,
2608 map->stripes[i].physical,
2609 &dev_extent_len);
2610 if (ret) {
2611 btrfs_abort_transaction(trans, root, ret);
2612 goto out;
2613 }
2614
2615 if (device->bytes_used > 0) {
2616 lock_chunks(root);
2617 btrfs_device_set_bytes_used(device,
2618 device->bytes_used - dev_extent_len);
2619 spin_lock(&root->fs_info->free_chunk_lock);
2620 root->fs_info->free_chunk_space += dev_extent_len;
2621 spin_unlock(&root->fs_info->free_chunk_lock);
2622 btrfs_clear_space_info_full(root->fs_info);
2623 unlock_chunks(root);
2624 }
2625
2626 if (map->stripes[i].dev) {
2627 ret = btrfs_update_device(trans, map->stripes[i].dev);
2628 if (ret) {
2629 btrfs_abort_transaction(trans, root, ret);
2630 goto out;
2631 }
2632 }
2633 }
Zhao Leia688a042015-02-09 20:31:44 +08002634 ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002635 if (ret) {
2636 btrfs_abort_transaction(trans, root, ret);
2637 goto out;
2638 }
2639
2640 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2641
2642 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2643 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2644 if (ret) {
2645 btrfs_abort_transaction(trans, root, ret);
2646 goto out;
2647 }
2648 }
2649
Filipe Manana04216822014-11-27 21:14:15 +00002650 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002651 if (ret) {
2652 btrfs_abort_transaction(trans, extent_root, ret);
2653 goto out;
2654 }
2655
Josef Bacik47ab2a62014-09-18 11:20:02 -04002656out:
2657 /* once for us */
2658 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002659 return ret;
2660}
2661
Christoph Hellwigb2950862008-12-02 09:54:17 -05002662static int btrfs_relocate_chunk(struct btrfs_root *root,
Zhao Leia688a042015-02-09 20:31:44 +08002663 u64 chunk_objectid,
2664 u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002665{
Chris Mason8f18cf12008-04-25 16:53:30 -04002666 struct btrfs_root *extent_root;
2667 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002668 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002669
2670 root = root->fs_info->chunk_root;
2671 extent_root = root->fs_info->extent_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002672
Josef Bacikba1bf482009-09-11 16:11:19 -04002673 ret = btrfs_can_relocate(extent_root, chunk_offset);
2674 if (ret)
2675 return -ENOSPC;
2676
Chris Mason8f18cf12008-04-25 16:53:30 -04002677 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002678 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002679 if (ret)
2680 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002681
Yan, Zhenga22285a2010-05-16 10:48:46 -04002682 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002683 if (IS_ERR(trans)) {
2684 ret = PTR_ERR(trans);
2685 btrfs_std_error(root->fs_info, ret);
2686 return ret;
2687 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002688
2689 /*
2690 * step two, delete the device extents and the
2691 * chunk tree entries
2692 */
Josef Bacik47ab2a62014-09-18 11:20:02 -04002693 ret = btrfs_remove_chunk(trans, root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002694 btrfs_end_transaction(trans, root);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002695 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002696}
2697
Yan Zheng2b820322008-11-17 21:11:30 -05002698static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2699{
2700 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2701 struct btrfs_path *path;
2702 struct extent_buffer *leaf;
2703 struct btrfs_chunk *chunk;
2704 struct btrfs_key key;
2705 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05002706 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002707 bool retried = false;
2708 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002709 int ret;
2710
2711 path = btrfs_alloc_path();
2712 if (!path)
2713 return -ENOMEM;
2714
Josef Bacikba1bf482009-09-11 16:11:19 -04002715again:
Yan Zheng2b820322008-11-17 21:11:30 -05002716 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2717 key.offset = (u64)-1;
2718 key.type = BTRFS_CHUNK_ITEM_KEY;
2719
2720 while (1) {
2721 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2722 if (ret < 0)
2723 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002724 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002725
2726 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2727 key.type);
2728 if (ret < 0)
2729 goto error;
2730 if (ret > 0)
2731 break;
2732
2733 leaf = path->nodes[0];
2734 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2735
2736 chunk = btrfs_item_ptr(leaf, path->slots[0],
2737 struct btrfs_chunk);
2738 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002739 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002740
2741 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Zhao Leia688a042015-02-09 20:31:44 +08002742 ret = btrfs_relocate_chunk(chunk_root,
Yan Zheng2b820322008-11-17 21:11:30 -05002743 found_key.objectid,
2744 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002745 if (ret == -ENOSPC)
2746 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302747 else
2748 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002749 }
2750
2751 if (found_key.offset == 0)
2752 break;
2753 key.offset = found_key.offset - 1;
2754 }
2755 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002756 if (failed && !retried) {
2757 failed = 0;
2758 retried = true;
2759 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302760 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002761 ret = -ENOSPC;
2762 }
Yan Zheng2b820322008-11-17 21:11:30 -05002763error:
2764 btrfs_free_path(path);
2765 return ret;
2766}
2767
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002768static int insert_balance_item(struct btrfs_root *root,
2769 struct btrfs_balance_control *bctl)
2770{
2771 struct btrfs_trans_handle *trans;
2772 struct btrfs_balance_item *item;
2773 struct btrfs_disk_balance_args disk_bargs;
2774 struct btrfs_path *path;
2775 struct extent_buffer *leaf;
2776 struct btrfs_key key;
2777 int ret, err;
2778
2779 path = btrfs_alloc_path();
2780 if (!path)
2781 return -ENOMEM;
2782
2783 trans = btrfs_start_transaction(root, 0);
2784 if (IS_ERR(trans)) {
2785 btrfs_free_path(path);
2786 return PTR_ERR(trans);
2787 }
2788
2789 key.objectid = BTRFS_BALANCE_OBJECTID;
2790 key.type = BTRFS_BALANCE_ITEM_KEY;
2791 key.offset = 0;
2792
2793 ret = btrfs_insert_empty_item(trans, root, path, &key,
2794 sizeof(*item));
2795 if (ret)
2796 goto out;
2797
2798 leaf = path->nodes[0];
2799 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2800
2801 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2802
2803 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2804 btrfs_set_balance_data(leaf, item, &disk_bargs);
2805 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2806 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2807 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2808 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2809
2810 btrfs_set_balance_flags(leaf, item, bctl->flags);
2811
2812 btrfs_mark_buffer_dirty(leaf);
2813out:
2814 btrfs_free_path(path);
2815 err = btrfs_commit_transaction(trans, root);
2816 if (err && !ret)
2817 ret = err;
2818 return ret;
2819}
2820
2821static int del_balance_item(struct btrfs_root *root)
2822{
2823 struct btrfs_trans_handle *trans;
2824 struct btrfs_path *path;
2825 struct btrfs_key key;
2826 int ret, err;
2827
2828 path = btrfs_alloc_path();
2829 if (!path)
2830 return -ENOMEM;
2831
2832 trans = btrfs_start_transaction(root, 0);
2833 if (IS_ERR(trans)) {
2834 btrfs_free_path(path);
2835 return PTR_ERR(trans);
2836 }
2837
2838 key.objectid = BTRFS_BALANCE_OBJECTID;
2839 key.type = BTRFS_BALANCE_ITEM_KEY;
2840 key.offset = 0;
2841
2842 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2843 if (ret < 0)
2844 goto out;
2845 if (ret > 0) {
2846 ret = -ENOENT;
2847 goto out;
2848 }
2849
2850 ret = btrfs_del_item(trans, root, path);
2851out:
2852 btrfs_free_path(path);
2853 err = btrfs_commit_transaction(trans, root);
2854 if (err && !ret)
2855 ret = err;
2856 return ret;
2857}
2858
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002859/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002860 * This is a heuristic used to reduce the number of chunks balanced on
2861 * resume after balance was interrupted.
2862 */
2863static void update_balance_args(struct btrfs_balance_control *bctl)
2864{
2865 /*
2866 * Turn on soft mode for chunk types that were being converted.
2867 */
2868 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2869 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2870 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2871 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2872 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2873 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2874
2875 /*
2876 * Turn on usage filter if is not already used. The idea is
2877 * that chunks that we have already balanced should be
2878 * reasonably full. Don't do it for chunks that are being
2879 * converted - that will keep us from relocating unconverted
2880 * (albeit full) chunks.
2881 */
2882 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2883 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2884 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2885 bctl->data.usage = 90;
2886 }
2887 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2888 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2889 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2890 bctl->sys.usage = 90;
2891 }
2892 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2893 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2894 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2895 bctl->meta.usage = 90;
2896 }
2897}
2898
2899/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002900 * Should be called with both balance and volume mutexes held to
2901 * serialize other volume operations (add_dev/rm_dev/resize) with
2902 * restriper. Same goes for unset_balance_control.
2903 */
2904static void set_balance_control(struct btrfs_balance_control *bctl)
2905{
2906 struct btrfs_fs_info *fs_info = bctl->fs_info;
2907
2908 BUG_ON(fs_info->balance_ctl);
2909
2910 spin_lock(&fs_info->balance_lock);
2911 fs_info->balance_ctl = bctl;
2912 spin_unlock(&fs_info->balance_lock);
2913}
2914
2915static void unset_balance_control(struct btrfs_fs_info *fs_info)
2916{
2917 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2918
2919 BUG_ON(!fs_info->balance_ctl);
2920
2921 spin_lock(&fs_info->balance_lock);
2922 fs_info->balance_ctl = NULL;
2923 spin_unlock(&fs_info->balance_lock);
2924
2925 kfree(bctl);
2926}
2927
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002928/*
2929 * Balance filters. Return 1 if chunk should be filtered out
2930 * (should not be balanced).
2931 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002932static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002933 struct btrfs_balance_args *bargs)
2934{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002935 chunk_type = chunk_to_extended(chunk_type) &
2936 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002937
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002938 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002939 return 0;
2940
2941 return 1;
2942}
2943
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002944static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2945 struct btrfs_balance_args *bargs)
2946{
2947 struct btrfs_block_group_cache *cache;
2948 u64 chunk_used, user_thresh;
2949 int ret = 1;
2950
2951 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2952 chunk_used = btrfs_block_group_used(&cache->item);
2953
Ilya Dryomova105bb82013-01-21 15:15:56 +02002954 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002955 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002956 else if (bargs->usage > 100)
2957 user_thresh = cache->key.offset;
2958 else
2959 user_thresh = div_factor_fine(cache->key.offset,
2960 bargs->usage);
2961
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002962 if (chunk_used < user_thresh)
2963 ret = 0;
2964
2965 btrfs_put_block_group(cache);
2966 return ret;
2967}
2968
Ilya Dryomov409d4042012-01-16 22:04:47 +02002969static int chunk_devid_filter(struct extent_buffer *leaf,
2970 struct btrfs_chunk *chunk,
2971 struct btrfs_balance_args *bargs)
2972{
2973 struct btrfs_stripe *stripe;
2974 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2975 int i;
2976
2977 for (i = 0; i < num_stripes; i++) {
2978 stripe = btrfs_stripe_nr(chunk, i);
2979 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2980 return 0;
2981 }
2982
2983 return 1;
2984}
2985
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002986/* [pstart, pend) */
2987static int chunk_drange_filter(struct extent_buffer *leaf,
2988 struct btrfs_chunk *chunk,
2989 u64 chunk_offset,
2990 struct btrfs_balance_args *bargs)
2991{
2992 struct btrfs_stripe *stripe;
2993 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2994 u64 stripe_offset;
2995 u64 stripe_length;
2996 int factor;
2997 int i;
2998
2999 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3000 return 0;
3001
3002 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003003 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3004 factor = num_stripes / 2;
3005 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3006 factor = num_stripes - 1;
3007 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3008 factor = num_stripes - 2;
3009 } else {
3010 factor = num_stripes;
3011 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003012
3013 for (i = 0; i < num_stripes; i++) {
3014 stripe = btrfs_stripe_nr(chunk, i);
3015 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3016 continue;
3017
3018 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3019 stripe_length = btrfs_chunk_length(leaf, chunk);
3020 do_div(stripe_length, factor);
3021
3022 if (stripe_offset < bargs->pend &&
3023 stripe_offset + stripe_length > bargs->pstart)
3024 return 0;
3025 }
3026
3027 return 1;
3028}
3029
Ilya Dryomovea671762012-01-16 22:04:48 +02003030/* [vstart, vend) */
3031static int chunk_vrange_filter(struct extent_buffer *leaf,
3032 struct btrfs_chunk *chunk,
3033 u64 chunk_offset,
3034 struct btrfs_balance_args *bargs)
3035{
3036 if (chunk_offset < bargs->vend &&
3037 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3038 /* at least part of the chunk is inside this vrange */
3039 return 0;
3040
3041 return 1;
3042}
3043
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003044static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003045 struct btrfs_balance_args *bargs)
3046{
3047 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3048 return 0;
3049
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003050 chunk_type = chunk_to_extended(chunk_type) &
3051 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003052
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003053 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003054 return 1;
3055
3056 return 0;
3057}
3058
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003059static int should_balance_chunk(struct btrfs_root *root,
3060 struct extent_buffer *leaf,
3061 struct btrfs_chunk *chunk, u64 chunk_offset)
3062{
3063 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3064 struct btrfs_balance_args *bargs = NULL;
3065 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3066
3067 /* type filter */
3068 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3069 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3070 return 0;
3071 }
3072
3073 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3074 bargs = &bctl->data;
3075 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3076 bargs = &bctl->sys;
3077 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3078 bargs = &bctl->meta;
3079
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003080 /* profiles filter */
3081 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3082 chunk_profiles_filter(chunk_type, bargs)) {
3083 return 0;
3084 }
3085
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003086 /* usage filter */
3087 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3088 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3089 return 0;
3090 }
3091
Ilya Dryomov409d4042012-01-16 22:04:47 +02003092 /* devid filter */
3093 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3094 chunk_devid_filter(leaf, chunk, bargs)) {
3095 return 0;
3096 }
3097
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003098 /* drange filter, makes sense only with devid filter */
3099 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3100 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3101 return 0;
3102 }
3103
Ilya Dryomovea671762012-01-16 22:04:48 +02003104 /* vrange filter */
3105 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3106 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3107 return 0;
3108 }
3109
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003110 /* soft profile changing mode */
3111 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3112 chunk_soft_convert_filter(chunk_type, bargs)) {
3113 return 0;
3114 }
3115
David Sterba7d824b62014-05-07 17:37:51 +02003116 /*
3117 * limited by count, must be the last filter
3118 */
3119 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3120 if (bargs->limit == 0)
3121 return 0;
3122 else
3123 bargs->limit--;
3124 }
3125
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003126 return 1;
3127}
3128
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003129static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003130{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003131 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003132 struct btrfs_root *chunk_root = fs_info->chunk_root;
3133 struct btrfs_root *dev_root = fs_info->dev_root;
3134 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003135 struct btrfs_device *device;
3136 u64 old_size;
3137 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003138 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003139 struct btrfs_path *path;
3140 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003141 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003142 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003143 struct extent_buffer *leaf;
3144 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003145 int ret;
3146 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003147 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003148 u64 limit_data = bctl->data.limit;
3149 u64 limit_meta = bctl->meta.limit;
3150 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003151
Chris Masonec44a352008-04-28 15:29:52 -04003152 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003153 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003154 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003155 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003156 size_to_free = div_factor(old_size, 1);
3157 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003158 if (!device->writeable ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003159 btrfs_device_get_total_bytes(device) -
3160 btrfs_device_get_bytes_used(device) > size_to_free ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003161 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003162 continue;
3163
3164 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003165 if (ret == -ENOSPC)
3166 break;
Chris Masonec44a352008-04-28 15:29:52 -04003167 BUG_ON(ret);
3168
Yan, Zhenga22285a2010-05-16 10:48:46 -04003169 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003170 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003171
3172 ret = btrfs_grow_device(trans, device, old_size);
3173 BUG_ON(ret);
3174
3175 btrfs_end_transaction(trans, dev_root);
3176 }
3177
3178 /* step two, relocate all the chunks */
3179 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003180 if (!path) {
3181 ret = -ENOMEM;
3182 goto error;
3183 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003184
3185 /* zero out stat counters */
3186 spin_lock(&fs_info->balance_lock);
3187 memset(&bctl->stat, 0, sizeof(bctl->stat));
3188 spin_unlock(&fs_info->balance_lock);
3189again:
David Sterba7d824b62014-05-07 17:37:51 +02003190 if (!counting) {
3191 bctl->data.limit = limit_data;
3192 bctl->meta.limit = limit_meta;
3193 bctl->sys.limit = limit_sys;
3194 }
Chris Masonec44a352008-04-28 15:29:52 -04003195 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3196 key.offset = (u64)-1;
3197 key.type = BTRFS_CHUNK_ITEM_KEY;
3198
Chris Masond3977122009-01-05 21:25:51 -05003199 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003200 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003201 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003202 ret = -ECANCELED;
3203 goto error;
3204 }
3205
Chris Masonec44a352008-04-28 15:29:52 -04003206 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3207 if (ret < 0)
3208 goto error;
3209
3210 /*
3211 * this shouldn't happen, it means the last relocate
3212 * failed
3213 */
3214 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003215 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003216
3217 ret = btrfs_previous_item(chunk_root, path, 0,
3218 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003219 if (ret) {
3220 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003221 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003222 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003223
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003224 leaf = path->nodes[0];
3225 slot = path->slots[0];
3226 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3227
Chris Masonec44a352008-04-28 15:29:52 -04003228 if (found_key.objectid != key.objectid)
3229 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04003230
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003231 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3232
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003233 if (!counting) {
3234 spin_lock(&fs_info->balance_lock);
3235 bctl->stat.considered++;
3236 spin_unlock(&fs_info->balance_lock);
3237 }
3238
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003239 ret = should_balance_chunk(chunk_root, leaf, chunk,
3240 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003241 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003242 if (!ret)
3243 goto loop;
3244
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003245 if (counting) {
3246 spin_lock(&fs_info->balance_lock);
3247 bctl->stat.expected++;
3248 spin_unlock(&fs_info->balance_lock);
3249 goto loop;
3250 }
3251
Chris Masonec44a352008-04-28 15:29:52 -04003252 ret = btrfs_relocate_chunk(chunk_root,
Chris Masonec44a352008-04-28 15:29:52 -04003253 found_key.objectid,
3254 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003255 if (ret && ret != -ENOSPC)
3256 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003257 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003258 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003259 } else {
3260 spin_lock(&fs_info->balance_lock);
3261 bctl->stat.completed++;
3262 spin_unlock(&fs_info->balance_lock);
3263 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003264loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003265 if (found_key.offset == 0)
3266 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003267 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003268 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003269
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003270 if (counting) {
3271 btrfs_release_path(path);
3272 counting = false;
3273 goto again;
3274 }
Chris Masonec44a352008-04-28 15:29:52 -04003275error:
3276 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003277 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003278 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003279 enospc_errors);
3280 if (!ret)
3281 ret = -ENOSPC;
3282 }
3283
Chris Masonec44a352008-04-28 15:29:52 -04003284 return ret;
3285}
3286
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003287/**
3288 * alloc_profile_is_valid - see if a given profile is valid and reduced
3289 * @flags: profile to validate
3290 * @extended: if true @flags is treated as an extended profile
3291 */
3292static int alloc_profile_is_valid(u64 flags, int extended)
3293{
3294 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3295 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3296
3297 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3298
3299 /* 1) check that all other bits are zeroed */
3300 if (flags & ~mask)
3301 return 0;
3302
3303 /* 2) see if profile is reduced */
3304 if (flags == 0)
3305 return !extended; /* "0" is valid for usual profiles */
3306
3307 /* true if exactly one bit set */
3308 return (flags & (flags - 1)) == 0;
3309}
3310
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003311static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3312{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003313 /* cancel requested || normal exit path */
3314 return atomic_read(&fs_info->balance_cancel_req) ||
3315 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3316 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003317}
3318
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003319static void __cancel_balance(struct btrfs_fs_info *fs_info)
3320{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003321 int ret;
3322
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003323 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003324 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003325 if (ret)
3326 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003327
3328 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003329}
3330
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003331/*
3332 * Should be called with both balance and volume mutexes held
3333 */
3334int btrfs_balance(struct btrfs_balance_control *bctl,
3335 struct btrfs_ioctl_balance_args *bargs)
3336{
3337 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003338 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003339 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003340 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003341 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003342 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003343
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003344 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003345 atomic_read(&fs_info->balance_pause_req) ||
3346 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003347 ret = -EINVAL;
3348 goto out;
3349 }
3350
Ilya Dryomove4837f82012-03-27 17:09:17 +03003351 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3352 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3353 mixed = 1;
3354
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003355 /*
3356 * In case of mixed groups both data and meta should be picked,
3357 * and identical options should be given for both of them.
3358 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003359 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3360 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003361 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3362 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3363 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003364 btrfs_err(fs_info, "with mixed groups data and "
3365 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003366 ret = -EINVAL;
3367 goto out;
3368 }
3369 }
3370
Stefan Behrens8dabb742012-11-06 13:15:27 +01003371 num_devices = fs_info->fs_devices->num_devices;
3372 btrfs_dev_replace_lock(&fs_info->dev_replace);
3373 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3374 BUG_ON(num_devices < 1);
3375 num_devices--;
3376 }
3377 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003378 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003379 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003380 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003381 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003382 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003383 if (num_devices > 2)
3384 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3385 if (num_devices > 3)
3386 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3387 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003388 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3389 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3390 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003391 btrfs_err(fs_info, "unable to start balance with target "
3392 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003393 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003394 ret = -EINVAL;
3395 goto out;
3396 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003397 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3398 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3399 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003400 btrfs_err(fs_info,
3401 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003402 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003403 ret = -EINVAL;
3404 goto out;
3405 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003406 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3407 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3408 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003409 btrfs_err(fs_info,
3410 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003411 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003412 ret = -EINVAL;
3413 goto out;
3414 }
3415
Ilya Dryomove4837f82012-03-27 17:09:17 +03003416 /* allow dup'ed data chunks only in mixed mode */
3417 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003418 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003419 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003420 ret = -EINVAL;
3421 goto out;
3422 }
3423
3424 /* allow to reduce meta or sys integrity only if force set */
3425 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003426 BTRFS_BLOCK_GROUP_RAID10 |
3427 BTRFS_BLOCK_GROUP_RAID5 |
3428 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003429 do {
3430 seq = read_seqbegin(&fs_info->profiles_lock);
3431
3432 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3433 (fs_info->avail_system_alloc_bits & allowed) &&
3434 !(bctl->sys.target & allowed)) ||
3435 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3436 (fs_info->avail_metadata_alloc_bits & allowed) &&
3437 !(bctl->meta.target & allowed))) {
3438 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003439 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003440 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003441 btrfs_err(fs_info, "balance will reduce metadata "
3442 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003443 ret = -EINVAL;
3444 goto out;
3445 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003446 }
Miao Xiede98ced2013-01-29 10:13:12 +00003447 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003448
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003449 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3450 int num_tolerated_disk_barrier_failures;
3451 u64 target = bctl->sys.target;
3452
3453 num_tolerated_disk_barrier_failures =
3454 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3455 if (num_tolerated_disk_barrier_failures > 0 &&
3456 (target &
3457 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3458 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3459 num_tolerated_disk_barrier_failures = 0;
3460 else if (num_tolerated_disk_barrier_failures > 1 &&
3461 (target &
3462 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3463 num_tolerated_disk_barrier_failures = 1;
3464
3465 fs_info->num_tolerated_disk_barrier_failures =
3466 num_tolerated_disk_barrier_failures;
3467 }
3468
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003469 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003470 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003471 goto out;
3472
Ilya Dryomov59641012012-01-16 22:04:48 +02003473 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3474 BUG_ON(ret == -EEXIST);
3475 set_balance_control(bctl);
3476 } else {
3477 BUG_ON(ret != -EEXIST);
3478 spin_lock(&fs_info->balance_lock);
3479 update_balance_args(bctl);
3480 spin_unlock(&fs_info->balance_lock);
3481 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003482
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003483 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003484 mutex_unlock(&fs_info->balance_mutex);
3485
3486 ret = __btrfs_balance(fs_info);
3487
3488 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003489 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003490
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003491 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3492 fs_info->num_tolerated_disk_barrier_failures =
3493 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3494 }
3495
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003496 if (bargs) {
3497 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003498 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003499 }
3500
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003501 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3502 balance_need_close(fs_info)) {
3503 __cancel_balance(fs_info);
3504 }
3505
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003506 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003507
3508 return ret;
3509out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003510 if (bctl->flags & BTRFS_BALANCE_RESUME)
3511 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003512 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003513 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003514 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3515 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003516 return ret;
3517}
3518
3519static int balance_kthread(void *data)
3520{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003521 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003522 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003523
3524 mutex_lock(&fs_info->volume_mutex);
3525 mutex_lock(&fs_info->balance_mutex);
3526
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003527 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003528 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003529 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003530 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003531
3532 mutex_unlock(&fs_info->balance_mutex);
3533 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003534
Ilya Dryomov59641012012-01-16 22:04:48 +02003535 return ret;
3536}
3537
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003538int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3539{
3540 struct task_struct *tsk;
3541
3542 spin_lock(&fs_info->balance_lock);
3543 if (!fs_info->balance_ctl) {
3544 spin_unlock(&fs_info->balance_lock);
3545 return 0;
3546 }
3547 spin_unlock(&fs_info->balance_lock);
3548
3549 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003550 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003551 return 0;
3552 }
3553
3554 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303555 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003556}
3557
Ilya Dryomov68310a52012-06-22 12:24:12 -06003558int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003559{
Ilya Dryomov59641012012-01-16 22:04:48 +02003560 struct btrfs_balance_control *bctl;
3561 struct btrfs_balance_item *item;
3562 struct btrfs_disk_balance_args disk_bargs;
3563 struct btrfs_path *path;
3564 struct extent_buffer *leaf;
3565 struct btrfs_key key;
3566 int ret;
3567
3568 path = btrfs_alloc_path();
3569 if (!path)
3570 return -ENOMEM;
3571
Ilya Dryomov68310a52012-06-22 12:24:12 -06003572 key.objectid = BTRFS_BALANCE_OBJECTID;
3573 key.type = BTRFS_BALANCE_ITEM_KEY;
3574 key.offset = 0;
3575
3576 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3577 if (ret < 0)
3578 goto out;
3579 if (ret > 0) { /* ret = -ENOENT; */
3580 ret = 0;
3581 goto out;
3582 }
3583
Ilya Dryomov59641012012-01-16 22:04:48 +02003584 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3585 if (!bctl) {
3586 ret = -ENOMEM;
3587 goto out;
3588 }
3589
Ilya Dryomov59641012012-01-16 22:04:48 +02003590 leaf = path->nodes[0];
3591 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3592
Ilya Dryomov68310a52012-06-22 12:24:12 -06003593 bctl->fs_info = fs_info;
3594 bctl->flags = btrfs_balance_flags(leaf, item);
3595 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003596
3597 btrfs_balance_data(leaf, item, &disk_bargs);
3598 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3599 btrfs_balance_meta(leaf, item, &disk_bargs);
3600 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3601 btrfs_balance_sys(leaf, item, &disk_bargs);
3602 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3603
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003604 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3605
Ilya Dryomov68310a52012-06-22 12:24:12 -06003606 mutex_lock(&fs_info->volume_mutex);
3607 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003608
Ilya Dryomov68310a52012-06-22 12:24:12 -06003609 set_balance_control(bctl);
3610
3611 mutex_unlock(&fs_info->balance_mutex);
3612 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003613out:
3614 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003615 return ret;
3616}
3617
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003618int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3619{
3620 int ret = 0;
3621
3622 mutex_lock(&fs_info->balance_mutex);
3623 if (!fs_info->balance_ctl) {
3624 mutex_unlock(&fs_info->balance_mutex);
3625 return -ENOTCONN;
3626 }
3627
3628 if (atomic_read(&fs_info->balance_running)) {
3629 atomic_inc(&fs_info->balance_pause_req);
3630 mutex_unlock(&fs_info->balance_mutex);
3631
3632 wait_event(fs_info->balance_wait_q,
3633 atomic_read(&fs_info->balance_running) == 0);
3634
3635 mutex_lock(&fs_info->balance_mutex);
3636 /* we are good with balance_ctl ripped off from under us */
3637 BUG_ON(atomic_read(&fs_info->balance_running));
3638 atomic_dec(&fs_info->balance_pause_req);
3639 } else {
3640 ret = -ENOTCONN;
3641 }
3642
3643 mutex_unlock(&fs_info->balance_mutex);
3644 return ret;
3645}
3646
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003647int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3648{
Ilya Dryomove649e582013-10-10 20:40:21 +03003649 if (fs_info->sb->s_flags & MS_RDONLY)
3650 return -EROFS;
3651
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003652 mutex_lock(&fs_info->balance_mutex);
3653 if (!fs_info->balance_ctl) {
3654 mutex_unlock(&fs_info->balance_mutex);
3655 return -ENOTCONN;
3656 }
3657
3658 atomic_inc(&fs_info->balance_cancel_req);
3659 /*
3660 * if we are running just wait and return, balance item is
3661 * deleted in btrfs_balance in this case
3662 */
3663 if (atomic_read(&fs_info->balance_running)) {
3664 mutex_unlock(&fs_info->balance_mutex);
3665 wait_event(fs_info->balance_wait_q,
3666 atomic_read(&fs_info->balance_running) == 0);
3667 mutex_lock(&fs_info->balance_mutex);
3668 } else {
3669 /* __cancel_balance needs volume_mutex */
3670 mutex_unlock(&fs_info->balance_mutex);
3671 mutex_lock(&fs_info->volume_mutex);
3672 mutex_lock(&fs_info->balance_mutex);
3673
3674 if (fs_info->balance_ctl)
3675 __cancel_balance(fs_info);
3676
3677 mutex_unlock(&fs_info->volume_mutex);
3678 }
3679
3680 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3681 atomic_dec(&fs_info->balance_cancel_req);
3682 mutex_unlock(&fs_info->balance_mutex);
3683 return 0;
3684}
3685
Stefan Behrens803b2f52013-08-15 17:11:21 +02003686static int btrfs_uuid_scan_kthread(void *data)
3687{
3688 struct btrfs_fs_info *fs_info = data;
3689 struct btrfs_root *root = fs_info->tree_root;
3690 struct btrfs_key key;
3691 struct btrfs_key max_key;
3692 struct btrfs_path *path = NULL;
3693 int ret = 0;
3694 struct extent_buffer *eb;
3695 int slot;
3696 struct btrfs_root_item root_item;
3697 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003698 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003699
3700 path = btrfs_alloc_path();
3701 if (!path) {
3702 ret = -ENOMEM;
3703 goto out;
3704 }
3705
3706 key.objectid = 0;
3707 key.type = BTRFS_ROOT_ITEM_KEY;
3708 key.offset = 0;
3709
3710 max_key.objectid = (u64)-1;
3711 max_key.type = BTRFS_ROOT_ITEM_KEY;
3712 max_key.offset = (u64)-1;
3713
Stefan Behrens803b2f52013-08-15 17:11:21 +02003714 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003715 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003716 if (ret) {
3717 if (ret > 0)
3718 ret = 0;
3719 break;
3720 }
3721
3722 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3723 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3724 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3725 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3726 goto skip;
3727
3728 eb = path->nodes[0];
3729 slot = path->slots[0];
3730 item_size = btrfs_item_size_nr(eb, slot);
3731 if (item_size < sizeof(root_item))
3732 goto skip;
3733
Stefan Behrens803b2f52013-08-15 17:11:21 +02003734 read_extent_buffer(eb, &root_item,
3735 btrfs_item_ptr_offset(eb, slot),
3736 (int)sizeof(root_item));
3737 if (btrfs_root_refs(&root_item) == 0)
3738 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003739
3740 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3741 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3742 if (trans)
3743 goto update_tree;
3744
3745 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003746 /*
3747 * 1 - subvol uuid item
3748 * 1 - received_subvol uuid item
3749 */
3750 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3751 if (IS_ERR(trans)) {
3752 ret = PTR_ERR(trans);
3753 break;
3754 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003755 continue;
3756 } else {
3757 goto skip;
3758 }
3759update_tree:
3760 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003761 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3762 root_item.uuid,
3763 BTRFS_UUID_KEY_SUBVOL,
3764 key.objectid);
3765 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003766 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003767 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003768 break;
3769 }
3770 }
3771
3772 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003773 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3774 root_item.received_uuid,
3775 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3776 key.objectid);
3777 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003778 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003779 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003780 break;
3781 }
3782 }
3783
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003784skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003785 if (trans) {
3786 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003787 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003788 if (ret)
3789 break;
3790 }
3791
Stefan Behrens803b2f52013-08-15 17:11:21 +02003792 btrfs_release_path(path);
3793 if (key.offset < (u64)-1) {
3794 key.offset++;
3795 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3796 key.offset = 0;
3797 key.type = BTRFS_ROOT_ITEM_KEY;
3798 } else if (key.objectid < (u64)-1) {
3799 key.offset = 0;
3800 key.type = BTRFS_ROOT_ITEM_KEY;
3801 key.objectid++;
3802 } else {
3803 break;
3804 }
3805 cond_resched();
3806 }
3807
3808out:
3809 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003810 if (trans && !IS_ERR(trans))
3811 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003812 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05003813 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003814 else
3815 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003816 up(&fs_info->uuid_tree_rescan_sem);
3817 return 0;
3818}
3819
Stefan Behrens70f80172013-08-15 17:11:23 +02003820/*
3821 * Callback for btrfs_uuid_tree_iterate().
3822 * returns:
3823 * 0 check succeeded, the entry is not outdated.
3824 * < 0 if an error occured.
3825 * > 0 if the check failed, which means the caller shall remove the entry.
3826 */
3827static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3828 u8 *uuid, u8 type, u64 subid)
3829{
3830 struct btrfs_key key;
3831 int ret = 0;
3832 struct btrfs_root *subvol_root;
3833
3834 if (type != BTRFS_UUID_KEY_SUBVOL &&
3835 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3836 goto out;
3837
3838 key.objectid = subid;
3839 key.type = BTRFS_ROOT_ITEM_KEY;
3840 key.offset = (u64)-1;
3841 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3842 if (IS_ERR(subvol_root)) {
3843 ret = PTR_ERR(subvol_root);
3844 if (ret == -ENOENT)
3845 ret = 1;
3846 goto out;
3847 }
3848
3849 switch (type) {
3850 case BTRFS_UUID_KEY_SUBVOL:
3851 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3852 ret = 1;
3853 break;
3854 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3855 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3856 BTRFS_UUID_SIZE))
3857 ret = 1;
3858 break;
3859 }
3860
3861out:
3862 return ret;
3863}
3864
3865static int btrfs_uuid_rescan_kthread(void *data)
3866{
3867 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3868 int ret;
3869
3870 /*
3871 * 1st step is to iterate through the existing UUID tree and
3872 * to delete all entries that contain outdated data.
3873 * 2nd step is to add all missing entries to the UUID tree.
3874 */
3875 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3876 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003877 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003878 up(&fs_info->uuid_tree_rescan_sem);
3879 return ret;
3880 }
3881 return btrfs_uuid_scan_kthread(data);
3882}
3883
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003884int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3885{
3886 struct btrfs_trans_handle *trans;
3887 struct btrfs_root *tree_root = fs_info->tree_root;
3888 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003889 struct task_struct *task;
3890 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003891
3892 /*
3893 * 1 - root node
3894 * 1 - root item
3895 */
3896 trans = btrfs_start_transaction(tree_root, 2);
3897 if (IS_ERR(trans))
3898 return PTR_ERR(trans);
3899
3900 uuid_root = btrfs_create_tree(trans, fs_info,
3901 BTRFS_UUID_TREE_OBJECTID);
3902 if (IS_ERR(uuid_root)) {
3903 btrfs_abort_transaction(trans, tree_root,
3904 PTR_ERR(uuid_root));
3905 return PTR_ERR(uuid_root);
3906 }
3907
3908 fs_info->uuid_root = uuid_root;
3909
Stefan Behrens803b2f52013-08-15 17:11:21 +02003910 ret = btrfs_commit_transaction(trans, tree_root);
3911 if (ret)
3912 return ret;
3913
3914 down(&fs_info->uuid_tree_rescan_sem);
3915 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3916 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02003917 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003918 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02003919 up(&fs_info->uuid_tree_rescan_sem);
3920 return PTR_ERR(task);
3921 }
3922
3923 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003924}
Stefan Behrens803b2f52013-08-15 17:11:21 +02003925
Stefan Behrens70f80172013-08-15 17:11:23 +02003926int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3927{
3928 struct task_struct *task;
3929
3930 down(&fs_info->uuid_tree_rescan_sem);
3931 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3932 if (IS_ERR(task)) {
3933 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003934 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02003935 up(&fs_info->uuid_tree_rescan_sem);
3936 return PTR_ERR(task);
3937 }
3938
3939 return 0;
3940}
3941
Chris Mason8f18cf12008-04-25 16:53:30 -04003942/*
3943 * shrinking a device means finding all of the device extents past
3944 * the new size, and then following the back refs to the chunks.
3945 * The chunk relocation code actually frees the device extent
3946 */
3947int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3948{
3949 struct btrfs_trans_handle *trans;
3950 struct btrfs_root *root = device->dev_root;
3951 struct btrfs_dev_extent *dev_extent = NULL;
3952 struct btrfs_path *path;
3953 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04003954 u64 chunk_objectid;
3955 u64 chunk_offset;
3956 int ret;
3957 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003958 int failed = 0;
3959 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003960 struct extent_buffer *l;
3961 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003962 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003963 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08003964 u64 old_size = btrfs_device_get_total_bytes(device);
3965 u64 diff = old_size - new_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04003966
Stefan Behrens63a212a2012-11-05 18:29:28 +01003967 if (device->is_tgtdev_for_dev_replace)
3968 return -EINVAL;
3969
Chris Mason8f18cf12008-04-25 16:53:30 -04003970 path = btrfs_alloc_path();
3971 if (!path)
3972 return -ENOMEM;
3973
Chris Mason8f18cf12008-04-25 16:53:30 -04003974 path->reada = 2;
3975
Chris Mason7d9eb122008-07-08 14:19:17 -04003976 lock_chunks(root);
3977
Miao Xie7cc8e582014-09-03 21:35:38 +08003978 btrfs_device_set_total_bytes(device, new_size);
Josef Bacik2bf64752011-09-26 17:12:22 -04003979 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003980 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003981 spin_lock(&root->fs_info->free_chunk_lock);
3982 root->fs_info->free_chunk_space -= diff;
3983 spin_unlock(&root->fs_info->free_chunk_lock);
3984 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003985 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003986
Josef Bacikba1bf482009-09-11 16:11:19 -04003987again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003988 key.objectid = device->devid;
3989 key.offset = (u64)-1;
3990 key.type = BTRFS_DEV_EXTENT_KEY;
3991
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003992 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003993 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3994 if (ret < 0)
3995 goto done;
3996
3997 ret = btrfs_previous_item(root, path, 0, key.type);
3998 if (ret < 0)
3999 goto done;
4000 if (ret) {
4001 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004002 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004003 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004004 }
4005
4006 l = path->nodes[0];
4007 slot = path->slots[0];
4008 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4009
Josef Bacikba1bf482009-09-11 16:11:19 -04004010 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004011 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004012 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004013 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004014
4015 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4016 length = btrfs_dev_extent_length(l, dev_extent);
4017
Josef Bacikba1bf482009-09-11 16:11:19 -04004018 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004019 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004020 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004021 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004022
Chris Mason8f18cf12008-04-25 16:53:30 -04004023 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
4024 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004025 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004026
Zhao Leia688a042015-02-09 20:31:44 +08004027 ret = btrfs_relocate_chunk(root, chunk_objectid, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04004028 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004029 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004030 if (ret == -ENOSPC)
4031 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004032 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004033
4034 if (failed && !retried) {
4035 failed = 0;
4036 retried = true;
4037 goto again;
4038 } else if (failed && retried) {
4039 ret = -ENOSPC;
4040 lock_chunks(root);
4041
Miao Xie7cc8e582014-09-03 21:35:38 +08004042 btrfs_device_set_total_bytes(device, old_size);
Josef Bacikba1bf482009-09-11 16:11:19 -04004043 if (device->writeable)
4044 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04004045 spin_lock(&root->fs_info->free_chunk_lock);
4046 root->fs_info->free_chunk_space += diff;
4047 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04004048 unlock_chunks(root);
4049 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004050 }
4051
Chris Balld6397ba2009-04-27 07:29:03 -04004052 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004053 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004054 if (IS_ERR(trans)) {
4055 ret = PTR_ERR(trans);
4056 goto done;
4057 }
4058
Chris Balld6397ba2009-04-27 07:29:03 -04004059 lock_chunks(root);
Miao Xie7cc8e582014-09-03 21:35:38 +08004060 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004061 if (list_empty(&device->resized_list))
4062 list_add_tail(&device->resized_list,
4063 &root->fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004064
Chris Balld6397ba2009-04-27 07:29:03 -04004065 WARN_ON(diff > old_total);
4066 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4067 unlock_chunks(root);
Miao Xie2196d6e2014-09-03 21:35:41 +08004068
4069 /* Now btrfs_update_device() will change the on-disk size. */
4070 ret = btrfs_update_device(trans, device);
Chris Balld6397ba2009-04-27 07:29:03 -04004071 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004072done:
4073 btrfs_free_path(path);
4074 return ret;
4075}
4076
Li Zefan125ccb02011-12-08 15:07:24 +08004077static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004078 struct btrfs_key *key,
4079 struct btrfs_chunk *chunk, int item_size)
4080{
David Sterba6c417612011-04-13 15:41:04 +02004081 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004082 struct btrfs_disk_key disk_key;
4083 u32 array_size;
4084 u8 *ptr;
4085
Miao Xiefe48a5c2014-09-03 21:35:39 +08004086 lock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004087 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004088 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004089 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4090 unlock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004091 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004092 }
Chris Mason0b86a832008-03-24 15:01:56 -04004093
4094 ptr = super_copy->sys_chunk_array + array_size;
4095 btrfs_cpu_key_to_disk(&disk_key, key);
4096 memcpy(ptr, &disk_key, sizeof(disk_key));
4097 ptr += sizeof(disk_key);
4098 memcpy(ptr, chunk, item_size);
4099 item_size += sizeof(disk_key);
4100 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004101 unlock_chunks(root);
4102
Chris Mason0b86a832008-03-24 15:01:56 -04004103 return 0;
4104}
4105
Miao Xieb2117a32011-01-05 10:07:28 +00004106/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004107 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004108 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004109static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004110{
Arne Jansen73c5de02011-04-12 12:07:57 +02004111 const struct btrfs_device_info *di_a = a;
4112 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004113
Arne Jansen73c5de02011-04-12 12:07:57 +02004114 if (di_a->max_avail > di_b->max_avail)
4115 return -1;
4116 if (di_a->max_avail < di_b->max_avail)
4117 return 1;
4118 if (di_a->total_avail > di_b->total_avail)
4119 return -1;
4120 if (di_a->total_avail < di_b->total_avail)
4121 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004122 return 0;
4123}
4124
David Sterbae8c9f182015-01-02 18:23:10 +01004125static const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00004126 [BTRFS_RAID_RAID10] = {
4127 .sub_stripes = 2,
4128 .dev_stripes = 1,
4129 .devs_max = 0, /* 0 == as many as possible */
4130 .devs_min = 4,
4131 .devs_increment = 2,
4132 .ncopies = 2,
4133 },
4134 [BTRFS_RAID_RAID1] = {
4135 .sub_stripes = 1,
4136 .dev_stripes = 1,
4137 .devs_max = 2,
4138 .devs_min = 2,
4139 .devs_increment = 2,
4140 .ncopies = 2,
4141 },
4142 [BTRFS_RAID_DUP] = {
4143 .sub_stripes = 1,
4144 .dev_stripes = 2,
4145 .devs_max = 1,
4146 .devs_min = 1,
4147 .devs_increment = 1,
4148 .ncopies = 2,
4149 },
4150 [BTRFS_RAID_RAID0] = {
4151 .sub_stripes = 1,
4152 .dev_stripes = 1,
4153 .devs_max = 0,
4154 .devs_min = 2,
4155 .devs_increment = 1,
4156 .ncopies = 1,
4157 },
4158 [BTRFS_RAID_SINGLE] = {
4159 .sub_stripes = 1,
4160 .dev_stripes = 1,
4161 .devs_max = 1,
4162 .devs_min = 1,
4163 .devs_increment = 1,
4164 .ncopies = 1,
4165 },
Chris Masone942f882013-02-20 14:06:05 -05004166 [BTRFS_RAID_RAID5] = {
4167 .sub_stripes = 1,
4168 .dev_stripes = 1,
4169 .devs_max = 0,
4170 .devs_min = 2,
4171 .devs_increment = 1,
4172 .ncopies = 2,
4173 },
4174 [BTRFS_RAID_RAID6] = {
4175 .sub_stripes = 1,
4176 .dev_stripes = 1,
4177 .devs_max = 0,
4178 .devs_min = 3,
4179 .devs_increment = 1,
4180 .ncopies = 3,
4181 },
Liu Bo31e50222012-11-21 14:18:10 +00004182};
4183
David Woodhouse53b381b2013-01-29 18:40:14 -05004184static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4185{
4186 /* TODO allow them to set a preferred stripe size */
4187 return 64 * 1024;
4188}
4189
4190static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4191{
Zhao Leiffe2d202015-01-20 15:11:44 +08004192 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004193 return;
4194
Miao Xieceda0862013-04-11 10:30:16 +00004195 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004196}
4197
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004198#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4199 - sizeof(struct btrfs_item) \
4200 - sizeof(struct btrfs_chunk)) \
4201 / sizeof(struct btrfs_stripe) + 1)
4202
4203#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4204 - 2 * sizeof(struct btrfs_disk_key) \
4205 - 2 * sizeof(struct btrfs_chunk)) \
4206 / sizeof(struct btrfs_stripe) + 1)
4207
Miao Xieb2117a32011-01-05 10:07:28 +00004208static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004209 struct btrfs_root *extent_root, u64 start,
4210 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004211{
4212 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004213 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4214 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004215 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004216 struct extent_map_tree *em_tree;
4217 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004218 struct btrfs_device_info *devices_info = NULL;
4219 u64 total_avail;
4220 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004221 int data_stripes; /* number of stripes that count for
4222 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004223 int sub_stripes; /* sub_stripes info for map */
4224 int dev_stripes; /* stripes per dev */
4225 int devs_max; /* max devs to use */
4226 int devs_min; /* min devs needed */
4227 int devs_increment; /* ndevs has to be a multiple of this */
4228 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004229 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004230 u64 max_stripe_size;
4231 u64 max_chunk_size;
4232 u64 stripe_size;
4233 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004234 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004235 int ndevs;
4236 int i;
4237 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004238 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004239
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004240 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004241
Miao Xieb2117a32011-01-05 10:07:28 +00004242 if (list_empty(&fs_devices->alloc_list))
4243 return -ENOSPC;
4244
Liu Bo31e50222012-11-21 14:18:10 +00004245 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004246
Liu Bo31e50222012-11-21 14:18:10 +00004247 sub_stripes = btrfs_raid_array[index].sub_stripes;
4248 dev_stripes = btrfs_raid_array[index].dev_stripes;
4249 devs_max = btrfs_raid_array[index].devs_max;
4250 devs_min = btrfs_raid_array[index].devs_min;
4251 devs_increment = btrfs_raid_array[index].devs_increment;
4252 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004253
4254 if (type & BTRFS_BLOCK_GROUP_DATA) {
4255 max_stripe_size = 1024 * 1024 * 1024;
4256 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004257 if (!devs_max)
4258 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004259 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004260 /* for larger filesystems, use larger metadata chunks */
4261 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4262 max_stripe_size = 1024 * 1024 * 1024;
4263 else
4264 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004265 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004266 if (!devs_max)
4267 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004268 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004269 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004270 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004271 if (!devs_max)
4272 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004273 } else {
David Sterba351fd352014-05-15 16:48:20 +02004274 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004275 type);
4276 BUG_ON(1);
4277 }
4278
4279 /* we don't want a chunk larger than 10% of writeable space */
4280 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4281 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004282
4283 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
4284 GFP_NOFS);
4285 if (!devices_info)
4286 return -ENOMEM;
4287
Arne Jansen73c5de02011-04-12 12:07:57 +02004288 cur = fs_devices->alloc_list.next;
4289
4290 /*
4291 * in the first pass through the devices list, we gather information
4292 * about the available holes on each device.
4293 */
4294 ndevs = 0;
4295 while (cur != &fs_devices->alloc_list) {
4296 struct btrfs_device *device;
4297 u64 max_avail;
4298 u64 dev_offset;
4299
4300 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4301
4302 cur = cur->next;
4303
4304 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004305 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004306 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004307 continue;
4308 }
4309
Stefan Behrens63a212a2012-11-05 18:29:28 +01004310 if (!device->in_fs_metadata ||
4311 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004312 continue;
4313
4314 if (device->total_bytes > device->bytes_used)
4315 total_avail = device->total_bytes - device->bytes_used;
4316 else
4317 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004318
4319 /* If there is no space on this device, skip it. */
4320 if (total_avail == 0)
4321 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004322
Josef Bacik6df9a952013-06-27 13:22:46 -04004323 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004324 max_stripe_size * dev_stripes,
4325 &dev_offset, &max_avail);
4326 if (ret && ret != -ENOSPC)
4327 goto error;
4328
4329 if (ret == 0)
4330 max_avail = max_stripe_size * dev_stripes;
4331
4332 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4333 continue;
4334
Eric Sandeen063d0062013-01-31 00:55:01 +00004335 if (ndevs == fs_devices->rw_devices) {
4336 WARN(1, "%s: found more than %llu devices\n",
4337 __func__, fs_devices->rw_devices);
4338 break;
4339 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004340 devices_info[ndevs].dev_offset = dev_offset;
4341 devices_info[ndevs].max_avail = max_avail;
4342 devices_info[ndevs].total_avail = total_avail;
4343 devices_info[ndevs].dev = device;
4344 ++ndevs;
4345 }
4346
4347 /*
4348 * now sort the devices by hole size / available space
4349 */
4350 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4351 btrfs_cmp_device_info, NULL);
4352
4353 /* round down to number of usable stripes */
4354 ndevs -= ndevs % devs_increment;
4355
4356 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4357 ret = -ENOSPC;
4358 goto error;
4359 }
4360
4361 if (devs_max && ndevs > devs_max)
4362 ndevs = devs_max;
4363 /*
4364 * the primary goal is to maximize the number of stripes, so use as many
4365 * devices as possible, even if the stripes are not maximum sized.
4366 */
4367 stripe_size = devices_info[ndevs-1].max_avail;
4368 num_stripes = ndevs * dev_stripes;
4369
David Woodhouse53b381b2013-01-29 18:40:14 -05004370 /*
4371 * this will have to be fixed for RAID1 and RAID10 over
4372 * more drives
4373 */
4374 data_stripes = num_stripes / ncopies;
4375
David Woodhouse53b381b2013-01-29 18:40:14 -05004376 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4377 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4378 btrfs_super_stripesize(info->super_copy));
4379 data_stripes = num_stripes - 1;
4380 }
4381 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4382 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4383 btrfs_super_stripesize(info->super_copy));
4384 data_stripes = num_stripes - 2;
4385 }
Chris Mason86db2572013-02-20 16:23:40 -05004386
4387 /*
4388 * Use the number of data stripes to figure out how big this chunk
4389 * is really going to be in terms of logical address space,
4390 * and compare that answer with the max chunk size
4391 */
4392 if (stripe_size * data_stripes > max_chunk_size) {
4393 u64 mask = (1ULL << 24) - 1;
4394 stripe_size = max_chunk_size;
4395 do_div(stripe_size, data_stripes);
4396
4397 /* bump the answer up to a 16MB boundary */
4398 stripe_size = (stripe_size + mask) & ~mask;
4399
4400 /* but don't go higher than the limits we found
4401 * while searching for free extents
4402 */
4403 if (stripe_size > devices_info[ndevs-1].max_avail)
4404 stripe_size = devices_info[ndevs-1].max_avail;
4405 }
4406
Arne Jansen73c5de02011-04-12 12:07:57 +02004407 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004408
4409 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05004410 do_div(stripe_size, raid_stripe_len);
4411 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004412
Miao Xieb2117a32011-01-05 10:07:28 +00004413 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4414 if (!map) {
4415 ret = -ENOMEM;
4416 goto error;
4417 }
4418 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004419
Arne Jansen73c5de02011-04-12 12:07:57 +02004420 for (i = 0; i < ndevs; ++i) {
4421 for (j = 0; j < dev_stripes; ++j) {
4422 int s = i * dev_stripes + j;
4423 map->stripes[s].dev = devices_info[i].dev;
4424 map->stripes[s].physical = devices_info[i].dev_offset +
4425 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004426 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004427 }
Chris Mason593060d2008-03-25 16:50:33 -04004428 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004429 map->stripe_len = raid_stripe_len;
4430 map->io_align = raid_stripe_len;
4431 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004432 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004433 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004434
David Woodhouse53b381b2013-01-29 18:40:14 -05004435 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004436
Arne Jansen73c5de02011-04-12 12:07:57 +02004437 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004438
David Sterba172ddd62011-04-21 00:48:27 +02004439 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004440 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004441 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004442 ret = -ENOMEM;
4443 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004444 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004445 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004446 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004447 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004448 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004449 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004450 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004451 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004452
Chris Mason0b86a832008-03-24 15:01:56 -04004453 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004454 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004455 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004456 if (!ret) {
4457 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4458 atomic_inc(&em->refs);
4459 }
Chris Mason890871b2009-09-02 16:24:52 -04004460 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004461 if (ret) {
4462 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004463 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004464 }
Yan Zheng2b820322008-11-17 21:11:30 -05004465
Josef Bacik04487482013-01-29 15:03:37 -05004466 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4467 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4468 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004469 if (ret)
4470 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004471
Miao Xie7cc8e582014-09-03 21:35:38 +08004472 for (i = 0; i < map->num_stripes; i++) {
4473 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4474 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4475 }
Miao Xie43530c42014-09-03 21:35:36 +08004476
Miao Xie1c116182014-09-03 21:35:37 +08004477 spin_lock(&extent_root->fs_info->free_chunk_lock);
4478 extent_root->fs_info->free_chunk_space -= (stripe_size *
4479 map->num_stripes);
4480 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4481
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004482 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004483 check_raid56_incompat_flag(extent_root->fs_info, type);
4484
Miao Xieb2117a32011-01-05 10:07:28 +00004485 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004486 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004487
Josef Bacik6df9a952013-06-27 13:22:46 -04004488error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004489 write_lock(&em_tree->lock);
4490 remove_extent_mapping(em_tree, em);
4491 write_unlock(&em_tree->lock);
4492
4493 /* One for our allocation */
4494 free_extent_map(em);
4495 /* One for the tree reference */
4496 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004497 /* One for the pending_chunks list reference */
4498 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004499error:
Miao Xieb2117a32011-01-05 10:07:28 +00004500 kfree(devices_info);
4501 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004502}
4503
Josef Bacik6df9a952013-06-27 13:22:46 -04004504int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004505 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004506 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004507{
Yan Zheng2b820322008-11-17 21:11:30 -05004508 struct btrfs_key key;
4509 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4510 struct btrfs_device *device;
4511 struct btrfs_chunk *chunk;
4512 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004513 struct extent_map_tree *em_tree;
4514 struct extent_map *em;
4515 struct map_lookup *map;
4516 size_t item_size;
4517 u64 dev_offset;
4518 u64 stripe_size;
4519 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004520 int ret;
4521
Josef Bacik6df9a952013-06-27 13:22:46 -04004522 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4523 read_lock(&em_tree->lock);
4524 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4525 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004526
Josef Bacik6df9a952013-06-27 13:22:46 -04004527 if (!em) {
4528 btrfs_crit(extent_root->fs_info, "unable to find logical "
4529 "%Lu len %Lu", chunk_offset, chunk_size);
4530 return -EINVAL;
4531 }
4532
4533 if (em->start != chunk_offset || em->len != chunk_size) {
4534 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004535 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004536 chunk_size, em->start, em->len);
4537 free_extent_map(em);
4538 return -EINVAL;
4539 }
4540
4541 map = (struct map_lookup *)em->bdev;
4542 item_size = btrfs_chunk_item_size(map->num_stripes);
4543 stripe_size = em->orig_block_len;
4544
4545 chunk = kzalloc(item_size, GFP_NOFS);
4546 if (!chunk) {
4547 ret = -ENOMEM;
4548 goto out;
4549 }
4550
4551 for (i = 0; i < map->num_stripes; i++) {
4552 device = map->stripes[i].dev;
4553 dev_offset = map->stripes[i].physical;
4554
Yan Zheng2b820322008-11-17 21:11:30 -05004555 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004556 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004557 goto out;
4558 ret = btrfs_alloc_dev_extent(trans, device,
4559 chunk_root->root_key.objectid,
4560 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4561 chunk_offset, dev_offset,
4562 stripe_size);
4563 if (ret)
4564 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004565 }
4566
Yan Zheng2b820322008-11-17 21:11:30 -05004567 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004568 for (i = 0; i < map->num_stripes; i++) {
4569 device = map->stripes[i].dev;
4570 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004571
4572 btrfs_set_stack_stripe_devid(stripe, device->devid);
4573 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4574 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4575 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004576 }
4577
4578 btrfs_set_stack_chunk_length(chunk, chunk_size);
4579 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4580 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4581 btrfs_set_stack_chunk_type(chunk, map->type);
4582 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4583 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4584 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4585 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4586 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4587
4588 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4589 key.type = BTRFS_CHUNK_ITEM_KEY;
4590 key.offset = chunk_offset;
4591
4592 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004593 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4594 /*
4595 * TODO: Cleanup of inserted chunk root in case of
4596 * failure.
4597 */
Li Zefan125ccb02011-12-08 15:07:24 +08004598 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004599 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004600 }
liubo1abe9b82011-03-24 11:18:59 +00004601
Josef Bacik6df9a952013-06-27 13:22:46 -04004602out:
Yan Zheng2b820322008-11-17 21:11:30 -05004603 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004604 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004605 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004606}
4607
4608/*
4609 * Chunk allocation falls into two parts. The first part does works
4610 * that make the new allocated chunk useable, but not do any operation
4611 * that modifies the chunk tree. The second part does the works that
4612 * require modifying the chunk tree. This division is important for the
4613 * bootstrap process of adding storage to a seed btrfs.
4614 */
4615int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4616 struct btrfs_root *extent_root, u64 type)
4617{
4618 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004619
Josef Bacik6df9a952013-06-27 13:22:46 -04004620 chunk_offset = find_next_chunk(extent_root->fs_info);
4621 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004622}
4623
Chris Masond3977122009-01-05 21:25:51 -05004624static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004625 struct btrfs_root *root,
4626 struct btrfs_device *device)
4627{
4628 u64 chunk_offset;
4629 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004630 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004631 struct btrfs_fs_info *fs_info = root->fs_info;
4632 struct btrfs_root *extent_root = fs_info->extent_root;
4633 int ret;
4634
Josef Bacik6df9a952013-06-27 13:22:46 -04004635 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004636 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004637 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4638 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004639 if (ret)
4640 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004641
Josef Bacik6df9a952013-06-27 13:22:46 -04004642 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004643 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004644 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4645 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004646 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004647}
4648
Miao Xied20983b2014-07-03 18:22:13 +08004649static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4650{
4651 int max_errors;
4652
4653 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4654 BTRFS_BLOCK_GROUP_RAID10 |
4655 BTRFS_BLOCK_GROUP_RAID5 |
4656 BTRFS_BLOCK_GROUP_DUP)) {
4657 max_errors = 1;
4658 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4659 max_errors = 2;
4660 } else {
4661 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004662 }
4663
Miao Xied20983b2014-07-03 18:22:13 +08004664 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05004665}
4666
4667int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4668{
4669 struct extent_map *em;
4670 struct map_lookup *map;
4671 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4672 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004673 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004674 int i;
4675
Chris Mason890871b2009-09-02 16:24:52 -04004676 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004677 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004678 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004679 if (!em)
4680 return 1;
4681
4682 map = (struct map_lookup *)em->bdev;
4683 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004684 if (map->stripes[i].dev->missing) {
4685 miss_ndevs++;
4686 continue;
4687 }
4688
Yan Zheng2b820322008-11-17 21:11:30 -05004689 if (!map->stripes[i].dev->writeable) {
4690 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004691 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004692 }
4693 }
Miao Xied20983b2014-07-03 18:22:13 +08004694
4695 /*
4696 * If the number of missing devices is larger than max errors,
4697 * we can not write the data into that chunk successfully, so
4698 * set it readonly.
4699 */
4700 if (miss_ndevs > btrfs_chunk_max_errors(map))
4701 readonly = 1;
4702end:
Yan Zheng2b820322008-11-17 21:11:30 -05004703 free_extent_map(em);
4704 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004705}
4706
4707void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4708{
David Sterbaa8067e02011-04-21 00:34:43 +02004709 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004710}
4711
4712void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4713{
4714 struct extent_map *em;
4715
Chris Masond3977122009-01-05 21:25:51 -05004716 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004717 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004718 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4719 if (em)
4720 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004721 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004722 if (!em)
4723 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004724 /* once for us */
4725 free_extent_map(em);
4726 /* once for the tree */
4727 free_extent_map(em);
4728 }
4729}
4730
Stefan Behrens5d964052012-11-05 14:59:07 +01004731int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004732{
Stefan Behrens5d964052012-11-05 14:59:07 +01004733 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004734 struct extent_map *em;
4735 struct map_lookup *map;
4736 struct extent_map_tree *em_tree = &map_tree->map_tree;
4737 int ret;
4738
Chris Mason890871b2009-09-02 16:24:52 -04004739 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004740 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004741 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004742
Josef Bacikfb7669b2013-04-23 10:53:18 -04004743 /*
4744 * We could return errors for these cases, but that could get ugly and
4745 * we'd probably do the same thing which is just not do anything else
4746 * and exit, so return 1 so the callers don't try to use other copies.
4747 */
4748 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004749 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004750 logical+len);
4751 return 1;
4752 }
4753
4754 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004755 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004756 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004757 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004758 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004759 return 1;
4760 }
4761
Chris Masonf1885912008-04-09 16:28:12 -04004762 map = (struct map_lookup *)em->bdev;
4763 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4764 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004765 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4766 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004767 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4768 ret = 2;
4769 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4770 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004771 else
4772 ret = 1;
4773 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004774
4775 btrfs_dev_replace_lock(&fs_info->dev_replace);
4776 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4777 ret++;
4778 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4779
Chris Masonf1885912008-04-09 16:28:12 -04004780 return ret;
4781}
4782
David Woodhouse53b381b2013-01-29 18:40:14 -05004783unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4784 struct btrfs_mapping_tree *map_tree,
4785 u64 logical)
4786{
4787 struct extent_map *em;
4788 struct map_lookup *map;
4789 struct extent_map_tree *em_tree = &map_tree->map_tree;
4790 unsigned long len = root->sectorsize;
4791
4792 read_lock(&em_tree->lock);
4793 em = lookup_extent_mapping(em_tree, logical, len);
4794 read_unlock(&em_tree->lock);
4795 BUG_ON(!em);
4796
4797 BUG_ON(em->start > logical || em->start + em->len < logical);
4798 map = (struct map_lookup *)em->bdev;
Zhao Leiffe2d202015-01-20 15:11:44 +08004799 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05004800 len = map->stripe_len * nr_data_stripes(map);
David Woodhouse53b381b2013-01-29 18:40:14 -05004801 free_extent_map(em);
4802 return len;
4803}
4804
4805int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4806 u64 logical, u64 len, int mirror_num)
4807{
4808 struct extent_map *em;
4809 struct map_lookup *map;
4810 struct extent_map_tree *em_tree = &map_tree->map_tree;
4811 int ret = 0;
4812
4813 read_lock(&em_tree->lock);
4814 em = lookup_extent_mapping(em_tree, logical, len);
4815 read_unlock(&em_tree->lock);
4816 BUG_ON(!em);
4817
4818 BUG_ON(em->start > logical || em->start + em->len < logical);
4819 map = (struct map_lookup *)em->bdev;
Zhao Leiffe2d202015-01-20 15:11:44 +08004820 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05004821 ret = 1;
4822 free_extent_map(em);
4823 return ret;
4824}
4825
Stefan Behrens30d98612012-11-06 14:52:18 +01004826static int find_live_mirror(struct btrfs_fs_info *fs_info,
4827 struct map_lookup *map, int first, int num,
4828 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004829{
4830 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004831 int tolerance;
4832 struct btrfs_device *srcdev;
4833
4834 if (dev_replace_is_ongoing &&
4835 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4836 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4837 srcdev = fs_info->dev_replace.srcdev;
4838 else
4839 srcdev = NULL;
4840
4841 /*
4842 * try to avoid the drive that is the source drive for a
4843 * dev-replace procedure, only choose it if no other non-missing
4844 * mirror is available
4845 */
4846 for (tolerance = 0; tolerance < 2; tolerance++) {
4847 if (map->stripes[optimal].dev->bdev &&
4848 (tolerance || map->stripes[optimal].dev != srcdev))
4849 return optimal;
4850 for (i = first; i < first + num; i++) {
4851 if (map->stripes[i].dev->bdev &&
4852 (tolerance || map->stripes[i].dev != srcdev))
4853 return i;
4854 }
Chris Masondfe25022008-05-13 13:46:40 -04004855 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004856
Chris Masondfe25022008-05-13 13:46:40 -04004857 /* we couldn't find one that doesn't fail. Just return something
4858 * and the io error handling code will clean up eventually
4859 */
4860 return optimal;
4861}
4862
David Woodhouse53b381b2013-01-29 18:40:14 -05004863static inline int parity_smaller(u64 a, u64 b)
4864{
4865 return a > b;
4866}
4867
4868/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08004869static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05004870{
4871 struct btrfs_bio_stripe s;
4872 int i;
4873 u64 l;
4874 int again = 1;
4875
4876 while (again) {
4877 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08004878 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08004879 if (parity_smaller(bbio->raid_map[i],
4880 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05004881 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08004882 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05004883 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08004884 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05004885 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08004886 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08004887
David Woodhouse53b381b2013-01-29 18:40:14 -05004888 again = 1;
4889 }
4890 }
4891 }
4892}
4893
Zhao Lei6e9606d2015-01-20 15:11:34 +08004894static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
4895{
4896 struct btrfs_bio *bbio = kzalloc(
4897 sizeof(struct btrfs_bio) +
4898 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
4899 sizeof(int) * (real_stripes) +
4900 sizeof(u64) * (real_stripes),
4901 GFP_NOFS);
4902 if (!bbio)
4903 return NULL;
4904
4905 atomic_set(&bbio->error, 0);
4906 atomic_set(&bbio->refs, 1);
4907
4908 return bbio;
4909}
4910
4911void btrfs_get_bbio(struct btrfs_bio *bbio)
4912{
4913 WARN_ON(!atomic_read(&bbio->refs));
4914 atomic_inc(&bbio->refs);
4915}
4916
4917void btrfs_put_bbio(struct btrfs_bio *bbio)
4918{
4919 if (!bbio)
4920 return;
4921 if (atomic_dec_and_test(&bbio->refs))
4922 kfree(bbio);
4923}
4924
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004925static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004926 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004927 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08004928 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04004929{
4930 struct extent_map *em;
4931 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004932 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004933 struct extent_map_tree *em_tree = &map_tree->map_tree;
4934 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004935 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004936 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004937 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004938 u64 stripe_nr_orig;
4939 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004940 u64 stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004941 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004942 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004943 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004944 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004945 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08004946 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004947 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004948 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4949 int dev_replace_is_ongoing = 0;
4950 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004951 int patch_the_first_stripe_for_dev_replace = 0;
4952 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004953 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004954
Chris Mason890871b2009-09-02 16:24:52 -04004955 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004956 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004957 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004958
Chris Mason3b951512008-04-17 11:29:12 -04004959 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004960 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004961 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004962 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004963 }
Chris Mason0b86a832008-03-24 15:01:56 -04004964
Josef Bacik9bb91872013-04-19 23:45:33 -04004965 if (em->start > logical || em->start + em->len < logical) {
4966 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02004967 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04004968 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004969 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04004970 return -EINVAL;
4971 }
4972
Chris Mason0b86a832008-03-24 15:01:56 -04004973 map = (struct map_lookup *)em->bdev;
4974 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004975
David Woodhouse53b381b2013-01-29 18:40:14 -05004976 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004977 stripe_nr = offset;
4978 /*
4979 * stripe_nr counts the total number of stripes we have to stride
4980 * to get to this block
4981 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004982 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004983
David Woodhouse53b381b2013-01-29 18:40:14 -05004984 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004985 BUG_ON(offset < stripe_offset);
4986
4987 /* stripe_offset is the offset of this block in its stripe*/
4988 stripe_offset = offset - stripe_offset;
4989
David Woodhouse53b381b2013-01-29 18:40:14 -05004990 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08004991 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05004992 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4993 raid56_full_stripe_start = offset;
4994
4995 /* allow a write of a full stripe, but make sure we don't
4996 * allow straddling of stripes
4997 */
4998 do_div(raid56_full_stripe_start, full_stripe_len);
4999 raid56_full_stripe_start *= full_stripe_len;
5000 }
5001
5002 if (rw & REQ_DISCARD) {
5003 /* we don't discard raid56 yet */
Zhao Leiffe2d202015-01-20 15:11:44 +08005004 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005005 ret = -EOPNOTSUPP;
5006 goto out;
5007 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005008 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005009 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5010 u64 max_len;
5011 /* For writes to RAID[56], allow a full stripeset across all disks.
5012 For other RAID types and for RAID[56] reads, just allow a single
5013 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005014 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
David Woodhouse53b381b2013-01-29 18:40:14 -05005015 (rw & REQ_WRITE)) {
5016 max_len = stripe_len * nr_data_stripes(map) -
5017 (offset - raid56_full_stripe_start);
5018 } else {
5019 /* we limit the length of each bio to what fits in a stripe */
5020 max_len = stripe_len - stripe_offset;
5021 }
5022 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005023 } else {
5024 *length = em->len - offset;
5025 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005026
David Woodhouse53b381b2013-01-29 18:40:14 -05005027 /* This is for when we're called from btrfs_merge_bio_hook() and all
5028 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005029 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005030 goto out;
5031
Stefan Behrens472262f2012-11-06 14:43:46 +01005032 btrfs_dev_replace_lock(dev_replace);
5033 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5034 if (!dev_replace_is_ongoing)
5035 btrfs_dev_replace_unlock(dev_replace);
5036
Stefan Behrensad6d6202012-11-06 15:06:47 +01005037 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
5038 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
5039 dev_replace->tgtdev != NULL) {
5040 /*
5041 * in dev-replace case, for repair case (that's the only
5042 * case where the mirror is selected explicitly when
5043 * calling btrfs_map_block), blocks left of the left cursor
5044 * can also be read from the target drive.
5045 * For REQ_GET_READ_MIRRORS, the target drive is added as
5046 * the last one to the array of stripes. For READ, it also
5047 * needs to be supported using the same mirror number.
5048 * If the requested block is not left of the left cursor,
5049 * EIO is returned. This can happen because btrfs_num_copies()
5050 * returns one more in the dev-replace case.
5051 */
5052 u64 tmp_length = *length;
5053 struct btrfs_bio *tmp_bbio = NULL;
5054 int tmp_num_stripes;
5055 u64 srcdev_devid = dev_replace->srcdev->devid;
5056 int index_srcdev = 0;
5057 int found = 0;
5058 u64 physical_of_found = 0;
5059
5060 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005061 logical, &tmp_length, &tmp_bbio, 0, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005062 if (ret) {
5063 WARN_ON(tmp_bbio != NULL);
5064 goto out;
5065 }
5066
5067 tmp_num_stripes = tmp_bbio->num_stripes;
5068 if (mirror_num > tmp_num_stripes) {
5069 /*
5070 * REQ_GET_READ_MIRRORS does not contain this
5071 * mirror, that means that the requested area
5072 * is not left of the left cursor
5073 */
5074 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005075 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005076 goto out;
5077 }
5078
5079 /*
5080 * process the rest of the function using the mirror_num
5081 * of the source drive. Therefore look it up first.
5082 * At the end, patch the device pointer to the one of the
5083 * target drive.
5084 */
5085 for (i = 0; i < tmp_num_stripes; i++) {
5086 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
5087 /*
5088 * In case of DUP, in order to keep it
5089 * simple, only add the mirror with the
5090 * lowest physical address
5091 */
5092 if (found &&
5093 physical_of_found <=
5094 tmp_bbio->stripes[i].physical)
5095 continue;
5096 index_srcdev = i;
5097 found = 1;
5098 physical_of_found =
5099 tmp_bbio->stripes[i].physical;
5100 }
5101 }
5102
5103 if (found) {
5104 mirror_num = index_srcdev + 1;
5105 patch_the_first_stripe_for_dev_replace = 1;
5106 physical_to_patch_in_first_stripe = physical_of_found;
5107 } else {
5108 WARN_ON(1);
5109 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005110 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005111 goto out;
5112 }
5113
Zhao Lei6e9606d2015-01-20 15:11:34 +08005114 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005115 } else if (mirror_num > map->num_stripes) {
5116 mirror_num = 0;
5117 }
5118
Chris Masonf2d8d742008-04-21 10:03:05 -04005119 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005120 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005121 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005122 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005123 do_div(stripe_nr_end, map->stripe_len);
5124 stripe_end_offset = stripe_nr_end * map->stripe_len -
5125 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005126
Li Dongyangfce3bb92011-03-24 10:24:26 +00005127 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5128 if (rw & REQ_DISCARD)
5129 num_stripes = min_t(u64, map->num_stripes,
5130 stripe_nr_end - stripe_nr_orig);
5131 stripe_index = do_div(stripe_nr, map->num_stripes);
Miao Xie28e1cc72014-09-12 18:44:02 +08005132 if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)))
5133 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005134 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005135 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005136 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005137 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005138 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005139 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005140 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005141 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005142 current->pid % map->num_stripes,
5143 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005144 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005145 }
Chris Mason2fff7342008-04-29 14:12:09 -04005146
Chris Mason611f0e02008-04-03 16:29:03 -04005147 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005148 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005149 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005150 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005151 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005152 } else {
5153 mirror_num = 1;
5154 }
Chris Mason2fff7342008-04-29 14:12:09 -04005155
Chris Mason321aecc2008-04-16 10:49:51 -04005156 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5157 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005158
5159 stripe_index = do_div(stripe_nr, factor);
5160 stripe_index *= map->sub_stripes;
5161
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005162 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005163 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005164 else if (rw & REQ_DISCARD)
5165 num_stripes = min_t(u64, map->sub_stripes *
5166 (stripe_nr_end - stripe_nr_orig),
5167 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005168 else if (mirror_num)
5169 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005170 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005171 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005172 stripe_index = find_live_mirror(fs_info, map,
5173 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005174 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005175 current->pid % map->sub_stripes,
5176 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005177 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005178 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005179
Zhao Leiffe2d202015-01-20 15:11:44 +08005180 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005181 if (need_raid_map &&
Miao Xieaf8e2d12014-10-23 14:42:50 +08005182 ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5183 mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005184 /* push stripe_nr back to the start of the full stripe */
5185 stripe_nr = raid56_full_stripe_start;
Zhao Lei6de65652014-11-13 11:45:40 +08005186 do_div(stripe_nr, stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005187
5188 /* RAID[56] write or recovery. Return all stripes */
5189 num_stripes = map->num_stripes;
5190 max_errors = nr_parity_stripes(map);
5191
David Woodhouse53b381b2013-01-29 18:40:14 -05005192 *length = map->stripe_len;
5193 stripe_index = 0;
5194 stripe_offset = 0;
5195 } else {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005196 u64 tmp;
5197
David Woodhouse53b381b2013-01-29 18:40:14 -05005198 /*
5199 * Mirror #0 or #1 means the original data block.
5200 * Mirror #2 is RAID5 parity block.
5201 * Mirror #3 is RAID6 Q block.
5202 */
5203 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5204 if (mirror_num > 1)
5205 stripe_index = nr_data_stripes(map) +
5206 mirror_num - 2;
5207
5208 /* We distribute the parity blocks across stripes */
5209 tmp = stripe_nr + stripe_index;
5210 stripe_index = do_div(tmp, map->num_stripes);
Miao Xie28e1cc72014-09-12 18:44:02 +08005211 if (!(rw & (REQ_WRITE | REQ_DISCARD |
5212 REQ_GET_READ_MIRRORS)) && mirror_num <= 1)
5213 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005214 }
Chris Mason8790d502008-04-03 16:29:03 -04005215 } else {
5216 /*
5217 * after this do_div call, stripe_nr is the number of stripes
5218 * on this device we have to walk to find the data, and
5219 * stripe_index is the number of our device in the stripe array
5220 */
5221 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005222 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005223 }
Chris Mason593060d2008-03-25 16:50:33 -04005224 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005225
Stefan Behrens472262f2012-11-06 14:43:46 +01005226 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005227 if (dev_replace_is_ongoing) {
5228 if (rw & (REQ_WRITE | REQ_DISCARD))
5229 num_alloc_stripes <<= 1;
5230 if (rw & REQ_GET_READ_MIRRORS)
5231 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005232 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005233 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005234
Zhao Lei6e9606d2015-01-20 15:11:34 +08005235 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005236 if (!bbio) {
5237 ret = -ENOMEM;
5238 goto out;
5239 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005240 if (dev_replace_is_ongoing)
5241 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005242
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005243 /* build raid_map */
Zhao Leiffe2d202015-01-20 15:11:44 +08005244 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK &&
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005245 need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5246 mirror_num > 1)) {
5247 u64 tmp;
5248 int i, rot;
5249
5250 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5251 sizeof(struct btrfs_bio_stripe) *
5252 num_alloc_stripes +
5253 sizeof(int) * tgtdev_indexes);
5254
5255 /* Work out the disk rotation on this stripe-set */
5256 tmp = stripe_nr;
5257 rot = do_div(tmp, num_stripes);
5258
5259 /* Fill in the logical address of each stripe */
5260 tmp = stripe_nr * nr_data_stripes(map);
5261 for (i = 0; i < nr_data_stripes(map); i++)
5262 bbio->raid_map[(i+rot) % num_stripes] =
5263 em->start + (tmp + i) * map->stripe_len;
5264
5265 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5266 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5267 bbio->raid_map[(i+rot+1) % num_stripes] =
5268 RAID6_Q_STRIPE;
5269 }
5270
Li Dongyangfce3bb92011-03-24 10:24:26 +00005271 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08005272 int factor = 0;
5273 int sub_stripes = 0;
5274 u64 stripes_per_dev = 0;
5275 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005276 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005277
5278 if (map->type &
5279 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5280 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5281 sub_stripes = 1;
5282 else
5283 sub_stripes = map->sub_stripes;
5284
5285 factor = map->num_stripes / sub_stripes;
5286 stripes_per_dev = div_u64_rem(stripe_nr_end -
5287 stripe_nr_orig,
5288 factor,
5289 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005290 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5291 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005292 }
5293
Li Dongyangfce3bb92011-03-24 10:24:26 +00005294 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005295 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005296 map->stripes[stripe_index].physical +
5297 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005298 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005299
Li Zefanec9ef7a2011-12-01 14:06:42 +08005300 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5301 BTRFS_BLOCK_GROUP_RAID10)) {
5302 bbio->stripes[i].length = stripes_per_dev *
5303 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005304
Li Zefanec9ef7a2011-12-01 14:06:42 +08005305 if (i / sub_stripes < remaining_stripes)
5306 bbio->stripes[i].length +=
5307 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005308
5309 /*
5310 * Special for the first stripe and
5311 * the last stripe:
5312 *
5313 * |-------|...|-------|
5314 * |----------|
5315 * off end_off
5316 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005317 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005318 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005319 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005320
5321 if (stripe_index >= last_stripe &&
5322 stripe_index <= (last_stripe +
5323 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005324 bbio->stripes[i].length -=
5325 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005326
Li Zefanec9ef7a2011-12-01 14:06:42 +08005327 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005328 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005329 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005330 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005331
5332 stripe_index++;
5333 if (stripe_index == map->num_stripes) {
5334 /* This could only happen for RAID0/10 */
5335 stripe_index = 0;
5336 stripe_nr++;
5337 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005338 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005339 } else {
5340 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005341 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005342 map->stripes[stripe_index].physical +
5343 stripe_offset +
5344 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005345 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005346 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005347 stripe_index++;
5348 }
Chris Mason593060d2008-03-25 16:50:33 -04005349 }
Li Zefande11cc12011-12-01 12:55:47 +08005350
Miao Xied20983b2014-07-03 18:22:13 +08005351 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5352 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005353
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005354 if (bbio->raid_map)
5355 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005356
Miao Xie2c8cdd62014-11-14 16:06:25 +08005357 tgtdev_indexes = 0;
Stefan Behrens472262f2012-11-06 14:43:46 +01005358 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5359 dev_replace->tgtdev != NULL) {
5360 int index_where_to_add;
5361 u64 srcdev_devid = dev_replace->srcdev->devid;
5362
5363 /*
5364 * duplicate the write operations while the dev replace
5365 * procedure is running. Since the copying of the old disk
5366 * to the new disk takes place at run time while the
5367 * filesystem is mounted writable, the regular write
5368 * operations to the old disk have to be duplicated to go
5369 * to the new disk as well.
5370 * Note that device->missing is handled by the caller, and
5371 * that the write to the old disk is already set up in the
5372 * stripes array.
5373 */
5374 index_where_to_add = num_stripes;
5375 for (i = 0; i < num_stripes; i++) {
5376 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5377 /* write to new disk, too */
5378 struct btrfs_bio_stripe *new =
5379 bbio->stripes + index_where_to_add;
5380 struct btrfs_bio_stripe *old =
5381 bbio->stripes + i;
5382
5383 new->physical = old->physical;
5384 new->length = old->length;
5385 new->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005386 bbio->tgtdev_map[i] = index_where_to_add;
Stefan Behrens472262f2012-11-06 14:43:46 +01005387 index_where_to_add++;
5388 max_errors++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005389 tgtdev_indexes++;
Stefan Behrens472262f2012-11-06 14:43:46 +01005390 }
5391 }
5392 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005393 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5394 dev_replace->tgtdev != NULL) {
5395 u64 srcdev_devid = dev_replace->srcdev->devid;
5396 int index_srcdev = 0;
5397 int found = 0;
5398 u64 physical_of_found = 0;
5399
5400 /*
5401 * During the dev-replace procedure, the target drive can
5402 * also be used to read data in case it is needed to repair
5403 * a corrupt block elsewhere. This is possible if the
5404 * requested area is left of the left cursor. In this area,
5405 * the target drive is a full copy of the source drive.
5406 */
5407 for (i = 0; i < num_stripes; i++) {
5408 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5409 /*
5410 * In case of DUP, in order to keep it
5411 * simple, only add the mirror with the
5412 * lowest physical address
5413 */
5414 if (found &&
5415 physical_of_found <=
5416 bbio->stripes[i].physical)
5417 continue;
5418 index_srcdev = i;
5419 found = 1;
5420 physical_of_found = bbio->stripes[i].physical;
5421 }
5422 }
5423 if (found) {
5424 u64 length = map->stripe_len;
5425
5426 if (physical_of_found + length <=
5427 dev_replace->cursor_left) {
5428 struct btrfs_bio_stripe *tgtdev_stripe =
5429 bbio->stripes + num_stripes;
5430
5431 tgtdev_stripe->physical = physical_of_found;
5432 tgtdev_stripe->length =
5433 bbio->stripes[index_srcdev].length;
5434 tgtdev_stripe->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005435 bbio->tgtdev_map[index_srcdev] = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005436
Miao Xie2c8cdd62014-11-14 16:06:25 +08005437 tgtdev_indexes++;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005438 num_stripes++;
5439 }
5440 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005441 }
5442
Li Zefande11cc12011-12-01 12:55:47 +08005443 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005444 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005445 bbio->num_stripes = num_stripes;
5446 bbio->max_errors = max_errors;
5447 bbio->mirror_num = mirror_num;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005448 bbio->num_tgtdevs = tgtdev_indexes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005449
5450 /*
5451 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5452 * mirror_num == num_stripes + 1 && dev_replace target drive is
5453 * available as a mirror
5454 */
5455 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5456 WARN_ON(num_stripes > 1);
5457 bbio->stripes[0].dev = dev_replace->tgtdev;
5458 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5459 bbio->mirror_num = map->num_stripes + 1;
5460 }
Chris Masoncea9e442008-04-09 16:28:12 -04005461out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005462 if (dev_replace_is_ongoing)
5463 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005464 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005465 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005466}
5467
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005468int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005469 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005470 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005471{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005472 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005473 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005474}
5475
Miao Xieaf8e2d12014-10-23 14:42:50 +08005476/* For Scrub/replace */
5477int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw,
5478 u64 logical, u64 *length,
5479 struct btrfs_bio **bbio_ret, int mirror_num,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005480 int need_raid_map)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005481{
5482 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005483 mirror_num, need_raid_map);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005484}
5485
Yan Zhenga512bbf2008-12-08 16:46:26 -05005486int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5487 u64 chunk_start, u64 physical, u64 devid,
5488 u64 **logical, int *naddrs, int *stripe_len)
5489{
5490 struct extent_map_tree *em_tree = &map_tree->map_tree;
5491 struct extent_map *em;
5492 struct map_lookup *map;
5493 u64 *buf;
5494 u64 bytenr;
5495 u64 length;
5496 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005497 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005498 int i, j, nr = 0;
5499
Chris Mason890871b2009-09-02 16:24:52 -04005500 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005501 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005502 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005503
Josef Bacik835d9742013-03-19 12:13:25 -04005504 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005505 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005506 chunk_start);
5507 return -EIO;
5508 }
5509
5510 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005511 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005512 em->start, chunk_start);
5513 free_extent_map(em);
5514 return -EIO;
5515 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005516 map = (struct map_lookup *)em->bdev;
5517
5518 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005519 rmap_len = map->stripe_len;
5520
Yan Zhenga512bbf2008-12-08 16:46:26 -05005521 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5522 do_div(length, map->num_stripes / map->sub_stripes);
5523 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5524 do_div(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08005525 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005526 do_div(length, nr_data_stripes(map));
5527 rmap_len = map->stripe_len * nr_data_stripes(map);
5528 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005529
5530 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005531 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005532
5533 for (i = 0; i < map->num_stripes; i++) {
5534 if (devid && map->stripes[i].dev->devid != devid)
5535 continue;
5536 if (map->stripes[i].physical > physical ||
5537 map->stripes[i].physical + length <= physical)
5538 continue;
5539
5540 stripe_nr = physical - map->stripes[i].physical;
5541 do_div(stripe_nr, map->stripe_len);
5542
5543 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5544 stripe_nr = stripe_nr * map->num_stripes + i;
5545 do_div(stripe_nr, map->sub_stripes);
5546 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5547 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005548 } /* else if RAID[56], multiply by nr_data_stripes().
5549 * Alternatively, just use rmap_len below instead of
5550 * map->stripe_len */
5551
5552 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005553 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005554 for (j = 0; j < nr; j++) {
5555 if (buf[j] == bytenr)
5556 break;
5557 }
Chris Mason934d3752008-12-08 16:43:10 -05005558 if (j == nr) {
5559 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005560 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005561 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005562 }
5563
Yan Zhenga512bbf2008-12-08 16:46:26 -05005564 *logical = buf;
5565 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005566 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005567
5568 free_extent_map(em);
5569 return 0;
5570}
5571
Miao Xie8408c712014-06-19 10:42:55 +08005572static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5573{
5574 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5575 bio_endio_nodec(bio, err);
5576 else
5577 bio_endio(bio, err);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005578 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08005579}
5580
Jan Schmidta1d3c472011-08-04 17:15:33 +02005581static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005582{
Chris Mason9be33952013-05-17 18:30:14 -04005583 struct btrfs_bio *bbio = bio->bi_private;
Miao Xiec404e0d2014-01-30 16:46:55 +08005584 struct btrfs_device *dev = bbio->stripes[0].dev;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005585 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005586
Stefan Behrens442a4f62012-05-25 16:06:08 +02005587 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005588 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005589 if (err == -EIO || err == -EREMOTEIO) {
5590 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005591 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005592
5593 BUG_ON(stripe_index >= bbio->num_stripes);
5594 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005595 if (dev->bdev) {
5596 if (bio->bi_rw & WRITE)
5597 btrfs_dev_stat_inc(dev,
5598 BTRFS_DEV_STAT_WRITE_ERRS);
5599 else
5600 btrfs_dev_stat_inc(dev,
5601 BTRFS_DEV_STAT_READ_ERRS);
5602 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5603 btrfs_dev_stat_inc(dev,
5604 BTRFS_DEV_STAT_FLUSH_ERRS);
5605 btrfs_dev_stat_print_on_error(dev);
5606 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005607 }
5608 }
Chris Mason8790d502008-04-03 16:29:03 -04005609
Jan Schmidta1d3c472011-08-04 17:15:33 +02005610 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005611 is_orig_bio = 1;
5612
Miao Xiec404e0d2014-01-30 16:46:55 +08005613 btrfs_bio_counter_dec(bbio->fs_info);
5614
Jan Schmidta1d3c472011-08-04 17:15:33 +02005615 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005616 if (!is_orig_bio) {
5617 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005618 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005619 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005620
Jan Schmidta1d3c472011-08-04 17:15:33 +02005621 bio->bi_private = bbio->private;
5622 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005623 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005624 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005625 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005626 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005627 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005628 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005629 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005630 /*
5631 * this bio is actually up to date, we didn't
5632 * go over the max number of errors
5633 */
5634 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005635 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005636 }
Miao Xiec55f1392014-06-19 10:42:54 +08005637
Miao Xie8408c712014-06-19 10:42:55 +08005638 btrfs_end_bbio(bbio, bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005639 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005640 bio_put(bio);
5641 }
Chris Mason8790d502008-04-03 16:29:03 -04005642}
5643
Chris Mason8b712842008-06-11 16:50:36 -04005644/*
5645 * see run_scheduled_bios for a description of why bios are collected for
5646 * async submit.
5647 *
5648 * This will add one bio to the pending list for a device and make sure
5649 * the work struct is scheduled.
5650 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005651static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5652 struct btrfs_device *device,
5653 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005654{
5655 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005656 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005657
David Woodhouse53b381b2013-01-29 18:40:14 -05005658 if (device->missing || !device->bdev) {
5659 bio_endio(bio, -EIO);
5660 return;
5661 }
5662
Chris Mason8b712842008-06-11 16:50:36 -04005663 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005664 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005665 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005666 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005667 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005668 return;
Chris Mason8b712842008-06-11 16:50:36 -04005669 }
5670
5671 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005672 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005673 * higher layers. Otherwise, the async bio makes it appear we have
5674 * made progress against dirty pages when we've really just put it
5675 * on a queue for later
5676 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005677 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005678 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005679 bio->bi_next = NULL;
5680 bio->bi_rw |= rw;
5681
5682 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005683 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005684 pending_bios = &device->pending_sync_bios;
5685 else
5686 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005687
Chris Masonffbd5172009-04-20 15:50:09 -04005688 if (pending_bios->tail)
5689 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005690
Chris Masonffbd5172009-04-20 15:50:09 -04005691 pending_bios->tail = bio;
5692 if (!pending_bios->head)
5693 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005694 if (device->running_pending)
5695 should_queue = 0;
5696
5697 spin_unlock(&device->io_lock);
5698
5699 if (should_queue)
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +08005700 btrfs_queue_work(root->fs_info->submit_workers,
5701 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005702}
5703
Josef Bacikde1ee922012-10-19 16:50:56 -04005704static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5705 sector_t sector)
5706{
5707 struct bio_vec *prev;
5708 struct request_queue *q = bdev_get_queue(bdev);
Akinobu Mita475bf362013-11-18 22:13:18 +09005709 unsigned int max_sectors = queue_max_sectors(q);
Josef Bacikde1ee922012-10-19 16:50:56 -04005710 struct bvec_merge_data bvm = {
5711 .bi_bdev = bdev,
5712 .bi_sector = sector,
5713 .bi_rw = bio->bi_rw,
5714 };
5715
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305716 if (WARN_ON(bio->bi_vcnt == 0))
Josef Bacikde1ee922012-10-19 16:50:56 -04005717 return 1;
Josef Bacikde1ee922012-10-19 16:50:56 -04005718
5719 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005720 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005721 return 0;
5722
5723 if (!q->merge_bvec_fn)
5724 return 1;
5725
Kent Overstreet4f024f32013-10-11 15:44:27 -07005726 bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len;
Josef Bacikde1ee922012-10-19 16:50:56 -04005727 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5728 return 0;
5729 return 1;
5730}
5731
5732static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5733 struct bio *bio, u64 physical, int dev_nr,
5734 int rw, int async)
5735{
5736 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5737
5738 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005739 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005740 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005741 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005742#ifdef DEBUG
5743 {
5744 struct rcu_string *name;
5745
5746 rcu_read_lock();
5747 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005748 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005749 "(%s id %llu), size=%u\n", rw,
Fabian Frederick1b6e4462014-09-24 20:23:05 +02005750 (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev,
5751 name->str, dev->devid, bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04005752 rcu_read_unlock();
5753 }
5754#endif
5755 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005756
5757 btrfs_bio_counter_inc_noblocked(root->fs_info);
5758
Josef Bacikde1ee922012-10-19 16:50:56 -04005759 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005760 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005761 else
5762 btrfsic_submit_bio(rw, bio);
5763}
5764
5765static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5766 struct bio *first_bio, struct btrfs_device *dev,
5767 int dev_nr, int rw, int async)
5768{
5769 struct bio_vec *bvec = first_bio->bi_io_vec;
5770 struct bio *bio;
5771 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5772 u64 physical = bbio->stripes[dev_nr].physical;
5773
5774again:
5775 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5776 if (!bio)
5777 return -ENOMEM;
5778
5779 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5780 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5781 bvec->bv_offset) < bvec->bv_len) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07005782 u64 len = bio->bi_iter.bi_size;
Josef Bacikde1ee922012-10-19 16:50:56 -04005783
5784 atomic_inc(&bbio->stripes_pending);
5785 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5786 rw, async);
5787 physical += len;
5788 goto again;
5789 }
5790 bvec++;
5791 }
5792
5793 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5794 return 0;
5795}
5796
5797static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5798{
5799 atomic_inc(&bbio->error);
5800 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005801 /* Shoud be the original bio. */
5802 WARN_ON(bio != bbio->orig_bio);
5803
Josef Bacikde1ee922012-10-19 16:50:56 -04005804 bio->bi_private = bbio->private;
5805 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005806 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005807 bio->bi_iter.bi_sector = logical >> 9;
Miao Xie8408c712014-06-19 10:42:55 +08005808
5809 btrfs_end_bbio(bbio, bio, -EIO);
Josef Bacikde1ee922012-10-19 16:50:56 -04005810 }
5811}
5812
Chris Masonf1885912008-04-09 16:28:12 -04005813int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005814 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005815{
Chris Mason0b86a832008-03-24 15:01:56 -04005816 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005817 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005818 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005819 u64 length = 0;
5820 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04005821 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08005822 int dev_nr;
5823 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005824 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005825
Kent Overstreet4f024f32013-10-11 15:44:27 -07005826 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005827 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005828
Miao Xiec404e0d2014-01-30 16:46:55 +08005829 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005830 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005831 mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08005832 if (ret) {
5833 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005834 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005835 }
Chris Masoncea9e442008-04-09 16:28:12 -04005836
Jan Schmidta1d3c472011-08-04 17:15:33 +02005837 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005838 bbio->orig_bio = first_bio;
5839 bbio->private = first_bio->bi_private;
5840 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005841 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005842 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5843
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005844 if (bbio->raid_map) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005845 /* In this case, map_length has been set to the length of
5846 a single stripe; not the whole write */
5847 if (rw & WRITE) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005848 ret = raid56_parity_write(root, bio, bbio, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005849 } else {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005850 ret = raid56_parity_recover(root, bio, bbio, map_length,
Miao Xie42452152014-11-25 16:39:28 +08005851 mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05005852 }
Miao Xie42452152014-11-25 16:39:28 +08005853
Miao Xiec404e0d2014-01-30 16:46:55 +08005854 btrfs_bio_counter_dec(root->fs_info);
5855 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005856 }
5857
Chris Masoncea9e442008-04-09 16:28:12 -04005858 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005859 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005860 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005861 BUG();
5862 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005863
Zhao Lei08da7572015-02-12 15:42:16 +08005864 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005865 dev = bbio->stripes[dev_nr].dev;
5866 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5867 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04005868 continue;
5869 }
5870
5871 /*
5872 * Check and see if we're ok with this bio based on it's size
5873 * and offset with the given device.
5874 */
5875 if (!bio_size_ok(dev->bdev, first_bio,
5876 bbio->stripes[dev_nr].physical >> 9)) {
5877 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5878 dev_nr, rw, async_submit);
5879 BUG_ON(ret);
Josef Bacikde1ee922012-10-19 16:50:56 -04005880 continue;
5881 }
5882
Jan Schmidta1d3c472011-08-04 17:15:33 +02005883 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005884 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005885 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005886 } else {
5887 bio = first_bio;
Miao Xiec55f1392014-06-19 10:42:54 +08005888 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
Chris Mason8790d502008-04-03 16:29:03 -04005889 }
Josef Bacik606686e2012-06-04 14:03:51 -04005890
Josef Bacikde1ee922012-10-19 16:50:56 -04005891 submit_stripe_bio(root, bbio, bio,
5892 bbio->stripes[dev_nr].physical, dev_nr, rw,
5893 async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04005894 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005895 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04005896 return 0;
5897}
5898
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005899struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005900 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005901{
Yan Zheng2b820322008-11-17 21:11:30 -05005902 struct btrfs_device *device;
5903 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005904
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005905 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005906 while (cur_devices) {
5907 if (!fsid ||
5908 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5909 device = __find_device(&cur_devices->devices,
5910 devid, uuid);
5911 if (device)
5912 return device;
5913 }
5914 cur_devices = cur_devices->seed;
5915 }
5916 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005917}
5918
Chris Masondfe25022008-05-13 13:46:40 -04005919static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
Miao Xie5f375832014-09-03 21:35:46 +08005920 struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04005921 u64 devid, u8 *dev_uuid)
5922{
5923 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04005924
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005925 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5926 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005927 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005928
5929 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005930 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04005931 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005932
5933 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05005934 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005935
Chris Masondfe25022008-05-13 13:46:40 -04005936 return device;
5937}
5938
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005939/**
5940 * btrfs_alloc_device - allocate struct btrfs_device
5941 * @fs_info: used only for generating a new devid, can be NULL if
5942 * devid is provided (i.e. @devid != NULL).
5943 * @devid: a pointer to devid for this device. If NULL a new devid
5944 * is generated.
5945 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5946 * is generated.
5947 *
5948 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5949 * on error. Returned struct is not linked onto any lists and can be
5950 * destroyed with kfree() right away.
5951 */
5952struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5953 const u64 *devid,
5954 const u8 *uuid)
5955{
5956 struct btrfs_device *dev;
5957 u64 tmp;
5958
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305959 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005960 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005961
5962 dev = __alloc_device();
5963 if (IS_ERR(dev))
5964 return dev;
5965
5966 if (devid)
5967 tmp = *devid;
5968 else {
5969 int ret;
5970
5971 ret = find_next_devid(fs_info, &tmp);
5972 if (ret) {
5973 kfree(dev);
5974 return ERR_PTR(ret);
5975 }
5976 }
5977 dev->devid = tmp;
5978
5979 if (uuid)
5980 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5981 else
5982 generate_random_uuid(dev->uuid);
5983
Liu Bo9e0af232014-08-15 23:36:53 +08005984 btrfs_init_work(&dev->work, btrfs_submit_helper,
5985 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005986
5987 return dev;
5988}
5989
Chris Mason0b86a832008-03-24 15:01:56 -04005990static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5991 struct extent_buffer *leaf,
5992 struct btrfs_chunk *chunk)
5993{
5994 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5995 struct map_lookup *map;
5996 struct extent_map *em;
5997 u64 logical;
5998 u64 length;
5999 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006000 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006001 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006002 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006003 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006004
Chris Masone17cade2008-04-15 15:41:47 -04006005 logical = key->offset;
6006 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04006007
Chris Mason890871b2009-09-02 16:24:52 -04006008 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006009 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006010 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006011
6012 /* already mapped? */
6013 if (em && em->start <= logical && em->start + em->len > logical) {
6014 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006015 return 0;
6016 } else if (em) {
6017 free_extent_map(em);
6018 }
Chris Mason0b86a832008-03-24 15:01:56 -04006019
David Sterba172ddd62011-04-21 00:48:27 +02006020 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006021 if (!em)
6022 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006023 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6024 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006025 if (!map) {
6026 free_extent_map(em);
6027 return -ENOMEM;
6028 }
6029
Wang Shilong298a8f92014-06-19 10:42:52 +08006030 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04006031 em->bdev = (struct block_device *)map;
6032 em->start = logical;
6033 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006034 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006035 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006036 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006037
Chris Mason593060d2008-03-25 16:50:33 -04006038 map->num_stripes = num_stripes;
6039 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6040 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6041 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
6042 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6043 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006044 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006045 for (i = 0; i < num_stripes; i++) {
6046 map->stripes[i].physical =
6047 btrfs_stripe_offset_nr(leaf, chunk, i);
6048 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006049 read_extent_buffer(leaf, uuid, (unsigned long)
6050 btrfs_stripe_dev_uuid_nr(chunk, i),
6051 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006052 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
6053 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04006054 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006055 free_extent_map(em);
6056 return -EIO;
6057 }
Chris Masondfe25022008-05-13 13:46:40 -04006058 if (!map->stripes[i].dev) {
6059 map->stripes[i].dev =
Miao Xie5f375832014-09-03 21:35:46 +08006060 add_missing_dev(root, root->fs_info->fs_devices,
6061 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006062 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04006063 free_extent_map(em);
6064 return -EIO;
6065 }
6066 }
6067 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04006068 }
6069
Chris Mason890871b2009-09-02 16:24:52 -04006070 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006071 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006072 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006073 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006074 free_extent_map(em);
6075
6076 return 0;
6077}
6078
Jeff Mahoney143bede2012-03-01 14:56:26 +01006079static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006080 struct btrfs_dev_item *dev_item,
6081 struct btrfs_device *device)
6082{
6083 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006084
6085 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006086 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6087 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006088 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006089 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006090 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006091 device->type = btrfs_device_type(leaf, dev_item);
6092 device->io_align = btrfs_device_io_align(leaf, dev_item);
6093 device->io_width = btrfs_device_io_width(leaf, dev_item);
6094 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006095 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006096 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006097
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006098 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006099 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006100}
6101
Miao Xie5f375832014-09-03 21:35:46 +08006102static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6103 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006104{
6105 struct btrfs_fs_devices *fs_devices;
6106 int ret;
6107
Li Zefanb367e472011-12-07 11:38:24 +08006108 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05006109
6110 fs_devices = root->fs_info->fs_devices->seed;
6111 while (fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006112 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE))
6113 return fs_devices;
6114
Yan Zheng2b820322008-11-17 21:11:30 -05006115 fs_devices = fs_devices->seed;
6116 }
6117
6118 fs_devices = find_fsid(fsid);
6119 if (!fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006120 if (!btrfs_test_opt(root, DEGRADED))
6121 return ERR_PTR(-ENOENT);
6122
6123 fs_devices = alloc_fs_devices(fsid);
6124 if (IS_ERR(fs_devices))
6125 return fs_devices;
6126
6127 fs_devices->seeding = 1;
6128 fs_devices->opened = 1;
6129 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006130 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006131
6132 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006133 if (IS_ERR(fs_devices))
6134 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006135
Christoph Hellwig97288f22008-12-02 06:36:09 -05006136 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006137 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006138 if (ret) {
6139 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006140 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006141 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006142 }
Yan Zheng2b820322008-11-17 21:11:30 -05006143
6144 if (!fs_devices->seeding) {
6145 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006146 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006147 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006148 goto out;
6149 }
6150
6151 fs_devices->seed = root->fs_info->fs_devices->seed;
6152 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006153out:
Miao Xie5f375832014-09-03 21:35:46 +08006154 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006155}
6156
Chris Mason0d81ba52008-03-24 15:02:07 -04006157static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006158 struct extent_buffer *leaf,
6159 struct btrfs_dev_item *dev_item)
6160{
Miao Xie5f375832014-09-03 21:35:46 +08006161 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006162 struct btrfs_device *device;
6163 u64 devid;
6164 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006165 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006166 u8 dev_uuid[BTRFS_UUID_SIZE];
6167
Chris Mason0b86a832008-03-24 15:01:56 -04006168 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006169 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006170 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006171 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006172 BTRFS_UUID_SIZE);
6173
6174 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
Miao Xie5f375832014-09-03 21:35:46 +08006175 fs_devices = open_seed_devices(root, fs_uuid);
6176 if (IS_ERR(fs_devices))
6177 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006178 }
6179
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006180 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006181 if (!device) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006182 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006183 return -EIO;
6184
Miao Xie5f375832014-09-03 21:35:46 +08006185 btrfs_warn(root->fs_info, "devid %llu missing", devid);
6186 device = add_missing_dev(root, fs_devices, devid, dev_uuid);
6187 if (!device)
6188 return -ENOMEM;
6189 } else {
6190 if (!device->bdev && !btrfs_test_opt(root, DEGRADED))
6191 return -EIO;
6192
6193 if(!device->bdev && !device->missing) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006194 /*
6195 * this happens when a device that was properly setup
6196 * in the device info lists suddenly goes bad.
6197 * device->bdev is NULL, and so we have to set
6198 * device->missing to one here
6199 */
Miao Xie5f375832014-09-03 21:35:46 +08006200 device->fs_devices->missing_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05006201 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006202 }
Miao Xie5f375832014-09-03 21:35:46 +08006203
6204 /* Move the device to its own fs_devices */
6205 if (device->fs_devices != fs_devices) {
6206 ASSERT(device->missing);
6207
6208 list_move(&device->dev_list, &fs_devices->devices);
6209 device->fs_devices->num_devices--;
6210 fs_devices->num_devices++;
6211
6212 device->fs_devices->missing_devices--;
6213 fs_devices->missing_devices++;
6214
6215 device->fs_devices = fs_devices;
6216 }
Yan Zheng2b820322008-11-17 21:11:30 -05006217 }
6218
6219 if (device->fs_devices != root->fs_info->fs_devices) {
6220 BUG_ON(device->writeable);
6221 if (device->generation !=
6222 btrfs_device_generation(leaf, dev_item))
6223 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006224 }
Chris Mason0b86a832008-03-24 15:01:56 -04006225
6226 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006227 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006228 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006229 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006230 spin_lock(&root->fs_info->free_chunk_lock);
6231 root->fs_info->free_chunk_space += device->total_bytes -
6232 device->bytes_used;
6233 spin_unlock(&root->fs_info->free_chunk_lock);
6234 }
Chris Mason0b86a832008-03-24 15:01:56 -04006235 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006236 return ret;
6237}
6238
Yan Zhenge4404d62008-12-12 10:03:26 -05006239int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006240{
David Sterba6c417612011-04-13 15:41:04 +02006241 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006242 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006243 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006244 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006245 u8 *array_ptr;
6246 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006247 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006248 u32 num_stripes;
6249 u32 array_size;
6250 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006251 u32 cur_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006252 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006253
David Sterbaa83fffb2014-06-15 02:39:54 +02006254 ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize);
6255 /*
6256 * This will create extent buffer of nodesize, superblock size is
6257 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6258 * overallocate but we can keep it as-is, only the first page is used.
6259 */
6260 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET);
Chris Masona061fc82008-05-07 11:43:44 -04006261 if (!sb)
6262 return -ENOMEM;
6263 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006264 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006265 /*
6266 * The sb extent buffer is artifical and just used to read the system array.
6267 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6268 * pages up-to-date when the page is larger: extent does not cover the
6269 * whole page and consequently check_page_uptodate does not find all
6270 * the page's extents up-to-date (the hole beyond sb),
6271 * write_extent_buffer then triggers a WARN_ON.
6272 *
6273 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6274 * but sb spans only this function. Add an explicit SetPageUptodate call
6275 * to silence the warning eg. on PowerPC 64.
6276 */
6277 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006278 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006279
Chris Masona061fc82008-05-07 11:43:44 -04006280 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006281 array_size = btrfs_super_sys_array_size(super_copy);
6282
David Sterba1ffb22c2014-10-31 19:02:42 +01006283 array_ptr = super_copy->sys_chunk_array;
6284 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6285 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006286
David Sterba1ffb22c2014-10-31 19:02:42 +01006287 while (cur_offset < array_size) {
6288 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006289 len = sizeof(*disk_key);
6290 if (cur_offset + len > array_size)
6291 goto out_short_read;
6292
Chris Mason0b86a832008-03-24 15:01:56 -04006293 btrfs_disk_key_to_cpu(&key, disk_key);
6294
David Sterba1ffb22c2014-10-31 19:02:42 +01006295 array_ptr += len;
6296 sb_array_offset += len;
6297 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006298
Chris Mason0d81ba52008-03-24 15:02:07 -04006299 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006300 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006301 /*
6302 * At least one btrfs_chunk with one stripe must be
6303 * present, exact stripe count check comes afterwards
6304 */
6305 len = btrfs_chunk_item_size(1);
6306 if (cur_offset + len > array_size)
6307 goto out_short_read;
6308
6309 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6310 len = btrfs_chunk_item_size(num_stripes);
6311 if (cur_offset + len > array_size)
6312 goto out_short_read;
6313
Chris Mason0d81ba52008-03-24 15:02:07 -04006314 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006315 if (ret)
6316 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006317 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006318 ret = -EIO;
6319 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006320 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006321 array_ptr += len;
6322 sb_array_offset += len;
6323 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006324 }
Chris Masona061fc82008-05-07 11:43:44 -04006325 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006326 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006327
6328out_short_read:
6329 printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n",
6330 len, cur_offset);
6331 free_extent_buffer(sb);
6332 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006333}
6334
6335int btrfs_read_chunk_tree(struct btrfs_root *root)
6336{
6337 struct btrfs_path *path;
6338 struct extent_buffer *leaf;
6339 struct btrfs_key key;
6340 struct btrfs_key found_key;
6341 int ret;
6342 int slot;
6343
6344 root = root->fs_info->chunk_root;
6345
6346 path = btrfs_alloc_path();
6347 if (!path)
6348 return -ENOMEM;
6349
Li Zefanb367e472011-12-07 11:38:24 +08006350 mutex_lock(&uuid_mutex);
6351 lock_chunks(root);
6352
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006353 /*
6354 * Read all device items, and then all the chunk items. All
6355 * device items are found before any chunk item (their object id
6356 * is smaller than the lowest possible object id for a chunk
6357 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006358 */
6359 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6360 key.offset = 0;
6361 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006362 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006363 if (ret < 0)
6364 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006365 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006366 leaf = path->nodes[0];
6367 slot = path->slots[0];
6368 if (slot >= btrfs_header_nritems(leaf)) {
6369 ret = btrfs_next_leaf(root, path);
6370 if (ret == 0)
6371 continue;
6372 if (ret < 0)
6373 goto error;
6374 break;
6375 }
6376 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006377 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6378 struct btrfs_dev_item *dev_item;
6379 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006380 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006381 ret = read_one_dev(root, leaf, dev_item);
6382 if (ret)
6383 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006384 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6385 struct btrfs_chunk *chunk;
6386 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6387 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006388 if (ret)
6389 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006390 }
6391 path->slots[0]++;
6392 }
Chris Mason0b86a832008-03-24 15:01:56 -04006393 ret = 0;
6394error:
Li Zefanb367e472011-12-07 11:38:24 +08006395 unlock_chunks(root);
6396 mutex_unlock(&uuid_mutex);
6397
Yan Zheng2b820322008-11-17 21:11:30 -05006398 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006399 return ret;
6400}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006401
Miao Xiecb517ea2013-05-15 07:48:19 +00006402void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6403{
6404 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6405 struct btrfs_device *device;
6406
Liu Bo29cc83f2014-05-11 23:14:59 +08006407 while (fs_devices) {
6408 mutex_lock(&fs_devices->device_list_mutex);
6409 list_for_each_entry(device, &fs_devices->devices, dev_list)
6410 device->dev_root = fs_info->dev_root;
6411 mutex_unlock(&fs_devices->device_list_mutex);
6412
6413 fs_devices = fs_devices->seed;
6414 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006415}
6416
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006417static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6418{
6419 int i;
6420
6421 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6422 btrfs_dev_stat_reset(dev, i);
6423}
6424
6425int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6426{
6427 struct btrfs_key key;
6428 struct btrfs_key found_key;
6429 struct btrfs_root *dev_root = fs_info->dev_root;
6430 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6431 struct extent_buffer *eb;
6432 int slot;
6433 int ret = 0;
6434 struct btrfs_device *device;
6435 struct btrfs_path *path = NULL;
6436 int i;
6437
6438 path = btrfs_alloc_path();
6439 if (!path) {
6440 ret = -ENOMEM;
6441 goto out;
6442 }
6443
6444 mutex_lock(&fs_devices->device_list_mutex);
6445 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6446 int item_size;
6447 struct btrfs_dev_stats_item *ptr;
6448
6449 key.objectid = 0;
6450 key.type = BTRFS_DEV_STATS_KEY;
6451 key.offset = device->devid;
6452 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6453 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006454 __btrfs_reset_dev_stats(device);
6455 device->dev_stats_valid = 1;
6456 btrfs_release_path(path);
6457 continue;
6458 }
6459 slot = path->slots[0];
6460 eb = path->nodes[0];
6461 btrfs_item_key_to_cpu(eb, &found_key, slot);
6462 item_size = btrfs_item_size_nr(eb, slot);
6463
6464 ptr = btrfs_item_ptr(eb, slot,
6465 struct btrfs_dev_stats_item);
6466
6467 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6468 if (item_size >= (1 + i) * sizeof(__le64))
6469 btrfs_dev_stat_set(device, i,
6470 btrfs_dev_stats_value(eb, ptr, i));
6471 else
6472 btrfs_dev_stat_reset(device, i);
6473 }
6474
6475 device->dev_stats_valid = 1;
6476 btrfs_dev_stat_print_on_load(device);
6477 btrfs_release_path(path);
6478 }
6479 mutex_unlock(&fs_devices->device_list_mutex);
6480
6481out:
6482 btrfs_free_path(path);
6483 return ret < 0 ? ret : 0;
6484}
6485
6486static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6487 struct btrfs_root *dev_root,
6488 struct btrfs_device *device)
6489{
6490 struct btrfs_path *path;
6491 struct btrfs_key key;
6492 struct extent_buffer *eb;
6493 struct btrfs_dev_stats_item *ptr;
6494 int ret;
6495 int i;
6496
6497 key.objectid = 0;
6498 key.type = BTRFS_DEV_STATS_KEY;
6499 key.offset = device->devid;
6500
6501 path = btrfs_alloc_path();
6502 BUG_ON(!path);
6503 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6504 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006505 printk_in_rcu(KERN_WARNING "BTRFS: "
6506 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006507 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006508 goto out;
6509 }
6510
6511 if (ret == 0 &&
6512 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6513 /* need to delete old one and insert a new one */
6514 ret = btrfs_del_item(trans, dev_root, path);
6515 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006516 printk_in_rcu(KERN_WARNING "BTRFS: "
6517 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006518 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006519 goto out;
6520 }
6521 ret = 1;
6522 }
6523
6524 if (ret == 1) {
6525 /* need to insert a new item */
6526 btrfs_release_path(path);
6527 ret = btrfs_insert_empty_item(trans, dev_root, path,
6528 &key, sizeof(*ptr));
6529 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006530 printk_in_rcu(KERN_WARNING "BTRFS: "
6531 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006532 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006533 goto out;
6534 }
6535 }
6536
6537 eb = path->nodes[0];
6538 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6539 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6540 btrfs_set_dev_stats_value(eb, ptr, i,
6541 btrfs_dev_stat_read(device, i));
6542 btrfs_mark_buffer_dirty(eb);
6543
6544out:
6545 btrfs_free_path(path);
6546 return ret;
6547}
6548
6549/*
6550 * called from commit_transaction. Writes all changed device stats to disk.
6551 */
6552int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6553 struct btrfs_fs_info *fs_info)
6554{
6555 struct btrfs_root *dev_root = fs_info->dev_root;
6556 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6557 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006558 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006559 int ret = 0;
6560
6561 mutex_lock(&fs_devices->device_list_mutex);
6562 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006563 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006564 continue;
6565
Miao Xieaddc3fa2014-07-24 11:37:11 +08006566 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006567 ret = update_dev_stat_item(trans, dev_root, device);
6568 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006569 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006570 }
6571 mutex_unlock(&fs_devices->device_list_mutex);
6572
6573 return ret;
6574}
6575
Stefan Behrens442a4f62012-05-25 16:06:08 +02006576void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6577{
6578 btrfs_dev_stat_inc(dev, index);
6579 btrfs_dev_stat_print_on_error(dev);
6580}
6581
Eric Sandeen48a3b632013-04-25 20:41:01 +00006582static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006583{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006584 if (!dev->dev_stats_valid)
6585 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006586 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6587 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006588 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006589 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6590 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6591 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006592 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6593 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006594}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006595
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006596static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6597{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006598 int i;
6599
6600 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6601 if (btrfs_dev_stat_read(dev, i) != 0)
6602 break;
6603 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6604 return; /* all values == 0, suppress message */
6605
Frank Holtonefe120a2013-12-20 11:37:06 -05006606 printk_in_rcu(KERN_INFO "BTRFS: "
6607 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006608 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006609 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6610 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6611 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6612 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6613 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6614}
6615
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006616int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006617 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006618{
6619 struct btrfs_device *dev;
6620 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6621 int i;
6622
6623 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006624 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006625 mutex_unlock(&fs_devices->device_list_mutex);
6626
6627 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006628 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006629 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006630 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006631 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006632 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006633 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006634 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6635 if (stats->nr_items > i)
6636 stats->values[i] =
6637 btrfs_dev_stat_read_and_reset(dev, i);
6638 else
6639 btrfs_dev_stat_reset(dev, i);
6640 }
6641 } else {
6642 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6643 if (stats->nr_items > i)
6644 stats->values[i] = btrfs_dev_stat_read(dev, i);
6645 }
6646 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6647 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6648 return 0;
6649}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006650
6651int btrfs_scratch_superblock(struct btrfs_device *device)
6652{
6653 struct buffer_head *bh;
6654 struct btrfs_super_block *disk_super;
6655
6656 bh = btrfs_read_dev_super(device->bdev);
6657 if (!bh)
6658 return -EINVAL;
6659 disk_super = (struct btrfs_super_block *)bh->b_data;
6660
6661 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6662 set_buffer_dirty(bh);
6663 sync_dirty_buffer(bh);
6664 brelse(bh);
6665
6666 return 0;
6667}
Miao Xie935e5cc2014-09-03 21:35:33 +08006668
6669/*
6670 * Update the size of all devices, which is used for writing out the
6671 * super blocks.
6672 */
6673void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
6674{
6675 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6676 struct btrfs_device *curr, *next;
6677
6678 if (list_empty(&fs_devices->resized_devices))
6679 return;
6680
6681 mutex_lock(&fs_devices->device_list_mutex);
6682 lock_chunks(fs_info->dev_root);
6683 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
6684 resized_list) {
6685 list_del_init(&curr->resized_list);
6686 curr->commit_total_bytes = curr->disk_total_bytes;
6687 }
6688 unlock_chunks(fs_info->dev_root);
6689 mutex_unlock(&fs_devices->device_list_mutex);
6690}
Miao Xiece7213c2014-09-03 21:35:34 +08006691
6692/* Must be invoked during the transaction commit */
6693void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
6694 struct btrfs_transaction *transaction)
6695{
6696 struct extent_map *em;
6697 struct map_lookup *map;
6698 struct btrfs_device *dev;
6699 int i;
6700
6701 if (list_empty(&transaction->pending_chunks))
6702 return;
6703
6704 /* In order to kick the device replace finish process */
6705 lock_chunks(root);
6706 list_for_each_entry(em, &transaction->pending_chunks, list) {
6707 map = (struct map_lookup *)em->bdev;
6708
6709 for (i = 0; i < map->num_stripes; i++) {
6710 dev = map->stripes[i].dev;
6711 dev->commit_bytes_used = dev->bytes_used;
6712 }
6713 }
6714 unlock_chunks(root);
6715}