blob: c3e2591dd7428696c6e3cdd82f5977a66d18ad40 [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);
Anand Jainc73eccf2015-03-10 06:38:30 +080055struct list_head *btrfs_get_fs_uuids(void)
56{
57 return &fs_uuids;
58}
Chris Mason8a4b83c2008-03-24 15:02:07 -040059
Ilya Dryomov2208a372013-08-12 14:33:03 +030060static struct btrfs_fs_devices *__alloc_fs_devices(void)
61{
62 struct btrfs_fs_devices *fs_devs;
63
64 fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS);
65 if (!fs_devs)
66 return ERR_PTR(-ENOMEM);
67
68 mutex_init(&fs_devs->device_list_mutex);
69
70 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +080071 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +030072 INIT_LIST_HEAD(&fs_devs->alloc_list);
73 INIT_LIST_HEAD(&fs_devs->list);
74
75 return fs_devs;
76}
77
78/**
79 * alloc_fs_devices - allocate struct btrfs_fs_devices
80 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
81 * generated.
82 *
83 * Return: a pointer to a new &struct btrfs_fs_devices on success;
84 * ERR_PTR() on error. Returned struct is not linked onto any lists and
85 * can be destroyed with kfree() right away.
86 */
87static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
88{
89 struct btrfs_fs_devices *fs_devs;
90
91 fs_devs = __alloc_fs_devices();
92 if (IS_ERR(fs_devs))
93 return fs_devs;
94
95 if (fsid)
96 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
97 else
98 generate_random_uuid(fs_devs->fsid);
99
100 return fs_devs;
101}
102
Yan Zhenge4404d62008-12-12 10:03:26 -0500103static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
104{
105 struct btrfs_device *device;
106 WARN_ON(fs_devices->opened);
107 while (!list_empty(&fs_devices->devices)) {
108 device = list_entry(fs_devices->devices.next,
109 struct btrfs_device, dev_list);
110 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -0400111 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500112 kfree(device);
113 }
114 kfree(fs_devices);
115}
116
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000117static void btrfs_kobject_uevent(struct block_device *bdev,
118 enum kobject_action action)
119{
120 int ret;
121
122 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
123 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500124 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000125 action,
126 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
127 &disk_to_dev(bdev->bd_disk)->kobj);
128}
129
Jeff Mahoney143bede2012-03-01 14:56:26 +0100130void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400131{
132 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400133
Yan Zheng2b820322008-11-17 21:11:30 -0500134 while (!list_empty(&fs_uuids)) {
135 fs_devices = list_entry(fs_uuids.next,
136 struct btrfs_fs_devices, list);
137 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500138 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400139 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400140}
141
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300142static struct btrfs_device *__alloc_device(void)
143{
144 struct btrfs_device *dev;
145
146 dev = kzalloc(sizeof(*dev), GFP_NOFS);
147 if (!dev)
148 return ERR_PTR(-ENOMEM);
149
150 INIT_LIST_HEAD(&dev->dev_list);
151 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800152 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300153
154 spin_lock_init(&dev->io_lock);
155
156 spin_lock_init(&dev->reada_lock);
157 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800158 atomic_set(&dev->dev_stats_ccnt, 0);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300159 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT);
160 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT);
161
162 return dev;
163}
164
Chris Masona1b32a52008-09-05 16:09:51 -0400165static noinline struct btrfs_device *__find_device(struct list_head *head,
166 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400167{
168 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400169
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500170 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400171 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400172 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400173 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400174 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400175 }
176 return NULL;
177}
178
Chris Masona1b32a52008-09-05 16:09:51 -0400179static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400180{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400181 struct btrfs_fs_devices *fs_devices;
182
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500183 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400184 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
185 return fs_devices;
186 }
187 return NULL;
188}
189
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100190static int
191btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
192 int flush, struct block_device **bdev,
193 struct buffer_head **bh)
194{
195 int ret;
196
197 *bdev = blkdev_get_by_path(device_path, flags, holder);
198
199 if (IS_ERR(*bdev)) {
200 ret = PTR_ERR(*bdev);
Frank Holtonefe120a2013-12-20 11:37:06 -0500201 printk(KERN_INFO "BTRFS: open %s failed\n", device_path);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100202 goto error;
203 }
204
205 if (flush)
206 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
207 ret = set_blocksize(*bdev, 4096);
208 if (ret) {
209 blkdev_put(*bdev, flags);
210 goto error;
211 }
212 invalidate_bdev(*bdev);
213 *bh = btrfs_read_dev_super(*bdev);
Anand Jain92fc03f2015-08-14 18:32:51 +0800214 if (IS_ERR(*bh)) {
215 ret = PTR_ERR(*bh);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100216 blkdev_put(*bdev, flags);
217 goto error;
218 }
219
220 return 0;
221
222error:
223 *bdev = NULL;
224 *bh = NULL;
225 return ret;
226}
227
Chris Masonffbd5172009-04-20 15:50:09 -0400228static void requeue_list(struct btrfs_pending_bios *pending_bios,
229 struct bio *head, struct bio *tail)
230{
231
232 struct bio *old_head;
233
234 old_head = pending_bios->head;
235 pending_bios->head = head;
236 if (pending_bios->tail)
237 tail->bi_next = old_head;
238 else
239 pending_bios->tail = tail;
240}
241
Chris Mason8b712842008-06-11 16:50:36 -0400242/*
243 * we try to collect pending bios for a device so we don't get a large
244 * number of procs sending bios down to the same device. This greatly
245 * improves the schedulers ability to collect and merge the bios.
246 *
247 * But, it also turns into a long list of bios to process and that is sure
248 * to eventually make the worker thread block. The solution here is to
249 * make some progress and then put this work struct back at the end of
250 * the list if the block device is congested. This way, multiple devices
251 * can make progress from a single worker thread.
252 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100253static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400254{
255 struct bio *pending;
256 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400257 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400258 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400259 struct bio *tail;
260 struct bio *cur;
261 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400262 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400263 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400264 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400265 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400266 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000267 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400268 struct blk_plug plug;
269
270 /*
271 * this function runs all the bios we've collected for
272 * a particular device. We don't want to wander off to
273 * another device without first sending all of these down.
274 * So, setup a plug here and finish it off before we return
275 */
276 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400277
Chris Masonbedf7622009-04-03 10:32:58 -0400278 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400279 fs_info = device->dev_root->fs_info;
280 limit = btrfs_async_submit_limit(fs_info);
281 limit = limit * 2 / 3;
282
Chris Mason8b712842008-06-11 16:50:36 -0400283loop:
284 spin_lock(&device->io_lock);
285
Chris Masona6837052009-02-04 09:19:41 -0500286loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400287 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400288
Chris Mason8b712842008-06-11 16:50:36 -0400289 /* take all the bios off the list at once and process them
290 * later on (without the lock held). But, remember the
291 * tail and other pointers so the bios can be properly reinserted
292 * into the list if we hit congestion
293 */
Chris Masond84275c2009-06-09 15:39:08 -0400294 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400295 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400296 force_reg = 1;
297 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400298 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400299 force_reg = 0;
300 }
Chris Masonffbd5172009-04-20 15:50:09 -0400301
302 pending = pending_bios->head;
303 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400304 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400305
306 /*
307 * if pending was null this time around, no bios need processing
308 * at all and we can stop. Otherwise it'll loop back up again
309 * and do an additional check so no bios are missed.
310 *
311 * device->running_pending is used to synchronize with the
312 * schedule_bio code.
313 */
Chris Masonffbd5172009-04-20 15:50:09 -0400314 if (device->pending_sync_bios.head == NULL &&
315 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400316 again = 0;
317 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400318 } else {
319 again = 1;
320 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400321 }
Chris Masonffbd5172009-04-20 15:50:09 -0400322
323 pending_bios->head = NULL;
324 pending_bios->tail = NULL;
325
Chris Mason8b712842008-06-11 16:50:36 -0400326 spin_unlock(&device->io_lock);
327
Chris Masond3977122009-01-05 21:25:51 -0500328 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400329
330 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400331 /* we want to work on both lists, but do more bios on the
332 * sync list than the regular list
333 */
334 if ((num_run > 32 &&
335 pending_bios != &device->pending_sync_bios &&
336 device->pending_sync_bios.head) ||
337 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
338 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400339 spin_lock(&device->io_lock);
340 requeue_list(pending_bios, pending, tail);
341 goto loop_lock;
342 }
343
Chris Mason8b712842008-06-11 16:50:36 -0400344 cur = pending;
345 pending = pending->bi_next;
346 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400347
Josef Bacik66657b32012-08-01 15:36:24 -0400348 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400349 waitqueue_active(&fs_info->async_submit_wait))
350 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400351
Jens Axboedac56212015-04-17 16:23:59 -0600352 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400353
Chris Mason2ab1ba62011-08-04 14:28:36 -0400354 /*
355 * if we're doing the sync list, record that our
356 * plug has some sync requests on it
357 *
358 * If we're doing the regular list and there are
359 * sync requests sitting around, unplug before
360 * we add more
361 */
362 if (pending_bios == &device->pending_sync_bios) {
363 sync_pending = 1;
364 } else if (sync_pending) {
365 blk_finish_plug(&plug);
366 blk_start_plug(&plug);
367 sync_pending = 0;
368 }
369
Stefan Behrens21adbd52011-11-09 13:44:05 +0100370 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400371 num_run++;
372 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100373
374 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400375
376 /*
377 * we made progress, there is more work to do and the bdi
378 * is now congested. Back off and let other work structs
379 * run instead
380 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400381 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500382 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400383 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400384
Chris Masonb765ead2009-04-03 10:27:10 -0400385 ioc = current->io_context;
386
387 /*
388 * the main goal here is that we don't want to
389 * block if we're going to be able to submit
390 * more requests without blocking.
391 *
392 * This code does two great things, it pokes into
393 * the elevator code from a filesystem _and_
394 * it makes assumptions about how batching works.
395 */
396 if (ioc && ioc->nr_batch_requests > 0 &&
397 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
398 (last_waited == 0 ||
399 ioc->last_waited == last_waited)) {
400 /*
401 * we want to go through our batch of
402 * requests and stop. So, we copy out
403 * the ioc->last_waited time and test
404 * against it before looping
405 */
406 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100407 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400408 continue;
409 }
Chris Mason8b712842008-06-11 16:50:36 -0400410 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400411 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500412 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400413
414 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800415 btrfs_queue_work(fs_info->submit_workers,
416 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400417 goto done;
418 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500419 /* unplug every 64 requests just for good measure */
420 if (batch_run % 64 == 0) {
421 blk_finish_plug(&plug);
422 blk_start_plug(&plug);
423 sync_pending = 0;
424 }
Chris Mason8b712842008-06-11 16:50:36 -0400425 }
Chris Masonffbd5172009-04-20 15:50:09 -0400426
Chris Mason51684082010-03-10 15:33:32 -0500427 cond_resched();
428 if (again)
429 goto loop;
430
431 spin_lock(&device->io_lock);
432 if (device->pending_bios.head || device->pending_sync_bios.head)
433 goto loop_lock;
434 spin_unlock(&device->io_lock);
435
Chris Mason8b712842008-06-11 16:50:36 -0400436done:
Chris Mason211588a2011-04-19 20:12:40 -0400437 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400438}
439
Christoph Hellwigb2950862008-12-02 09:54:17 -0500440static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400441{
442 struct btrfs_device *device;
443
444 device = container_of(work, struct btrfs_device, work);
445 run_scheduled_bios(device);
446}
447
Anand Jain4fde46f2015-06-17 21:10:48 +0800448
449void btrfs_free_stale_device(struct btrfs_device *cur_dev)
450{
451 struct btrfs_fs_devices *fs_devs;
452 struct btrfs_device *dev;
453
454 if (!cur_dev->name)
455 return;
456
457 list_for_each_entry(fs_devs, &fs_uuids, list) {
458 int del = 1;
459
460 if (fs_devs->opened)
461 continue;
462 if (fs_devs->seeding)
463 continue;
464
465 list_for_each_entry(dev, &fs_devs->devices, dev_list) {
466
467 if (dev == cur_dev)
468 continue;
469 if (!dev->name)
470 continue;
471
472 /*
473 * Todo: This won't be enough. What if the same device
474 * comes back (with new uuid and) with its mapper path?
475 * But for now, this does help as mostly an admin will
476 * either use mapper or non mapper path throughout.
477 */
478 rcu_read_lock();
479 del = strcmp(rcu_str_deref(dev->name),
480 rcu_str_deref(cur_dev->name));
481 rcu_read_unlock();
482 if (!del)
483 break;
484 }
485
486 if (!del) {
487 /* delete the stale device */
488 if (fs_devs->num_devices == 1) {
489 btrfs_sysfs_remove_fsid(fs_devs);
490 list_del(&fs_devs->list);
491 free_fs_devices(fs_devs);
492 } else {
493 fs_devs->num_devices--;
494 list_del(&dev->dev_list);
495 rcu_string_free(dev->name);
496 kfree(dev);
497 }
498 break;
499 }
500 }
501}
502
David Sterba60999ca2014-03-26 18:26:36 +0100503/*
504 * Add new device to list of registered devices
505 *
506 * Returns:
507 * 1 - first time device is seen
508 * 0 - device already known
509 * < 0 - error
510 */
Chris Masona1b32a52008-09-05 16:09:51 -0400511static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400512 struct btrfs_super_block *disk_super,
513 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
514{
515 struct btrfs_device *device;
516 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400517 struct rcu_string *name;
David Sterba60999ca2014-03-26 18:26:36 +0100518 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400519 u64 found_transid = btrfs_super_generation(disk_super);
520
521 fs_devices = find_fsid(disk_super->fsid);
522 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300523 fs_devices = alloc_fs_devices(disk_super->fsid);
524 if (IS_ERR(fs_devices))
525 return PTR_ERR(fs_devices);
526
Chris Mason8a4b83c2008-03-24 15:02:07 -0400527 list_add(&fs_devices->list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300528
Chris Mason8a4b83c2008-03-24 15:02:07 -0400529 device = NULL;
530 } else {
Chris Masona4437552008-04-18 10:29:38 -0400531 device = __find_device(&fs_devices->devices, devid,
532 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400533 }
Miao Xie443f24f2014-07-24 11:37:15 +0800534
Chris Mason8a4b83c2008-03-24 15:02:07 -0400535 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500536 if (fs_devices->opened)
537 return -EBUSY;
538
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300539 device = btrfs_alloc_device(NULL, &devid,
540 disk_super->dev_item.uuid);
541 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400542 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300543 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400544 }
Josef Bacik606686e2012-06-04 14:03:51 -0400545
546 name = rcu_string_strdup(path, GFP_NOFS);
547 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400548 kfree(device);
549 return -ENOMEM;
550 }
Josef Bacik606686e2012-06-04 14:03:51 -0400551 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200552
Chris Masone5e9a522009-06-10 15:17:02 -0400553 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000554 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100555 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400556 mutex_unlock(&fs_devices->device_list_mutex);
557
David Sterba60999ca2014-03-26 18:26:36 +0100558 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500559 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400560 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800561 /*
562 * When FS is already mounted.
563 * 1. If you are here and if the device->name is NULL that
564 * means this device was missing at time of FS mount.
565 * 2. If you are here and if the device->name is different
566 * from 'path' that means either
567 * a. The same device disappeared and reappeared with
568 * different name. or
569 * b. The missing-disk-which-was-replaced, has
570 * reappeared now.
571 *
572 * We must allow 1 and 2a above. But 2b would be a spurious
573 * and unintentional.
574 *
575 * Further in case of 1 and 2a above, the disk at 'path'
576 * would have missed some transaction when it was away and
577 * in case of 2a the stale bdev has to be updated as well.
578 * 2b must not be allowed at all time.
579 */
580
581 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700582 * For now, we do allow update to btrfs_fs_device through the
583 * btrfs dev scan cli after FS has been mounted. We're still
584 * tracking a problem where systems fail mount by subvolume id
585 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800586 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700587 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800588 /*
589 * That is if the FS is _not_ mounted and if you
590 * are here, that means there is more than one
591 * disk with same uuid and devid.We keep the one
592 * with larger generation number or the last-in if
593 * generation are equal.
594 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700595 return -EEXIST;
Anand Jain77bdae42014-07-03 18:22:06 +0800596 }
Anand Jainb96de002014-07-03 18:22:05 +0800597
Josef Bacik606686e2012-06-04 14:03:51 -0400598 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000599 if (!name)
600 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400601 rcu_string_free(device->name);
602 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500603 if (device->missing) {
604 fs_devices->missing_devices--;
605 device->missing = 0;
606 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400607 }
608
Anand Jain77bdae42014-07-03 18:22:06 +0800609 /*
610 * Unmount does not free the btrfs_device struct but would zero
611 * generation along with most of the other members. So just update
612 * it back. We need it to pick the disk with largest generation
613 * (as above).
614 */
615 if (!fs_devices->opened)
616 device->generation = found_transid;
617
Anand Jain4fde46f2015-06-17 21:10:48 +0800618 /*
619 * if there is new btrfs on an already registered device,
620 * then remove the stale device entry.
621 */
622 btrfs_free_stale_device(device);
623
Chris Mason8a4b83c2008-03-24 15:02:07 -0400624 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100625
626 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400627}
628
Yan Zhenge4404d62008-12-12 10:03:26 -0500629static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
630{
631 struct btrfs_fs_devices *fs_devices;
632 struct btrfs_device *device;
633 struct btrfs_device *orig_dev;
634
Ilya Dryomov2208a372013-08-12 14:33:03 +0300635 fs_devices = alloc_fs_devices(orig->fsid);
636 if (IS_ERR(fs_devices))
637 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500638
Miao Xieadbbb862014-09-03 21:35:42 +0800639 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400640 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500641
Xiao Guangrong46224702011-04-20 10:08:47 +0000642 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500643 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400644 struct rcu_string *name;
645
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300646 device = btrfs_alloc_device(NULL, &orig_dev->devid,
647 orig_dev->uuid);
648 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500649 goto error;
650
Josef Bacik606686e2012-06-04 14:03:51 -0400651 /*
652 * This is ok to do without rcu read locked because we hold the
653 * uuid mutex so nothing we touch in here is going to disappear.
654 */
Anand Jaine755f782014-06-30 17:12:47 +0800655 if (orig_dev->name) {
656 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
657 if (!name) {
658 kfree(device);
659 goto error;
660 }
661 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400662 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500663
Yan Zhenge4404d62008-12-12 10:03:26 -0500664 list_add(&device->dev_list, &fs_devices->devices);
665 device->fs_devices = fs_devices;
666 fs_devices->num_devices++;
667 }
Miao Xieadbbb862014-09-03 21:35:42 +0800668 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500669 return fs_devices;
670error:
Miao Xieadbbb862014-09-03 21:35:42 +0800671 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500672 free_fs_devices(fs_devices);
673 return ERR_PTR(-ENOMEM);
674}
675
Eric Sandeen9eaed212014-08-01 18:12:35 -0500676void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400677{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500678 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800679 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500680
Chris Masondfe25022008-05-13 13:46:40 -0400681 mutex_lock(&uuid_mutex);
682again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000683 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500684 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500685 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100686 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800687 (!latest_dev ||
688 device->generation > latest_dev->generation)) {
689 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500690 }
Yan Zheng2b820322008-11-17 21:11:30 -0500691 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500692 }
Yan Zheng2b820322008-11-17 21:11:30 -0500693
Stefan Behrens8dabb742012-11-06 13:15:27 +0100694 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
695 /*
696 * In the first step, keep the device which has
697 * the correct fsid and the devid that is used
698 * for the dev_replace procedure.
699 * In the second step, the dev_replace state is
700 * read from the device tree and it is known
701 * whether the procedure is really active or
702 * not, which means whether this device is
703 * used or whether it should be removed.
704 */
705 if (step == 0 || device->is_tgtdev_for_dev_replace) {
706 continue;
707 }
708 }
Yan Zheng2b820322008-11-17 21:11:30 -0500709 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100710 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500711 device->bdev = NULL;
712 fs_devices->open_devices--;
713 }
714 if (device->writeable) {
715 list_del_init(&device->dev_alloc_list);
716 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100717 if (!device->is_tgtdev_for_dev_replace)
718 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500719 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500720 list_del_init(&device->dev_list);
721 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400722 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500723 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400724 }
Yan Zheng2b820322008-11-17 21:11:30 -0500725
726 if (fs_devices->seed) {
727 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500728 goto again;
729 }
730
Miao Xie443f24f2014-07-24 11:37:15 +0800731 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500732
Chris Masondfe25022008-05-13 13:46:40 -0400733 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400734}
Chris Masona0af4692008-05-13 16:03:06 -0400735
Xiao Guangrong1f781602011-04-20 10:09:16 +0000736static void __free_device(struct work_struct *work)
737{
738 struct btrfs_device *device;
739
740 device = container_of(work, struct btrfs_device, rcu_work);
741
742 if (device->bdev)
743 blkdev_put(device->bdev, device->mode);
744
Josef Bacik606686e2012-06-04 14:03:51 -0400745 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000746 kfree(device);
747}
748
749static void free_device(struct rcu_head *head)
750{
751 struct btrfs_device *device;
752
753 device = container_of(head, struct btrfs_device, rcu);
754
755 INIT_WORK(&device->rcu_work, __free_device);
756 schedule_work(&device->rcu_work);
757}
758
Yan Zheng2b820322008-11-17 21:11:30 -0500759static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400760{
Sasha Levin2037a092015-05-12 19:31:37 -0400761 struct btrfs_device *device, *tmp;
Yan Zhenge4404d62008-12-12 10:03:26 -0500762
Yan Zheng2b820322008-11-17 21:11:30 -0500763 if (--fs_devices->opened > 0)
764 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400765
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000766 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -0400767 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000768 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400769 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000770
771 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400772 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000773
Ilya Dryomovf747cab2013-10-10 20:37:29 +0300774 if (device->writeable &&
775 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500776 list_del_init(&device->dev_alloc_list);
777 fs_devices->rw_devices--;
778 }
779
Josef Bacik726551e2013-08-26 13:45:53 -0400780 if (device->missing)
781 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000782
Ilya Dryomova1e87802013-08-12 14:33:04 +0300783 new_device = btrfs_alloc_device(NULL, &device->devid,
784 device->uuid);
785 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400786
787 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400788 if (device->name) {
789 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300790 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400791 rcu_assign_pointer(new_device->name, name);
792 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300793
Xiao Guangrong1f781602011-04-20 10:09:16 +0000794 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300795 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000796
797 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400798 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000799 mutex_unlock(&fs_devices->device_list_mutex);
800
Yan Zhenge4404d62008-12-12 10:03:26 -0500801 WARN_ON(fs_devices->open_devices);
802 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500803 fs_devices->opened = 0;
804 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500805
Chris Mason8a4b83c2008-03-24 15:02:07 -0400806 return 0;
807}
808
Yan Zheng2b820322008-11-17 21:11:30 -0500809int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
810{
Yan Zhenge4404d62008-12-12 10:03:26 -0500811 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500812 int ret;
813
814 mutex_lock(&uuid_mutex);
815 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500816 if (!fs_devices->opened) {
817 seed_devices = fs_devices->seed;
818 fs_devices->seed = NULL;
819 }
Yan Zheng2b820322008-11-17 21:11:30 -0500820 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500821
822 while (seed_devices) {
823 fs_devices = seed_devices;
824 seed_devices = fs_devices->seed;
825 __btrfs_close_devices(fs_devices);
826 free_fs_devices(fs_devices);
827 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000828 /*
829 * Wait for rcu kworkers under __btrfs_close_devices
830 * to finish all blkdev_puts so device is really
831 * free when umount is done.
832 */
833 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500834 return ret;
835}
836
Yan Zhenge4404d62008-12-12 10:03:26 -0500837static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
838 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400839{
Josef Bacikd5e20032011-08-04 14:52:27 +0000840 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400841 struct block_device *bdev;
842 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400843 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +0800844 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -0400845 struct buffer_head *bh;
846 struct btrfs_super_block *disk_super;
Chris Masona0af4692008-05-13 16:03:06 -0400847 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500848 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400849 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400850
Tejun Heod4d77622010-11-13 11:55:18 +0100851 flags |= FMODE_EXCL;
852
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500853 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400854 if (device->bdev)
855 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400856 if (!device->name)
857 continue;
858
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000859 /* Just open everything we can; ignore failures here */
860 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
861 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100862 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400863
864 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000865 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400866 if (devid != device->devid)
867 goto error_brelse;
868
Yan Zheng2b820322008-11-17 21:11:30 -0500869 if (memcmp(device->uuid, disk_super->dev_item.uuid,
870 BTRFS_UUID_SIZE))
871 goto error_brelse;
872
873 device->generation = btrfs_super_generation(disk_super);
Miao Xie443f24f2014-07-24 11:37:15 +0800874 if (!latest_dev ||
875 device->generation > latest_dev->generation)
876 latest_dev = device;
Chris Masona0af4692008-05-13 16:03:06 -0400877
Yan Zheng2b820322008-11-17 21:11:30 -0500878 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
879 device->writeable = 0;
880 } else {
881 device->writeable = !bdev_read_only(bdev);
882 seeding = 0;
883 }
884
Josef Bacikd5e20032011-08-04 14:52:27 +0000885 q = bdev_get_queue(bdev);
Miao Xie90180da2014-09-03 21:35:30 +0800886 if (blk_queue_discard(q))
Josef Bacikd5e20032011-08-04 14:52:27 +0000887 device->can_discard = 1;
Josef Bacikd5e20032011-08-04 14:52:27 +0000888
Chris Mason8a4b83c2008-03-24 15:02:07 -0400889 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400890 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500891 device->mode = flags;
892
Chris Masonc2898112009-06-10 09:51:32 -0400893 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
894 fs_devices->rotating = 1;
895
Chris Masona0af4692008-05-13 16:03:06 -0400896 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300897 if (device->writeable &&
898 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500899 fs_devices->rw_devices++;
900 list_add(&device->dev_alloc_list,
901 &fs_devices->alloc_list);
902 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000903 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400904 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400905
Chris Masona0af4692008-05-13 16:03:06 -0400906error_brelse:
907 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100908 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400909 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400910 }
Chris Masona0af4692008-05-13 16:03:06 -0400911 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300912 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400913 goto out;
914 }
Yan Zheng2b820322008-11-17 21:11:30 -0500915 fs_devices->seeding = seeding;
916 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +0800917 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -0500918 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400919out:
Yan Zheng2b820322008-11-17 21:11:30 -0500920 return ret;
921}
922
923int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500924 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500925{
926 int ret;
927
928 mutex_lock(&uuid_mutex);
929 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500930 fs_devices->opened++;
931 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500932 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500933 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500934 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400935 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400936 return ret;
937}
938
David Sterba6f60cbd2013-02-15 11:31:02 -0700939/*
940 * Look for a btrfs signature on a device. This may be called out of the mount path
941 * and we are not allowed to call set_blocksize during the scan. The superblock
942 * is read via pagecache
943 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500944int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400945 struct btrfs_fs_devices **fs_devices_ret)
946{
947 struct btrfs_super_block *disk_super;
948 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700949 struct page *page;
950 void *p;
951 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400952 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400953 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400954 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700955 u64 bytenr;
956 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400957
David Sterba6f60cbd2013-02-15 11:31:02 -0700958 /*
959 * we would like to check all the supers, but that would make
960 * a btrfs mount succeed after a mkfs from a different FS.
961 * So, we need to add a special mount option to scan for
962 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
963 */
964 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100965 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500966 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700967
968 bdev = blkdev_get_by_path(path, flags, holder);
969
970 if (IS_ERR(bdev)) {
971 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100972 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700973 }
974
975 /* make sure our super fits in the device */
976 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
977 goto error_bdev_put;
978
979 /* make sure our super fits in the page */
980 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
981 goto error_bdev_put;
982
983 /* make sure our super doesn't straddle pages on disk */
984 index = bytenr >> PAGE_CACHE_SHIFT;
985 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
986 goto error_bdev_put;
987
988 /* pull in the page with our super */
989 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
990 index, GFP_NOFS);
991
992 if (IS_ERR_OR_NULL(page))
993 goto error_bdev_put;
994
995 p = kmap(page);
996
997 /* align our pointer to the offset of the super block */
998 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
999
1000 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +08001001 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -07001002 goto error_unmap;
1003
Xiao Guangronga3438322010-01-06 11:48:18 +00001004 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -04001005 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -04001006 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -07001007
Chris Mason8a4b83c2008-03-24 15:02:07 -04001008 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +01001009 if (ret > 0) {
1010 if (disk_super->label[0]) {
1011 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
1012 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
1013 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
1014 } else {
1015 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
1016 }
1017
1018 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
1019 ret = 0;
1020 }
Josef Bacik02db0842012-06-21 16:03:58 -04001021 if (!ret && fs_devices_ret)
1022 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001023
1024error_unmap:
1025 kunmap(page);
1026 page_cache_release(page);
1027
1028error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001029 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001030error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001031 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001032 return ret;
1033}
Chris Mason0b86a832008-03-24 15:01:56 -04001034
Miao Xie6d07bce2011-01-05 10:07:31 +00001035/* helper to account the used device space in the range */
1036int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1037 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -04001038{
1039 struct btrfs_key key;
1040 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +00001041 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001042 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +00001043 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001044 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +00001045 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001046 struct extent_buffer *l;
1047
Miao Xie6d07bce2011-01-05 10:07:31 +00001048 *length = 0;
1049
Stefan Behrens63a212a2012-11-05 18:29:28 +01001050 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +00001051 return 0;
1052
Yan Zheng2b820322008-11-17 21:11:30 -05001053 path = btrfs_alloc_path();
1054 if (!path)
1055 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001056 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -04001057
Chris Mason0b86a832008-03-24 15:01:56 -04001058 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001059 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001060 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001061
1062 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001063 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001064 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001065 if (ret > 0) {
1066 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1067 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001068 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001069 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001070
Chris Mason0b86a832008-03-24 15:01:56 -04001071 while (1) {
1072 l = path->nodes[0];
1073 slot = path->slots[0];
1074 if (slot >= btrfs_header_nritems(l)) {
1075 ret = btrfs_next_leaf(root, path);
1076 if (ret == 0)
1077 continue;
1078 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001079 goto out;
1080
1081 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001082 }
1083 btrfs_item_key_to_cpu(l, &key, slot);
1084
1085 if (key.objectid < device->devid)
1086 goto next;
1087
1088 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001089 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001090
David Sterba962a2982014-06-04 18:41:45 +02001091 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001092 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001093
Chris Mason0b86a832008-03-24 15:01:56 -04001094 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001095 extent_end = key.offset + btrfs_dev_extent_length(l,
1096 dev_extent);
1097 if (key.offset <= start && extent_end > end) {
1098 *length = end - start + 1;
1099 break;
1100 } else if (key.offset <= start && extent_end > start)
1101 *length += extent_end - start;
1102 else if (key.offset > start && extent_end <= end)
1103 *length += extent_end - key.offset;
1104 else if (key.offset > start && key.offset <= end) {
1105 *length += end - key.offset + 1;
1106 break;
1107 } else if (key.offset > end)
1108 break;
1109
1110next:
1111 path->slots[0]++;
1112 }
1113 ret = 0;
1114out:
1115 btrfs_free_path(path);
1116 return ret;
1117}
1118
Jeff Mahoney499f3772015-06-15 09:41:17 -04001119static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001120 struct btrfs_device *device,
1121 u64 *start, u64 len)
1122{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001123 struct btrfs_fs_info *fs_info = device->dev_root->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001124 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001125 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001126 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001127 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001128
Jeff Mahoney499f3772015-06-15 09:41:17 -04001129 if (transaction)
1130 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001131again:
1132 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001133 struct map_lookup *map;
1134 int i;
1135
1136 map = (struct map_lookup *)em->bdev;
1137 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001138 u64 end;
1139
Josef Bacik6df9a952013-06-27 13:22:46 -04001140 if (map->stripes[i].dev != device)
1141 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001142 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001143 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001144 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001145 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001146 /*
1147 * Make sure that while processing the pinned list we do
1148 * not override our *start with a lower value, because
1149 * we can have pinned chunks that fall within this
1150 * device hole and that have lower physical addresses
1151 * than the pending chunks we processed before. If we
1152 * do not take this special care we can end up getting
1153 * 2 pending chunks that start at the same physical
1154 * device offsets because the end offset of a pinned
1155 * chunk can be equal to the start offset of some
1156 * pending chunk.
1157 */
1158 end = map->stripes[i].physical + em->orig_block_len;
1159 if (end > *start) {
1160 *start = end;
1161 ret = 1;
1162 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001163 }
1164 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001165 if (search_list != &fs_info->pinned_chunks) {
1166 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001167 goto again;
1168 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001169
1170 return ret;
1171}
1172
1173
Chris Mason0b86a832008-03-24 15:01:56 -04001174/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001175 * find_free_dev_extent_start - find free space in the specified device
1176 * @device: the device which we search the free space in
1177 * @num_bytes: the size of the free space that we need
1178 * @search_start: the position from which to begin the search
1179 * @start: store the start of the free space.
1180 * @len: the size of the free space. that we find, or the size
1181 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001182 *
Chris Mason0b86a832008-03-24 15:01:56 -04001183 * this uses a pretty simple search, the expectation is that it is
1184 * called very infrequently and that a given device has a small number
1185 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001186 *
1187 * @start is used to store the start of the free space if we find. But if we
1188 * don't find suitable free space, it will be used to store the start position
1189 * of the max free space.
1190 *
1191 * @len is used to store the size of the free space that we find.
1192 * But if we don't find suitable free space, it is used to store the size of
1193 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001194 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001195int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1196 struct btrfs_device *device, u64 num_bytes,
1197 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001198{
1199 struct btrfs_key key;
1200 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001201 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001202 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001203 u64 hole_size;
1204 u64 max_hole_start;
1205 u64 max_hole_size;
1206 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001207 u64 search_end = device->total_bytes;
1208 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001209 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001210 struct extent_buffer *l;
1211
Josef Bacik6df9a952013-06-27 13:22:46 -04001212 path = btrfs_alloc_path();
1213 if (!path)
1214 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001215
Miao Xie7bfc8372011-01-05 10:07:26 +00001216 max_hole_start = search_start;
1217 max_hole_size = 0;
1218
Zhao Leif2ab7612015-02-16 18:52:17 +08001219again:
Stefan Behrens63a212a2012-11-05 18:29:28 +01001220 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001221 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001222 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001223 }
1224
Miao Xie7bfc8372011-01-05 10:07:26 +00001225 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001226 path->search_commit_root = 1;
1227 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001228
Chris Mason0b86a832008-03-24 15:01:56 -04001229 key.objectid = device->devid;
1230 key.offset = search_start;
1231 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001232
Li Zefan125ccb02011-12-08 15:07:24 +08001233 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001234 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001235 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001236 if (ret > 0) {
1237 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1238 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001239 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001240 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001241
Chris Mason0b86a832008-03-24 15:01:56 -04001242 while (1) {
1243 l = path->nodes[0];
1244 slot = path->slots[0];
1245 if (slot >= btrfs_header_nritems(l)) {
1246 ret = btrfs_next_leaf(root, path);
1247 if (ret == 0)
1248 continue;
1249 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001250 goto out;
1251
1252 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001253 }
1254 btrfs_item_key_to_cpu(l, &key, slot);
1255
1256 if (key.objectid < device->devid)
1257 goto next;
1258
1259 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001260 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001261
David Sterba962a2982014-06-04 18:41:45 +02001262 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001263 goto next;
1264
Miao Xie7bfc8372011-01-05 10:07:26 +00001265 if (key.offset > search_start) {
1266 hole_size = key.offset - search_start;
1267
Josef Bacik6df9a952013-06-27 13:22:46 -04001268 /*
1269 * Have to check before we set max_hole_start, otherwise
1270 * we could end up sending back this offset anyway.
1271 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001272 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001273 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001274 hole_size)) {
1275 if (key.offset >= search_start) {
1276 hole_size = key.offset - search_start;
1277 } else {
1278 WARN_ON_ONCE(1);
1279 hole_size = 0;
1280 }
1281 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001282
Miao Xie7bfc8372011-01-05 10:07:26 +00001283 if (hole_size > max_hole_size) {
1284 max_hole_start = search_start;
1285 max_hole_size = hole_size;
1286 }
1287
1288 /*
1289 * If this free space is greater than which we need,
1290 * it must be the max free space that we have found
1291 * until now, so max_hole_start must point to the start
1292 * of this free space and the length of this free space
1293 * is stored in max_hole_size. Thus, we return
1294 * max_hole_start and max_hole_size and go back to the
1295 * caller.
1296 */
1297 if (hole_size >= num_bytes) {
1298 ret = 0;
1299 goto out;
1300 }
1301 }
1302
Chris Mason0b86a832008-03-24 15:01:56 -04001303 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001304 extent_end = key.offset + btrfs_dev_extent_length(l,
1305 dev_extent);
1306 if (extent_end > search_start)
1307 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001308next:
1309 path->slots[0]++;
1310 cond_resched();
1311 }
Chris Mason0b86a832008-03-24 15:01:56 -04001312
liubo38c01b92011-08-02 02:39:03 +00001313 /*
1314 * At this point, search_start should be the end of
1315 * allocated dev extents, and when shrinking the device,
1316 * search_end may be smaller than search_start.
1317 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001318 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001319 hole_size = search_end - search_start;
1320
Jeff Mahoney499f3772015-06-15 09:41:17 -04001321 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001322 hole_size)) {
1323 btrfs_release_path(path);
1324 goto again;
1325 }
Chris Mason0b86a832008-03-24 15:01:56 -04001326
Zhao Leif2ab7612015-02-16 18:52:17 +08001327 if (hole_size > max_hole_size) {
1328 max_hole_start = search_start;
1329 max_hole_size = hole_size;
1330 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001331 }
1332
Miao Xie7bfc8372011-01-05 10:07:26 +00001333 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001334 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001335 ret = -ENOSPC;
1336 else
1337 ret = 0;
1338
1339out:
Yan Zheng2b820322008-11-17 21:11:30 -05001340 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001341 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001342 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001343 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001344 return ret;
1345}
1346
Jeff Mahoney499f3772015-06-15 09:41:17 -04001347int find_free_dev_extent(struct btrfs_trans_handle *trans,
1348 struct btrfs_device *device, u64 num_bytes,
1349 u64 *start, u64 *len)
1350{
1351 struct btrfs_root *root = device->dev_root;
1352 u64 search_start;
1353
1354 /* FIXME use last free of some kind */
1355
1356 /*
1357 * we don't want to overwrite the superblock on the drive,
1358 * so we make sure to start at an offset of at least 1MB
1359 */
1360 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
1361 return find_free_dev_extent_start(trans->transaction, device,
1362 num_bytes, search_start, start, len);
1363}
1364
Christoph Hellwigb2950862008-12-02 09:54:17 -05001365static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001366 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001367 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001368{
1369 int ret;
1370 struct btrfs_path *path;
1371 struct btrfs_root *root = device->dev_root;
1372 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001373 struct btrfs_key found_key;
1374 struct extent_buffer *leaf = NULL;
1375 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001376
1377 path = btrfs_alloc_path();
1378 if (!path)
1379 return -ENOMEM;
1380
1381 key.objectid = device->devid;
1382 key.offset = start;
1383 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001384again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001385 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001386 if (ret > 0) {
1387 ret = btrfs_previous_item(root, path, key.objectid,
1388 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001389 if (ret)
1390 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001391 leaf = path->nodes[0];
1392 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1393 extent = btrfs_item_ptr(leaf, path->slots[0],
1394 struct btrfs_dev_extent);
1395 BUG_ON(found_key.offset > start || found_key.offset +
1396 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001397 key = found_key;
1398 btrfs_release_path(path);
1399 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001400 } else if (ret == 0) {
1401 leaf = path->nodes[0];
1402 extent = btrfs_item_ptr(leaf, path->slots[0],
1403 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001404 } else {
Anand Jaina4553fe2015-09-25 14:43:01 +08001405 btrfs_std_error(root->fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001406 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001407 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001408
Miao Xie2196d6e2014-09-03 21:35:41 +08001409 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1410
Chris Mason8f18cf12008-04-25 16:53:30 -04001411 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001412 if (ret) {
Anand Jaina4553fe2015-09-25 14:43:01 +08001413 btrfs_std_error(root->fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001414 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001415 } else {
1416 trans->transaction->have_free_bgs = 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001417 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001418out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001419 btrfs_free_path(path);
1420 return ret;
1421}
1422
Eric Sandeen48a3b632013-04-25 20:41:01 +00001423static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1424 struct btrfs_device *device,
1425 u64 chunk_tree, u64 chunk_objectid,
1426 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001427{
1428 int ret;
1429 struct btrfs_path *path;
1430 struct btrfs_root *root = device->dev_root;
1431 struct btrfs_dev_extent *extent;
1432 struct extent_buffer *leaf;
1433 struct btrfs_key key;
1434
Chris Masondfe25022008-05-13 13:46:40 -04001435 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001436 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001437 path = btrfs_alloc_path();
1438 if (!path)
1439 return -ENOMEM;
1440
Chris Mason0b86a832008-03-24 15:01:56 -04001441 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001442 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001443 key.type = BTRFS_DEV_EXTENT_KEY;
1444 ret = btrfs_insert_empty_item(trans, root, path, &key,
1445 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001446 if (ret)
1447 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001448
1449 leaf = path->nodes[0];
1450 extent = btrfs_item_ptr(leaf, path->slots[0],
1451 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001452 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1453 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1454 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1455
1456 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001457 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001458
Chris Mason0b86a832008-03-24 15:01:56 -04001459 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1460 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001461out:
Chris Mason0b86a832008-03-24 15:01:56 -04001462 btrfs_free_path(path);
1463 return ret;
1464}
1465
Josef Bacik6df9a952013-06-27 13:22:46 -04001466static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001467{
Josef Bacik6df9a952013-06-27 13:22:46 -04001468 struct extent_map_tree *em_tree;
1469 struct extent_map *em;
1470 struct rb_node *n;
1471 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001472
Josef Bacik6df9a952013-06-27 13:22:46 -04001473 em_tree = &fs_info->mapping_tree.map_tree;
1474 read_lock(&em_tree->lock);
1475 n = rb_last(&em_tree->map);
1476 if (n) {
1477 em = rb_entry(n, struct extent_map, rb_node);
1478 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001479 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001480 read_unlock(&em_tree->lock);
1481
Chris Mason0b86a832008-03-24 15:01:56 -04001482 return ret;
1483}
1484
Ilya Dryomov53f10652013-08-12 14:33:01 +03001485static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1486 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001487{
1488 int ret;
1489 struct btrfs_key key;
1490 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001491 struct btrfs_path *path;
1492
Yan Zheng2b820322008-11-17 21:11:30 -05001493 path = btrfs_alloc_path();
1494 if (!path)
1495 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001496
1497 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1498 key.type = BTRFS_DEV_ITEM_KEY;
1499 key.offset = (u64)-1;
1500
Ilya Dryomov53f10652013-08-12 14:33:01 +03001501 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001502 if (ret < 0)
1503 goto error;
1504
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001505 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001506
Ilya Dryomov53f10652013-08-12 14:33:01 +03001507 ret = btrfs_previous_item(fs_info->chunk_root, path,
1508 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001509 BTRFS_DEV_ITEM_KEY);
1510 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001511 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001512 } else {
1513 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1514 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001515 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001516 }
1517 ret = 0;
1518error:
Yan Zheng2b820322008-11-17 21:11:30 -05001519 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001520 return ret;
1521}
1522
1523/*
1524 * the device information is stored in the chunk root
1525 * the btrfs_device struct should be fully filled in
1526 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001527static int btrfs_add_device(struct btrfs_trans_handle *trans,
1528 struct btrfs_root *root,
1529 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001530{
1531 int ret;
1532 struct btrfs_path *path;
1533 struct btrfs_dev_item *dev_item;
1534 struct extent_buffer *leaf;
1535 struct btrfs_key key;
1536 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001537
1538 root = root->fs_info->chunk_root;
1539
1540 path = btrfs_alloc_path();
1541 if (!path)
1542 return -ENOMEM;
1543
Chris Mason0b86a832008-03-24 15:01:56 -04001544 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1545 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001546 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001547
1548 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001549 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001550 if (ret)
1551 goto out;
1552
1553 leaf = path->nodes[0];
1554 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1555
1556 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001557 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001558 btrfs_set_device_type(leaf, dev_item, device->type);
1559 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1560 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1561 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001562 btrfs_set_device_total_bytes(leaf, dev_item,
1563 btrfs_device_get_disk_total_bytes(device));
1564 btrfs_set_device_bytes_used(leaf, dev_item,
1565 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001566 btrfs_set_device_group(leaf, dev_item, 0);
1567 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1568 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001569 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001570
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001571 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001572 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001573 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001574 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001575 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001576
Yan Zheng2b820322008-11-17 21:11:30 -05001577 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001578out:
1579 btrfs_free_path(path);
1580 return ret;
1581}
Chris Mason8f18cf12008-04-25 16:53:30 -04001582
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001583/*
1584 * Function to update ctime/mtime for a given device path.
1585 * Mainly used for ctime/mtime based probe like libblkid.
1586 */
1587static void update_dev_time(char *path_name)
1588{
1589 struct file *filp;
1590
1591 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001592 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001593 return;
1594 file_update_time(filp);
1595 filp_close(filp, NULL);
1596 return;
1597}
1598
Chris Masona061fc82008-05-07 11:43:44 -04001599static int btrfs_rm_dev_item(struct btrfs_root *root,
1600 struct btrfs_device *device)
1601{
1602 int ret;
1603 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001604 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001605 struct btrfs_trans_handle *trans;
1606
1607 root = root->fs_info->chunk_root;
1608
1609 path = btrfs_alloc_path();
1610 if (!path)
1611 return -ENOMEM;
1612
Yan, Zhenga22285a2010-05-16 10:48:46 -04001613 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001614 if (IS_ERR(trans)) {
1615 btrfs_free_path(path);
1616 return PTR_ERR(trans);
1617 }
Chris Masona061fc82008-05-07 11:43:44 -04001618 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1619 key.type = BTRFS_DEV_ITEM_KEY;
1620 key.offset = device->devid;
1621
1622 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1623 if (ret < 0)
1624 goto out;
1625
1626 if (ret > 0) {
1627 ret = -ENOENT;
1628 goto out;
1629 }
1630
1631 ret = btrfs_del_item(trans, root, path);
1632 if (ret)
1633 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001634out:
1635 btrfs_free_path(path);
1636 btrfs_commit_transaction(trans, root);
1637 return ret;
1638}
1639
1640int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1641{
1642 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001643 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001644 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001645 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001646 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001647 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001648 u64 all_avail;
1649 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001650 u64 num_devices;
1651 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001652 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001653 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001654 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001655
Chris Masona061fc82008-05-07 11:43:44 -04001656 mutex_lock(&uuid_mutex);
1657
Miao Xiede98ced2013-01-29 10:13:12 +00001658 do {
1659 seq = read_seqbegin(&root->fs_info->profiles_lock);
1660
1661 all_avail = root->fs_info->avail_data_alloc_bits |
1662 root->fs_info->avail_system_alloc_bits |
1663 root->fs_info->avail_metadata_alloc_bits;
1664 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001665
Stefan Behrens8dabb742012-11-06 13:15:27 +01001666 num_devices = root->fs_info->fs_devices->num_devices;
1667 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1668 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1669 WARN_ON(num_devices < 1);
1670 num_devices--;
1671 }
1672 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1673
1674 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001675 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001676 goto out;
1677 }
1678
Stefan Behrens8dabb742012-11-06 13:15:27 +01001679 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001680 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001681 goto out;
1682 }
1683
David Woodhouse53b381b2013-01-29 18:40:14 -05001684 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1685 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001686 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001687 goto out;
1688 }
1689 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1690 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001691 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001692 goto out;
1693 }
1694
Chris Masondfe25022008-05-13 13:46:40 -04001695 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001696 struct list_head *devices;
1697 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001698
Chris Masondfe25022008-05-13 13:46:40 -04001699 device = NULL;
1700 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001701 /*
1702 * It is safe to read the devices since the volume_mutex
1703 * is held.
1704 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001705 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001706 if (tmp->in_fs_metadata &&
1707 !tmp->is_tgtdev_for_dev_replace &&
1708 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001709 device = tmp;
1710 break;
1711 }
1712 }
1713 bdev = NULL;
1714 bh = NULL;
1715 disk_super = NULL;
1716 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001717 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001718 goto out;
1719 }
Chris Masondfe25022008-05-13 13:46:40 -04001720 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001721 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001722 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001723 root->fs_info->bdev_holder, 0,
1724 &bdev, &bh);
1725 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001726 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001727 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001728 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001729 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001730 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001731 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001732 if (!device) {
1733 ret = -ENOENT;
1734 goto error_brelse;
1735 }
Chris Masondfe25022008-05-13 13:46:40 -04001736 }
Yan Zheng2b820322008-11-17 21:11:30 -05001737
Stefan Behrens63a212a2012-11-05 18:29:28 +01001738 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001739 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001740 goto error_brelse;
1741 }
1742
Yan Zheng2b820322008-11-17 21:11:30 -05001743 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001744 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001745 goto error_brelse;
1746 }
1747
1748 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001749 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001750 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001751 device->fs_devices->rw_devices--;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001752 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001753 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001754 }
Chris Masona061fc82008-05-07 11:43:44 -04001755
Carey Underwoodd7901552013-03-04 16:37:06 -06001756 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001757 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001758 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001759 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001760 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001761
Stefan Behrens63a212a2012-11-05 18:29:28 +01001762 /*
1763 * TODO: the superblock still includes this device in its num_devices
1764 * counter although write_all_supers() is not locked out. This
1765 * could give a filesystem state which requires a degraded mount.
1766 */
Chris Masona061fc82008-05-07 11:43:44 -04001767 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1768 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001769 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001770
Yan Zheng2b820322008-11-17 21:11:30 -05001771 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001772 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001773
1774 /*
1775 * the device list mutex makes sure that we don't change
1776 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001777 * the device supers. Whoever is writing all supers, should
1778 * lock the device list mutex before getting the number of
1779 * devices in the super block (super_copy). Conversely,
1780 * whoever updates the number of devices in the super block
1781 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001782 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001783
1784 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001785 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001786 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001787
Yan Zhenge4404d62008-12-12 10:03:26 -05001788 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001789 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001790
Chris Masoncd02dca2010-12-13 14:56:23 -05001791 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001792 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001793
Yan Zheng2b820322008-11-17 21:11:30 -05001794 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1795 struct btrfs_device, dev_list);
1796 if (device->bdev == root->fs_info->sb->s_bdev)
1797 root->fs_info->sb->s_bdev = next_device->bdev;
1798 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1799 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1800
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001801 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001802 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001803 /* remove sysfs entry */
Anand Jain32576042015-08-14 18:32:49 +08001804 btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001805 }
Anand Jain99994cd2014-06-03 11:36:00 +08001806
Xiao Guangrong1f781602011-04-20 10:09:16 +00001807 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001808
David Sterba6c417612011-04-13 15:41:04 +02001809 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1810 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001811 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001812
Xiao Guangrong1f781602011-04-20 10:09:16 +00001813 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001814 struct btrfs_fs_devices *fs_devices;
1815 fs_devices = root->fs_info->fs_devices;
1816 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001817 if (fs_devices->seed == cur_devices) {
1818 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001819 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001820 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001821 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001822 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001823 cur_devices->seed = NULL;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001824 __btrfs_close_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001825 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001826 }
1827
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001828 root->fs_info->num_tolerated_disk_barrier_failures =
1829 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1830
Yan Zheng2b820322008-11-17 21:11:30 -05001831 /*
1832 * at this point, the device is zero sized. We want to
1833 * remove it from the devices list and zero out the old super
1834 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001835 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001836 u64 bytenr;
1837 int i;
1838
Chris Masondfe25022008-05-13 13:46:40 -04001839 /* make sure this device isn't detected as part of
1840 * the FS anymore
1841 */
1842 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1843 set_buffer_dirty(bh);
1844 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001845
1846 /* clear the mirror copies of super block on the disk
1847 * being removed, 0th copy is been taken care above and
1848 * the below would take of the rest
1849 */
1850 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1851 bytenr = btrfs_sb_offset(i);
1852 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1853 i_size_read(bdev->bd_inode))
1854 break;
1855
1856 brelse(bh);
1857 bh = __bread(bdev, bytenr / 4096,
1858 BTRFS_SUPER_INFO_SIZE);
1859 if (!bh)
1860 continue;
1861
1862 disk_super = (struct btrfs_super_block *)bh->b_data;
1863
1864 if (btrfs_super_bytenr(disk_super) != bytenr ||
1865 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1866 continue;
1867 }
1868 memset(&disk_super->magic, 0,
1869 sizeof(disk_super->magic));
1870 set_buffer_dirty(bh);
1871 sync_dirty_buffer(bh);
1872 }
Chris Masondfe25022008-05-13 13:46:40 -04001873 }
Chris Masona061fc82008-05-07 11:43:44 -04001874
Chris Masona061fc82008-05-07 11:43:44 -04001875 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001876
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001877 if (bdev) {
1878 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001879 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001880
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001881 /* Update ctime/mtime for device path for libblkid */
1882 update_dev_time(device_path);
1883 }
1884
Chris Masona061fc82008-05-07 11:43:44 -04001885error_brelse:
1886 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001887 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001888 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001889out:
1890 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001891 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001892error_undo:
1893 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001894 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001895 list_add(&device->dev_alloc_list,
1896 &root->fs_info->fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001897 device->fs_devices->rw_devices++;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001898 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001899 }
1900 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001901}
1902
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001903void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
1904 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01001905{
Anand Jaind51908c2014-08-13 14:24:19 +08001906 struct btrfs_fs_devices *fs_devices;
1907
Stefan Behrense93c89c2012-11-05 17:33:06 +01001908 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001909
Anand Jain25e8e912014-08-20 10:56:56 +08001910 /*
1911 * in case of fs with no seed, srcdev->fs_devices will point
1912 * to fs_devices of fs_info. However when the dev being replaced is
1913 * a seed dev it will point to the seed's local fs_devices. In short
1914 * srcdev will have its correct fs_devices in both the cases.
1915 */
1916 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08001917
Stefan Behrense93c89c2012-11-05 17:33:06 +01001918 list_del_rcu(&srcdev->dev_list);
1919 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08001920 fs_devices->num_devices--;
Miao Xie82372bc2014-09-03 21:35:44 +08001921 if (srcdev->missing)
Anand Jaind51908c2014-08-13 14:24:19 +08001922 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001923
Miao Xie82372bc2014-09-03 21:35:44 +08001924 if (srcdev->writeable) {
1925 fs_devices->rw_devices--;
1926 /* zero out the old super if it is writable */
1927 btrfs_scratch_superblock(srcdev);
Ilya Dryomov13572722013-10-02 20:41:01 +03001928 }
1929
Miao Xie82372bc2014-09-03 21:35:44 +08001930 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08001931 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001932}
1933
1934void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
1935 struct btrfs_device *srcdev)
1936{
1937 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001938
Stefan Behrense93c89c2012-11-05 17:33:06 +01001939 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001940
1941 /*
1942 * unless fs_devices is seed fs, num_devices shouldn't go
1943 * zero
1944 */
1945 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
1946
1947 /* if this is no devs we rather delete the fs_devices */
1948 if (!fs_devices->num_devices) {
1949 struct btrfs_fs_devices *tmp_fs_devices;
1950
1951 tmp_fs_devices = fs_info->fs_devices;
1952 while (tmp_fs_devices) {
1953 if (tmp_fs_devices->seed == fs_devices) {
1954 tmp_fs_devices->seed = fs_devices->seed;
1955 break;
1956 }
1957 tmp_fs_devices = tmp_fs_devices->seed;
1958 }
1959 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08001960 __btrfs_close_devices(fs_devices);
1961 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001962 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01001963}
1964
1965void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1966 struct btrfs_device *tgtdev)
1967{
1968 struct btrfs_device *next_device;
1969
Miao Xie67a2c452014-09-03 21:35:43 +08001970 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001971 WARN_ON(!tgtdev);
1972 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jaind2ff1b22015-03-10 06:38:42 +08001973
Anand Jain32576042015-08-14 18:32:49 +08001974 btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08001975
Stefan Behrense93c89c2012-11-05 17:33:06 +01001976 if (tgtdev->bdev) {
1977 btrfs_scratch_superblock(tgtdev);
1978 fs_info->fs_devices->open_devices--;
1979 }
1980 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001981
1982 next_device = list_entry(fs_info->fs_devices->devices.next,
1983 struct btrfs_device, dev_list);
1984 if (tgtdev->bdev == fs_info->sb->s_bdev)
1985 fs_info->sb->s_bdev = next_device->bdev;
1986 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1987 fs_info->fs_devices->latest_bdev = next_device->bdev;
1988 list_del_rcu(&tgtdev->dev_list);
1989
1990 call_rcu(&tgtdev->rcu, free_device);
1991
1992 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08001993 mutex_unlock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001994}
1995
Eric Sandeen48a3b632013-04-25 20:41:01 +00001996static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1997 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001998{
1999 int ret = 0;
2000 struct btrfs_super_block *disk_super;
2001 u64 devid;
2002 u8 *dev_uuid;
2003 struct block_device *bdev;
2004 struct buffer_head *bh;
2005
2006 *device = NULL;
2007 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
2008 root->fs_info->bdev_holder, 0, &bdev, &bh);
2009 if (ret)
2010 return ret;
2011 disk_super = (struct btrfs_super_block *)bh->b_data;
2012 devid = btrfs_stack_device_id(&disk_super->dev_item);
2013 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002014 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002015 disk_super->fsid);
2016 brelse(bh);
2017 if (!*device)
2018 ret = -ENOENT;
2019 blkdev_put(bdev, FMODE_READ);
2020 return ret;
2021}
2022
2023int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
2024 char *device_path,
2025 struct btrfs_device **device)
2026{
2027 *device = NULL;
2028 if (strcmp(device_path, "missing") == 0) {
2029 struct list_head *devices;
2030 struct btrfs_device *tmp;
2031
2032 devices = &root->fs_info->fs_devices->devices;
2033 /*
2034 * It is safe to read the devices since the volume_mutex
2035 * is held by the caller.
2036 */
2037 list_for_each_entry(tmp, devices, dev_list) {
2038 if (tmp->in_fs_metadata && !tmp->bdev) {
2039 *device = tmp;
2040 break;
2041 }
2042 }
2043
Anand Jaind74a6252015-08-14 18:32:56 +08002044 if (!*device)
2045 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002046
2047 return 0;
2048 } else {
2049 return btrfs_find_device_by_path(root, device_path, device);
2050 }
2051}
2052
Yan Zheng2b820322008-11-17 21:11:30 -05002053/*
2054 * does all the dirty work required for changing file system's UUID.
2055 */
Li Zefan125ccb02011-12-08 15:07:24 +08002056static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05002057{
2058 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2059 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002060 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02002061 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002062 struct btrfs_device *device;
2063 u64 super_flags;
2064
2065 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05002066 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002067 return -EINVAL;
2068
Ilya Dryomov2208a372013-08-12 14:33:03 +03002069 seed_devices = __alloc_fs_devices();
2070 if (IS_ERR(seed_devices))
2071 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002072
Yan Zhenge4404d62008-12-12 10:03:26 -05002073 old_devices = clone_fs_devices(fs_devices);
2074 if (IS_ERR(old_devices)) {
2075 kfree(seed_devices);
2076 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002077 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002078
Yan Zheng2b820322008-11-17 21:11:30 -05002079 list_add(&old_devices->list, &fs_uuids);
2080
Yan Zhenge4404d62008-12-12 10:03:26 -05002081 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2082 seed_devices->opened = 1;
2083 INIT_LIST_HEAD(&seed_devices->devices);
2084 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002085 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002086
2087 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002088 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2089 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002090 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002091 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002092
2093 lock_chunks(root);
2094 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2095 unlock_chunks(root);
Yan Zhenge4404d62008-12-12 10:03:26 -05002096
Yan Zheng2b820322008-11-17 21:11:30 -05002097 fs_devices->seeding = 0;
2098 fs_devices->num_devices = 0;
2099 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002100 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002101 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002102 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002103
2104 generate_random_uuid(fs_devices->fsid);
2105 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2106 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002107 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2108
Yan Zheng2b820322008-11-17 21:11:30 -05002109 super_flags = btrfs_super_flags(disk_super) &
2110 ~BTRFS_SUPER_FLAG_SEEDING;
2111 btrfs_set_super_flags(disk_super, super_flags);
2112
2113 return 0;
2114}
2115
2116/*
2117 * strore the expected generation for seed devices in device items.
2118 */
2119static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2120 struct btrfs_root *root)
2121{
2122 struct btrfs_path *path;
2123 struct extent_buffer *leaf;
2124 struct btrfs_dev_item *dev_item;
2125 struct btrfs_device *device;
2126 struct btrfs_key key;
2127 u8 fs_uuid[BTRFS_UUID_SIZE];
2128 u8 dev_uuid[BTRFS_UUID_SIZE];
2129 u64 devid;
2130 int ret;
2131
2132 path = btrfs_alloc_path();
2133 if (!path)
2134 return -ENOMEM;
2135
2136 root = root->fs_info->chunk_root;
2137 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2138 key.offset = 0;
2139 key.type = BTRFS_DEV_ITEM_KEY;
2140
2141 while (1) {
2142 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2143 if (ret < 0)
2144 goto error;
2145
2146 leaf = path->nodes[0];
2147next_slot:
2148 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2149 ret = btrfs_next_leaf(root, path);
2150 if (ret > 0)
2151 break;
2152 if (ret < 0)
2153 goto error;
2154 leaf = path->nodes[0];
2155 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002156 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002157 continue;
2158 }
2159
2160 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2161 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2162 key.type != BTRFS_DEV_ITEM_KEY)
2163 break;
2164
2165 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2166 struct btrfs_dev_item);
2167 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002168 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002169 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002170 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002171 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002172 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2173 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002174 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002175
2176 if (device->fs_devices->seeding) {
2177 btrfs_set_device_generation(leaf, dev_item,
2178 device->generation);
2179 btrfs_mark_buffer_dirty(leaf);
2180 }
2181
2182 path->slots[0]++;
2183 goto next_slot;
2184 }
2185 ret = 0;
2186error:
2187 btrfs_free_path(path);
2188 return ret;
2189}
2190
Chris Mason788f20e2008-04-28 15:29:42 -04002191int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2192{
Josef Bacikd5e20032011-08-04 14:52:27 +00002193 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002194 struct btrfs_trans_handle *trans;
2195 struct btrfs_device *device;
2196 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002197 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002198 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002199 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002200 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002201 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002202 int ret = 0;
2203
Yan Zheng2b820322008-11-17 21:11:30 -05002204 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002205 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002206
Li Zefana5d16332011-12-07 20:08:40 -05002207 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002208 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002209 if (IS_ERR(bdev))
2210 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002211
Yan Zheng2b820322008-11-17 21:11:30 -05002212 if (root->fs_info->fs_devices->seeding) {
2213 seeding_dev = 1;
2214 down_write(&sb->s_umount);
2215 mutex_lock(&uuid_mutex);
2216 }
2217
Chris Mason8c8bee12008-09-29 11:19:10 -04002218 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002219
Chris Mason788f20e2008-04-28 15:29:42 -04002220 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002221
2222 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002223 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002224 if (device->bdev == bdev) {
2225 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002226 mutex_unlock(
2227 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002228 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002229 }
2230 }
Liu Bod25628b2012-11-14 14:35:30 +00002231 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002232
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002233 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2234 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002235 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002236 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002237 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002238 }
2239
Josef Bacik606686e2012-06-04 14:03:51 -04002240 name = rcu_string_strdup(device_path, GFP_NOFS);
2241 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002242 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002243 ret = -ENOMEM;
2244 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002245 }
Josef Bacik606686e2012-06-04 14:03:51 -04002246 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002247
Yan, Zhenga22285a2010-05-16 10:48:46 -04002248 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002249 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002250 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002251 kfree(device);
2252 ret = PTR_ERR(trans);
2253 goto error;
2254 }
2255
Josef Bacikd5e20032011-08-04 14:52:27 +00002256 q = bdev_get_queue(bdev);
2257 if (blk_queue_discard(q))
2258 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002259 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002260 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002261 device->io_width = root->sectorsize;
2262 device->io_align = root->sectorsize;
2263 device->sector_size = root->sectorsize;
2264 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002265 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002266 device->commit_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002267 device->dev_root = root->fs_info->dev_root;
2268 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002269 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002270 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002271 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002272 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002273 set_blocksize(device->bdev, 4096);
2274
Yan Zheng2b820322008-11-17 21:11:30 -05002275 if (seeding_dev) {
2276 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002277 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002278 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002279 }
2280
2281 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002282
Chris Masone5e9a522009-06-10 15:17:02 -04002283 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002284 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002285 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002286 list_add(&device->dev_alloc_list,
2287 &root->fs_info->fs_devices->alloc_list);
2288 root->fs_info->fs_devices->num_devices++;
2289 root->fs_info->fs_devices->open_devices++;
2290 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002291 root->fs_info->fs_devices->total_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -05002292 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2293
Josef Bacik2bf64752011-09-26 17:12:22 -04002294 spin_lock(&root->fs_info->free_chunk_lock);
2295 root->fs_info->free_chunk_space += device->total_bytes;
2296 spin_unlock(&root->fs_info->free_chunk_lock);
2297
Chris Masonc2898112009-06-10 09:51:32 -04002298 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2299 root->fs_info->fs_devices->rotating = 1;
2300
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002301 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002302 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002303 tmp + device->total_bytes);
Chris Mason788f20e2008-04-28 15:29:42 -04002304
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002305 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002306 btrfs_set_super_num_devices(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002307 tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002308
2309 /* add sysfs device entry */
Anand Jaine3bd6972015-08-14 18:32:48 +08002310 btrfs_sysfs_add_device_link(root->fs_info->fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002311
Miao Xie2196d6e2014-09-03 21:35:41 +08002312 /*
2313 * we've got more storage, clear any full flags on the space
2314 * infos
2315 */
2316 btrfs_clear_space_info_full(root->fs_info);
2317
2318 unlock_chunks(root);
Chris Masone5e9a522009-06-10 15:17:02 -04002319 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002320
Yan Zheng2b820322008-11-17 21:11:30 -05002321 if (seeding_dev) {
Miao Xie2196d6e2014-09-03 21:35:41 +08002322 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05002323 ret = init_first_rw_device(trans, root, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002324 unlock_chunks(root);
David Sterba005d6422012-09-18 07:52:32 -06002325 if (ret) {
2326 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002327 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002328 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002329 }
2330
2331 ret = btrfs_add_device(trans, root, device);
2332 if (ret) {
2333 btrfs_abort_transaction(trans, root, ret);
2334 goto error_trans;
2335 }
2336
2337 if (seeding_dev) {
2338 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2339
Yan Zheng2b820322008-11-17 21:11:30 -05002340 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002341 if (ret) {
2342 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002343 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002344 }
Anand Jainb2373f22014-06-03 11:36:03 +08002345
2346 /* Sprouting would change fsid of the mounted root,
2347 * so rename the fsid on the sysfs
2348 */
2349 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2350 root->fs_info->fsid);
Anand Jainc1b7e472015-08-14 18:32:50 +08002351 if (kobject_rename(&root->fs_info->fs_devices->fsid_kobj,
Anand Jain2e7910d2015-03-10 06:38:29 +08002352 fsid_buf))
Anand Jain2421a8c2015-03-10 06:38:40 +08002353 pr_warn("BTRFS: sysfs: failed to create fsid for sprout\n");
Yan Zheng2b820322008-11-17 21:11:30 -05002354 }
2355
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002356 root->fs_info->num_tolerated_disk_barrier_failures =
2357 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002358 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002359
2360 if (seeding_dev) {
2361 mutex_unlock(&uuid_mutex);
2362 up_write(&sb->s_umount);
2363
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002364 if (ret) /* transaction commit */
2365 return ret;
2366
Yan Zheng2b820322008-11-17 21:11:30 -05002367 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002368 if (ret < 0)
Anand Jaina4553fe2015-09-25 14:43:01 +08002369 btrfs_std_error(root->fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002370 "Failed to relocate sys chunks after "
2371 "device initialization. This can be fixed "
2372 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002373 trans = btrfs_attach_transaction(root);
2374 if (IS_ERR(trans)) {
2375 if (PTR_ERR(trans) == -ENOENT)
2376 return 0;
2377 return PTR_ERR(trans);
2378 }
2379 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002380 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002381
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002382 /* Update ctime/mtime for libblkid */
2383 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002384 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002385
2386error_trans:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002387 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002388 rcu_string_free(device->name);
Anand Jain32576042015-08-14 18:32:49 +08002389 btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002390 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002391error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002392 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002393 if (seeding_dev) {
2394 mutex_unlock(&uuid_mutex);
2395 up_write(&sb->s_umount);
2396 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002397 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002398}
2399
Stefan Behrense93c89c2012-11-05 17:33:06 +01002400int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002401 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002402 struct btrfs_device **device_out)
2403{
2404 struct request_queue *q;
2405 struct btrfs_device *device;
2406 struct block_device *bdev;
2407 struct btrfs_fs_info *fs_info = root->fs_info;
2408 struct list_head *devices;
2409 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002410 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002411 int ret = 0;
2412
2413 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002414 if (fs_info->fs_devices->seeding) {
2415 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002416 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002417 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002418
2419 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2420 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002421 if (IS_ERR(bdev)) {
2422 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002423 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002424 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002425
2426 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2427
2428 devices = &fs_info->fs_devices->devices;
2429 list_for_each_entry(device, devices, dev_list) {
2430 if (device->bdev == bdev) {
Miao Xie1c433662014-09-03 21:35:32 +08002431 btrfs_err(fs_info, "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002432 ret = -EEXIST;
2433 goto error;
2434 }
2435 }
2436
Miao Xie1c433662014-09-03 21:35:32 +08002437
Miao Xie7cc8e582014-09-03 21:35:38 +08002438 if (i_size_read(bdev->bd_inode) <
2439 btrfs_device_get_total_bytes(srcdev)) {
Miao Xie1c433662014-09-03 21:35:32 +08002440 btrfs_err(fs_info, "target device is smaller than source device!");
2441 ret = -EINVAL;
2442 goto error;
2443 }
2444
2445
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002446 device = btrfs_alloc_device(NULL, &devid, NULL);
2447 if (IS_ERR(device)) {
2448 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002449 goto error;
2450 }
2451
2452 name = rcu_string_strdup(device_path, GFP_NOFS);
2453 if (!name) {
2454 kfree(device);
2455 ret = -ENOMEM;
2456 goto error;
2457 }
2458 rcu_assign_pointer(device->name, name);
2459
2460 q = bdev_get_queue(bdev);
2461 if (blk_queue_discard(q))
2462 device->can_discard = 1;
2463 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2464 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002465 device->generation = 0;
2466 device->io_width = root->sectorsize;
2467 device->io_align = root->sectorsize;
2468 device->sector_size = root->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002469 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2470 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2471 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002472 ASSERT(list_empty(&srcdev->resized_list));
2473 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002474 device->commit_bytes_used = device->bytes_used;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002475 device->dev_root = fs_info->dev_root;
2476 device->bdev = bdev;
2477 device->in_fs_metadata = 1;
2478 device->is_tgtdev_for_dev_replace = 1;
2479 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002480 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002481 set_blocksize(device->bdev, 4096);
2482 device->fs_devices = fs_info->fs_devices;
2483 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2484 fs_info->fs_devices->num_devices++;
2485 fs_info->fs_devices->open_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002486 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2487
2488 *device_out = device;
2489 return ret;
2490
2491error:
2492 blkdev_put(bdev, FMODE_EXCL);
2493 return ret;
2494}
2495
2496void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2497 struct btrfs_device *tgtdev)
2498{
2499 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2500 tgtdev->io_width = fs_info->dev_root->sectorsize;
2501 tgtdev->io_align = fs_info->dev_root->sectorsize;
2502 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2503 tgtdev->dev_root = fs_info->dev_root;
2504 tgtdev->in_fs_metadata = 1;
2505}
2506
Chris Masond3977122009-01-05 21:25:51 -05002507static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2508 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002509{
2510 int ret;
2511 struct btrfs_path *path;
2512 struct btrfs_root *root;
2513 struct btrfs_dev_item *dev_item;
2514 struct extent_buffer *leaf;
2515 struct btrfs_key key;
2516
2517 root = device->dev_root->fs_info->chunk_root;
2518
2519 path = btrfs_alloc_path();
2520 if (!path)
2521 return -ENOMEM;
2522
2523 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2524 key.type = BTRFS_DEV_ITEM_KEY;
2525 key.offset = device->devid;
2526
2527 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2528 if (ret < 0)
2529 goto out;
2530
2531 if (ret > 0) {
2532 ret = -ENOENT;
2533 goto out;
2534 }
2535
2536 leaf = path->nodes[0];
2537 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2538
2539 btrfs_set_device_id(leaf, dev_item, device->devid);
2540 btrfs_set_device_type(leaf, dev_item, device->type);
2541 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2542 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2543 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002544 btrfs_set_device_total_bytes(leaf, dev_item,
2545 btrfs_device_get_disk_total_bytes(device));
2546 btrfs_set_device_bytes_used(leaf, dev_item,
2547 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002548 btrfs_mark_buffer_dirty(leaf);
2549
2550out:
2551 btrfs_free_path(path);
2552 return ret;
2553}
2554
Miao Xie2196d6e2014-09-03 21:35:41 +08002555int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002556 struct btrfs_device *device, u64 new_size)
2557{
2558 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002559 device->dev_root->fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002560 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002561 u64 old_total;
2562 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002563
Yan Zheng2b820322008-11-17 21:11:30 -05002564 if (!device->writeable)
2565 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002566
2567 lock_chunks(device->dev_root);
2568 old_total = btrfs_super_total_bytes(super_copy);
2569 diff = new_size - device->total_bytes;
2570
Stefan Behrens63a212a2012-11-05 18:29:28 +01002571 if (new_size <= device->total_bytes ||
Miao Xie2196d6e2014-09-03 21:35:41 +08002572 device->is_tgtdev_for_dev_replace) {
2573 unlock_chunks(device->dev_root);
Yan Zheng2b820322008-11-17 21:11:30 -05002574 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002575 }
Yan Zheng2b820322008-11-17 21:11:30 -05002576
Miao Xie935e5cc2014-09-03 21:35:33 +08002577 fs_devices = device->dev_root->fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002578
2579 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002580 device->fs_devices->total_rw_bytes += diff;
2581
Miao Xie7cc8e582014-09-03 21:35:38 +08002582 btrfs_device_set_total_bytes(device, new_size);
2583 btrfs_device_set_disk_total_bytes(device, new_size);
Chris Mason4184ea72009-03-10 12:39:20 -04002584 btrfs_clear_space_info_full(device->dev_root->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002585 if (list_empty(&device->resized_list))
2586 list_add_tail(&device->resized_list,
2587 &fs_devices->resized_devices);
Miao Xie2196d6e2014-09-03 21:35:41 +08002588 unlock_chunks(device->dev_root);
Chris Mason4184ea72009-03-10 12:39:20 -04002589
Chris Mason8f18cf12008-04-25 16:53:30 -04002590 return btrfs_update_device(trans, device);
2591}
2592
2593static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Zhao Leia688a042015-02-09 20:31:44 +08002594 struct btrfs_root *root, u64 chunk_objectid,
Chris Mason8f18cf12008-04-25 16:53:30 -04002595 u64 chunk_offset)
2596{
2597 int ret;
2598 struct btrfs_path *path;
2599 struct btrfs_key key;
2600
2601 root = root->fs_info->chunk_root;
2602 path = btrfs_alloc_path();
2603 if (!path)
2604 return -ENOMEM;
2605
2606 key.objectid = chunk_objectid;
2607 key.offset = chunk_offset;
2608 key.type = BTRFS_CHUNK_ITEM_KEY;
2609
2610 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002611 if (ret < 0)
2612 goto out;
2613 else if (ret > 0) { /* Logic error or corruption */
Anand Jaina4553fe2015-09-25 14:43:01 +08002614 btrfs_std_error(root->fs_info, -ENOENT,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002615 "Failed lookup while freeing chunk.");
2616 ret = -ENOENT;
2617 goto out;
2618 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002619
2620 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002621 if (ret < 0)
Anand Jaina4553fe2015-09-25 14:43:01 +08002622 btrfs_std_error(root->fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002623 "Failed to delete chunk item.");
2624out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002625 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002626 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002627}
2628
Christoph Hellwigb2950862008-12-02 09:54:17 -05002629static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002630 chunk_offset)
2631{
David Sterba6c417612011-04-13 15:41:04 +02002632 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002633 struct btrfs_disk_key *disk_key;
2634 struct btrfs_chunk *chunk;
2635 u8 *ptr;
2636 int ret = 0;
2637 u32 num_stripes;
2638 u32 array_size;
2639 u32 len = 0;
2640 u32 cur;
2641 struct btrfs_key key;
2642
Miao Xie2196d6e2014-09-03 21:35:41 +08002643 lock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002644 array_size = btrfs_super_sys_array_size(super_copy);
2645
2646 ptr = super_copy->sys_chunk_array;
2647 cur = 0;
2648
2649 while (cur < array_size) {
2650 disk_key = (struct btrfs_disk_key *)ptr;
2651 btrfs_disk_key_to_cpu(&key, disk_key);
2652
2653 len = sizeof(*disk_key);
2654
2655 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2656 chunk = (struct btrfs_chunk *)(ptr + len);
2657 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2658 len += btrfs_chunk_item_size(num_stripes);
2659 } else {
2660 ret = -EIO;
2661 break;
2662 }
2663 if (key.objectid == chunk_objectid &&
2664 key.offset == chunk_offset) {
2665 memmove(ptr, ptr + len, array_size - (cur + len));
2666 array_size -= len;
2667 btrfs_set_super_sys_array_size(super_copy, array_size);
2668 } else {
2669 ptr += len;
2670 cur += len;
2671 }
2672 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002673 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002674 return ret;
2675}
2676
Josef Bacik47ab2a62014-09-18 11:20:02 -04002677int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2678 struct btrfs_root *root, u64 chunk_offset)
2679{
2680 struct extent_map_tree *em_tree;
2681 struct extent_map *em;
2682 struct btrfs_root *extent_root = root->fs_info->extent_root;
2683 struct map_lookup *map;
2684 u64 dev_extent_len = 0;
2685 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002686 int i, ret = 0;
2687
2688 /* Just in case */
2689 root = root->fs_info->chunk_root;
2690 em_tree = &root->fs_info->mapping_tree.map_tree;
2691
2692 read_lock(&em_tree->lock);
2693 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2694 read_unlock(&em_tree->lock);
2695
2696 if (!em || em->start > chunk_offset ||
2697 em->start + em->len < chunk_offset) {
2698 /*
2699 * This is a logic error, but we don't want to just rely on the
2700 * user having built with ASSERT enabled, so if ASSERT doens't
2701 * do anything we still error out.
2702 */
2703 ASSERT(0);
2704 if (em)
2705 free_extent_map(em);
2706 return -EINVAL;
2707 }
2708 map = (struct map_lookup *)em->bdev;
Filipe Manana39c2d7f2015-05-20 14:01:55 +01002709 lock_chunks(root->fs_info->chunk_root);
Filipe Manana4617ea32015-06-09 17:48:21 +01002710 check_system_chunk(trans, extent_root, map->type);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01002711 unlock_chunks(root->fs_info->chunk_root);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002712
2713 for (i = 0; i < map->num_stripes; i++) {
2714 struct btrfs_device *device = map->stripes[i].dev;
2715 ret = btrfs_free_dev_extent(trans, device,
2716 map->stripes[i].physical,
2717 &dev_extent_len);
2718 if (ret) {
2719 btrfs_abort_transaction(trans, root, ret);
2720 goto out;
2721 }
2722
2723 if (device->bytes_used > 0) {
2724 lock_chunks(root);
2725 btrfs_device_set_bytes_used(device,
2726 device->bytes_used - dev_extent_len);
2727 spin_lock(&root->fs_info->free_chunk_lock);
2728 root->fs_info->free_chunk_space += dev_extent_len;
2729 spin_unlock(&root->fs_info->free_chunk_lock);
2730 btrfs_clear_space_info_full(root->fs_info);
2731 unlock_chunks(root);
2732 }
2733
2734 if (map->stripes[i].dev) {
2735 ret = btrfs_update_device(trans, map->stripes[i].dev);
2736 if (ret) {
2737 btrfs_abort_transaction(trans, root, ret);
2738 goto out;
2739 }
2740 }
2741 }
Zhao Leia688a042015-02-09 20:31:44 +08002742 ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002743 if (ret) {
2744 btrfs_abort_transaction(trans, root, ret);
2745 goto out;
2746 }
2747
2748 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2749
2750 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2751 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2752 if (ret) {
2753 btrfs_abort_transaction(trans, root, ret);
2754 goto out;
2755 }
2756 }
2757
Filipe Manana04216822014-11-27 21:14:15 +00002758 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002759 if (ret) {
2760 btrfs_abort_transaction(trans, extent_root, ret);
2761 goto out;
2762 }
2763
Josef Bacik47ab2a62014-09-18 11:20:02 -04002764out:
2765 /* once for us */
2766 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002767 return ret;
2768}
2769
Zhaoleidc2ee4e2015-08-05 18:00:04 +08002770static int btrfs_relocate_chunk(struct btrfs_root *root, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002771{
Chris Mason8f18cf12008-04-25 16:53:30 -04002772 struct btrfs_root *extent_root;
2773 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002774 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002775
2776 root = root->fs_info->chunk_root;
2777 extent_root = root->fs_info->extent_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002778
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002779 /*
2780 * Prevent races with automatic removal of unused block groups.
2781 * After we relocate and before we remove the chunk with offset
2782 * chunk_offset, automatic removal of the block group can kick in,
2783 * resulting in a failure when calling btrfs_remove_chunk() below.
2784 *
2785 * Make sure to acquire this mutex before doing a tree search (dev
2786 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2787 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2788 * we release the path used to search the chunk/dev tree and before
2789 * the current task acquires this mutex and calls us.
2790 */
2791 ASSERT(mutex_is_locked(&root->fs_info->delete_unused_bgs_mutex));
2792
Josef Bacikba1bf482009-09-11 16:11:19 -04002793 ret = btrfs_can_relocate(extent_root, chunk_offset);
2794 if (ret)
2795 return -ENOSPC;
2796
Chris Mason8f18cf12008-04-25 16:53:30 -04002797 /* step one, relocate all the extents inside this chunk */
Zhaolei55e3a602015-08-05 16:43:30 +08002798 btrfs_scrub_pause(root);
Zheng Yan1a40e232008-09-26 10:09:34 -04002799 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Zhaolei55e3a602015-08-05 16:43:30 +08002800 btrfs_scrub_continue(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002801 if (ret)
2802 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002803
Yan, Zhenga22285a2010-05-16 10:48:46 -04002804 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002805 if (IS_ERR(trans)) {
2806 ret = PTR_ERR(trans);
Anand Jaina4553fe2015-09-25 14:43:01 +08002807 btrfs_std_error(root->fs_info, ret, NULL);
Liu Bo0f788c52013-03-04 16:25:40 +00002808 return ret;
2809 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002810
2811 /*
2812 * step two, delete the device extents and the
2813 * chunk tree entries
2814 */
Josef Bacik47ab2a62014-09-18 11:20:02 -04002815 ret = btrfs_remove_chunk(trans, root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002816 btrfs_end_transaction(trans, root);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002817 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002818}
2819
Yan Zheng2b820322008-11-17 21:11:30 -05002820static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2821{
2822 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2823 struct btrfs_path *path;
2824 struct extent_buffer *leaf;
2825 struct btrfs_chunk *chunk;
2826 struct btrfs_key key;
2827 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05002828 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002829 bool retried = false;
2830 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002831 int ret;
2832
2833 path = btrfs_alloc_path();
2834 if (!path)
2835 return -ENOMEM;
2836
Josef Bacikba1bf482009-09-11 16:11:19 -04002837again:
Yan Zheng2b820322008-11-17 21:11:30 -05002838 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2839 key.offset = (u64)-1;
2840 key.type = BTRFS_CHUNK_ITEM_KEY;
2841
2842 while (1) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002843 mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002844 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002845 if (ret < 0) {
2846 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002847 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002848 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002849 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002850
2851 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2852 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002853 if (ret)
2854 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002855 if (ret < 0)
2856 goto error;
2857 if (ret > 0)
2858 break;
2859
2860 leaf = path->nodes[0];
2861 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2862
2863 chunk = btrfs_item_ptr(leaf, path->slots[0],
2864 struct btrfs_chunk);
2865 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002866 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002867
2868 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Zhao Leia688a042015-02-09 20:31:44 +08002869 ret = btrfs_relocate_chunk(chunk_root,
Yan Zheng2b820322008-11-17 21:11:30 -05002870 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002871 if (ret == -ENOSPC)
2872 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302873 else
2874 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002875 }
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002876 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002877
2878 if (found_key.offset == 0)
2879 break;
2880 key.offset = found_key.offset - 1;
2881 }
2882 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002883 if (failed && !retried) {
2884 failed = 0;
2885 retried = true;
2886 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302887 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002888 ret = -ENOSPC;
2889 }
Yan Zheng2b820322008-11-17 21:11:30 -05002890error:
2891 btrfs_free_path(path);
2892 return ret;
2893}
2894
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002895static int insert_balance_item(struct btrfs_root *root,
2896 struct btrfs_balance_control *bctl)
2897{
2898 struct btrfs_trans_handle *trans;
2899 struct btrfs_balance_item *item;
2900 struct btrfs_disk_balance_args disk_bargs;
2901 struct btrfs_path *path;
2902 struct extent_buffer *leaf;
2903 struct btrfs_key key;
2904 int ret, err;
2905
2906 path = btrfs_alloc_path();
2907 if (!path)
2908 return -ENOMEM;
2909
2910 trans = btrfs_start_transaction(root, 0);
2911 if (IS_ERR(trans)) {
2912 btrfs_free_path(path);
2913 return PTR_ERR(trans);
2914 }
2915
2916 key.objectid = BTRFS_BALANCE_OBJECTID;
2917 key.type = BTRFS_BALANCE_ITEM_KEY;
2918 key.offset = 0;
2919
2920 ret = btrfs_insert_empty_item(trans, root, path, &key,
2921 sizeof(*item));
2922 if (ret)
2923 goto out;
2924
2925 leaf = path->nodes[0];
2926 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2927
2928 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2929
2930 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2931 btrfs_set_balance_data(leaf, item, &disk_bargs);
2932 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2933 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2934 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2935 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2936
2937 btrfs_set_balance_flags(leaf, item, bctl->flags);
2938
2939 btrfs_mark_buffer_dirty(leaf);
2940out:
2941 btrfs_free_path(path);
2942 err = btrfs_commit_transaction(trans, root);
2943 if (err && !ret)
2944 ret = err;
2945 return ret;
2946}
2947
2948static int del_balance_item(struct btrfs_root *root)
2949{
2950 struct btrfs_trans_handle *trans;
2951 struct btrfs_path *path;
2952 struct btrfs_key key;
2953 int ret, err;
2954
2955 path = btrfs_alloc_path();
2956 if (!path)
2957 return -ENOMEM;
2958
2959 trans = btrfs_start_transaction(root, 0);
2960 if (IS_ERR(trans)) {
2961 btrfs_free_path(path);
2962 return PTR_ERR(trans);
2963 }
2964
2965 key.objectid = BTRFS_BALANCE_OBJECTID;
2966 key.type = BTRFS_BALANCE_ITEM_KEY;
2967 key.offset = 0;
2968
2969 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2970 if (ret < 0)
2971 goto out;
2972 if (ret > 0) {
2973 ret = -ENOENT;
2974 goto out;
2975 }
2976
2977 ret = btrfs_del_item(trans, root, path);
2978out:
2979 btrfs_free_path(path);
2980 err = btrfs_commit_transaction(trans, root);
2981 if (err && !ret)
2982 ret = err;
2983 return ret;
2984}
2985
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002986/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002987 * This is a heuristic used to reduce the number of chunks balanced on
2988 * resume after balance was interrupted.
2989 */
2990static void update_balance_args(struct btrfs_balance_control *bctl)
2991{
2992 /*
2993 * Turn on soft mode for chunk types that were being converted.
2994 */
2995 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2996 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2997 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2998 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2999 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3000 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3001
3002 /*
3003 * Turn on usage filter if is not already used. The idea is
3004 * that chunks that we have already balanced should be
3005 * reasonably full. Don't do it for chunks that are being
3006 * converted - that will keep us from relocating unconverted
3007 * (albeit full) chunks.
3008 */
3009 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3010 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3011 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3012 bctl->data.usage = 90;
3013 }
3014 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3015 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3016 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3017 bctl->sys.usage = 90;
3018 }
3019 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3020 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3021 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3022 bctl->meta.usage = 90;
3023 }
3024}
3025
3026/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003027 * Should be called with both balance and volume mutexes held to
3028 * serialize other volume operations (add_dev/rm_dev/resize) with
3029 * restriper. Same goes for unset_balance_control.
3030 */
3031static void set_balance_control(struct btrfs_balance_control *bctl)
3032{
3033 struct btrfs_fs_info *fs_info = bctl->fs_info;
3034
3035 BUG_ON(fs_info->balance_ctl);
3036
3037 spin_lock(&fs_info->balance_lock);
3038 fs_info->balance_ctl = bctl;
3039 spin_unlock(&fs_info->balance_lock);
3040}
3041
3042static void unset_balance_control(struct btrfs_fs_info *fs_info)
3043{
3044 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3045
3046 BUG_ON(!fs_info->balance_ctl);
3047
3048 spin_lock(&fs_info->balance_lock);
3049 fs_info->balance_ctl = NULL;
3050 spin_unlock(&fs_info->balance_lock);
3051
3052 kfree(bctl);
3053}
3054
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003055/*
3056 * Balance filters. Return 1 if chunk should be filtered out
3057 * (should not be balanced).
3058 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003059static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003060 struct btrfs_balance_args *bargs)
3061{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003062 chunk_type = chunk_to_extended(chunk_type) &
3063 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003064
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003065 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003066 return 0;
3067
3068 return 1;
3069}
3070
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003071static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
3072 struct btrfs_balance_args *bargs)
3073{
3074 struct btrfs_block_group_cache *cache;
3075 u64 chunk_used, user_thresh;
3076 int ret = 1;
3077
3078 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3079 chunk_used = btrfs_block_group_used(&cache->item);
3080
Ilya Dryomova105bb82013-01-21 15:15:56 +02003081 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003082 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003083 else if (bargs->usage > 100)
3084 user_thresh = cache->key.offset;
3085 else
3086 user_thresh = div_factor_fine(cache->key.offset,
3087 bargs->usage);
3088
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003089 if (chunk_used < user_thresh)
3090 ret = 0;
3091
3092 btrfs_put_block_group(cache);
3093 return ret;
3094}
3095
Ilya Dryomov409d4042012-01-16 22:04:47 +02003096static int chunk_devid_filter(struct extent_buffer *leaf,
3097 struct btrfs_chunk *chunk,
3098 struct btrfs_balance_args *bargs)
3099{
3100 struct btrfs_stripe *stripe;
3101 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3102 int i;
3103
3104 for (i = 0; i < num_stripes; i++) {
3105 stripe = btrfs_stripe_nr(chunk, i);
3106 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3107 return 0;
3108 }
3109
3110 return 1;
3111}
3112
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003113/* [pstart, pend) */
3114static int chunk_drange_filter(struct extent_buffer *leaf,
3115 struct btrfs_chunk *chunk,
3116 u64 chunk_offset,
3117 struct btrfs_balance_args *bargs)
3118{
3119 struct btrfs_stripe *stripe;
3120 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3121 u64 stripe_offset;
3122 u64 stripe_length;
3123 int factor;
3124 int i;
3125
3126 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3127 return 0;
3128
3129 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003130 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3131 factor = num_stripes / 2;
3132 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3133 factor = num_stripes - 1;
3134 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3135 factor = num_stripes - 2;
3136 } else {
3137 factor = num_stripes;
3138 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003139
3140 for (i = 0; i < num_stripes; i++) {
3141 stripe = btrfs_stripe_nr(chunk, i);
3142 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3143 continue;
3144
3145 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3146 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003147 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003148
3149 if (stripe_offset < bargs->pend &&
3150 stripe_offset + stripe_length > bargs->pstart)
3151 return 0;
3152 }
3153
3154 return 1;
3155}
3156
Ilya Dryomovea671762012-01-16 22:04:48 +02003157/* [vstart, vend) */
3158static int chunk_vrange_filter(struct extent_buffer *leaf,
3159 struct btrfs_chunk *chunk,
3160 u64 chunk_offset,
3161 struct btrfs_balance_args *bargs)
3162{
3163 if (chunk_offset < bargs->vend &&
3164 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3165 /* at least part of the chunk is inside this vrange */
3166 return 0;
3167
3168 return 1;
3169}
3170
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003171static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003172 struct btrfs_balance_args *bargs)
3173{
3174 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3175 return 0;
3176
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003177 chunk_type = chunk_to_extended(chunk_type) &
3178 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003179
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003180 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003181 return 1;
3182
3183 return 0;
3184}
3185
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003186static int should_balance_chunk(struct btrfs_root *root,
3187 struct extent_buffer *leaf,
3188 struct btrfs_chunk *chunk, u64 chunk_offset)
3189{
3190 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3191 struct btrfs_balance_args *bargs = NULL;
3192 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3193
3194 /* type filter */
3195 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3196 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3197 return 0;
3198 }
3199
3200 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3201 bargs = &bctl->data;
3202 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3203 bargs = &bctl->sys;
3204 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3205 bargs = &bctl->meta;
3206
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003207 /* profiles filter */
3208 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3209 chunk_profiles_filter(chunk_type, bargs)) {
3210 return 0;
3211 }
3212
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003213 /* usage filter */
3214 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3215 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3216 return 0;
3217 }
3218
Ilya Dryomov409d4042012-01-16 22:04:47 +02003219 /* devid filter */
3220 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3221 chunk_devid_filter(leaf, chunk, bargs)) {
3222 return 0;
3223 }
3224
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003225 /* drange filter, makes sense only with devid filter */
3226 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3227 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3228 return 0;
3229 }
3230
Ilya Dryomovea671762012-01-16 22:04:48 +02003231 /* vrange filter */
3232 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3233 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3234 return 0;
3235 }
3236
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003237 /* soft profile changing mode */
3238 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3239 chunk_soft_convert_filter(chunk_type, bargs)) {
3240 return 0;
3241 }
3242
David Sterba7d824b62014-05-07 17:37:51 +02003243 /*
3244 * limited by count, must be the last filter
3245 */
3246 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3247 if (bargs->limit == 0)
3248 return 0;
3249 else
3250 bargs->limit--;
3251 }
3252
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003253 return 1;
3254}
3255
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003256static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003257{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003258 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003259 struct btrfs_root *chunk_root = fs_info->chunk_root;
3260 struct btrfs_root *dev_root = fs_info->dev_root;
3261 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003262 struct btrfs_device *device;
3263 u64 old_size;
3264 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003265 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003266 struct btrfs_path *path;
3267 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003268 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003269 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003270 struct extent_buffer *leaf;
3271 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003272 int ret;
3273 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003274 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003275 u64 limit_data = bctl->data.limit;
3276 u64 limit_meta = bctl->meta.limit;
3277 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003278
Chris Masonec44a352008-04-28 15:29:52 -04003279 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003280 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003281 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003282 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003283 size_to_free = div_factor(old_size, 1);
3284 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003285 if (!device->writeable ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003286 btrfs_device_get_total_bytes(device) -
3287 btrfs_device_get_bytes_used(device) > size_to_free ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003288 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003289 continue;
3290
3291 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003292 if (ret == -ENOSPC)
3293 break;
Chris Masonec44a352008-04-28 15:29:52 -04003294 BUG_ON(ret);
3295
Yan, Zhenga22285a2010-05-16 10:48:46 -04003296 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003297 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003298
3299 ret = btrfs_grow_device(trans, device, old_size);
3300 BUG_ON(ret);
3301
3302 btrfs_end_transaction(trans, dev_root);
3303 }
3304
3305 /* step two, relocate all the chunks */
3306 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003307 if (!path) {
3308 ret = -ENOMEM;
3309 goto error;
3310 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003311
3312 /* zero out stat counters */
3313 spin_lock(&fs_info->balance_lock);
3314 memset(&bctl->stat, 0, sizeof(bctl->stat));
3315 spin_unlock(&fs_info->balance_lock);
3316again:
David Sterba7d824b62014-05-07 17:37:51 +02003317 if (!counting) {
3318 bctl->data.limit = limit_data;
3319 bctl->meta.limit = limit_meta;
3320 bctl->sys.limit = limit_sys;
3321 }
Chris Masonec44a352008-04-28 15:29:52 -04003322 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3323 key.offset = (u64)-1;
3324 key.type = BTRFS_CHUNK_ITEM_KEY;
3325
Chris Masond3977122009-01-05 21:25:51 -05003326 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003327 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003328 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003329 ret = -ECANCELED;
3330 goto error;
3331 }
3332
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003333 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003334 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003335 if (ret < 0) {
3336 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003337 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003338 }
Chris Masonec44a352008-04-28 15:29:52 -04003339
3340 /*
3341 * this shouldn't happen, it means the last relocate
3342 * failed
3343 */
3344 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003345 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003346
3347 ret = btrfs_previous_item(chunk_root, path, 0,
3348 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003349 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003350 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003351 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003352 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003353 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003354
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003355 leaf = path->nodes[0];
3356 slot = path->slots[0];
3357 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3358
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003359 if (found_key.objectid != key.objectid) {
3360 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003361 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003362 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003363
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003364 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3365
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003366 if (!counting) {
3367 spin_lock(&fs_info->balance_lock);
3368 bctl->stat.considered++;
3369 spin_unlock(&fs_info->balance_lock);
3370 }
3371
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003372 ret = should_balance_chunk(chunk_root, leaf, chunk,
3373 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003374 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003375 if (!ret) {
3376 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003377 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003378 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003379
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003380 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003381 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003382 spin_lock(&fs_info->balance_lock);
3383 bctl->stat.expected++;
3384 spin_unlock(&fs_info->balance_lock);
3385 goto loop;
3386 }
3387
Chris Masonec44a352008-04-28 15:29:52 -04003388 ret = btrfs_relocate_chunk(chunk_root,
Chris Masonec44a352008-04-28 15:29:52 -04003389 found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003390 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik508794e2011-07-02 21:24:41 +00003391 if (ret && ret != -ENOSPC)
3392 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003393 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003394 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003395 } else {
3396 spin_lock(&fs_info->balance_lock);
3397 bctl->stat.completed++;
3398 spin_unlock(&fs_info->balance_lock);
3399 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003400loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003401 if (found_key.offset == 0)
3402 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003403 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003404 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003405
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003406 if (counting) {
3407 btrfs_release_path(path);
3408 counting = false;
3409 goto again;
3410 }
Chris Masonec44a352008-04-28 15:29:52 -04003411error:
3412 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003413 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003414 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003415 enospc_errors);
3416 if (!ret)
3417 ret = -ENOSPC;
3418 }
3419
Chris Masonec44a352008-04-28 15:29:52 -04003420 return ret;
3421}
3422
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003423/**
3424 * alloc_profile_is_valid - see if a given profile is valid and reduced
3425 * @flags: profile to validate
3426 * @extended: if true @flags is treated as an extended profile
3427 */
3428static int alloc_profile_is_valid(u64 flags, int extended)
3429{
3430 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3431 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3432
3433 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3434
3435 /* 1) check that all other bits are zeroed */
3436 if (flags & ~mask)
3437 return 0;
3438
3439 /* 2) see if profile is reduced */
3440 if (flags == 0)
3441 return !extended; /* "0" is valid for usual profiles */
3442
3443 /* true if exactly one bit set */
3444 return (flags & (flags - 1)) == 0;
3445}
3446
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003447static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3448{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003449 /* cancel requested || normal exit path */
3450 return atomic_read(&fs_info->balance_cancel_req) ||
3451 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3452 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003453}
3454
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003455static void __cancel_balance(struct btrfs_fs_info *fs_info)
3456{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003457 int ret;
3458
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003459 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003460 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003461 if (ret)
Anand Jaina4553fe2015-09-25 14:43:01 +08003462 btrfs_std_error(fs_info, ret, NULL);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003463
3464 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003465}
3466
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003467/*
3468 * Should be called with both balance and volume mutexes held
3469 */
3470int btrfs_balance(struct btrfs_balance_control *bctl,
3471 struct btrfs_ioctl_balance_args *bargs)
3472{
3473 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003474 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003475 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003476 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003477 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003478 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003479
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003480 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003481 atomic_read(&fs_info->balance_pause_req) ||
3482 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003483 ret = -EINVAL;
3484 goto out;
3485 }
3486
Ilya Dryomove4837f82012-03-27 17:09:17 +03003487 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3488 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3489 mixed = 1;
3490
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003491 /*
3492 * In case of mixed groups both data and meta should be picked,
3493 * and identical options should be given for both of them.
3494 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003495 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3496 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003497 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3498 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3499 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003500 btrfs_err(fs_info, "with mixed groups data and "
3501 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003502 ret = -EINVAL;
3503 goto out;
3504 }
3505 }
3506
Stefan Behrens8dabb742012-11-06 13:15:27 +01003507 num_devices = fs_info->fs_devices->num_devices;
3508 btrfs_dev_replace_lock(&fs_info->dev_replace);
3509 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3510 BUG_ON(num_devices < 1);
3511 num_devices--;
3512 }
3513 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003514 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003515 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003516 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003517 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003518 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003519 if (num_devices > 2)
3520 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3521 if (num_devices > 3)
3522 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3523 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003524 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3525 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3526 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003527 btrfs_err(fs_info, "unable to start balance with target "
3528 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003529 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003530 ret = -EINVAL;
3531 goto out;
3532 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003533 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3534 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3535 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003536 btrfs_err(fs_info,
3537 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003538 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003539 ret = -EINVAL;
3540 goto out;
3541 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003542 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3543 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3544 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003545 btrfs_err(fs_info,
3546 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003547 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003548 ret = -EINVAL;
3549 goto out;
3550 }
3551
Ilya Dryomove4837f82012-03-27 17:09:17 +03003552 /* allow dup'ed data chunks only in mixed mode */
3553 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003554 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003555 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003556 ret = -EINVAL;
3557 goto out;
3558 }
3559
3560 /* allow to reduce meta or sys integrity only if force set */
3561 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003562 BTRFS_BLOCK_GROUP_RAID10 |
3563 BTRFS_BLOCK_GROUP_RAID5 |
3564 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003565 do {
3566 seq = read_seqbegin(&fs_info->profiles_lock);
3567
3568 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3569 (fs_info->avail_system_alloc_bits & allowed) &&
3570 !(bctl->sys.target & allowed)) ||
3571 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3572 (fs_info->avail_metadata_alloc_bits & allowed) &&
3573 !(bctl->meta.target & allowed))) {
3574 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003575 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003576 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003577 btrfs_err(fs_info, "balance will reduce metadata "
3578 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003579 ret = -EINVAL;
3580 goto out;
3581 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003582 }
Miao Xiede98ced2013-01-29 10:13:12 +00003583 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003584
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003585 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
Zhao Lei943c6e92015-08-19 15:54:15 +08003586 fs_info->num_tolerated_disk_barrier_failures = min(
3587 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info),
3588 btrfs_get_num_tolerated_disk_barrier_failures(
3589 bctl->sys.target));
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003590 }
3591
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003592 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003593 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003594 goto out;
3595
Ilya Dryomov59641012012-01-16 22:04:48 +02003596 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3597 BUG_ON(ret == -EEXIST);
3598 set_balance_control(bctl);
3599 } else {
3600 BUG_ON(ret != -EEXIST);
3601 spin_lock(&fs_info->balance_lock);
3602 update_balance_args(bctl);
3603 spin_unlock(&fs_info->balance_lock);
3604 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003605
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003606 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003607 mutex_unlock(&fs_info->balance_mutex);
3608
3609 ret = __btrfs_balance(fs_info);
3610
3611 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003612 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003613
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003614 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3615 fs_info->num_tolerated_disk_barrier_failures =
3616 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3617 }
3618
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003619 if (bargs) {
3620 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003621 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003622 }
3623
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003624 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3625 balance_need_close(fs_info)) {
3626 __cancel_balance(fs_info);
3627 }
3628
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003629 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003630
3631 return ret;
3632out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003633 if (bctl->flags & BTRFS_BALANCE_RESUME)
3634 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003635 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003636 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003637 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3638 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003639 return ret;
3640}
3641
3642static int balance_kthread(void *data)
3643{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003644 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003645 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003646
3647 mutex_lock(&fs_info->volume_mutex);
3648 mutex_lock(&fs_info->balance_mutex);
3649
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003650 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003651 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003652 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003653 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003654
3655 mutex_unlock(&fs_info->balance_mutex);
3656 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003657
Ilya Dryomov59641012012-01-16 22:04:48 +02003658 return ret;
3659}
3660
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003661int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3662{
3663 struct task_struct *tsk;
3664
3665 spin_lock(&fs_info->balance_lock);
3666 if (!fs_info->balance_ctl) {
3667 spin_unlock(&fs_info->balance_lock);
3668 return 0;
3669 }
3670 spin_unlock(&fs_info->balance_lock);
3671
3672 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003673 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003674 return 0;
3675 }
3676
3677 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303678 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003679}
3680
Ilya Dryomov68310a52012-06-22 12:24:12 -06003681int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003682{
Ilya Dryomov59641012012-01-16 22:04:48 +02003683 struct btrfs_balance_control *bctl;
3684 struct btrfs_balance_item *item;
3685 struct btrfs_disk_balance_args disk_bargs;
3686 struct btrfs_path *path;
3687 struct extent_buffer *leaf;
3688 struct btrfs_key key;
3689 int ret;
3690
3691 path = btrfs_alloc_path();
3692 if (!path)
3693 return -ENOMEM;
3694
Ilya Dryomov68310a52012-06-22 12:24:12 -06003695 key.objectid = BTRFS_BALANCE_OBJECTID;
3696 key.type = BTRFS_BALANCE_ITEM_KEY;
3697 key.offset = 0;
3698
3699 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3700 if (ret < 0)
3701 goto out;
3702 if (ret > 0) { /* ret = -ENOENT; */
3703 ret = 0;
3704 goto out;
3705 }
3706
Ilya Dryomov59641012012-01-16 22:04:48 +02003707 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3708 if (!bctl) {
3709 ret = -ENOMEM;
3710 goto out;
3711 }
3712
Ilya Dryomov59641012012-01-16 22:04:48 +02003713 leaf = path->nodes[0];
3714 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3715
Ilya Dryomov68310a52012-06-22 12:24:12 -06003716 bctl->fs_info = fs_info;
3717 bctl->flags = btrfs_balance_flags(leaf, item);
3718 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003719
3720 btrfs_balance_data(leaf, item, &disk_bargs);
3721 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3722 btrfs_balance_meta(leaf, item, &disk_bargs);
3723 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3724 btrfs_balance_sys(leaf, item, &disk_bargs);
3725 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3726
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003727 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3728
Ilya Dryomov68310a52012-06-22 12:24:12 -06003729 mutex_lock(&fs_info->volume_mutex);
3730 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003731
Ilya Dryomov68310a52012-06-22 12:24:12 -06003732 set_balance_control(bctl);
3733
3734 mutex_unlock(&fs_info->balance_mutex);
3735 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003736out:
3737 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003738 return ret;
3739}
3740
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003741int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3742{
3743 int ret = 0;
3744
3745 mutex_lock(&fs_info->balance_mutex);
3746 if (!fs_info->balance_ctl) {
3747 mutex_unlock(&fs_info->balance_mutex);
3748 return -ENOTCONN;
3749 }
3750
3751 if (atomic_read(&fs_info->balance_running)) {
3752 atomic_inc(&fs_info->balance_pause_req);
3753 mutex_unlock(&fs_info->balance_mutex);
3754
3755 wait_event(fs_info->balance_wait_q,
3756 atomic_read(&fs_info->balance_running) == 0);
3757
3758 mutex_lock(&fs_info->balance_mutex);
3759 /* we are good with balance_ctl ripped off from under us */
3760 BUG_ON(atomic_read(&fs_info->balance_running));
3761 atomic_dec(&fs_info->balance_pause_req);
3762 } else {
3763 ret = -ENOTCONN;
3764 }
3765
3766 mutex_unlock(&fs_info->balance_mutex);
3767 return ret;
3768}
3769
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003770int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3771{
Ilya Dryomove649e582013-10-10 20:40:21 +03003772 if (fs_info->sb->s_flags & MS_RDONLY)
3773 return -EROFS;
3774
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003775 mutex_lock(&fs_info->balance_mutex);
3776 if (!fs_info->balance_ctl) {
3777 mutex_unlock(&fs_info->balance_mutex);
3778 return -ENOTCONN;
3779 }
3780
3781 atomic_inc(&fs_info->balance_cancel_req);
3782 /*
3783 * if we are running just wait and return, balance item is
3784 * deleted in btrfs_balance in this case
3785 */
3786 if (atomic_read(&fs_info->balance_running)) {
3787 mutex_unlock(&fs_info->balance_mutex);
3788 wait_event(fs_info->balance_wait_q,
3789 atomic_read(&fs_info->balance_running) == 0);
3790 mutex_lock(&fs_info->balance_mutex);
3791 } else {
3792 /* __cancel_balance needs volume_mutex */
3793 mutex_unlock(&fs_info->balance_mutex);
3794 mutex_lock(&fs_info->volume_mutex);
3795 mutex_lock(&fs_info->balance_mutex);
3796
3797 if (fs_info->balance_ctl)
3798 __cancel_balance(fs_info);
3799
3800 mutex_unlock(&fs_info->volume_mutex);
3801 }
3802
3803 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3804 atomic_dec(&fs_info->balance_cancel_req);
3805 mutex_unlock(&fs_info->balance_mutex);
3806 return 0;
3807}
3808
Stefan Behrens803b2f52013-08-15 17:11:21 +02003809static int btrfs_uuid_scan_kthread(void *data)
3810{
3811 struct btrfs_fs_info *fs_info = data;
3812 struct btrfs_root *root = fs_info->tree_root;
3813 struct btrfs_key key;
3814 struct btrfs_key max_key;
3815 struct btrfs_path *path = NULL;
3816 int ret = 0;
3817 struct extent_buffer *eb;
3818 int slot;
3819 struct btrfs_root_item root_item;
3820 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003821 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003822
3823 path = btrfs_alloc_path();
3824 if (!path) {
3825 ret = -ENOMEM;
3826 goto out;
3827 }
3828
3829 key.objectid = 0;
3830 key.type = BTRFS_ROOT_ITEM_KEY;
3831 key.offset = 0;
3832
3833 max_key.objectid = (u64)-1;
3834 max_key.type = BTRFS_ROOT_ITEM_KEY;
3835 max_key.offset = (u64)-1;
3836
Stefan Behrens803b2f52013-08-15 17:11:21 +02003837 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003838 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003839 if (ret) {
3840 if (ret > 0)
3841 ret = 0;
3842 break;
3843 }
3844
3845 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3846 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3847 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3848 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3849 goto skip;
3850
3851 eb = path->nodes[0];
3852 slot = path->slots[0];
3853 item_size = btrfs_item_size_nr(eb, slot);
3854 if (item_size < sizeof(root_item))
3855 goto skip;
3856
Stefan Behrens803b2f52013-08-15 17:11:21 +02003857 read_extent_buffer(eb, &root_item,
3858 btrfs_item_ptr_offset(eb, slot),
3859 (int)sizeof(root_item));
3860 if (btrfs_root_refs(&root_item) == 0)
3861 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003862
3863 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3864 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3865 if (trans)
3866 goto update_tree;
3867
3868 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003869 /*
3870 * 1 - subvol uuid item
3871 * 1 - received_subvol uuid item
3872 */
3873 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3874 if (IS_ERR(trans)) {
3875 ret = PTR_ERR(trans);
3876 break;
3877 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003878 continue;
3879 } else {
3880 goto skip;
3881 }
3882update_tree:
3883 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003884 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3885 root_item.uuid,
3886 BTRFS_UUID_KEY_SUBVOL,
3887 key.objectid);
3888 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003889 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003890 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003891 break;
3892 }
3893 }
3894
3895 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003896 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3897 root_item.received_uuid,
3898 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3899 key.objectid);
3900 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003901 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003902 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003903 break;
3904 }
3905 }
3906
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003907skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003908 if (trans) {
3909 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003910 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003911 if (ret)
3912 break;
3913 }
3914
Stefan Behrens803b2f52013-08-15 17:11:21 +02003915 btrfs_release_path(path);
3916 if (key.offset < (u64)-1) {
3917 key.offset++;
3918 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3919 key.offset = 0;
3920 key.type = BTRFS_ROOT_ITEM_KEY;
3921 } else if (key.objectid < (u64)-1) {
3922 key.offset = 0;
3923 key.type = BTRFS_ROOT_ITEM_KEY;
3924 key.objectid++;
3925 } else {
3926 break;
3927 }
3928 cond_resched();
3929 }
3930
3931out:
3932 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003933 if (trans && !IS_ERR(trans))
3934 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003935 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05003936 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003937 else
3938 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003939 up(&fs_info->uuid_tree_rescan_sem);
3940 return 0;
3941}
3942
Stefan Behrens70f80172013-08-15 17:11:23 +02003943/*
3944 * Callback for btrfs_uuid_tree_iterate().
3945 * returns:
3946 * 0 check succeeded, the entry is not outdated.
3947 * < 0 if an error occured.
3948 * > 0 if the check failed, which means the caller shall remove the entry.
3949 */
3950static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3951 u8 *uuid, u8 type, u64 subid)
3952{
3953 struct btrfs_key key;
3954 int ret = 0;
3955 struct btrfs_root *subvol_root;
3956
3957 if (type != BTRFS_UUID_KEY_SUBVOL &&
3958 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3959 goto out;
3960
3961 key.objectid = subid;
3962 key.type = BTRFS_ROOT_ITEM_KEY;
3963 key.offset = (u64)-1;
3964 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3965 if (IS_ERR(subvol_root)) {
3966 ret = PTR_ERR(subvol_root);
3967 if (ret == -ENOENT)
3968 ret = 1;
3969 goto out;
3970 }
3971
3972 switch (type) {
3973 case BTRFS_UUID_KEY_SUBVOL:
3974 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3975 ret = 1;
3976 break;
3977 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3978 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3979 BTRFS_UUID_SIZE))
3980 ret = 1;
3981 break;
3982 }
3983
3984out:
3985 return ret;
3986}
3987
3988static int btrfs_uuid_rescan_kthread(void *data)
3989{
3990 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3991 int ret;
3992
3993 /*
3994 * 1st step is to iterate through the existing UUID tree and
3995 * to delete all entries that contain outdated data.
3996 * 2nd step is to add all missing entries to the UUID tree.
3997 */
3998 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3999 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004000 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004001 up(&fs_info->uuid_tree_rescan_sem);
4002 return ret;
4003 }
4004 return btrfs_uuid_scan_kthread(data);
4005}
4006
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004007int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4008{
4009 struct btrfs_trans_handle *trans;
4010 struct btrfs_root *tree_root = fs_info->tree_root;
4011 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004012 struct task_struct *task;
4013 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004014
4015 /*
4016 * 1 - root node
4017 * 1 - root item
4018 */
4019 trans = btrfs_start_transaction(tree_root, 2);
4020 if (IS_ERR(trans))
4021 return PTR_ERR(trans);
4022
4023 uuid_root = btrfs_create_tree(trans, fs_info,
4024 BTRFS_UUID_TREE_OBJECTID);
4025 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004026 ret = PTR_ERR(uuid_root);
4027 btrfs_abort_transaction(trans, tree_root, ret);
4028 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004029 }
4030
4031 fs_info->uuid_root = uuid_root;
4032
Stefan Behrens803b2f52013-08-15 17:11:21 +02004033 ret = btrfs_commit_transaction(trans, tree_root);
4034 if (ret)
4035 return ret;
4036
4037 down(&fs_info->uuid_tree_rescan_sem);
4038 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4039 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004040 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004041 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004042 up(&fs_info->uuid_tree_rescan_sem);
4043 return PTR_ERR(task);
4044 }
4045
4046 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004047}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004048
Stefan Behrens70f80172013-08-15 17:11:23 +02004049int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4050{
4051 struct task_struct *task;
4052
4053 down(&fs_info->uuid_tree_rescan_sem);
4054 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4055 if (IS_ERR(task)) {
4056 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004057 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004058 up(&fs_info->uuid_tree_rescan_sem);
4059 return PTR_ERR(task);
4060 }
4061
4062 return 0;
4063}
4064
Chris Mason8f18cf12008-04-25 16:53:30 -04004065/*
4066 * shrinking a device means finding all of the device extents past
4067 * the new size, and then following the back refs to the chunks.
4068 * The chunk relocation code actually frees the device extent
4069 */
4070int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4071{
4072 struct btrfs_trans_handle *trans;
4073 struct btrfs_root *root = device->dev_root;
4074 struct btrfs_dev_extent *dev_extent = NULL;
4075 struct btrfs_path *path;
4076 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004077 u64 chunk_offset;
4078 int ret;
4079 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004080 int failed = 0;
4081 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004082 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004083 struct extent_buffer *l;
4084 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02004085 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004086 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004087 u64 old_size = btrfs_device_get_total_bytes(device);
4088 u64 diff = old_size - new_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04004089
Stefan Behrens63a212a2012-11-05 18:29:28 +01004090 if (device->is_tgtdev_for_dev_replace)
4091 return -EINVAL;
4092
Chris Mason8f18cf12008-04-25 16:53:30 -04004093 path = btrfs_alloc_path();
4094 if (!path)
4095 return -ENOMEM;
4096
Chris Mason8f18cf12008-04-25 16:53:30 -04004097 path->reada = 2;
4098
Chris Mason7d9eb122008-07-08 14:19:17 -04004099 lock_chunks(root);
4100
Miao Xie7cc8e582014-09-03 21:35:38 +08004101 btrfs_device_set_total_bytes(device, new_size);
Josef Bacik2bf64752011-09-26 17:12:22 -04004102 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004103 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04004104 spin_lock(&root->fs_info->free_chunk_lock);
4105 root->fs_info->free_chunk_space -= diff;
4106 spin_unlock(&root->fs_info->free_chunk_lock);
4107 }
Chris Mason7d9eb122008-07-08 14:19:17 -04004108 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004109
Josef Bacikba1bf482009-09-11 16:11:19 -04004110again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004111 key.objectid = device->devid;
4112 key.offset = (u64)-1;
4113 key.type = BTRFS_DEV_EXTENT_KEY;
4114
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004115 do {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004116 mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004117 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004118 if (ret < 0) {
4119 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004120 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004121 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004122
4123 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004124 if (ret)
4125 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004126 if (ret < 0)
4127 goto done;
4128 if (ret) {
4129 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004130 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004131 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004132 }
4133
4134 l = path->nodes[0];
4135 slot = path->slots[0];
4136 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4137
Josef Bacikba1bf482009-09-11 16:11:19 -04004138 if (key.objectid != device->devid) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004139 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004140 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004141 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004142 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004143
4144 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4145 length = btrfs_dev_extent_length(l, dev_extent);
4146
Josef Bacikba1bf482009-09-11 16:11:19 -04004147 if (key.offset + length <= new_size) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004148 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004149 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004150 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004151 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004152
Chris Mason8f18cf12008-04-25 16:53:30 -04004153 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004154 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004155
Zhaoleidc2ee4e2015-08-05 18:00:04 +08004156 ret = btrfs_relocate_chunk(root, chunk_offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004157 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04004158 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004159 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004160 if (ret == -ENOSPC)
4161 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004162 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004163
4164 if (failed && !retried) {
4165 failed = 0;
4166 retried = true;
4167 goto again;
4168 } else if (failed && retried) {
4169 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004170 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004171 }
4172
Chris Balld6397ba2009-04-27 07:29:03 -04004173 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004174 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004175 if (IS_ERR(trans)) {
4176 ret = PTR_ERR(trans);
4177 goto done;
4178 }
4179
Chris Balld6397ba2009-04-27 07:29:03 -04004180 lock_chunks(root);
Filipe Manana53e489b2015-06-02 14:43:21 +01004181
4182 /*
4183 * We checked in the above loop all device extents that were already in
4184 * the device tree. However before we have updated the device's
4185 * total_bytes to the new size, we might have had chunk allocations that
4186 * have not complete yet (new block groups attached to transaction
4187 * handles), and therefore their device extents were not yet in the
4188 * device tree and we missed them in the loop above. So if we have any
4189 * pending chunk using a device extent that overlaps the device range
4190 * that we can not use anymore, commit the current transaction and
4191 * repeat the search on the device tree - this way we guarantee we will
4192 * not have chunks using device extents that end beyond 'new_size'.
4193 */
4194 if (!checked_pending_chunks) {
4195 u64 start = new_size;
4196 u64 len = old_size - new_size;
4197
Jeff Mahoney499f3772015-06-15 09:41:17 -04004198 if (contains_pending_extent(trans->transaction, device,
4199 &start, len)) {
Filipe Manana53e489b2015-06-02 14:43:21 +01004200 unlock_chunks(root);
4201 checked_pending_chunks = true;
4202 failed = 0;
4203 retried = false;
4204 ret = btrfs_commit_transaction(trans, root);
4205 if (ret)
4206 goto done;
4207 goto again;
4208 }
4209 }
4210
Miao Xie7cc8e582014-09-03 21:35:38 +08004211 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004212 if (list_empty(&device->resized_list))
4213 list_add_tail(&device->resized_list,
4214 &root->fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004215
Chris Balld6397ba2009-04-27 07:29:03 -04004216 WARN_ON(diff > old_total);
4217 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4218 unlock_chunks(root);
Miao Xie2196d6e2014-09-03 21:35:41 +08004219
4220 /* Now btrfs_update_device() will change the on-disk size. */
4221 ret = btrfs_update_device(trans, device);
Chris Balld6397ba2009-04-27 07:29:03 -04004222 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004223done:
4224 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004225 if (ret) {
4226 lock_chunks(root);
4227 btrfs_device_set_total_bytes(device, old_size);
4228 if (device->writeable)
4229 device->fs_devices->total_rw_bytes += diff;
4230 spin_lock(&root->fs_info->free_chunk_lock);
4231 root->fs_info->free_chunk_space += diff;
4232 spin_unlock(&root->fs_info->free_chunk_lock);
4233 unlock_chunks(root);
4234 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004235 return ret;
4236}
4237
Li Zefan125ccb02011-12-08 15:07:24 +08004238static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004239 struct btrfs_key *key,
4240 struct btrfs_chunk *chunk, int item_size)
4241{
David Sterba6c417612011-04-13 15:41:04 +02004242 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004243 struct btrfs_disk_key disk_key;
4244 u32 array_size;
4245 u8 *ptr;
4246
Miao Xiefe48a5c2014-09-03 21:35:39 +08004247 lock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004248 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004249 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004250 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4251 unlock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004252 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004253 }
Chris Mason0b86a832008-03-24 15:01:56 -04004254
4255 ptr = super_copy->sys_chunk_array + array_size;
4256 btrfs_cpu_key_to_disk(&disk_key, key);
4257 memcpy(ptr, &disk_key, sizeof(disk_key));
4258 ptr += sizeof(disk_key);
4259 memcpy(ptr, chunk, item_size);
4260 item_size += sizeof(disk_key);
4261 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004262 unlock_chunks(root);
4263
Chris Mason0b86a832008-03-24 15:01:56 -04004264 return 0;
4265}
4266
Miao Xieb2117a32011-01-05 10:07:28 +00004267/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004268 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004269 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004270static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004271{
Arne Jansen73c5de02011-04-12 12:07:57 +02004272 const struct btrfs_device_info *di_a = a;
4273 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004274
Arne Jansen73c5de02011-04-12 12:07:57 +02004275 if (di_a->max_avail > di_b->max_avail)
4276 return -1;
4277 if (di_a->max_avail < di_b->max_avail)
4278 return 1;
4279 if (di_a->total_avail > di_b->total_avail)
4280 return -1;
4281 if (di_a->total_avail < di_b->total_avail)
4282 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004283 return 0;
4284}
4285
David Sterbae8c9f182015-01-02 18:23:10 +01004286static const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00004287 [BTRFS_RAID_RAID10] = {
4288 .sub_stripes = 2,
4289 .dev_stripes = 1,
4290 .devs_max = 0, /* 0 == as many as possible */
4291 .devs_min = 4,
4292 .devs_increment = 2,
4293 .ncopies = 2,
4294 },
4295 [BTRFS_RAID_RAID1] = {
4296 .sub_stripes = 1,
4297 .dev_stripes = 1,
4298 .devs_max = 2,
4299 .devs_min = 2,
4300 .devs_increment = 2,
4301 .ncopies = 2,
4302 },
4303 [BTRFS_RAID_DUP] = {
4304 .sub_stripes = 1,
4305 .dev_stripes = 2,
4306 .devs_max = 1,
4307 .devs_min = 1,
4308 .devs_increment = 1,
4309 .ncopies = 2,
4310 },
4311 [BTRFS_RAID_RAID0] = {
4312 .sub_stripes = 1,
4313 .dev_stripes = 1,
4314 .devs_max = 0,
4315 .devs_min = 2,
4316 .devs_increment = 1,
4317 .ncopies = 1,
4318 },
4319 [BTRFS_RAID_SINGLE] = {
4320 .sub_stripes = 1,
4321 .dev_stripes = 1,
4322 .devs_max = 1,
4323 .devs_min = 1,
4324 .devs_increment = 1,
4325 .ncopies = 1,
4326 },
Chris Masone942f882013-02-20 14:06:05 -05004327 [BTRFS_RAID_RAID5] = {
4328 .sub_stripes = 1,
4329 .dev_stripes = 1,
4330 .devs_max = 0,
4331 .devs_min = 2,
4332 .devs_increment = 1,
4333 .ncopies = 2,
4334 },
4335 [BTRFS_RAID_RAID6] = {
4336 .sub_stripes = 1,
4337 .dev_stripes = 1,
4338 .devs_max = 0,
4339 .devs_min = 3,
4340 .devs_increment = 1,
4341 .ncopies = 3,
4342 },
Liu Bo31e50222012-11-21 14:18:10 +00004343};
4344
David Woodhouse53b381b2013-01-29 18:40:14 -05004345static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4346{
4347 /* TODO allow them to set a preferred stripe size */
4348 return 64 * 1024;
4349}
4350
4351static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4352{
Zhao Leiffe2d202015-01-20 15:11:44 +08004353 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004354 return;
4355
Miao Xieceda0862013-04-11 10:30:16 +00004356 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004357}
4358
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004359#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4360 - sizeof(struct btrfs_item) \
4361 - sizeof(struct btrfs_chunk)) \
4362 / sizeof(struct btrfs_stripe) + 1)
4363
4364#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4365 - 2 * sizeof(struct btrfs_disk_key) \
4366 - 2 * sizeof(struct btrfs_chunk)) \
4367 / sizeof(struct btrfs_stripe) + 1)
4368
Miao Xieb2117a32011-01-05 10:07:28 +00004369static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004370 struct btrfs_root *extent_root, u64 start,
4371 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004372{
4373 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004374 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4375 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004376 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004377 struct extent_map_tree *em_tree;
4378 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004379 struct btrfs_device_info *devices_info = NULL;
4380 u64 total_avail;
4381 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004382 int data_stripes; /* number of stripes that count for
4383 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004384 int sub_stripes; /* sub_stripes info for map */
4385 int dev_stripes; /* stripes per dev */
4386 int devs_max; /* max devs to use */
4387 int devs_min; /* min devs needed */
4388 int devs_increment; /* ndevs has to be a multiple of this */
4389 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004390 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004391 u64 max_stripe_size;
4392 u64 max_chunk_size;
4393 u64 stripe_size;
4394 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004395 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004396 int ndevs;
4397 int i;
4398 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004399 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004400
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004401 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004402
Miao Xieb2117a32011-01-05 10:07:28 +00004403 if (list_empty(&fs_devices->alloc_list))
4404 return -ENOSPC;
4405
Liu Bo31e50222012-11-21 14:18:10 +00004406 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004407
Liu Bo31e50222012-11-21 14:18:10 +00004408 sub_stripes = btrfs_raid_array[index].sub_stripes;
4409 dev_stripes = btrfs_raid_array[index].dev_stripes;
4410 devs_max = btrfs_raid_array[index].devs_max;
4411 devs_min = btrfs_raid_array[index].devs_min;
4412 devs_increment = btrfs_raid_array[index].devs_increment;
4413 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004414
4415 if (type & BTRFS_BLOCK_GROUP_DATA) {
4416 max_stripe_size = 1024 * 1024 * 1024;
4417 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004418 if (!devs_max)
4419 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004420 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004421 /* for larger filesystems, use larger metadata chunks */
4422 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4423 max_stripe_size = 1024 * 1024 * 1024;
4424 else
4425 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004426 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004427 if (!devs_max)
4428 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004429 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004430 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004431 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004432 if (!devs_max)
4433 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004434 } else {
David Sterba351fd352014-05-15 16:48:20 +02004435 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004436 type);
4437 BUG_ON(1);
4438 }
4439
4440 /* we don't want a chunk larger than 10% of writeable space */
4441 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4442 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004443
David Sterba31e818f2015-02-20 18:00:26 +01004444 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004445 GFP_NOFS);
4446 if (!devices_info)
4447 return -ENOMEM;
4448
Arne Jansen73c5de02011-04-12 12:07:57 +02004449 cur = fs_devices->alloc_list.next;
4450
4451 /*
4452 * in the first pass through the devices list, we gather information
4453 * about the available holes on each device.
4454 */
4455 ndevs = 0;
4456 while (cur != &fs_devices->alloc_list) {
4457 struct btrfs_device *device;
4458 u64 max_avail;
4459 u64 dev_offset;
4460
4461 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4462
4463 cur = cur->next;
4464
4465 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004466 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004467 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004468 continue;
4469 }
4470
Stefan Behrens63a212a2012-11-05 18:29:28 +01004471 if (!device->in_fs_metadata ||
4472 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004473 continue;
4474
4475 if (device->total_bytes > device->bytes_used)
4476 total_avail = device->total_bytes - device->bytes_used;
4477 else
4478 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004479
4480 /* If there is no space on this device, skip it. */
4481 if (total_avail == 0)
4482 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004483
Josef Bacik6df9a952013-06-27 13:22:46 -04004484 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004485 max_stripe_size * dev_stripes,
4486 &dev_offset, &max_avail);
4487 if (ret && ret != -ENOSPC)
4488 goto error;
4489
4490 if (ret == 0)
4491 max_avail = max_stripe_size * dev_stripes;
4492
4493 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4494 continue;
4495
Eric Sandeen063d0062013-01-31 00:55:01 +00004496 if (ndevs == fs_devices->rw_devices) {
4497 WARN(1, "%s: found more than %llu devices\n",
4498 __func__, fs_devices->rw_devices);
4499 break;
4500 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004501 devices_info[ndevs].dev_offset = dev_offset;
4502 devices_info[ndevs].max_avail = max_avail;
4503 devices_info[ndevs].total_avail = total_avail;
4504 devices_info[ndevs].dev = device;
4505 ++ndevs;
4506 }
4507
4508 /*
4509 * now sort the devices by hole size / available space
4510 */
4511 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4512 btrfs_cmp_device_info, NULL);
4513
4514 /* round down to number of usable stripes */
4515 ndevs -= ndevs % devs_increment;
4516
4517 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4518 ret = -ENOSPC;
4519 goto error;
4520 }
4521
4522 if (devs_max && ndevs > devs_max)
4523 ndevs = devs_max;
4524 /*
4525 * the primary goal is to maximize the number of stripes, so use as many
4526 * devices as possible, even if the stripes are not maximum sized.
4527 */
4528 stripe_size = devices_info[ndevs-1].max_avail;
4529 num_stripes = ndevs * dev_stripes;
4530
David Woodhouse53b381b2013-01-29 18:40:14 -05004531 /*
4532 * this will have to be fixed for RAID1 and RAID10 over
4533 * more drives
4534 */
4535 data_stripes = num_stripes / ncopies;
4536
David Woodhouse53b381b2013-01-29 18:40:14 -05004537 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4538 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4539 btrfs_super_stripesize(info->super_copy));
4540 data_stripes = num_stripes - 1;
4541 }
4542 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4543 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4544 btrfs_super_stripesize(info->super_copy));
4545 data_stripes = num_stripes - 2;
4546 }
Chris Mason86db2572013-02-20 16:23:40 -05004547
4548 /*
4549 * Use the number of data stripes to figure out how big this chunk
4550 * is really going to be in terms of logical address space,
4551 * and compare that answer with the max chunk size
4552 */
4553 if (stripe_size * data_stripes > max_chunk_size) {
4554 u64 mask = (1ULL << 24) - 1;
David Sterbab8b93ad2015-01-16 17:26:13 +01004555
4556 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004557
4558 /* bump the answer up to a 16MB boundary */
4559 stripe_size = (stripe_size + mask) & ~mask;
4560
4561 /* but don't go higher than the limits we found
4562 * while searching for free extents
4563 */
4564 if (stripe_size > devices_info[ndevs-1].max_avail)
4565 stripe_size = devices_info[ndevs-1].max_avail;
4566 }
4567
David Sterbab8b93ad2015-01-16 17:26:13 +01004568 stripe_size = div_u64(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004569
4570 /* align to BTRFS_STRIPE_LEN */
David Sterbab8b93ad2015-01-16 17:26:13 +01004571 stripe_size = div_u64(stripe_size, raid_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05004572 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004573
Miao Xieb2117a32011-01-05 10:07:28 +00004574 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4575 if (!map) {
4576 ret = -ENOMEM;
4577 goto error;
4578 }
4579 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004580
Arne Jansen73c5de02011-04-12 12:07:57 +02004581 for (i = 0; i < ndevs; ++i) {
4582 for (j = 0; j < dev_stripes; ++j) {
4583 int s = i * dev_stripes + j;
4584 map->stripes[s].dev = devices_info[i].dev;
4585 map->stripes[s].physical = devices_info[i].dev_offset +
4586 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004587 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004588 }
Chris Mason593060d2008-03-25 16:50:33 -04004589 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004590 map->stripe_len = raid_stripe_len;
4591 map->io_align = raid_stripe_len;
4592 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004593 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004594 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004595
David Woodhouse53b381b2013-01-29 18:40:14 -05004596 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004597
Arne Jansen73c5de02011-04-12 12:07:57 +02004598 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004599
David Sterba172ddd62011-04-21 00:48:27 +02004600 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004601 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004602 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004603 ret = -ENOMEM;
4604 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004605 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004606 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004607 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004608 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004609 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004610 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004611 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004612 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004613
Chris Mason0b86a832008-03-24 15:01:56 -04004614 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004615 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004616 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004617 if (!ret) {
4618 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4619 atomic_inc(&em->refs);
4620 }
Chris Mason890871b2009-09-02 16:24:52 -04004621 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004622 if (ret) {
4623 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004624 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004625 }
Yan Zheng2b820322008-11-17 21:11:30 -05004626
Josef Bacik04487482013-01-29 15:03:37 -05004627 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4628 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4629 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004630 if (ret)
4631 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004632
Miao Xie7cc8e582014-09-03 21:35:38 +08004633 for (i = 0; i < map->num_stripes; i++) {
4634 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4635 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4636 }
Miao Xie43530c42014-09-03 21:35:36 +08004637
Miao Xie1c116182014-09-03 21:35:37 +08004638 spin_lock(&extent_root->fs_info->free_chunk_lock);
4639 extent_root->fs_info->free_chunk_space -= (stripe_size *
4640 map->num_stripes);
4641 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4642
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004643 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004644 check_raid56_incompat_flag(extent_root->fs_info, type);
4645
Miao Xieb2117a32011-01-05 10:07:28 +00004646 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004647 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004648
Josef Bacik6df9a952013-06-27 13:22:46 -04004649error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004650 write_lock(&em_tree->lock);
4651 remove_extent_mapping(em_tree, em);
4652 write_unlock(&em_tree->lock);
4653
4654 /* One for our allocation */
4655 free_extent_map(em);
4656 /* One for the tree reference */
4657 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004658 /* One for the pending_chunks list reference */
4659 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004660error:
Miao Xieb2117a32011-01-05 10:07:28 +00004661 kfree(devices_info);
4662 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004663}
4664
Josef Bacik6df9a952013-06-27 13:22:46 -04004665int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004666 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004667 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004668{
Yan Zheng2b820322008-11-17 21:11:30 -05004669 struct btrfs_key key;
4670 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4671 struct btrfs_device *device;
4672 struct btrfs_chunk *chunk;
4673 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004674 struct extent_map_tree *em_tree;
4675 struct extent_map *em;
4676 struct map_lookup *map;
4677 size_t item_size;
4678 u64 dev_offset;
4679 u64 stripe_size;
4680 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004681 int ret;
4682
Josef Bacik6df9a952013-06-27 13:22:46 -04004683 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4684 read_lock(&em_tree->lock);
4685 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4686 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004687
Josef Bacik6df9a952013-06-27 13:22:46 -04004688 if (!em) {
4689 btrfs_crit(extent_root->fs_info, "unable to find logical "
4690 "%Lu len %Lu", chunk_offset, chunk_size);
4691 return -EINVAL;
4692 }
4693
4694 if (em->start != chunk_offset || em->len != chunk_size) {
4695 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004696 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004697 chunk_size, em->start, em->len);
4698 free_extent_map(em);
4699 return -EINVAL;
4700 }
4701
4702 map = (struct map_lookup *)em->bdev;
4703 item_size = btrfs_chunk_item_size(map->num_stripes);
4704 stripe_size = em->orig_block_len;
4705
4706 chunk = kzalloc(item_size, GFP_NOFS);
4707 if (!chunk) {
4708 ret = -ENOMEM;
4709 goto out;
4710 }
4711
4712 for (i = 0; i < map->num_stripes; i++) {
4713 device = map->stripes[i].dev;
4714 dev_offset = map->stripes[i].physical;
4715
Yan Zheng2b820322008-11-17 21:11:30 -05004716 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004717 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004718 goto out;
4719 ret = btrfs_alloc_dev_extent(trans, device,
4720 chunk_root->root_key.objectid,
4721 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4722 chunk_offset, dev_offset,
4723 stripe_size);
4724 if (ret)
4725 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004726 }
4727
Yan Zheng2b820322008-11-17 21:11:30 -05004728 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004729 for (i = 0; i < map->num_stripes; i++) {
4730 device = map->stripes[i].dev;
4731 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004732
4733 btrfs_set_stack_stripe_devid(stripe, device->devid);
4734 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4735 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4736 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004737 }
4738
4739 btrfs_set_stack_chunk_length(chunk, chunk_size);
4740 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4741 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4742 btrfs_set_stack_chunk_type(chunk, map->type);
4743 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4744 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4745 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4746 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4747 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4748
4749 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4750 key.type = BTRFS_CHUNK_ITEM_KEY;
4751 key.offset = chunk_offset;
4752
4753 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004754 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4755 /*
4756 * TODO: Cleanup of inserted chunk root in case of
4757 * failure.
4758 */
Li Zefan125ccb02011-12-08 15:07:24 +08004759 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004760 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004761 }
liubo1abe9b82011-03-24 11:18:59 +00004762
Josef Bacik6df9a952013-06-27 13:22:46 -04004763out:
Yan Zheng2b820322008-11-17 21:11:30 -05004764 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004765 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004766 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004767}
4768
4769/*
4770 * Chunk allocation falls into two parts. The first part does works
4771 * that make the new allocated chunk useable, but not do any operation
4772 * that modifies the chunk tree. The second part does the works that
4773 * require modifying the chunk tree. This division is important for the
4774 * bootstrap process of adding storage to a seed btrfs.
4775 */
4776int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4777 struct btrfs_root *extent_root, u64 type)
4778{
4779 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004780
Filipe Mananaa9629592015-05-18 19:11:40 +01004781 ASSERT(mutex_is_locked(&extent_root->fs_info->chunk_mutex));
Josef Bacik6df9a952013-06-27 13:22:46 -04004782 chunk_offset = find_next_chunk(extent_root->fs_info);
4783 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004784}
4785
Chris Masond3977122009-01-05 21:25:51 -05004786static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004787 struct btrfs_root *root,
4788 struct btrfs_device *device)
4789{
4790 u64 chunk_offset;
4791 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004792 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004793 struct btrfs_fs_info *fs_info = root->fs_info;
4794 struct btrfs_root *extent_root = fs_info->extent_root;
4795 int ret;
4796
Josef Bacik6df9a952013-06-27 13:22:46 -04004797 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004798 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004799 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4800 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004801 if (ret)
4802 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004803
Josef Bacik6df9a952013-06-27 13:22:46 -04004804 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004805 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004806 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4807 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004808 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004809}
4810
Miao Xied20983b2014-07-03 18:22:13 +08004811static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4812{
4813 int max_errors;
4814
4815 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4816 BTRFS_BLOCK_GROUP_RAID10 |
4817 BTRFS_BLOCK_GROUP_RAID5 |
4818 BTRFS_BLOCK_GROUP_DUP)) {
4819 max_errors = 1;
4820 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4821 max_errors = 2;
4822 } else {
4823 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004824 }
4825
Miao Xied20983b2014-07-03 18:22:13 +08004826 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05004827}
4828
4829int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4830{
4831 struct extent_map *em;
4832 struct map_lookup *map;
4833 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4834 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004835 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004836 int i;
4837
Chris Mason890871b2009-09-02 16:24:52 -04004838 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004839 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004840 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004841 if (!em)
4842 return 1;
4843
4844 map = (struct map_lookup *)em->bdev;
4845 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004846 if (map->stripes[i].dev->missing) {
4847 miss_ndevs++;
4848 continue;
4849 }
4850
Yan Zheng2b820322008-11-17 21:11:30 -05004851 if (!map->stripes[i].dev->writeable) {
4852 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004853 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004854 }
4855 }
Miao Xied20983b2014-07-03 18:22:13 +08004856
4857 /*
4858 * If the number of missing devices is larger than max errors,
4859 * we can not write the data into that chunk successfully, so
4860 * set it readonly.
4861 */
4862 if (miss_ndevs > btrfs_chunk_max_errors(map))
4863 readonly = 1;
4864end:
Yan Zheng2b820322008-11-17 21:11:30 -05004865 free_extent_map(em);
4866 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004867}
4868
4869void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4870{
David Sterbaa8067e02011-04-21 00:34:43 +02004871 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004872}
4873
4874void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4875{
4876 struct extent_map *em;
4877
Chris Masond3977122009-01-05 21:25:51 -05004878 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004879 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004880 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4881 if (em)
4882 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004883 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004884 if (!em)
4885 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004886 /* once for us */
4887 free_extent_map(em);
4888 /* once for the tree */
4889 free_extent_map(em);
4890 }
4891}
4892
Stefan Behrens5d964052012-11-05 14:59:07 +01004893int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004894{
Stefan Behrens5d964052012-11-05 14:59:07 +01004895 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004896 struct extent_map *em;
4897 struct map_lookup *map;
4898 struct extent_map_tree *em_tree = &map_tree->map_tree;
4899 int ret;
4900
Chris Mason890871b2009-09-02 16:24:52 -04004901 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004902 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004903 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004904
Josef Bacikfb7669b2013-04-23 10:53:18 -04004905 /*
4906 * We could return errors for these cases, but that could get ugly and
4907 * we'd probably do the same thing which is just not do anything else
4908 * and exit, so return 1 so the callers don't try to use other copies.
4909 */
4910 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004911 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004912 logical+len);
4913 return 1;
4914 }
4915
4916 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004917 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004918 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004919 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004920 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004921 return 1;
4922 }
4923
Chris Masonf1885912008-04-09 16:28:12 -04004924 map = (struct map_lookup *)em->bdev;
4925 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4926 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004927 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4928 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004929 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4930 ret = 2;
4931 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4932 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004933 else
4934 ret = 1;
4935 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004936
4937 btrfs_dev_replace_lock(&fs_info->dev_replace);
4938 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4939 ret++;
4940 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4941
Chris Masonf1885912008-04-09 16:28:12 -04004942 return ret;
4943}
4944
David Woodhouse53b381b2013-01-29 18:40:14 -05004945unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4946 struct btrfs_mapping_tree *map_tree,
4947 u64 logical)
4948{
4949 struct extent_map *em;
4950 struct map_lookup *map;
4951 struct extent_map_tree *em_tree = &map_tree->map_tree;
4952 unsigned long len = root->sectorsize;
4953
4954 read_lock(&em_tree->lock);
4955 em = lookup_extent_mapping(em_tree, logical, len);
4956 read_unlock(&em_tree->lock);
4957 BUG_ON(!em);
4958
4959 BUG_ON(em->start > logical || em->start + em->len < logical);
4960 map = (struct map_lookup *)em->bdev;
Zhao Leiffe2d202015-01-20 15:11:44 +08004961 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05004962 len = map->stripe_len * nr_data_stripes(map);
David Woodhouse53b381b2013-01-29 18:40:14 -05004963 free_extent_map(em);
4964 return len;
4965}
4966
4967int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4968 u64 logical, u64 len, int mirror_num)
4969{
4970 struct extent_map *em;
4971 struct map_lookup *map;
4972 struct extent_map_tree *em_tree = &map_tree->map_tree;
4973 int ret = 0;
4974
4975 read_lock(&em_tree->lock);
4976 em = lookup_extent_mapping(em_tree, logical, len);
4977 read_unlock(&em_tree->lock);
4978 BUG_ON(!em);
4979
4980 BUG_ON(em->start > logical || em->start + em->len < logical);
4981 map = (struct map_lookup *)em->bdev;
Zhao Leiffe2d202015-01-20 15:11:44 +08004982 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05004983 ret = 1;
4984 free_extent_map(em);
4985 return ret;
4986}
4987
Stefan Behrens30d98612012-11-06 14:52:18 +01004988static int find_live_mirror(struct btrfs_fs_info *fs_info,
4989 struct map_lookup *map, int first, int num,
4990 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004991{
4992 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004993 int tolerance;
4994 struct btrfs_device *srcdev;
4995
4996 if (dev_replace_is_ongoing &&
4997 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4998 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4999 srcdev = fs_info->dev_replace.srcdev;
5000 else
5001 srcdev = NULL;
5002
5003 /*
5004 * try to avoid the drive that is the source drive for a
5005 * dev-replace procedure, only choose it if no other non-missing
5006 * mirror is available
5007 */
5008 for (tolerance = 0; tolerance < 2; tolerance++) {
5009 if (map->stripes[optimal].dev->bdev &&
5010 (tolerance || map->stripes[optimal].dev != srcdev))
5011 return optimal;
5012 for (i = first; i < first + num; i++) {
5013 if (map->stripes[i].dev->bdev &&
5014 (tolerance || map->stripes[i].dev != srcdev))
5015 return i;
5016 }
Chris Masondfe25022008-05-13 13:46:40 -04005017 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005018
Chris Masondfe25022008-05-13 13:46:40 -04005019 /* we couldn't find one that doesn't fail. Just return something
5020 * and the io error handling code will clean up eventually
5021 */
5022 return optimal;
5023}
5024
David Woodhouse53b381b2013-01-29 18:40:14 -05005025static inline int parity_smaller(u64 a, u64 b)
5026{
5027 return a > b;
5028}
5029
5030/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005031static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005032{
5033 struct btrfs_bio_stripe s;
5034 int i;
5035 u64 l;
5036 int again = 1;
5037
5038 while (again) {
5039 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005040 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005041 if (parity_smaller(bbio->raid_map[i],
5042 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005043 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005044 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005045 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005046 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005047 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005048 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005049
David Woodhouse53b381b2013-01-29 18:40:14 -05005050 again = 1;
5051 }
5052 }
5053 }
5054}
5055
Zhao Lei6e9606d2015-01-20 15:11:34 +08005056static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5057{
5058 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005059 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005060 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005061 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005062 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005063 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005064 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005065 /*
5066 * plus the raid_map, which includes both the tgt dev
5067 * and the stripes
5068 */
5069 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005070 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005071
5072 atomic_set(&bbio->error, 0);
5073 atomic_set(&bbio->refs, 1);
5074
5075 return bbio;
5076}
5077
5078void btrfs_get_bbio(struct btrfs_bio *bbio)
5079{
5080 WARN_ON(!atomic_read(&bbio->refs));
5081 atomic_inc(&bbio->refs);
5082}
5083
5084void btrfs_put_bbio(struct btrfs_bio *bbio)
5085{
5086 if (!bbio)
5087 return;
5088 if (atomic_dec_and_test(&bbio->refs))
5089 kfree(bbio);
5090}
5091
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005092static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005093 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005094 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005095 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005096{
5097 struct extent_map *em;
5098 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005099 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04005100 struct extent_map_tree *em_tree = &map_tree->map_tree;
5101 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005102 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005103 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04005104 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005105 u64 stripe_nr_orig;
5106 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05005107 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005108 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005109 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005110 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005111 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005112 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005113 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005114 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005115 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5116 int dev_replace_is_ongoing = 0;
5117 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005118 int patch_the_first_stripe_for_dev_replace = 0;
5119 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005120 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005121
Chris Mason890871b2009-09-02 16:24:52 -04005122 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005123 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04005124 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04005125
Chris Mason3b951512008-04-17 11:29:12 -04005126 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005127 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005128 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04005129 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04005130 }
Chris Mason0b86a832008-03-24 15:01:56 -04005131
Josef Bacik9bb91872013-04-19 23:45:33 -04005132 if (em->start > logical || em->start + em->len < logical) {
5133 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02005134 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04005135 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08005136 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005137 return -EINVAL;
5138 }
5139
Chris Mason0b86a832008-03-24 15:01:56 -04005140 map = (struct map_lookup *)em->bdev;
5141 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005142
David Woodhouse53b381b2013-01-29 18:40:14 -05005143 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005144 stripe_nr = offset;
5145 /*
5146 * stripe_nr counts the total number of stripes we have to stride
5147 * to get to this block
5148 */
David Sterba47c57132015-02-20 18:43:47 +01005149 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005150
David Woodhouse53b381b2013-01-29 18:40:14 -05005151 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005152 BUG_ON(offset < stripe_offset);
5153
5154 /* stripe_offset is the offset of this block in its stripe*/
5155 stripe_offset = offset - stripe_offset;
5156
David Woodhouse53b381b2013-01-29 18:40:14 -05005157 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005158 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005159 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5160 raid56_full_stripe_start = offset;
5161
5162 /* allow a write of a full stripe, but make sure we don't
5163 * allow straddling of stripes
5164 */
David Sterba47c57132015-02-20 18:43:47 +01005165 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5166 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005167 raid56_full_stripe_start *= full_stripe_len;
5168 }
5169
5170 if (rw & REQ_DISCARD) {
5171 /* we don't discard raid56 yet */
Zhao Leiffe2d202015-01-20 15:11:44 +08005172 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005173 ret = -EOPNOTSUPP;
5174 goto out;
5175 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005176 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005177 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5178 u64 max_len;
5179 /* For writes to RAID[56], allow a full stripeset across all disks.
5180 For other RAID types and for RAID[56] reads, just allow a single
5181 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005182 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
David Woodhouse53b381b2013-01-29 18:40:14 -05005183 (rw & REQ_WRITE)) {
5184 max_len = stripe_len * nr_data_stripes(map) -
5185 (offset - raid56_full_stripe_start);
5186 } else {
5187 /* we limit the length of each bio to what fits in a stripe */
5188 max_len = stripe_len - stripe_offset;
5189 }
5190 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005191 } else {
5192 *length = em->len - offset;
5193 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005194
David Woodhouse53b381b2013-01-29 18:40:14 -05005195 /* This is for when we're called from btrfs_merge_bio_hook() and all
5196 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005197 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005198 goto out;
5199
Stefan Behrens472262f2012-11-06 14:43:46 +01005200 btrfs_dev_replace_lock(dev_replace);
5201 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5202 if (!dev_replace_is_ongoing)
5203 btrfs_dev_replace_unlock(dev_replace);
5204
Stefan Behrensad6d6202012-11-06 15:06:47 +01005205 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
5206 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
5207 dev_replace->tgtdev != NULL) {
5208 /*
5209 * in dev-replace case, for repair case (that's the only
5210 * case where the mirror is selected explicitly when
5211 * calling btrfs_map_block), blocks left of the left cursor
5212 * can also be read from the target drive.
5213 * For REQ_GET_READ_MIRRORS, the target drive is added as
5214 * the last one to the array of stripes. For READ, it also
5215 * needs to be supported using the same mirror number.
5216 * If the requested block is not left of the left cursor,
5217 * EIO is returned. This can happen because btrfs_num_copies()
5218 * returns one more in the dev-replace case.
5219 */
5220 u64 tmp_length = *length;
5221 struct btrfs_bio *tmp_bbio = NULL;
5222 int tmp_num_stripes;
5223 u64 srcdev_devid = dev_replace->srcdev->devid;
5224 int index_srcdev = 0;
5225 int found = 0;
5226 u64 physical_of_found = 0;
5227
5228 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005229 logical, &tmp_length, &tmp_bbio, 0, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005230 if (ret) {
5231 WARN_ON(tmp_bbio != NULL);
5232 goto out;
5233 }
5234
5235 tmp_num_stripes = tmp_bbio->num_stripes;
5236 if (mirror_num > tmp_num_stripes) {
5237 /*
5238 * REQ_GET_READ_MIRRORS does not contain this
5239 * mirror, that means that the requested area
5240 * is not left of the left cursor
5241 */
5242 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005243 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005244 goto out;
5245 }
5246
5247 /*
5248 * process the rest of the function using the mirror_num
5249 * of the source drive. Therefore look it up first.
5250 * At the end, patch the device pointer to the one of the
5251 * target drive.
5252 */
5253 for (i = 0; i < tmp_num_stripes; i++) {
5254 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
5255 /*
5256 * In case of DUP, in order to keep it
5257 * simple, only add the mirror with the
5258 * lowest physical address
5259 */
5260 if (found &&
5261 physical_of_found <=
5262 tmp_bbio->stripes[i].physical)
5263 continue;
5264 index_srcdev = i;
5265 found = 1;
5266 physical_of_found =
5267 tmp_bbio->stripes[i].physical;
5268 }
5269 }
5270
5271 if (found) {
5272 mirror_num = index_srcdev + 1;
5273 patch_the_first_stripe_for_dev_replace = 1;
5274 physical_to_patch_in_first_stripe = physical_of_found;
5275 } else {
5276 WARN_ON(1);
5277 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005278 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005279 goto out;
5280 }
5281
Zhao Lei6e9606d2015-01-20 15:11:34 +08005282 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005283 } else if (mirror_num > map->num_stripes) {
5284 mirror_num = 0;
5285 }
5286
Chris Masonf2d8d742008-04-21 10:03:05 -04005287 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005288 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005289 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005290 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
David Sterbab8b93ad2015-01-16 17:26:13 +01005291 stripe_nr_end = div_u64(stripe_nr_end, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005292 stripe_end_offset = stripe_nr_end * map->stripe_len -
5293 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005294
Li Dongyangfce3bb92011-03-24 10:24:26 +00005295 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5296 if (rw & REQ_DISCARD)
5297 num_stripes = min_t(u64, map->num_stripes,
5298 stripe_nr_end - stripe_nr_orig);
David Sterba47c57132015-02-20 18:43:47 +01005299 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5300 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005301 if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)))
5302 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005303 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005304 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005305 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005306 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005307 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005308 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005309 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005310 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005311 current->pid % map->num_stripes,
5312 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005313 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005314 }
Chris Mason2fff7342008-04-29 14:12:09 -04005315
Chris Mason611f0e02008-04-03 16:29:03 -04005316 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005317 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005318 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005319 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005320 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005321 } else {
5322 mirror_num = 1;
5323 }
Chris Mason2fff7342008-04-29 14:12:09 -04005324
Chris Mason321aecc2008-04-16 10:49:51 -04005325 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005326 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005327
David Sterba47c57132015-02-20 18:43:47 +01005328 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005329 stripe_index *= map->sub_stripes;
5330
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005331 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005332 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005333 else if (rw & REQ_DISCARD)
5334 num_stripes = min_t(u64, map->sub_stripes *
5335 (stripe_nr_end - stripe_nr_orig),
5336 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005337 else if (mirror_num)
5338 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005339 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005340 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005341 stripe_index = find_live_mirror(fs_info, map,
5342 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005343 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005344 current->pid % map->sub_stripes,
5345 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005346 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005347 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005348
Zhao Leiffe2d202015-01-20 15:11:44 +08005349 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005350 if (need_raid_map &&
Miao Xieaf8e2d12014-10-23 14:42:50 +08005351 ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5352 mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005353 /* push stripe_nr back to the start of the full stripe */
David Sterbab8b93ad2015-01-16 17:26:13 +01005354 stripe_nr = div_u64(raid56_full_stripe_start,
5355 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005356
5357 /* RAID[56] write or recovery. Return all stripes */
5358 num_stripes = map->num_stripes;
5359 max_errors = nr_parity_stripes(map);
5360
David Woodhouse53b381b2013-01-29 18:40:14 -05005361 *length = map->stripe_len;
5362 stripe_index = 0;
5363 stripe_offset = 0;
5364 } else {
5365 /*
5366 * Mirror #0 or #1 means the original data block.
5367 * Mirror #2 is RAID5 parity block.
5368 * Mirror #3 is RAID6 Q block.
5369 */
David Sterba47c57132015-02-20 18:43:47 +01005370 stripe_nr = div_u64_rem(stripe_nr,
5371 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005372 if (mirror_num > 1)
5373 stripe_index = nr_data_stripes(map) +
5374 mirror_num - 2;
5375
5376 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005377 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5378 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005379 if (!(rw & (REQ_WRITE | REQ_DISCARD |
5380 REQ_GET_READ_MIRRORS)) && mirror_num <= 1)
5381 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005382 }
Chris Mason8790d502008-04-03 16:29:03 -04005383 } else {
5384 /*
David Sterba47c57132015-02-20 18:43:47 +01005385 * after this, stripe_nr is the number of stripes on this
5386 * device we have to walk to find the data, and stripe_index is
5387 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005388 */
David Sterba47c57132015-02-20 18:43:47 +01005389 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5390 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005391 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005392 }
Chris Mason593060d2008-03-25 16:50:33 -04005393 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005394
Stefan Behrens472262f2012-11-06 14:43:46 +01005395 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005396 if (dev_replace_is_ongoing) {
5397 if (rw & (REQ_WRITE | REQ_DISCARD))
5398 num_alloc_stripes <<= 1;
5399 if (rw & REQ_GET_READ_MIRRORS)
5400 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005401 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005402 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005403
Zhao Lei6e9606d2015-01-20 15:11:34 +08005404 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005405 if (!bbio) {
5406 ret = -ENOMEM;
5407 goto out;
5408 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005409 if (dev_replace_is_ongoing)
5410 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005411
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005412 /* build raid_map */
Zhao Leiffe2d202015-01-20 15:11:44 +08005413 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK &&
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005414 need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5415 mirror_num > 1)) {
5416 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005417 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005418
5419 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5420 sizeof(struct btrfs_bio_stripe) *
5421 num_alloc_stripes +
5422 sizeof(int) * tgtdev_indexes);
5423
5424 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005425 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005426
5427 /* Fill in the logical address of each stripe */
5428 tmp = stripe_nr * nr_data_stripes(map);
5429 for (i = 0; i < nr_data_stripes(map); i++)
5430 bbio->raid_map[(i+rot) % num_stripes] =
5431 em->start + (tmp + i) * map->stripe_len;
5432
5433 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5434 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5435 bbio->raid_map[(i+rot+1) % num_stripes] =
5436 RAID6_Q_STRIPE;
5437 }
5438
Li Dongyangfce3bb92011-03-24 10:24:26 +00005439 if (rw & REQ_DISCARD) {
David Sterba9d644a62015-02-20 18:42:11 +01005440 u32 factor = 0;
5441 u32 sub_stripes = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005442 u64 stripes_per_dev = 0;
5443 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005444 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005445
5446 if (map->type &
5447 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5448 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5449 sub_stripes = 1;
5450 else
5451 sub_stripes = map->sub_stripes;
5452
5453 factor = map->num_stripes / sub_stripes;
5454 stripes_per_dev = div_u64_rem(stripe_nr_end -
5455 stripe_nr_orig,
5456 factor,
5457 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005458 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5459 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005460 }
5461
Li Dongyangfce3bb92011-03-24 10:24:26 +00005462 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005463 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005464 map->stripes[stripe_index].physical +
5465 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005466 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005467
Li Zefanec9ef7a2011-12-01 14:06:42 +08005468 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5469 BTRFS_BLOCK_GROUP_RAID10)) {
5470 bbio->stripes[i].length = stripes_per_dev *
5471 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005472
Li Zefanec9ef7a2011-12-01 14:06:42 +08005473 if (i / sub_stripes < remaining_stripes)
5474 bbio->stripes[i].length +=
5475 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005476
5477 /*
5478 * Special for the first stripe and
5479 * the last stripe:
5480 *
5481 * |-------|...|-------|
5482 * |----------|
5483 * off end_off
5484 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005485 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005486 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005487 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005488
5489 if (stripe_index >= last_stripe &&
5490 stripe_index <= (last_stripe +
5491 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005492 bbio->stripes[i].length -=
5493 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005494
Li Zefanec9ef7a2011-12-01 14:06:42 +08005495 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005496 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005497 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005498 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005499
5500 stripe_index++;
5501 if (stripe_index == map->num_stripes) {
5502 /* This could only happen for RAID0/10 */
5503 stripe_index = 0;
5504 stripe_nr++;
5505 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005506 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005507 } else {
5508 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005509 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005510 map->stripes[stripe_index].physical +
5511 stripe_offset +
5512 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005513 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005514 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005515 stripe_index++;
5516 }
Chris Mason593060d2008-03-25 16:50:33 -04005517 }
Li Zefande11cc12011-12-01 12:55:47 +08005518
Miao Xied20983b2014-07-03 18:22:13 +08005519 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5520 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005521
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005522 if (bbio->raid_map)
5523 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005524
Miao Xie2c8cdd62014-11-14 16:06:25 +08005525 tgtdev_indexes = 0;
Stefan Behrens472262f2012-11-06 14:43:46 +01005526 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5527 dev_replace->tgtdev != NULL) {
5528 int index_where_to_add;
5529 u64 srcdev_devid = dev_replace->srcdev->devid;
5530
5531 /*
5532 * duplicate the write operations while the dev replace
5533 * procedure is running. Since the copying of the old disk
5534 * to the new disk takes place at run time while the
5535 * filesystem is mounted writable, the regular write
5536 * operations to the old disk have to be duplicated to go
5537 * to the new disk as well.
5538 * Note that device->missing is handled by the caller, and
5539 * that the write to the old disk is already set up in the
5540 * stripes array.
5541 */
5542 index_where_to_add = num_stripes;
5543 for (i = 0; i < num_stripes; i++) {
5544 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5545 /* write to new disk, too */
5546 struct btrfs_bio_stripe *new =
5547 bbio->stripes + index_where_to_add;
5548 struct btrfs_bio_stripe *old =
5549 bbio->stripes + i;
5550
5551 new->physical = old->physical;
5552 new->length = old->length;
5553 new->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005554 bbio->tgtdev_map[i] = index_where_to_add;
Stefan Behrens472262f2012-11-06 14:43:46 +01005555 index_where_to_add++;
5556 max_errors++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005557 tgtdev_indexes++;
Stefan Behrens472262f2012-11-06 14:43:46 +01005558 }
5559 }
5560 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005561 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5562 dev_replace->tgtdev != NULL) {
5563 u64 srcdev_devid = dev_replace->srcdev->devid;
5564 int index_srcdev = 0;
5565 int found = 0;
5566 u64 physical_of_found = 0;
5567
5568 /*
5569 * During the dev-replace procedure, the target drive can
5570 * also be used to read data in case it is needed to repair
5571 * a corrupt block elsewhere. This is possible if the
5572 * requested area is left of the left cursor. In this area,
5573 * the target drive is a full copy of the source drive.
5574 */
5575 for (i = 0; i < num_stripes; i++) {
5576 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5577 /*
5578 * In case of DUP, in order to keep it
5579 * simple, only add the mirror with the
5580 * lowest physical address
5581 */
5582 if (found &&
5583 physical_of_found <=
5584 bbio->stripes[i].physical)
5585 continue;
5586 index_srcdev = i;
5587 found = 1;
5588 physical_of_found = bbio->stripes[i].physical;
5589 }
5590 }
5591 if (found) {
David Sterba258ece02015-02-24 19:45:15 +01005592 if (physical_of_found + map->stripe_len <=
Stefan Behrensad6d6202012-11-06 15:06:47 +01005593 dev_replace->cursor_left) {
5594 struct btrfs_bio_stripe *tgtdev_stripe =
5595 bbio->stripes + num_stripes;
5596
5597 tgtdev_stripe->physical = physical_of_found;
5598 tgtdev_stripe->length =
5599 bbio->stripes[index_srcdev].length;
5600 tgtdev_stripe->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005601 bbio->tgtdev_map[index_srcdev] = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005602
Miao Xie2c8cdd62014-11-14 16:06:25 +08005603 tgtdev_indexes++;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005604 num_stripes++;
5605 }
5606 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005607 }
5608
Li Zefande11cc12011-12-01 12:55:47 +08005609 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005610 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005611 bbio->num_stripes = num_stripes;
5612 bbio->max_errors = max_errors;
5613 bbio->mirror_num = mirror_num;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005614 bbio->num_tgtdevs = tgtdev_indexes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005615
5616 /*
5617 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5618 * mirror_num == num_stripes + 1 && dev_replace target drive is
5619 * available as a mirror
5620 */
5621 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5622 WARN_ON(num_stripes > 1);
5623 bbio->stripes[0].dev = dev_replace->tgtdev;
5624 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5625 bbio->mirror_num = map->num_stripes + 1;
5626 }
Chris Masoncea9e442008-04-09 16:28:12 -04005627out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005628 if (dev_replace_is_ongoing)
5629 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005630 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005631 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005632}
5633
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005634int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005635 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005636 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005637{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005638 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005639 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005640}
5641
Miao Xieaf8e2d12014-10-23 14:42:50 +08005642/* For Scrub/replace */
5643int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw,
5644 u64 logical, u64 *length,
5645 struct btrfs_bio **bbio_ret, int mirror_num,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005646 int need_raid_map)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005647{
5648 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005649 mirror_num, need_raid_map);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005650}
5651
Yan Zhenga512bbf2008-12-08 16:46:26 -05005652int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5653 u64 chunk_start, u64 physical, u64 devid,
5654 u64 **logical, int *naddrs, int *stripe_len)
5655{
5656 struct extent_map_tree *em_tree = &map_tree->map_tree;
5657 struct extent_map *em;
5658 struct map_lookup *map;
5659 u64 *buf;
5660 u64 bytenr;
5661 u64 length;
5662 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005663 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005664 int i, j, nr = 0;
5665
Chris Mason890871b2009-09-02 16:24:52 -04005666 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005667 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005668 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005669
Josef Bacik835d9742013-03-19 12:13:25 -04005670 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005671 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005672 chunk_start);
5673 return -EIO;
5674 }
5675
5676 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005677 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005678 em->start, chunk_start);
5679 free_extent_map(em);
5680 return -EIO;
5681 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005682 map = (struct map_lookup *)em->bdev;
5683
5684 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005685 rmap_len = map->stripe_len;
5686
Yan Zhenga512bbf2008-12-08 16:46:26 -05005687 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01005688 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005689 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01005690 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08005691 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01005692 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005693 rmap_len = map->stripe_len * nr_data_stripes(map);
5694 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005695
David Sterba31e818f2015-02-20 18:00:26 +01005696 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005697 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005698
5699 for (i = 0; i < map->num_stripes; i++) {
5700 if (devid && map->stripes[i].dev->devid != devid)
5701 continue;
5702 if (map->stripes[i].physical > physical ||
5703 map->stripes[i].physical + length <= physical)
5704 continue;
5705
5706 stripe_nr = physical - map->stripes[i].physical;
David Sterbab8b93ad2015-01-16 17:26:13 +01005707 stripe_nr = div_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005708
5709 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5710 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01005711 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005712 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5713 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005714 } /* else if RAID[56], multiply by nr_data_stripes().
5715 * Alternatively, just use rmap_len below instead of
5716 * map->stripe_len */
5717
5718 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005719 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005720 for (j = 0; j < nr; j++) {
5721 if (buf[j] == bytenr)
5722 break;
5723 }
Chris Mason934d3752008-12-08 16:43:10 -05005724 if (j == nr) {
5725 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005726 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005727 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005728 }
5729
Yan Zhenga512bbf2008-12-08 16:46:26 -05005730 *logical = buf;
5731 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005732 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005733
5734 free_extent_map(em);
5735 return 0;
5736}
5737
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005738static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08005739{
Mike Snitzer326e1db2015-05-22 09:14:03 -04005740 bio->bi_private = bbio->private;
5741 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005742 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04005743
Zhao Lei6e9606d2015-01-20 15:11:34 +08005744 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08005745}
5746
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005747static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04005748{
Chris Mason9be33952013-05-17 18:30:14 -04005749 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005750 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005751
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005752 if (bio->bi_error) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005753 atomic_inc(&bbio->error);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005754 if (bio->bi_error == -EIO || bio->bi_error == -EREMOTEIO) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02005755 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005756 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08005757 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005758
5759 BUG_ON(stripe_index >= bbio->num_stripes);
5760 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005761 if (dev->bdev) {
5762 if (bio->bi_rw & WRITE)
5763 btrfs_dev_stat_inc(dev,
5764 BTRFS_DEV_STAT_WRITE_ERRS);
5765 else
5766 btrfs_dev_stat_inc(dev,
5767 BTRFS_DEV_STAT_READ_ERRS);
5768 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5769 btrfs_dev_stat_inc(dev,
5770 BTRFS_DEV_STAT_FLUSH_ERRS);
5771 btrfs_dev_stat_print_on_error(dev);
5772 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005773 }
5774 }
Chris Mason8790d502008-04-03 16:29:03 -04005775
Jan Schmidta1d3c472011-08-04 17:15:33 +02005776 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005777 is_orig_bio = 1;
5778
Miao Xiec404e0d2014-01-30 16:46:55 +08005779 btrfs_bio_counter_dec(bbio->fs_info);
5780
Jan Schmidta1d3c472011-08-04 17:15:33 +02005781 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005782 if (!is_orig_bio) {
5783 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005784 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005785 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005786
Chris Mason9be33952013-05-17 18:30:14 -04005787 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005788 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005789 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005790 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005791 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005792 bio->bi_error = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005793 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005794 /*
5795 * this bio is actually up to date, we didn't
5796 * go over the max number of errors
5797 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005798 bio->bi_error = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005799 }
Miao Xiec55f1392014-06-19 10:42:54 +08005800
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005801 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005802 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005803 bio_put(bio);
5804 }
Chris Mason8790d502008-04-03 16:29:03 -04005805}
5806
Chris Mason8b712842008-06-11 16:50:36 -04005807/*
5808 * see run_scheduled_bios for a description of why bios are collected for
5809 * async submit.
5810 *
5811 * This will add one bio to the pending list for a device and make sure
5812 * the work struct is scheduled.
5813 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005814static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5815 struct btrfs_device *device,
5816 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005817{
5818 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005819 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005820
David Woodhouse53b381b2013-01-29 18:40:14 -05005821 if (device->missing || !device->bdev) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005822 bio_io_error(bio);
David Woodhouse53b381b2013-01-29 18:40:14 -05005823 return;
5824 }
5825
Chris Mason8b712842008-06-11 16:50:36 -04005826 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005827 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005828 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005829 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005830 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005831 return;
Chris Mason8b712842008-06-11 16:50:36 -04005832 }
5833
5834 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005835 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005836 * higher layers. Otherwise, the async bio makes it appear we have
5837 * made progress against dirty pages when we've really just put it
5838 * on a queue for later
5839 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005840 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005841 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005842 bio->bi_next = NULL;
5843 bio->bi_rw |= rw;
5844
5845 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005846 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005847 pending_bios = &device->pending_sync_bios;
5848 else
5849 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005850
Chris Masonffbd5172009-04-20 15:50:09 -04005851 if (pending_bios->tail)
5852 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005853
Chris Masonffbd5172009-04-20 15:50:09 -04005854 pending_bios->tail = bio;
5855 if (!pending_bios->head)
5856 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005857 if (device->running_pending)
5858 should_queue = 0;
5859
5860 spin_unlock(&device->io_lock);
5861
5862 if (should_queue)
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +08005863 btrfs_queue_work(root->fs_info->submit_workers,
5864 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005865}
5866
Josef Bacikde1ee922012-10-19 16:50:56 -04005867static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5868 struct bio *bio, u64 physical, int dev_nr,
5869 int rw, int async)
5870{
5871 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5872
5873 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005874 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005875 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005876 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005877#ifdef DEBUG
5878 {
5879 struct rcu_string *name;
5880
5881 rcu_read_lock();
5882 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005883 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005884 "(%s id %llu), size=%u\n", rw,
Fabian Frederick1b6e4462014-09-24 20:23:05 +02005885 (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev,
5886 name->str, dev->devid, bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04005887 rcu_read_unlock();
5888 }
5889#endif
5890 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005891
5892 btrfs_bio_counter_inc_noblocked(root->fs_info);
5893
Josef Bacikde1ee922012-10-19 16:50:56 -04005894 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005895 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005896 else
5897 btrfsic_submit_bio(rw, bio);
5898}
5899
Josef Bacikde1ee922012-10-19 16:50:56 -04005900static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5901{
5902 atomic_inc(&bbio->error);
5903 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005904 /* Shoud be the original bio. */
5905 WARN_ON(bio != bbio->orig_bio);
5906
Chris Mason9be33952013-05-17 18:30:14 -04005907 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005908 bio->bi_iter.bi_sector = logical >> 9;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005909 bio->bi_error = -EIO;
5910 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005911 }
5912}
5913
Chris Masonf1885912008-04-09 16:28:12 -04005914int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005915 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005916{
Chris Mason0b86a832008-03-24 15:01:56 -04005917 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005918 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005919 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005920 u64 length = 0;
5921 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04005922 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08005923 int dev_nr;
5924 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005925 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005926
Kent Overstreet4f024f32013-10-11 15:44:27 -07005927 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005928 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005929
Miao Xiec404e0d2014-01-30 16:46:55 +08005930 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005931 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005932 mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08005933 if (ret) {
5934 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005935 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005936 }
Chris Masoncea9e442008-04-09 16:28:12 -04005937
Jan Schmidta1d3c472011-08-04 17:15:33 +02005938 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005939 bbio->orig_bio = first_bio;
5940 bbio->private = first_bio->bi_private;
5941 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005942 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005943 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5944
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005945 if (bbio->raid_map) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005946 /* In this case, map_length has been set to the length of
5947 a single stripe; not the whole write */
5948 if (rw & WRITE) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005949 ret = raid56_parity_write(root, bio, bbio, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005950 } else {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005951 ret = raid56_parity_recover(root, bio, bbio, map_length,
Miao Xie42452152014-11-25 16:39:28 +08005952 mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05005953 }
Miao Xie42452152014-11-25 16:39:28 +08005954
Miao Xiec404e0d2014-01-30 16:46:55 +08005955 btrfs_bio_counter_dec(root->fs_info);
5956 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005957 }
5958
Chris Masoncea9e442008-04-09 16:28:12 -04005959 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005960 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005961 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005962 BUG();
5963 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005964
Zhao Lei08da7572015-02-12 15:42:16 +08005965 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005966 dev = bbio->stripes[dev_nr].dev;
5967 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5968 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04005969 continue;
5970 }
5971
Jan Schmidta1d3c472011-08-04 17:15:33 +02005972 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005973 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005974 BUG_ON(!bio); /* -ENOMEM */
Mike Snitzer326e1db2015-05-22 09:14:03 -04005975 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005976 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04005977
Josef Bacikde1ee922012-10-19 16:50:56 -04005978 submit_stripe_bio(root, bbio, bio,
5979 bbio->stripes[dev_nr].physical, dev_nr, rw,
5980 async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04005981 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005982 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04005983 return 0;
5984}
5985
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005986struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005987 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005988{
Yan Zheng2b820322008-11-17 21:11:30 -05005989 struct btrfs_device *device;
5990 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005991
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005992 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005993 while (cur_devices) {
5994 if (!fsid ||
5995 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5996 device = __find_device(&cur_devices->devices,
5997 devid, uuid);
5998 if (device)
5999 return device;
6000 }
6001 cur_devices = cur_devices->seed;
6002 }
6003 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006004}
6005
Chris Masondfe25022008-05-13 13:46:40 -04006006static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
Miao Xie5f375832014-09-03 21:35:46 +08006007 struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006008 u64 devid, u8 *dev_uuid)
6009{
6010 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006011
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006012 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6013 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05006014 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006015
6016 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006017 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006018 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006019
6020 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05006021 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006022
Chris Masondfe25022008-05-13 13:46:40 -04006023 return device;
6024}
6025
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006026/**
6027 * btrfs_alloc_device - allocate struct btrfs_device
6028 * @fs_info: used only for generating a new devid, can be NULL if
6029 * devid is provided (i.e. @devid != NULL).
6030 * @devid: a pointer to devid for this device. If NULL a new devid
6031 * is generated.
6032 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6033 * is generated.
6034 *
6035 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6036 * on error. Returned struct is not linked onto any lists and can be
6037 * destroyed with kfree() right away.
6038 */
6039struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6040 const u64 *devid,
6041 const u8 *uuid)
6042{
6043 struct btrfs_device *dev;
6044 u64 tmp;
6045
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306046 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006047 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006048
6049 dev = __alloc_device();
6050 if (IS_ERR(dev))
6051 return dev;
6052
6053 if (devid)
6054 tmp = *devid;
6055 else {
6056 int ret;
6057
6058 ret = find_next_devid(fs_info, &tmp);
6059 if (ret) {
6060 kfree(dev);
6061 return ERR_PTR(ret);
6062 }
6063 }
6064 dev->devid = tmp;
6065
6066 if (uuid)
6067 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6068 else
6069 generate_random_uuid(dev->uuid);
6070
Liu Bo9e0af232014-08-15 23:36:53 +08006071 btrfs_init_work(&dev->work, btrfs_submit_helper,
6072 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006073
6074 return dev;
6075}
6076
Chris Mason0b86a832008-03-24 15:01:56 -04006077static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6078 struct extent_buffer *leaf,
6079 struct btrfs_chunk *chunk)
6080{
6081 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6082 struct map_lookup *map;
6083 struct extent_map *em;
6084 u64 logical;
6085 u64 length;
6086 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006087 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006088 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006089 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006090 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006091
Chris Masone17cade2008-04-15 15:41:47 -04006092 logical = key->offset;
6093 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04006094
Chris Mason890871b2009-09-02 16:24:52 -04006095 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006096 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006097 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006098
6099 /* already mapped? */
6100 if (em && em->start <= logical && em->start + em->len > logical) {
6101 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006102 return 0;
6103 } else if (em) {
6104 free_extent_map(em);
6105 }
Chris Mason0b86a832008-03-24 15:01:56 -04006106
David Sterba172ddd62011-04-21 00:48:27 +02006107 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006108 if (!em)
6109 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006110 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6111 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006112 if (!map) {
6113 free_extent_map(em);
6114 return -ENOMEM;
6115 }
6116
Wang Shilong298a8f92014-06-19 10:42:52 +08006117 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04006118 em->bdev = (struct block_device *)map;
6119 em->start = logical;
6120 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006121 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006122 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006123 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006124
Chris Mason593060d2008-03-25 16:50:33 -04006125 map->num_stripes = num_stripes;
6126 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6127 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6128 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
6129 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6130 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006131 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006132 for (i = 0; i < num_stripes; i++) {
6133 map->stripes[i].physical =
6134 btrfs_stripe_offset_nr(leaf, chunk, i);
6135 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006136 read_extent_buffer(leaf, uuid, (unsigned long)
6137 btrfs_stripe_dev_uuid_nr(chunk, i),
6138 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006139 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
6140 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04006141 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006142 free_extent_map(em);
6143 return -EIO;
6144 }
Chris Masondfe25022008-05-13 13:46:40 -04006145 if (!map->stripes[i].dev) {
6146 map->stripes[i].dev =
Miao Xie5f375832014-09-03 21:35:46 +08006147 add_missing_dev(root, root->fs_info->fs_devices,
6148 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006149 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04006150 free_extent_map(em);
6151 return -EIO;
6152 }
Anand Jain816fceb2015-04-27 12:46:18 +08006153 btrfs_warn(root->fs_info, "devid %llu uuid %pU is missing",
6154 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006155 }
6156 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04006157 }
6158
Chris Mason890871b2009-09-02 16:24:52 -04006159 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006160 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006161 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006162 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006163 free_extent_map(em);
6164
6165 return 0;
6166}
6167
Jeff Mahoney143bede2012-03-01 14:56:26 +01006168static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006169 struct btrfs_dev_item *dev_item,
6170 struct btrfs_device *device)
6171{
6172 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006173
6174 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006175 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6176 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006177 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006178 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006179 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006180 device->type = btrfs_device_type(leaf, dev_item);
6181 device->io_align = btrfs_device_io_align(leaf, dev_item);
6182 device->io_width = btrfs_device_io_width(leaf, dev_item);
6183 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006184 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006185 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006186
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006187 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006188 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006189}
6190
Miao Xie5f375832014-09-03 21:35:46 +08006191static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6192 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006193{
6194 struct btrfs_fs_devices *fs_devices;
6195 int ret;
6196
Li Zefanb367e472011-12-07 11:38:24 +08006197 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05006198
6199 fs_devices = root->fs_info->fs_devices->seed;
6200 while (fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006201 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE))
6202 return fs_devices;
6203
Yan Zheng2b820322008-11-17 21:11:30 -05006204 fs_devices = fs_devices->seed;
6205 }
6206
6207 fs_devices = find_fsid(fsid);
6208 if (!fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006209 if (!btrfs_test_opt(root, DEGRADED))
6210 return ERR_PTR(-ENOENT);
6211
6212 fs_devices = alloc_fs_devices(fsid);
6213 if (IS_ERR(fs_devices))
6214 return fs_devices;
6215
6216 fs_devices->seeding = 1;
6217 fs_devices->opened = 1;
6218 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006219 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006220
6221 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006222 if (IS_ERR(fs_devices))
6223 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006224
Christoph Hellwig97288f22008-12-02 06:36:09 -05006225 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006226 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006227 if (ret) {
6228 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006229 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006230 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006231 }
Yan Zheng2b820322008-11-17 21:11:30 -05006232
6233 if (!fs_devices->seeding) {
6234 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006235 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006236 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006237 goto out;
6238 }
6239
6240 fs_devices->seed = root->fs_info->fs_devices->seed;
6241 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006242out:
Miao Xie5f375832014-09-03 21:35:46 +08006243 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006244}
6245
Chris Mason0d81ba52008-03-24 15:02:07 -04006246static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006247 struct extent_buffer *leaf,
6248 struct btrfs_dev_item *dev_item)
6249{
Miao Xie5f375832014-09-03 21:35:46 +08006250 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006251 struct btrfs_device *device;
6252 u64 devid;
6253 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006254 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006255 u8 dev_uuid[BTRFS_UUID_SIZE];
6256
Chris Mason0b86a832008-03-24 15:01:56 -04006257 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006258 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006259 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006260 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006261 BTRFS_UUID_SIZE);
6262
6263 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
Miao Xie5f375832014-09-03 21:35:46 +08006264 fs_devices = open_seed_devices(root, fs_uuid);
6265 if (IS_ERR(fs_devices))
6266 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006267 }
6268
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006269 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006270 if (!device) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006271 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006272 return -EIO;
6273
Miao Xie5f375832014-09-03 21:35:46 +08006274 device = add_missing_dev(root, fs_devices, devid, dev_uuid);
6275 if (!device)
6276 return -ENOMEM;
Anand Jain33b97e42015-05-08 04:34:35 +08006277 btrfs_warn(root->fs_info, "devid %llu uuid %pU missing",
6278 devid, dev_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006279 } else {
6280 if (!device->bdev && !btrfs_test_opt(root, DEGRADED))
6281 return -EIO;
6282
6283 if(!device->bdev && !device->missing) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006284 /*
6285 * this happens when a device that was properly setup
6286 * in the device info lists suddenly goes bad.
6287 * device->bdev is NULL, and so we have to set
6288 * device->missing to one here
6289 */
Miao Xie5f375832014-09-03 21:35:46 +08006290 device->fs_devices->missing_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05006291 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006292 }
Miao Xie5f375832014-09-03 21:35:46 +08006293
6294 /* Move the device to its own fs_devices */
6295 if (device->fs_devices != fs_devices) {
6296 ASSERT(device->missing);
6297
6298 list_move(&device->dev_list, &fs_devices->devices);
6299 device->fs_devices->num_devices--;
6300 fs_devices->num_devices++;
6301
6302 device->fs_devices->missing_devices--;
6303 fs_devices->missing_devices++;
6304
6305 device->fs_devices = fs_devices;
6306 }
Yan Zheng2b820322008-11-17 21:11:30 -05006307 }
6308
6309 if (device->fs_devices != root->fs_info->fs_devices) {
6310 BUG_ON(device->writeable);
6311 if (device->generation !=
6312 btrfs_device_generation(leaf, dev_item))
6313 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006314 }
Chris Mason0b86a832008-03-24 15:01:56 -04006315
6316 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006317 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006318 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006319 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006320 spin_lock(&root->fs_info->free_chunk_lock);
6321 root->fs_info->free_chunk_space += device->total_bytes -
6322 device->bytes_used;
6323 spin_unlock(&root->fs_info->free_chunk_lock);
6324 }
Chris Mason0b86a832008-03-24 15:01:56 -04006325 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006326 return ret;
6327}
6328
Yan Zhenge4404d62008-12-12 10:03:26 -05006329int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006330{
David Sterba6c417612011-04-13 15:41:04 +02006331 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006332 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006333 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006334 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006335 u8 *array_ptr;
6336 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006337 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006338 u32 num_stripes;
6339 u32 array_size;
6340 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006341 u32 cur_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006342 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006343
David Sterbaa83fffb2014-06-15 02:39:54 +02006344 ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize);
6345 /*
6346 * This will create extent buffer of nodesize, superblock size is
6347 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6348 * overallocate but we can keep it as-is, only the first page is used.
6349 */
6350 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET);
Chris Masona061fc82008-05-07 11:43:44 -04006351 if (!sb)
6352 return -ENOMEM;
6353 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006354 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006355 /*
6356 * The sb extent buffer is artifical and just used to read the system array.
6357 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6358 * pages up-to-date when the page is larger: extent does not cover the
6359 * whole page and consequently check_page_uptodate does not find all
6360 * the page's extents up-to-date (the hole beyond sb),
6361 * write_extent_buffer then triggers a WARN_ON.
6362 *
6363 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6364 * but sb spans only this function. Add an explicit SetPageUptodate call
6365 * to silence the warning eg. on PowerPC 64.
6366 */
6367 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006368 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006369
Chris Masona061fc82008-05-07 11:43:44 -04006370 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006371 array_size = btrfs_super_sys_array_size(super_copy);
6372
David Sterba1ffb22c2014-10-31 19:02:42 +01006373 array_ptr = super_copy->sys_chunk_array;
6374 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6375 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006376
David Sterba1ffb22c2014-10-31 19:02:42 +01006377 while (cur_offset < array_size) {
6378 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006379 len = sizeof(*disk_key);
6380 if (cur_offset + len > array_size)
6381 goto out_short_read;
6382
Chris Mason0b86a832008-03-24 15:01:56 -04006383 btrfs_disk_key_to_cpu(&key, disk_key);
6384
David Sterba1ffb22c2014-10-31 19:02:42 +01006385 array_ptr += len;
6386 sb_array_offset += len;
6387 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006388
Chris Mason0d81ba52008-03-24 15:02:07 -04006389 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006390 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006391 /*
6392 * At least one btrfs_chunk with one stripe must be
6393 * present, exact stripe count check comes afterwards
6394 */
6395 len = btrfs_chunk_item_size(1);
6396 if (cur_offset + len > array_size)
6397 goto out_short_read;
6398
6399 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6400 len = btrfs_chunk_item_size(num_stripes);
6401 if (cur_offset + len > array_size)
6402 goto out_short_read;
6403
Chris Mason0d81ba52008-03-24 15:02:07 -04006404 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006405 if (ret)
6406 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006407 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006408 ret = -EIO;
6409 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006410 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006411 array_ptr += len;
6412 sb_array_offset += len;
6413 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006414 }
Chris Masona061fc82008-05-07 11:43:44 -04006415 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006416 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006417
6418out_short_read:
6419 printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n",
6420 len, cur_offset);
6421 free_extent_buffer(sb);
6422 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006423}
6424
6425int btrfs_read_chunk_tree(struct btrfs_root *root)
6426{
6427 struct btrfs_path *path;
6428 struct extent_buffer *leaf;
6429 struct btrfs_key key;
6430 struct btrfs_key found_key;
6431 int ret;
6432 int slot;
6433
6434 root = root->fs_info->chunk_root;
6435
6436 path = btrfs_alloc_path();
6437 if (!path)
6438 return -ENOMEM;
6439
Li Zefanb367e472011-12-07 11:38:24 +08006440 mutex_lock(&uuid_mutex);
6441 lock_chunks(root);
6442
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006443 /*
6444 * Read all device items, and then all the chunk items. All
6445 * device items are found before any chunk item (their object id
6446 * is smaller than the lowest possible object id for a chunk
6447 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006448 */
6449 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6450 key.offset = 0;
6451 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006452 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006453 if (ret < 0)
6454 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006455 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006456 leaf = path->nodes[0];
6457 slot = path->slots[0];
6458 if (slot >= btrfs_header_nritems(leaf)) {
6459 ret = btrfs_next_leaf(root, path);
6460 if (ret == 0)
6461 continue;
6462 if (ret < 0)
6463 goto error;
6464 break;
6465 }
6466 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006467 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6468 struct btrfs_dev_item *dev_item;
6469 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006470 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006471 ret = read_one_dev(root, leaf, dev_item);
6472 if (ret)
6473 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006474 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6475 struct btrfs_chunk *chunk;
6476 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6477 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006478 if (ret)
6479 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006480 }
6481 path->slots[0]++;
6482 }
Chris Mason0b86a832008-03-24 15:01:56 -04006483 ret = 0;
6484error:
Li Zefanb367e472011-12-07 11:38:24 +08006485 unlock_chunks(root);
6486 mutex_unlock(&uuid_mutex);
6487
Yan Zheng2b820322008-11-17 21:11:30 -05006488 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006489 return ret;
6490}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006491
Miao Xiecb517ea2013-05-15 07:48:19 +00006492void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6493{
6494 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6495 struct btrfs_device *device;
6496
Liu Bo29cc83f2014-05-11 23:14:59 +08006497 while (fs_devices) {
6498 mutex_lock(&fs_devices->device_list_mutex);
6499 list_for_each_entry(device, &fs_devices->devices, dev_list)
6500 device->dev_root = fs_info->dev_root;
6501 mutex_unlock(&fs_devices->device_list_mutex);
6502
6503 fs_devices = fs_devices->seed;
6504 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006505}
6506
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006507static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6508{
6509 int i;
6510
6511 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6512 btrfs_dev_stat_reset(dev, i);
6513}
6514
6515int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6516{
6517 struct btrfs_key key;
6518 struct btrfs_key found_key;
6519 struct btrfs_root *dev_root = fs_info->dev_root;
6520 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6521 struct extent_buffer *eb;
6522 int slot;
6523 int ret = 0;
6524 struct btrfs_device *device;
6525 struct btrfs_path *path = NULL;
6526 int i;
6527
6528 path = btrfs_alloc_path();
6529 if (!path) {
6530 ret = -ENOMEM;
6531 goto out;
6532 }
6533
6534 mutex_lock(&fs_devices->device_list_mutex);
6535 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6536 int item_size;
6537 struct btrfs_dev_stats_item *ptr;
6538
6539 key.objectid = 0;
6540 key.type = BTRFS_DEV_STATS_KEY;
6541 key.offset = device->devid;
6542 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6543 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006544 __btrfs_reset_dev_stats(device);
6545 device->dev_stats_valid = 1;
6546 btrfs_release_path(path);
6547 continue;
6548 }
6549 slot = path->slots[0];
6550 eb = path->nodes[0];
6551 btrfs_item_key_to_cpu(eb, &found_key, slot);
6552 item_size = btrfs_item_size_nr(eb, slot);
6553
6554 ptr = btrfs_item_ptr(eb, slot,
6555 struct btrfs_dev_stats_item);
6556
6557 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6558 if (item_size >= (1 + i) * sizeof(__le64))
6559 btrfs_dev_stat_set(device, i,
6560 btrfs_dev_stats_value(eb, ptr, i));
6561 else
6562 btrfs_dev_stat_reset(device, i);
6563 }
6564
6565 device->dev_stats_valid = 1;
6566 btrfs_dev_stat_print_on_load(device);
6567 btrfs_release_path(path);
6568 }
6569 mutex_unlock(&fs_devices->device_list_mutex);
6570
6571out:
6572 btrfs_free_path(path);
6573 return ret < 0 ? ret : 0;
6574}
6575
6576static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6577 struct btrfs_root *dev_root,
6578 struct btrfs_device *device)
6579{
6580 struct btrfs_path *path;
6581 struct btrfs_key key;
6582 struct extent_buffer *eb;
6583 struct btrfs_dev_stats_item *ptr;
6584 int ret;
6585 int i;
6586
6587 key.objectid = 0;
6588 key.type = BTRFS_DEV_STATS_KEY;
6589 key.offset = device->devid;
6590
6591 path = btrfs_alloc_path();
6592 BUG_ON(!path);
6593 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6594 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006595 printk_in_rcu(KERN_WARNING "BTRFS: "
6596 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006597 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006598 goto out;
6599 }
6600
6601 if (ret == 0 &&
6602 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6603 /* need to delete old one and insert a new one */
6604 ret = btrfs_del_item(trans, dev_root, path);
6605 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006606 printk_in_rcu(KERN_WARNING "BTRFS: "
6607 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006608 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006609 goto out;
6610 }
6611 ret = 1;
6612 }
6613
6614 if (ret == 1) {
6615 /* need to insert a new item */
6616 btrfs_release_path(path);
6617 ret = btrfs_insert_empty_item(trans, dev_root, path,
6618 &key, sizeof(*ptr));
6619 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006620 printk_in_rcu(KERN_WARNING "BTRFS: "
6621 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006622 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006623 goto out;
6624 }
6625 }
6626
6627 eb = path->nodes[0];
6628 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6629 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6630 btrfs_set_dev_stats_value(eb, ptr, i,
6631 btrfs_dev_stat_read(device, i));
6632 btrfs_mark_buffer_dirty(eb);
6633
6634out:
6635 btrfs_free_path(path);
6636 return ret;
6637}
6638
6639/*
6640 * called from commit_transaction. Writes all changed device stats to disk.
6641 */
6642int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6643 struct btrfs_fs_info *fs_info)
6644{
6645 struct btrfs_root *dev_root = fs_info->dev_root;
6646 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6647 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006648 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006649 int ret = 0;
6650
6651 mutex_lock(&fs_devices->device_list_mutex);
6652 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006653 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006654 continue;
6655
Miao Xieaddc3fa2014-07-24 11:37:11 +08006656 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006657 ret = update_dev_stat_item(trans, dev_root, device);
6658 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006659 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006660 }
6661 mutex_unlock(&fs_devices->device_list_mutex);
6662
6663 return ret;
6664}
6665
Stefan Behrens442a4f62012-05-25 16:06:08 +02006666void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6667{
6668 btrfs_dev_stat_inc(dev, index);
6669 btrfs_dev_stat_print_on_error(dev);
6670}
6671
Eric Sandeen48a3b632013-04-25 20:41:01 +00006672static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006673{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006674 if (!dev->dev_stats_valid)
6675 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006676 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6677 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006678 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006679 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6680 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6681 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006682 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6683 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006684}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006685
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006686static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6687{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006688 int i;
6689
6690 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6691 if (btrfs_dev_stat_read(dev, i) != 0)
6692 break;
6693 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6694 return; /* all values == 0, suppress message */
6695
Frank Holtonefe120a2013-12-20 11:37:06 -05006696 printk_in_rcu(KERN_INFO "BTRFS: "
6697 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006698 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006699 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6700 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6701 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6702 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6703 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6704}
6705
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006706int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006707 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006708{
6709 struct btrfs_device *dev;
6710 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6711 int i;
6712
6713 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006714 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006715 mutex_unlock(&fs_devices->device_list_mutex);
6716
6717 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006718 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006719 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006720 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006721 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006722 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006723 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006724 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6725 if (stats->nr_items > i)
6726 stats->values[i] =
6727 btrfs_dev_stat_read_and_reset(dev, i);
6728 else
6729 btrfs_dev_stat_reset(dev, i);
6730 }
6731 } else {
6732 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6733 if (stats->nr_items > i)
6734 stats->values[i] = btrfs_dev_stat_read(dev, i);
6735 }
6736 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6737 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6738 return 0;
6739}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006740
6741int btrfs_scratch_superblock(struct btrfs_device *device)
6742{
6743 struct buffer_head *bh;
6744 struct btrfs_super_block *disk_super;
6745
6746 bh = btrfs_read_dev_super(device->bdev);
Anand Jain92fc03f2015-08-14 18:32:51 +08006747 if (IS_ERR(bh))
6748 return PTR_ERR(bh);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006749 disk_super = (struct btrfs_super_block *)bh->b_data;
6750
6751 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6752 set_buffer_dirty(bh);
6753 sync_dirty_buffer(bh);
6754 brelse(bh);
6755
6756 return 0;
6757}
Miao Xie935e5cc2014-09-03 21:35:33 +08006758
6759/*
6760 * Update the size of all devices, which is used for writing out the
6761 * super blocks.
6762 */
6763void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
6764{
6765 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6766 struct btrfs_device *curr, *next;
6767
6768 if (list_empty(&fs_devices->resized_devices))
6769 return;
6770
6771 mutex_lock(&fs_devices->device_list_mutex);
6772 lock_chunks(fs_info->dev_root);
6773 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
6774 resized_list) {
6775 list_del_init(&curr->resized_list);
6776 curr->commit_total_bytes = curr->disk_total_bytes;
6777 }
6778 unlock_chunks(fs_info->dev_root);
6779 mutex_unlock(&fs_devices->device_list_mutex);
6780}
Miao Xiece7213c2014-09-03 21:35:34 +08006781
6782/* Must be invoked during the transaction commit */
6783void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
6784 struct btrfs_transaction *transaction)
6785{
6786 struct extent_map *em;
6787 struct map_lookup *map;
6788 struct btrfs_device *dev;
6789 int i;
6790
6791 if (list_empty(&transaction->pending_chunks))
6792 return;
6793
6794 /* In order to kick the device replace finish process */
6795 lock_chunks(root);
6796 list_for_each_entry(em, &transaction->pending_chunks, list) {
6797 map = (struct map_lookup *)em->bdev;
6798
6799 for (i = 0; i < map->num_stripes; i++) {
6800 dev = map->stripes[i].dev;
6801 dev->commit_bytes_used = dev->bytes_used;
6802 }
6803 }
6804 unlock_chunks(root);
6805}
Anand Jain5a13f432015-03-10 06:38:31 +08006806
6807void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
6808{
6809 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6810 while (fs_devices) {
6811 fs_devices->fs_info = fs_info;
6812 fs_devices = fs_devices->seed;
6813 }
6814}
6815
6816void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
6817{
6818 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6819 while (fs_devices) {
6820 fs_devices->fs_info = NULL;
6821 fs_devices = fs_devices->seed;
6822 }
6823}