blob: 2a65d2d45410541c31de1d3b48c0e3722a5e6395 [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
Zhao Leiaf902042015-09-15 21:08:06 +080045const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
46 [BTRFS_RAID_RAID10] = {
47 .sub_stripes = 2,
48 .dev_stripes = 1,
49 .devs_max = 0, /* 0 == as many as possible */
50 .devs_min = 4,
51 .devs_increment = 2,
52 .ncopies = 2,
53 },
54 [BTRFS_RAID_RAID1] = {
55 .sub_stripes = 1,
56 .dev_stripes = 1,
57 .devs_max = 2,
58 .devs_min = 2,
59 .devs_increment = 2,
60 .ncopies = 2,
61 },
62 [BTRFS_RAID_DUP] = {
63 .sub_stripes = 1,
64 .dev_stripes = 2,
65 .devs_max = 1,
66 .devs_min = 1,
67 .devs_increment = 1,
68 .ncopies = 2,
69 },
70 [BTRFS_RAID_RAID0] = {
71 .sub_stripes = 1,
72 .dev_stripes = 1,
73 .devs_max = 0,
74 .devs_min = 2,
75 .devs_increment = 1,
76 .ncopies = 1,
77 },
78 [BTRFS_RAID_SINGLE] = {
79 .sub_stripes = 1,
80 .dev_stripes = 1,
81 .devs_max = 1,
82 .devs_min = 1,
83 .devs_increment = 1,
84 .ncopies = 1,
85 },
86 [BTRFS_RAID_RAID5] = {
87 .sub_stripes = 1,
88 .dev_stripes = 1,
89 .devs_max = 0,
90 .devs_min = 2,
91 .devs_increment = 1,
92 .ncopies = 2,
93 },
94 [BTRFS_RAID_RAID6] = {
95 .sub_stripes = 1,
96 .dev_stripes = 1,
97 .devs_max = 0,
98 .devs_min = 3,
99 .devs_increment = 1,
100 .ncopies = 3,
101 },
102};
103
104const u64 const btrfs_raid_group[BTRFS_NR_RAID_TYPES] = {
105 [BTRFS_RAID_RAID10] = BTRFS_BLOCK_GROUP_RAID10,
106 [BTRFS_RAID_RAID1] = BTRFS_BLOCK_GROUP_RAID1,
107 [BTRFS_RAID_DUP] = BTRFS_BLOCK_GROUP_DUP,
108 [BTRFS_RAID_RAID0] = BTRFS_BLOCK_GROUP_RAID0,
109 [BTRFS_RAID_SINGLE] = 0,
110 [BTRFS_RAID_RAID5] = BTRFS_BLOCK_GROUP_RAID5,
111 [BTRFS_RAID_RAID6] = BTRFS_BLOCK_GROUP_RAID6,
112};
113
Yan Zheng2b820322008-11-17 21:11:30 -0500114static int init_first_rw_device(struct btrfs_trans_handle *trans,
115 struct btrfs_root *root,
116 struct btrfs_device *device);
117static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200118static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000119static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200120static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Yan Zheng2b820322008-11-17 21:11:30 -0500121
Miao Xie67a2c452014-09-03 21:35:43 +0800122DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400123static LIST_HEAD(fs_uuids);
Anand Jainc73eccf2015-03-10 06:38:30 +0800124struct list_head *btrfs_get_fs_uuids(void)
125{
126 return &fs_uuids;
127}
Chris Mason8a4b83c2008-03-24 15:02:07 -0400128
Ilya Dryomov2208a372013-08-12 14:33:03 +0300129static struct btrfs_fs_devices *__alloc_fs_devices(void)
130{
131 struct btrfs_fs_devices *fs_devs;
132
133 fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS);
134 if (!fs_devs)
135 return ERR_PTR(-ENOMEM);
136
137 mutex_init(&fs_devs->device_list_mutex);
138
139 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +0800140 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300141 INIT_LIST_HEAD(&fs_devs->alloc_list);
142 INIT_LIST_HEAD(&fs_devs->list);
143
144 return fs_devs;
145}
146
147/**
148 * alloc_fs_devices - allocate struct btrfs_fs_devices
149 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
150 * generated.
151 *
152 * Return: a pointer to a new &struct btrfs_fs_devices on success;
153 * ERR_PTR() on error. Returned struct is not linked onto any lists and
154 * can be destroyed with kfree() right away.
155 */
156static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
157{
158 struct btrfs_fs_devices *fs_devs;
159
160 fs_devs = __alloc_fs_devices();
161 if (IS_ERR(fs_devs))
162 return fs_devs;
163
164 if (fsid)
165 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
166 else
167 generate_random_uuid(fs_devs->fsid);
168
169 return fs_devs;
170}
171
Yan Zhenge4404d62008-12-12 10:03:26 -0500172static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
173{
174 struct btrfs_device *device;
175 WARN_ON(fs_devices->opened);
176 while (!list_empty(&fs_devices->devices)) {
177 device = list_entry(fs_devices->devices.next,
178 struct btrfs_device, dev_list);
179 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -0400180 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500181 kfree(device);
182 }
183 kfree(fs_devices);
184}
185
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000186static void btrfs_kobject_uevent(struct block_device *bdev,
187 enum kobject_action action)
188{
189 int ret;
190
191 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
192 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500193 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000194 action,
195 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
196 &disk_to_dev(bdev->bd_disk)->kobj);
197}
198
Jeff Mahoney143bede2012-03-01 14:56:26 +0100199void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400200{
201 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400202
Yan Zheng2b820322008-11-17 21:11:30 -0500203 while (!list_empty(&fs_uuids)) {
204 fs_devices = list_entry(fs_uuids.next,
205 struct btrfs_fs_devices, list);
206 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500207 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400208 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400209}
210
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300211static struct btrfs_device *__alloc_device(void)
212{
213 struct btrfs_device *dev;
214
215 dev = kzalloc(sizeof(*dev), GFP_NOFS);
216 if (!dev)
217 return ERR_PTR(-ENOMEM);
218
219 INIT_LIST_HEAD(&dev->dev_list);
220 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800221 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300222
223 spin_lock_init(&dev->io_lock);
224
225 spin_lock_init(&dev->reada_lock);
226 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800227 atomic_set(&dev->dev_stats_ccnt, 0);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300228 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT);
229 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT);
230
231 return dev;
232}
233
Chris Masona1b32a52008-09-05 16:09:51 -0400234static noinline struct btrfs_device *__find_device(struct list_head *head,
235 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400236{
237 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400238
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500239 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400240 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400241 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400242 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400243 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400244 }
245 return NULL;
246}
247
Chris Masona1b32a52008-09-05 16:09:51 -0400248static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400249{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400250 struct btrfs_fs_devices *fs_devices;
251
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500252 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400253 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
254 return fs_devices;
255 }
256 return NULL;
257}
258
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100259static int
260btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
261 int flush, struct block_device **bdev,
262 struct buffer_head **bh)
263{
264 int ret;
265
266 *bdev = blkdev_get_by_path(device_path, flags, holder);
267
268 if (IS_ERR(*bdev)) {
269 ret = PTR_ERR(*bdev);
Frank Holtonefe120a2013-12-20 11:37:06 -0500270 printk(KERN_INFO "BTRFS: open %s failed\n", device_path);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100271 goto error;
272 }
273
274 if (flush)
275 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
276 ret = set_blocksize(*bdev, 4096);
277 if (ret) {
278 blkdev_put(*bdev, flags);
279 goto error;
280 }
281 invalidate_bdev(*bdev);
282 *bh = btrfs_read_dev_super(*bdev);
283 if (!*bh) {
284 ret = -EINVAL;
285 blkdev_put(*bdev, flags);
286 goto error;
287 }
288
289 return 0;
290
291error:
292 *bdev = NULL;
293 *bh = NULL;
294 return ret;
295}
296
Chris Masonffbd5172009-04-20 15:50:09 -0400297static void requeue_list(struct btrfs_pending_bios *pending_bios,
298 struct bio *head, struct bio *tail)
299{
300
301 struct bio *old_head;
302
303 old_head = pending_bios->head;
304 pending_bios->head = head;
305 if (pending_bios->tail)
306 tail->bi_next = old_head;
307 else
308 pending_bios->tail = tail;
309}
310
Chris Mason8b712842008-06-11 16:50:36 -0400311/*
312 * we try to collect pending bios for a device so we don't get a large
313 * number of procs sending bios down to the same device. This greatly
314 * improves the schedulers ability to collect and merge the bios.
315 *
316 * But, it also turns into a long list of bios to process and that is sure
317 * to eventually make the worker thread block. The solution here is to
318 * make some progress and then put this work struct back at the end of
319 * the list if the block device is congested. This way, multiple devices
320 * can make progress from a single worker thread.
321 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100322static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400323{
324 struct bio *pending;
325 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400326 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400327 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400328 struct bio *tail;
329 struct bio *cur;
330 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400331 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400332 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400333 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400334 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400335 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000336 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400337 struct blk_plug plug;
338
339 /*
340 * this function runs all the bios we've collected for
341 * a particular device. We don't want to wander off to
342 * another device without first sending all of these down.
343 * So, setup a plug here and finish it off before we return
344 */
345 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400346
Chris Masonbedf7622009-04-03 10:32:58 -0400347 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400348 fs_info = device->dev_root->fs_info;
349 limit = btrfs_async_submit_limit(fs_info);
350 limit = limit * 2 / 3;
351
Chris Mason8b712842008-06-11 16:50:36 -0400352loop:
353 spin_lock(&device->io_lock);
354
Chris Masona6837052009-02-04 09:19:41 -0500355loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400356 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400357
Chris Mason8b712842008-06-11 16:50:36 -0400358 /* take all the bios off the list at once and process them
359 * later on (without the lock held). But, remember the
360 * tail and other pointers so the bios can be properly reinserted
361 * into the list if we hit congestion
362 */
Chris Masond84275c2009-06-09 15:39:08 -0400363 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400364 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400365 force_reg = 1;
366 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400367 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400368 force_reg = 0;
369 }
Chris Masonffbd5172009-04-20 15:50:09 -0400370
371 pending = pending_bios->head;
372 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400373 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400374
375 /*
376 * if pending was null this time around, no bios need processing
377 * at all and we can stop. Otherwise it'll loop back up again
378 * and do an additional check so no bios are missed.
379 *
380 * device->running_pending is used to synchronize with the
381 * schedule_bio code.
382 */
Chris Masonffbd5172009-04-20 15:50:09 -0400383 if (device->pending_sync_bios.head == NULL &&
384 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400385 again = 0;
386 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400387 } else {
388 again = 1;
389 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400390 }
Chris Masonffbd5172009-04-20 15:50:09 -0400391
392 pending_bios->head = NULL;
393 pending_bios->tail = NULL;
394
Chris Mason8b712842008-06-11 16:50:36 -0400395 spin_unlock(&device->io_lock);
396
Chris Masond3977122009-01-05 21:25:51 -0500397 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400398
399 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400400 /* we want to work on both lists, but do more bios on the
401 * sync list than the regular list
402 */
403 if ((num_run > 32 &&
404 pending_bios != &device->pending_sync_bios &&
405 device->pending_sync_bios.head) ||
406 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
407 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400408 spin_lock(&device->io_lock);
409 requeue_list(pending_bios, pending, tail);
410 goto loop_lock;
411 }
412
Chris Mason8b712842008-06-11 16:50:36 -0400413 cur = pending;
414 pending = pending->bi_next;
415 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400416
Josef Bacik66657b32012-08-01 15:36:24 -0400417 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400418 waitqueue_active(&fs_info->async_submit_wait))
419 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400420
Jens Axboedac56212015-04-17 16:23:59 -0600421 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400422
Chris Mason2ab1ba62011-08-04 14:28:36 -0400423 /*
424 * if we're doing the sync list, record that our
425 * plug has some sync requests on it
426 *
427 * If we're doing the regular list and there are
428 * sync requests sitting around, unplug before
429 * we add more
430 */
431 if (pending_bios == &device->pending_sync_bios) {
432 sync_pending = 1;
433 } else if (sync_pending) {
434 blk_finish_plug(&plug);
435 blk_start_plug(&plug);
436 sync_pending = 0;
437 }
438
Stefan Behrens21adbd52011-11-09 13:44:05 +0100439 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400440 num_run++;
441 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100442
443 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400444
445 /*
446 * we made progress, there is more work to do and the bdi
447 * is now congested. Back off and let other work structs
448 * run instead
449 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400450 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500451 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400452 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400453
Chris Masonb765ead2009-04-03 10:27:10 -0400454 ioc = current->io_context;
455
456 /*
457 * the main goal here is that we don't want to
458 * block if we're going to be able to submit
459 * more requests without blocking.
460 *
461 * This code does two great things, it pokes into
462 * the elevator code from a filesystem _and_
463 * it makes assumptions about how batching works.
464 */
465 if (ioc && ioc->nr_batch_requests > 0 &&
466 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
467 (last_waited == 0 ||
468 ioc->last_waited == last_waited)) {
469 /*
470 * we want to go through our batch of
471 * requests and stop. So, we copy out
472 * the ioc->last_waited time and test
473 * against it before looping
474 */
475 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100476 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400477 continue;
478 }
Chris Mason8b712842008-06-11 16:50:36 -0400479 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400480 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500481 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400482
483 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800484 btrfs_queue_work(fs_info->submit_workers,
485 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400486 goto done;
487 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500488 /* unplug every 64 requests just for good measure */
489 if (batch_run % 64 == 0) {
490 blk_finish_plug(&plug);
491 blk_start_plug(&plug);
492 sync_pending = 0;
493 }
Chris Mason8b712842008-06-11 16:50:36 -0400494 }
Chris Masonffbd5172009-04-20 15:50:09 -0400495
Chris Mason51684082010-03-10 15:33:32 -0500496 cond_resched();
497 if (again)
498 goto loop;
499
500 spin_lock(&device->io_lock);
501 if (device->pending_bios.head || device->pending_sync_bios.head)
502 goto loop_lock;
503 spin_unlock(&device->io_lock);
504
Chris Mason8b712842008-06-11 16:50:36 -0400505done:
Chris Mason211588a2011-04-19 20:12:40 -0400506 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400507}
508
Christoph Hellwigb2950862008-12-02 09:54:17 -0500509static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400510{
511 struct btrfs_device *device;
512
513 device = container_of(work, struct btrfs_device, work);
514 run_scheduled_bios(device);
515}
516
Anand Jain4fde46f2015-06-17 21:10:48 +0800517
518void btrfs_free_stale_device(struct btrfs_device *cur_dev)
519{
520 struct btrfs_fs_devices *fs_devs;
521 struct btrfs_device *dev;
522
523 if (!cur_dev->name)
524 return;
525
526 list_for_each_entry(fs_devs, &fs_uuids, list) {
527 int del = 1;
528
529 if (fs_devs->opened)
530 continue;
531 if (fs_devs->seeding)
532 continue;
533
534 list_for_each_entry(dev, &fs_devs->devices, dev_list) {
535
536 if (dev == cur_dev)
537 continue;
538 if (!dev->name)
539 continue;
540
541 /*
542 * Todo: This won't be enough. What if the same device
543 * comes back (with new uuid and) with its mapper path?
544 * But for now, this does help as mostly an admin will
545 * either use mapper or non mapper path throughout.
546 */
547 rcu_read_lock();
548 del = strcmp(rcu_str_deref(dev->name),
549 rcu_str_deref(cur_dev->name));
550 rcu_read_unlock();
551 if (!del)
552 break;
553 }
554
555 if (!del) {
556 /* delete the stale device */
557 if (fs_devs->num_devices == 1) {
558 btrfs_sysfs_remove_fsid(fs_devs);
559 list_del(&fs_devs->list);
560 free_fs_devices(fs_devs);
561 } else {
562 fs_devs->num_devices--;
563 list_del(&dev->dev_list);
564 rcu_string_free(dev->name);
565 kfree(dev);
566 }
567 break;
568 }
569 }
570}
571
David Sterba60999ca2014-03-26 18:26:36 +0100572/*
573 * Add new device to list of registered devices
574 *
575 * Returns:
576 * 1 - first time device is seen
577 * 0 - device already known
578 * < 0 - error
579 */
Chris Masona1b32a52008-09-05 16:09:51 -0400580static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400581 struct btrfs_super_block *disk_super,
582 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
583{
584 struct btrfs_device *device;
585 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400586 struct rcu_string *name;
David Sterba60999ca2014-03-26 18:26:36 +0100587 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400588 u64 found_transid = btrfs_super_generation(disk_super);
589
590 fs_devices = find_fsid(disk_super->fsid);
591 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300592 fs_devices = alloc_fs_devices(disk_super->fsid);
593 if (IS_ERR(fs_devices))
594 return PTR_ERR(fs_devices);
595
Chris Mason8a4b83c2008-03-24 15:02:07 -0400596 list_add(&fs_devices->list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300597
Chris Mason8a4b83c2008-03-24 15:02:07 -0400598 device = NULL;
599 } else {
Chris Masona4437552008-04-18 10:29:38 -0400600 device = __find_device(&fs_devices->devices, devid,
601 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400602 }
Miao Xie443f24f2014-07-24 11:37:15 +0800603
Chris Mason8a4b83c2008-03-24 15:02:07 -0400604 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500605 if (fs_devices->opened)
606 return -EBUSY;
607
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300608 device = btrfs_alloc_device(NULL, &devid,
609 disk_super->dev_item.uuid);
610 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400611 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300612 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400613 }
Josef Bacik606686e2012-06-04 14:03:51 -0400614
615 name = rcu_string_strdup(path, GFP_NOFS);
616 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400617 kfree(device);
618 return -ENOMEM;
619 }
Josef Bacik606686e2012-06-04 14:03:51 -0400620 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200621
Chris Masone5e9a522009-06-10 15:17:02 -0400622 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000623 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100624 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400625 mutex_unlock(&fs_devices->device_list_mutex);
626
David Sterba60999ca2014-03-26 18:26:36 +0100627 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500628 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400629 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800630 /*
631 * When FS is already mounted.
632 * 1. If you are here and if the device->name is NULL that
633 * means this device was missing at time of FS mount.
634 * 2. If you are here and if the device->name is different
635 * from 'path' that means either
636 * a. The same device disappeared and reappeared with
637 * different name. or
638 * b. The missing-disk-which-was-replaced, has
639 * reappeared now.
640 *
641 * We must allow 1 and 2a above. But 2b would be a spurious
642 * and unintentional.
643 *
644 * Further in case of 1 and 2a above, the disk at 'path'
645 * would have missed some transaction when it was away and
646 * in case of 2a the stale bdev has to be updated as well.
647 * 2b must not be allowed at all time.
648 */
649
650 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700651 * For now, we do allow update to btrfs_fs_device through the
652 * btrfs dev scan cli after FS has been mounted. We're still
653 * tracking a problem where systems fail mount by subvolume id
654 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800655 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700656 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800657 /*
658 * That is if the FS is _not_ mounted and if you
659 * are here, that means there is more than one
660 * disk with same uuid and devid.We keep the one
661 * with larger generation number or the last-in if
662 * generation are equal.
663 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700664 return -EEXIST;
Anand Jain77bdae42014-07-03 18:22:06 +0800665 }
Anand Jainb96de002014-07-03 18:22:05 +0800666
Josef Bacik606686e2012-06-04 14:03:51 -0400667 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000668 if (!name)
669 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400670 rcu_string_free(device->name);
671 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500672 if (device->missing) {
673 fs_devices->missing_devices--;
674 device->missing = 0;
675 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400676 }
677
Anand Jain77bdae42014-07-03 18:22:06 +0800678 /*
679 * Unmount does not free the btrfs_device struct but would zero
680 * generation along with most of the other members. So just update
681 * it back. We need it to pick the disk with largest generation
682 * (as above).
683 */
684 if (!fs_devices->opened)
685 device->generation = found_transid;
686
Anand Jain4fde46f2015-06-17 21:10:48 +0800687 /*
688 * if there is new btrfs on an already registered device,
689 * then remove the stale device entry.
690 */
691 btrfs_free_stale_device(device);
692
Chris Mason8a4b83c2008-03-24 15:02:07 -0400693 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100694
695 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400696}
697
Yan Zhenge4404d62008-12-12 10:03:26 -0500698static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
699{
700 struct btrfs_fs_devices *fs_devices;
701 struct btrfs_device *device;
702 struct btrfs_device *orig_dev;
703
Ilya Dryomov2208a372013-08-12 14:33:03 +0300704 fs_devices = alloc_fs_devices(orig->fsid);
705 if (IS_ERR(fs_devices))
706 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500707
Miao Xieadbbb862014-09-03 21:35:42 +0800708 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400709 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500710
Xiao Guangrong46224702011-04-20 10:08:47 +0000711 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500712 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400713 struct rcu_string *name;
714
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300715 device = btrfs_alloc_device(NULL, &orig_dev->devid,
716 orig_dev->uuid);
717 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500718 goto error;
719
Josef Bacik606686e2012-06-04 14:03:51 -0400720 /*
721 * This is ok to do without rcu read locked because we hold the
722 * uuid mutex so nothing we touch in here is going to disappear.
723 */
Anand Jaine755f782014-06-30 17:12:47 +0800724 if (orig_dev->name) {
725 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
726 if (!name) {
727 kfree(device);
728 goto error;
729 }
730 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400731 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500732
Yan Zhenge4404d62008-12-12 10:03:26 -0500733 list_add(&device->dev_list, &fs_devices->devices);
734 device->fs_devices = fs_devices;
735 fs_devices->num_devices++;
736 }
Miao Xieadbbb862014-09-03 21:35:42 +0800737 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500738 return fs_devices;
739error:
Miao Xieadbbb862014-09-03 21:35:42 +0800740 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500741 free_fs_devices(fs_devices);
742 return ERR_PTR(-ENOMEM);
743}
744
Eric Sandeen9eaed212014-08-01 18:12:35 -0500745void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400746{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500747 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800748 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500749
Chris Masondfe25022008-05-13 13:46:40 -0400750 mutex_lock(&uuid_mutex);
751again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000752 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500753 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500754 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100755 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800756 (!latest_dev ||
757 device->generation > latest_dev->generation)) {
758 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500759 }
Yan Zheng2b820322008-11-17 21:11:30 -0500760 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500761 }
Yan Zheng2b820322008-11-17 21:11:30 -0500762
Stefan Behrens8dabb742012-11-06 13:15:27 +0100763 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
764 /*
765 * In the first step, keep the device which has
766 * the correct fsid and the devid that is used
767 * for the dev_replace procedure.
768 * In the second step, the dev_replace state is
769 * read from the device tree and it is known
770 * whether the procedure is really active or
771 * not, which means whether this device is
772 * used or whether it should be removed.
773 */
774 if (step == 0 || device->is_tgtdev_for_dev_replace) {
775 continue;
776 }
777 }
Yan Zheng2b820322008-11-17 21:11:30 -0500778 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100779 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500780 device->bdev = NULL;
781 fs_devices->open_devices--;
782 }
783 if (device->writeable) {
784 list_del_init(&device->dev_alloc_list);
785 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100786 if (!device->is_tgtdev_for_dev_replace)
787 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500788 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500789 list_del_init(&device->dev_list);
790 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400791 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500792 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400793 }
Yan Zheng2b820322008-11-17 21:11:30 -0500794
795 if (fs_devices->seed) {
796 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500797 goto again;
798 }
799
Miao Xie443f24f2014-07-24 11:37:15 +0800800 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500801
Chris Masondfe25022008-05-13 13:46:40 -0400802 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400803}
Chris Masona0af4692008-05-13 16:03:06 -0400804
Xiao Guangrong1f781602011-04-20 10:09:16 +0000805static void __free_device(struct work_struct *work)
806{
807 struct btrfs_device *device;
808
809 device = container_of(work, struct btrfs_device, rcu_work);
810
811 if (device->bdev)
812 blkdev_put(device->bdev, device->mode);
813
Josef Bacik606686e2012-06-04 14:03:51 -0400814 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000815 kfree(device);
816}
817
818static void free_device(struct rcu_head *head)
819{
820 struct btrfs_device *device;
821
822 device = container_of(head, struct btrfs_device, rcu);
823
824 INIT_WORK(&device->rcu_work, __free_device);
825 schedule_work(&device->rcu_work);
826}
827
Yan Zheng2b820322008-11-17 21:11:30 -0500828static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400829{
Sasha Levin2037a092015-05-12 19:31:37 -0400830 struct btrfs_device *device, *tmp;
Yan Zhenge4404d62008-12-12 10:03:26 -0500831
Yan Zheng2b820322008-11-17 21:11:30 -0500832 if (--fs_devices->opened > 0)
833 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400834
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000835 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -0400836 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000837 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400838 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000839
840 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400841 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000842
Ilya Dryomovf747cab2013-10-10 20:37:29 +0300843 if (device->writeable &&
844 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500845 list_del_init(&device->dev_alloc_list);
846 fs_devices->rw_devices--;
847 }
848
Josef Bacik726551e2013-08-26 13:45:53 -0400849 if (device->missing)
850 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000851
Ilya Dryomova1e87802013-08-12 14:33:04 +0300852 new_device = btrfs_alloc_device(NULL, &device->devid,
853 device->uuid);
854 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400855
856 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400857 if (device->name) {
858 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300859 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400860 rcu_assign_pointer(new_device->name, name);
861 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300862
Xiao Guangrong1f781602011-04-20 10:09:16 +0000863 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300864 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000865
866 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400867 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000868 mutex_unlock(&fs_devices->device_list_mutex);
869
Yan Zhenge4404d62008-12-12 10:03:26 -0500870 WARN_ON(fs_devices->open_devices);
871 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500872 fs_devices->opened = 0;
873 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500874
Chris Mason8a4b83c2008-03-24 15:02:07 -0400875 return 0;
876}
877
Yan Zheng2b820322008-11-17 21:11:30 -0500878int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
879{
Yan Zhenge4404d62008-12-12 10:03:26 -0500880 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500881 int ret;
882
883 mutex_lock(&uuid_mutex);
884 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500885 if (!fs_devices->opened) {
886 seed_devices = fs_devices->seed;
887 fs_devices->seed = NULL;
888 }
Yan Zheng2b820322008-11-17 21:11:30 -0500889 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500890
891 while (seed_devices) {
892 fs_devices = seed_devices;
893 seed_devices = fs_devices->seed;
894 __btrfs_close_devices(fs_devices);
895 free_fs_devices(fs_devices);
896 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000897 /*
898 * Wait for rcu kworkers under __btrfs_close_devices
899 * to finish all blkdev_puts so device is really
900 * free when umount is done.
901 */
902 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500903 return ret;
904}
905
Yan Zhenge4404d62008-12-12 10:03:26 -0500906static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
907 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400908{
Josef Bacikd5e20032011-08-04 14:52:27 +0000909 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400910 struct block_device *bdev;
911 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400912 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +0800913 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -0400914 struct buffer_head *bh;
915 struct btrfs_super_block *disk_super;
Chris Masona0af4692008-05-13 16:03:06 -0400916 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500917 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400918 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400919
Tejun Heod4d77622010-11-13 11:55:18 +0100920 flags |= FMODE_EXCL;
921
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500922 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400923 if (device->bdev)
924 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400925 if (!device->name)
926 continue;
927
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000928 /* Just open everything we can; ignore failures here */
929 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
930 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100931 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400932
933 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000934 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400935 if (devid != device->devid)
936 goto error_brelse;
937
Yan Zheng2b820322008-11-17 21:11:30 -0500938 if (memcmp(device->uuid, disk_super->dev_item.uuid,
939 BTRFS_UUID_SIZE))
940 goto error_brelse;
941
942 device->generation = btrfs_super_generation(disk_super);
Miao Xie443f24f2014-07-24 11:37:15 +0800943 if (!latest_dev ||
944 device->generation > latest_dev->generation)
945 latest_dev = device;
Chris Masona0af4692008-05-13 16:03:06 -0400946
Yan Zheng2b820322008-11-17 21:11:30 -0500947 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
948 device->writeable = 0;
949 } else {
950 device->writeable = !bdev_read_only(bdev);
951 seeding = 0;
952 }
953
Josef Bacikd5e20032011-08-04 14:52:27 +0000954 q = bdev_get_queue(bdev);
Miao Xie90180da2014-09-03 21:35:30 +0800955 if (blk_queue_discard(q))
Josef Bacikd5e20032011-08-04 14:52:27 +0000956 device->can_discard = 1;
Josef Bacikd5e20032011-08-04 14:52:27 +0000957
Chris Mason8a4b83c2008-03-24 15:02:07 -0400958 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400959 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500960 device->mode = flags;
961
Chris Masonc2898112009-06-10 09:51:32 -0400962 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
963 fs_devices->rotating = 1;
964
Chris Masona0af4692008-05-13 16:03:06 -0400965 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300966 if (device->writeable &&
967 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500968 fs_devices->rw_devices++;
969 list_add(&device->dev_alloc_list,
970 &fs_devices->alloc_list);
971 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000972 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400973 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400974
Chris Masona0af4692008-05-13 16:03:06 -0400975error_brelse:
976 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100977 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400978 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400979 }
Chris Masona0af4692008-05-13 16:03:06 -0400980 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300981 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400982 goto out;
983 }
Yan Zheng2b820322008-11-17 21:11:30 -0500984 fs_devices->seeding = seeding;
985 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +0800986 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -0500987 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400988out:
Yan Zheng2b820322008-11-17 21:11:30 -0500989 return ret;
990}
991
992int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500993 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500994{
995 int ret;
996
997 mutex_lock(&uuid_mutex);
998 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500999 fs_devices->opened++;
1000 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001001 } else {
Chris Mason15916de2008-11-19 21:17:22 -05001002 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001003 }
Chris Mason8a4b83c2008-03-24 15:02:07 -04001004 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001005 return ret;
1006}
1007
David Sterba6f60cbd2013-02-15 11:31:02 -07001008/*
1009 * Look for a btrfs signature on a device. This may be called out of the mount path
1010 * and we are not allowed to call set_blocksize during the scan. The superblock
1011 * is read via pagecache
1012 */
Christoph Hellwig97288f22008-12-02 06:36:09 -05001013int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -04001014 struct btrfs_fs_devices **fs_devices_ret)
1015{
1016 struct btrfs_super_block *disk_super;
1017 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001018 struct page *page;
1019 void *p;
1020 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001021 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -04001022 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -04001023 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001024 u64 bytenr;
1025 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001026
David Sterba6f60cbd2013-02-15 11:31:02 -07001027 /*
1028 * we would like to check all the supers, but that would make
1029 * a btrfs mount succeed after a mkfs from a different FS.
1030 * So, we need to add a special mount option to scan for
1031 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1032 */
1033 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001034 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -05001035 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -07001036
1037 bdev = blkdev_get_by_path(path, flags, holder);
1038
1039 if (IS_ERR(bdev)) {
1040 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001041 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -07001042 }
1043
1044 /* make sure our super fits in the device */
1045 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
1046 goto error_bdev_put;
1047
1048 /* make sure our super fits in the page */
1049 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
1050 goto error_bdev_put;
1051
1052 /* make sure our super doesn't straddle pages on disk */
1053 index = bytenr >> PAGE_CACHE_SHIFT;
1054 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
1055 goto error_bdev_put;
1056
1057 /* pull in the page with our super */
1058 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1059 index, GFP_NOFS);
1060
1061 if (IS_ERR_OR_NULL(page))
1062 goto error_bdev_put;
1063
1064 p = kmap(page);
1065
1066 /* align our pointer to the offset of the super block */
1067 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
1068
1069 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +08001070 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -07001071 goto error_unmap;
1072
Xiao Guangronga3438322010-01-06 11:48:18 +00001073 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -04001074 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -04001075 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -07001076
Chris Mason8a4b83c2008-03-24 15:02:07 -04001077 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +01001078 if (ret > 0) {
1079 if (disk_super->label[0]) {
1080 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
1081 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
1082 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
1083 } else {
1084 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
1085 }
1086
1087 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
1088 ret = 0;
1089 }
Josef Bacik02db0842012-06-21 16:03:58 -04001090 if (!ret && fs_devices_ret)
1091 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001092
1093error_unmap:
1094 kunmap(page);
1095 page_cache_release(page);
1096
1097error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001098 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001099error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001100 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001101 return ret;
1102}
Chris Mason0b86a832008-03-24 15:01:56 -04001103
Miao Xie6d07bce2011-01-05 10:07:31 +00001104/* helper to account the used device space in the range */
1105int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1106 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -04001107{
1108 struct btrfs_key key;
1109 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +00001110 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001111 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +00001112 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001113 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +00001114 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001115 struct extent_buffer *l;
1116
Miao Xie6d07bce2011-01-05 10:07:31 +00001117 *length = 0;
1118
Stefan Behrens63a212a2012-11-05 18:29:28 +01001119 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +00001120 return 0;
1121
Yan Zheng2b820322008-11-17 21:11:30 -05001122 path = btrfs_alloc_path();
1123 if (!path)
1124 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001125 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -04001126
Chris Mason0b86a832008-03-24 15:01:56 -04001127 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001128 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001129 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001130
1131 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001132 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001133 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001134 if (ret > 0) {
1135 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1136 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001137 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001138 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001139
Chris Mason0b86a832008-03-24 15:01:56 -04001140 while (1) {
1141 l = path->nodes[0];
1142 slot = path->slots[0];
1143 if (slot >= btrfs_header_nritems(l)) {
1144 ret = btrfs_next_leaf(root, path);
1145 if (ret == 0)
1146 continue;
1147 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001148 goto out;
1149
1150 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001151 }
1152 btrfs_item_key_to_cpu(l, &key, slot);
1153
1154 if (key.objectid < device->devid)
1155 goto next;
1156
1157 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001158 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001159
David Sterba962a2982014-06-04 18:41:45 +02001160 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001161 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001162
Chris Mason0b86a832008-03-24 15:01:56 -04001163 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001164 extent_end = key.offset + btrfs_dev_extent_length(l,
1165 dev_extent);
1166 if (key.offset <= start && extent_end > end) {
1167 *length = end - start + 1;
1168 break;
1169 } else if (key.offset <= start && extent_end > start)
1170 *length += extent_end - start;
1171 else if (key.offset > start && extent_end <= end)
1172 *length += extent_end - key.offset;
1173 else if (key.offset > start && key.offset <= end) {
1174 *length += end - key.offset + 1;
1175 break;
1176 } else if (key.offset > end)
1177 break;
1178
1179next:
1180 path->slots[0]++;
1181 }
1182 ret = 0;
1183out:
1184 btrfs_free_path(path);
1185 return ret;
1186}
1187
Jeff Mahoney499f3772015-06-15 09:41:17 -04001188static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001189 struct btrfs_device *device,
1190 u64 *start, u64 len)
1191{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001192 struct btrfs_fs_info *fs_info = device->dev_root->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001193 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001194 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001195 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001196 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001197
Jeff Mahoney499f3772015-06-15 09:41:17 -04001198 if (transaction)
1199 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001200again:
1201 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001202 struct map_lookup *map;
1203 int i;
1204
1205 map = (struct map_lookup *)em->bdev;
1206 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001207 u64 end;
1208
Josef Bacik6df9a952013-06-27 13:22:46 -04001209 if (map->stripes[i].dev != device)
1210 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001211 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001212 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001213 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001214 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001215 /*
1216 * Make sure that while processing the pinned list we do
1217 * not override our *start with a lower value, because
1218 * we can have pinned chunks that fall within this
1219 * device hole and that have lower physical addresses
1220 * than the pending chunks we processed before. If we
1221 * do not take this special care we can end up getting
1222 * 2 pending chunks that start at the same physical
1223 * device offsets because the end offset of a pinned
1224 * chunk can be equal to the start offset of some
1225 * pending chunk.
1226 */
1227 end = map->stripes[i].physical + em->orig_block_len;
1228 if (end > *start) {
1229 *start = end;
1230 ret = 1;
1231 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001232 }
1233 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001234 if (search_list != &fs_info->pinned_chunks) {
1235 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001236 goto again;
1237 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001238
1239 return ret;
1240}
1241
1242
Chris Mason0b86a832008-03-24 15:01:56 -04001243/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001244 * find_free_dev_extent_start - find free space in the specified device
1245 * @device: the device which we search the free space in
1246 * @num_bytes: the size of the free space that we need
1247 * @search_start: the position from which to begin the search
1248 * @start: store the start of the free space.
1249 * @len: the size of the free space. that we find, or the size
1250 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001251 *
Chris Mason0b86a832008-03-24 15:01:56 -04001252 * this uses a pretty simple search, the expectation is that it is
1253 * called very infrequently and that a given device has a small number
1254 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001255 *
1256 * @start is used to store the start of the free space if we find. But if we
1257 * don't find suitable free space, it will be used to store the start position
1258 * of the max free space.
1259 *
1260 * @len is used to store the size of the free space that we find.
1261 * But if we don't find suitable free space, it is used to store the size of
1262 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001263 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001264int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1265 struct btrfs_device *device, u64 num_bytes,
1266 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001267{
1268 struct btrfs_key key;
1269 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001270 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001271 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001272 u64 hole_size;
1273 u64 max_hole_start;
1274 u64 max_hole_size;
1275 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001276 u64 search_end = device->total_bytes;
1277 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001278 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001279 struct extent_buffer *l;
1280
Josef Bacik6df9a952013-06-27 13:22:46 -04001281 path = btrfs_alloc_path();
1282 if (!path)
1283 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001284
Miao Xie7bfc8372011-01-05 10:07:26 +00001285 max_hole_start = search_start;
1286 max_hole_size = 0;
1287
Zhao Leif2ab7612015-02-16 18:52:17 +08001288again:
Stefan Behrens63a212a2012-11-05 18:29:28 +01001289 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001290 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001291 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001292 }
1293
Miao Xie7bfc8372011-01-05 10:07:26 +00001294 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001295 path->search_commit_root = 1;
1296 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001297
Chris Mason0b86a832008-03-24 15:01:56 -04001298 key.objectid = device->devid;
1299 key.offset = search_start;
1300 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001301
Li Zefan125ccb02011-12-08 15:07:24 +08001302 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001303 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001304 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001305 if (ret > 0) {
1306 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1307 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001308 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001309 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001310
Chris Mason0b86a832008-03-24 15:01:56 -04001311 while (1) {
1312 l = path->nodes[0];
1313 slot = path->slots[0];
1314 if (slot >= btrfs_header_nritems(l)) {
1315 ret = btrfs_next_leaf(root, path);
1316 if (ret == 0)
1317 continue;
1318 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001319 goto out;
1320
1321 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001322 }
1323 btrfs_item_key_to_cpu(l, &key, slot);
1324
1325 if (key.objectid < device->devid)
1326 goto next;
1327
1328 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001329 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001330
David Sterba962a2982014-06-04 18:41:45 +02001331 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001332 goto next;
1333
Miao Xie7bfc8372011-01-05 10:07:26 +00001334 if (key.offset > search_start) {
1335 hole_size = key.offset - search_start;
1336
Josef Bacik6df9a952013-06-27 13:22:46 -04001337 /*
1338 * Have to check before we set max_hole_start, otherwise
1339 * we could end up sending back this offset anyway.
1340 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001341 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001342 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001343 hole_size)) {
1344 if (key.offset >= search_start) {
1345 hole_size = key.offset - search_start;
1346 } else {
1347 WARN_ON_ONCE(1);
1348 hole_size = 0;
1349 }
1350 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001351
Miao Xie7bfc8372011-01-05 10:07:26 +00001352 if (hole_size > max_hole_size) {
1353 max_hole_start = search_start;
1354 max_hole_size = hole_size;
1355 }
1356
1357 /*
1358 * If this free space is greater than which we need,
1359 * it must be the max free space that we have found
1360 * until now, so max_hole_start must point to the start
1361 * of this free space and the length of this free space
1362 * is stored in max_hole_size. Thus, we return
1363 * max_hole_start and max_hole_size and go back to the
1364 * caller.
1365 */
1366 if (hole_size >= num_bytes) {
1367 ret = 0;
1368 goto out;
1369 }
1370 }
1371
Chris Mason0b86a832008-03-24 15:01:56 -04001372 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001373 extent_end = key.offset + btrfs_dev_extent_length(l,
1374 dev_extent);
1375 if (extent_end > search_start)
1376 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001377next:
1378 path->slots[0]++;
1379 cond_resched();
1380 }
Chris Mason0b86a832008-03-24 15:01:56 -04001381
liubo38c01b92011-08-02 02:39:03 +00001382 /*
1383 * At this point, search_start should be the end of
1384 * allocated dev extents, and when shrinking the device,
1385 * search_end may be smaller than search_start.
1386 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001387 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001388 hole_size = search_end - search_start;
1389
Jeff Mahoney499f3772015-06-15 09:41:17 -04001390 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001391 hole_size)) {
1392 btrfs_release_path(path);
1393 goto again;
1394 }
Chris Mason0b86a832008-03-24 15:01:56 -04001395
Zhao Leif2ab7612015-02-16 18:52:17 +08001396 if (hole_size > max_hole_size) {
1397 max_hole_start = search_start;
1398 max_hole_size = hole_size;
1399 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001400 }
1401
Miao Xie7bfc8372011-01-05 10:07:26 +00001402 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001403 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001404 ret = -ENOSPC;
1405 else
1406 ret = 0;
1407
1408out:
Yan Zheng2b820322008-11-17 21:11:30 -05001409 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001410 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001411 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001412 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001413 return ret;
1414}
1415
Jeff Mahoney499f3772015-06-15 09:41:17 -04001416int find_free_dev_extent(struct btrfs_trans_handle *trans,
1417 struct btrfs_device *device, u64 num_bytes,
1418 u64 *start, u64 *len)
1419{
1420 struct btrfs_root *root = device->dev_root;
1421 u64 search_start;
1422
1423 /* FIXME use last free of some kind */
1424
1425 /*
1426 * we don't want to overwrite the superblock on the drive,
1427 * so we make sure to start at an offset of at least 1MB
1428 */
1429 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
1430 return find_free_dev_extent_start(trans->transaction, device,
1431 num_bytes, search_start, start, len);
1432}
1433
Christoph Hellwigb2950862008-12-02 09:54:17 -05001434static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001435 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001436 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001437{
1438 int ret;
1439 struct btrfs_path *path;
1440 struct btrfs_root *root = device->dev_root;
1441 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001442 struct btrfs_key found_key;
1443 struct extent_buffer *leaf = NULL;
1444 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001445
1446 path = btrfs_alloc_path();
1447 if (!path)
1448 return -ENOMEM;
1449
1450 key.objectid = device->devid;
1451 key.offset = start;
1452 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001453again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001454 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001455 if (ret > 0) {
1456 ret = btrfs_previous_item(root, path, key.objectid,
1457 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001458 if (ret)
1459 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001460 leaf = path->nodes[0];
1461 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1462 extent = btrfs_item_ptr(leaf, path->slots[0],
1463 struct btrfs_dev_extent);
1464 BUG_ON(found_key.offset > start || found_key.offset +
1465 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001466 key = found_key;
1467 btrfs_release_path(path);
1468 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001469 } else if (ret == 0) {
1470 leaf = path->nodes[0];
1471 extent = btrfs_item_ptr(leaf, path->slots[0],
1472 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001473 } else {
1474 btrfs_error(root->fs_info, ret, "Slot search failed");
1475 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001476 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001477
Miao Xie2196d6e2014-09-03 21:35:41 +08001478 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1479
Chris Mason8f18cf12008-04-25 16:53:30 -04001480 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001481 if (ret) {
1482 btrfs_error(root->fs_info, ret,
1483 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001484 } else {
1485 trans->transaction->have_free_bgs = 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001486 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001487out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001488 btrfs_free_path(path);
1489 return ret;
1490}
1491
Eric Sandeen48a3b632013-04-25 20:41:01 +00001492static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1493 struct btrfs_device *device,
1494 u64 chunk_tree, u64 chunk_objectid,
1495 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001496{
1497 int ret;
1498 struct btrfs_path *path;
1499 struct btrfs_root *root = device->dev_root;
1500 struct btrfs_dev_extent *extent;
1501 struct extent_buffer *leaf;
1502 struct btrfs_key key;
1503
Chris Masondfe25022008-05-13 13:46:40 -04001504 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001505 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001506 path = btrfs_alloc_path();
1507 if (!path)
1508 return -ENOMEM;
1509
Chris Mason0b86a832008-03-24 15:01:56 -04001510 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001511 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001512 key.type = BTRFS_DEV_EXTENT_KEY;
1513 ret = btrfs_insert_empty_item(trans, root, path, &key,
1514 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001515 if (ret)
1516 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001517
1518 leaf = path->nodes[0];
1519 extent = btrfs_item_ptr(leaf, path->slots[0],
1520 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001521 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1522 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1523 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1524
1525 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001526 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001527
Chris Mason0b86a832008-03-24 15:01:56 -04001528 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1529 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001530out:
Chris Mason0b86a832008-03-24 15:01:56 -04001531 btrfs_free_path(path);
1532 return ret;
1533}
1534
Josef Bacik6df9a952013-06-27 13:22:46 -04001535static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001536{
Josef Bacik6df9a952013-06-27 13:22:46 -04001537 struct extent_map_tree *em_tree;
1538 struct extent_map *em;
1539 struct rb_node *n;
1540 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001541
Josef Bacik6df9a952013-06-27 13:22:46 -04001542 em_tree = &fs_info->mapping_tree.map_tree;
1543 read_lock(&em_tree->lock);
1544 n = rb_last(&em_tree->map);
1545 if (n) {
1546 em = rb_entry(n, struct extent_map, rb_node);
1547 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001548 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001549 read_unlock(&em_tree->lock);
1550
Chris Mason0b86a832008-03-24 15:01:56 -04001551 return ret;
1552}
1553
Ilya Dryomov53f10652013-08-12 14:33:01 +03001554static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1555 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001556{
1557 int ret;
1558 struct btrfs_key key;
1559 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001560 struct btrfs_path *path;
1561
Yan Zheng2b820322008-11-17 21:11:30 -05001562 path = btrfs_alloc_path();
1563 if (!path)
1564 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001565
1566 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1567 key.type = BTRFS_DEV_ITEM_KEY;
1568 key.offset = (u64)-1;
1569
Ilya Dryomov53f10652013-08-12 14:33:01 +03001570 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001571 if (ret < 0)
1572 goto error;
1573
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001574 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001575
Ilya Dryomov53f10652013-08-12 14:33:01 +03001576 ret = btrfs_previous_item(fs_info->chunk_root, path,
1577 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001578 BTRFS_DEV_ITEM_KEY);
1579 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001580 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001581 } else {
1582 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1583 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001584 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001585 }
1586 ret = 0;
1587error:
Yan Zheng2b820322008-11-17 21:11:30 -05001588 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001589 return ret;
1590}
1591
1592/*
1593 * the device information is stored in the chunk root
1594 * the btrfs_device struct should be fully filled in
1595 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001596static int btrfs_add_device(struct btrfs_trans_handle *trans,
1597 struct btrfs_root *root,
1598 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001599{
1600 int ret;
1601 struct btrfs_path *path;
1602 struct btrfs_dev_item *dev_item;
1603 struct extent_buffer *leaf;
1604 struct btrfs_key key;
1605 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001606
1607 root = root->fs_info->chunk_root;
1608
1609 path = btrfs_alloc_path();
1610 if (!path)
1611 return -ENOMEM;
1612
Chris Mason0b86a832008-03-24 15:01:56 -04001613 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1614 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001615 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001616
1617 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001618 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001619 if (ret)
1620 goto out;
1621
1622 leaf = path->nodes[0];
1623 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1624
1625 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001626 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001627 btrfs_set_device_type(leaf, dev_item, device->type);
1628 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1629 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1630 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001631 btrfs_set_device_total_bytes(leaf, dev_item,
1632 btrfs_device_get_disk_total_bytes(device));
1633 btrfs_set_device_bytes_used(leaf, dev_item,
1634 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001635 btrfs_set_device_group(leaf, dev_item, 0);
1636 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1637 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001638 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001639
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001640 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001641 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001642 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001643 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001644 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001645
Yan Zheng2b820322008-11-17 21:11:30 -05001646 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001647out:
1648 btrfs_free_path(path);
1649 return ret;
1650}
Chris Mason8f18cf12008-04-25 16:53:30 -04001651
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001652/*
1653 * Function to update ctime/mtime for a given device path.
1654 * Mainly used for ctime/mtime based probe like libblkid.
1655 */
1656static void update_dev_time(char *path_name)
1657{
1658 struct file *filp;
1659
1660 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001661 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001662 return;
1663 file_update_time(filp);
1664 filp_close(filp, NULL);
1665 return;
1666}
1667
Chris Masona061fc82008-05-07 11:43:44 -04001668static int btrfs_rm_dev_item(struct btrfs_root *root,
1669 struct btrfs_device *device)
1670{
1671 int ret;
1672 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001673 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001674 struct btrfs_trans_handle *trans;
1675
1676 root = root->fs_info->chunk_root;
1677
1678 path = btrfs_alloc_path();
1679 if (!path)
1680 return -ENOMEM;
1681
Yan, Zhenga22285a2010-05-16 10:48:46 -04001682 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001683 if (IS_ERR(trans)) {
1684 btrfs_free_path(path);
1685 return PTR_ERR(trans);
1686 }
Chris Masona061fc82008-05-07 11:43:44 -04001687 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1688 key.type = BTRFS_DEV_ITEM_KEY;
1689 key.offset = device->devid;
1690
1691 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1692 if (ret < 0)
1693 goto out;
1694
1695 if (ret > 0) {
1696 ret = -ENOENT;
1697 goto out;
1698 }
1699
1700 ret = btrfs_del_item(trans, root, path);
1701 if (ret)
1702 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001703out:
1704 btrfs_free_path(path);
1705 btrfs_commit_transaction(trans, root);
1706 return ret;
1707}
1708
1709int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1710{
1711 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001712 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001713 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001714 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001715 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001716 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001717 u64 all_avail;
1718 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001719 u64 num_devices;
1720 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001721 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001722 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001723 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001724
Chris Masona061fc82008-05-07 11:43:44 -04001725 mutex_lock(&uuid_mutex);
1726
Miao Xiede98ced2013-01-29 10:13:12 +00001727 do {
1728 seq = read_seqbegin(&root->fs_info->profiles_lock);
1729
1730 all_avail = root->fs_info->avail_data_alloc_bits |
1731 root->fs_info->avail_system_alloc_bits |
1732 root->fs_info->avail_metadata_alloc_bits;
1733 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001734
Stefan Behrens8dabb742012-11-06 13:15:27 +01001735 num_devices = root->fs_info->fs_devices->num_devices;
1736 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1737 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1738 WARN_ON(num_devices < 1);
1739 num_devices--;
1740 }
1741 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1742
1743 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001744 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001745 goto out;
1746 }
1747
Stefan Behrens8dabb742012-11-06 13:15:27 +01001748 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001749 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001750 goto out;
1751 }
1752
David Woodhouse53b381b2013-01-29 18:40:14 -05001753 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1754 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001755 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001756 goto out;
1757 }
1758 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1759 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001760 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001761 goto out;
1762 }
1763
Chris Masondfe25022008-05-13 13:46:40 -04001764 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001765 struct list_head *devices;
1766 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001767
Chris Masondfe25022008-05-13 13:46:40 -04001768 device = NULL;
1769 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001770 /*
1771 * It is safe to read the devices since the volume_mutex
1772 * is held.
1773 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001774 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001775 if (tmp->in_fs_metadata &&
1776 !tmp->is_tgtdev_for_dev_replace &&
1777 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001778 device = tmp;
1779 break;
1780 }
1781 }
1782 bdev = NULL;
1783 bh = NULL;
1784 disk_super = NULL;
1785 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001786 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001787 goto out;
1788 }
Chris Masondfe25022008-05-13 13:46:40 -04001789 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001790 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001791 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001792 root->fs_info->bdev_holder, 0,
1793 &bdev, &bh);
1794 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001795 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001796 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001797 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001798 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001799 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001800 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001801 if (!device) {
1802 ret = -ENOENT;
1803 goto error_brelse;
1804 }
Chris Masondfe25022008-05-13 13:46:40 -04001805 }
Yan Zheng2b820322008-11-17 21:11:30 -05001806
Stefan Behrens63a212a2012-11-05 18:29:28 +01001807 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001808 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001809 goto error_brelse;
1810 }
1811
Yan Zheng2b820322008-11-17 21:11:30 -05001812 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001813 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001814 goto error_brelse;
1815 }
1816
1817 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001818 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001819 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001820 device->fs_devices->rw_devices--;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001821 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001822 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001823 }
Chris Masona061fc82008-05-07 11:43:44 -04001824
Carey Underwoodd7901552013-03-04 16:37:06 -06001825 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001826 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001827 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001828 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001829 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001830
Stefan Behrens63a212a2012-11-05 18:29:28 +01001831 /*
1832 * TODO: the superblock still includes this device in its num_devices
1833 * counter although write_all_supers() is not locked out. This
1834 * could give a filesystem state which requires a degraded mount.
1835 */
Chris Masona061fc82008-05-07 11:43:44 -04001836 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1837 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001838 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001839
Yan Zheng2b820322008-11-17 21:11:30 -05001840 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001841 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001842
1843 /*
1844 * the device list mutex makes sure that we don't change
1845 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001846 * the device supers. Whoever is writing all supers, should
1847 * lock the device list mutex before getting the number of
1848 * devices in the super block (super_copy). Conversely,
1849 * whoever updates the number of devices in the super block
1850 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001851 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001852
1853 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001854 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001855 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001856
Yan Zhenge4404d62008-12-12 10:03:26 -05001857 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001858 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001859
Chris Masoncd02dca2010-12-13 14:56:23 -05001860 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001861 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001862
Yan Zheng2b820322008-11-17 21:11:30 -05001863 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1864 struct btrfs_device, dev_list);
1865 if (device->bdev == root->fs_info->sb->s_bdev)
1866 root->fs_info->sb->s_bdev = next_device->bdev;
1867 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1868 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1869
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001870 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001871 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001872 /* remove sysfs entry */
Anand Jain6c14a162015-03-10 06:38:34 +08001873 btrfs_kobj_rm_device(root->fs_info->fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001874 }
Anand Jain99994cd2014-06-03 11:36:00 +08001875
Xiao Guangrong1f781602011-04-20 10:09:16 +00001876 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001877
David Sterba6c417612011-04-13 15:41:04 +02001878 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1879 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001880 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001881
Xiao Guangrong1f781602011-04-20 10:09:16 +00001882 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001883 struct btrfs_fs_devices *fs_devices;
1884 fs_devices = root->fs_info->fs_devices;
1885 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001886 if (fs_devices->seed == cur_devices) {
1887 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001888 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001889 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001890 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001891 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001892 cur_devices->seed = NULL;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001893 __btrfs_close_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001894 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001895 }
1896
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001897 root->fs_info->num_tolerated_disk_barrier_failures =
1898 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1899
Yan Zheng2b820322008-11-17 21:11:30 -05001900 /*
1901 * at this point, the device is zero sized. We want to
1902 * remove it from the devices list and zero out the old super
1903 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001904 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001905 u64 bytenr;
1906 int i;
1907
Chris Masondfe25022008-05-13 13:46:40 -04001908 /* make sure this device isn't detected as part of
1909 * the FS anymore
1910 */
1911 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1912 set_buffer_dirty(bh);
1913 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001914
1915 /* clear the mirror copies of super block on the disk
1916 * being removed, 0th copy is been taken care above and
1917 * the below would take of the rest
1918 */
1919 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1920 bytenr = btrfs_sb_offset(i);
1921 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1922 i_size_read(bdev->bd_inode))
1923 break;
1924
1925 brelse(bh);
1926 bh = __bread(bdev, bytenr / 4096,
1927 BTRFS_SUPER_INFO_SIZE);
1928 if (!bh)
1929 continue;
1930
1931 disk_super = (struct btrfs_super_block *)bh->b_data;
1932
1933 if (btrfs_super_bytenr(disk_super) != bytenr ||
1934 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1935 continue;
1936 }
1937 memset(&disk_super->magic, 0,
1938 sizeof(disk_super->magic));
1939 set_buffer_dirty(bh);
1940 sync_dirty_buffer(bh);
1941 }
Chris Masondfe25022008-05-13 13:46:40 -04001942 }
Chris Masona061fc82008-05-07 11:43:44 -04001943
Chris Masona061fc82008-05-07 11:43:44 -04001944 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001945
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001946 if (bdev) {
1947 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001948 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001949
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001950 /* Update ctime/mtime for device path for libblkid */
1951 update_dev_time(device_path);
1952 }
1953
Chris Masona061fc82008-05-07 11:43:44 -04001954error_brelse:
1955 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001956 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001957 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001958out:
1959 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001960 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001961error_undo:
1962 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001963 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001964 list_add(&device->dev_alloc_list,
1965 &root->fs_info->fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001966 device->fs_devices->rw_devices++;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001967 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001968 }
1969 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001970}
1971
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001972void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
1973 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01001974{
Anand Jaind51908c2014-08-13 14:24:19 +08001975 struct btrfs_fs_devices *fs_devices;
1976
Stefan Behrense93c89c2012-11-05 17:33:06 +01001977 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001978
Anand Jain25e8e912014-08-20 10:56:56 +08001979 /*
1980 * in case of fs with no seed, srcdev->fs_devices will point
1981 * to fs_devices of fs_info. However when the dev being replaced is
1982 * a seed dev it will point to the seed's local fs_devices. In short
1983 * srcdev will have its correct fs_devices in both the cases.
1984 */
1985 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08001986
Stefan Behrense93c89c2012-11-05 17:33:06 +01001987 list_del_rcu(&srcdev->dev_list);
1988 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08001989 fs_devices->num_devices--;
Miao Xie82372bc2014-09-03 21:35:44 +08001990 if (srcdev->missing)
Anand Jaind51908c2014-08-13 14:24:19 +08001991 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001992
Miao Xie82372bc2014-09-03 21:35:44 +08001993 if (srcdev->writeable) {
1994 fs_devices->rw_devices--;
1995 /* zero out the old super if it is writable */
1996 btrfs_scratch_superblock(srcdev);
Ilya Dryomov13572722013-10-02 20:41:01 +03001997 }
1998
Miao Xie82372bc2014-09-03 21:35:44 +08001999 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002000 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002001}
2002
2003void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2004 struct btrfs_device *srcdev)
2005{
2006 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002007
Stefan Behrense93c89c2012-11-05 17:33:06 +01002008 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002009
2010 /*
2011 * unless fs_devices is seed fs, num_devices shouldn't go
2012 * zero
2013 */
2014 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
2015
2016 /* if this is no devs we rather delete the fs_devices */
2017 if (!fs_devices->num_devices) {
2018 struct btrfs_fs_devices *tmp_fs_devices;
2019
2020 tmp_fs_devices = fs_info->fs_devices;
2021 while (tmp_fs_devices) {
2022 if (tmp_fs_devices->seed == fs_devices) {
2023 tmp_fs_devices->seed = fs_devices->seed;
2024 break;
2025 }
2026 tmp_fs_devices = tmp_fs_devices->seed;
2027 }
2028 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08002029 __btrfs_close_devices(fs_devices);
2030 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002031 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002032}
2033
2034void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2035 struct btrfs_device *tgtdev)
2036{
2037 struct btrfs_device *next_device;
2038
Miao Xie67a2c452014-09-03 21:35:43 +08002039 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002040 WARN_ON(!tgtdev);
2041 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002042
2043 btrfs_kobj_rm_device(fs_info->fs_devices, tgtdev);
2044
Stefan Behrense93c89c2012-11-05 17:33:06 +01002045 if (tgtdev->bdev) {
2046 btrfs_scratch_superblock(tgtdev);
2047 fs_info->fs_devices->open_devices--;
2048 }
2049 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002050
2051 next_device = list_entry(fs_info->fs_devices->devices.next,
2052 struct btrfs_device, dev_list);
2053 if (tgtdev->bdev == fs_info->sb->s_bdev)
2054 fs_info->sb->s_bdev = next_device->bdev;
2055 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
2056 fs_info->fs_devices->latest_bdev = next_device->bdev;
2057 list_del_rcu(&tgtdev->dev_list);
2058
2059 call_rcu(&tgtdev->rcu, free_device);
2060
2061 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08002062 mutex_unlock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002063}
2064
Eric Sandeen48a3b632013-04-25 20:41:01 +00002065static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
2066 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002067{
2068 int ret = 0;
2069 struct btrfs_super_block *disk_super;
2070 u64 devid;
2071 u8 *dev_uuid;
2072 struct block_device *bdev;
2073 struct buffer_head *bh;
2074
2075 *device = NULL;
2076 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
2077 root->fs_info->bdev_holder, 0, &bdev, &bh);
2078 if (ret)
2079 return ret;
2080 disk_super = (struct btrfs_super_block *)bh->b_data;
2081 devid = btrfs_stack_device_id(&disk_super->dev_item);
2082 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002083 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002084 disk_super->fsid);
2085 brelse(bh);
2086 if (!*device)
2087 ret = -ENOENT;
2088 blkdev_put(bdev, FMODE_READ);
2089 return ret;
2090}
2091
2092int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
2093 char *device_path,
2094 struct btrfs_device **device)
2095{
2096 *device = NULL;
2097 if (strcmp(device_path, "missing") == 0) {
2098 struct list_head *devices;
2099 struct btrfs_device *tmp;
2100
2101 devices = &root->fs_info->fs_devices->devices;
2102 /*
2103 * It is safe to read the devices since the volume_mutex
2104 * is held by the caller.
2105 */
2106 list_for_each_entry(tmp, devices, dev_list) {
2107 if (tmp->in_fs_metadata && !tmp->bdev) {
2108 *device = tmp;
2109 break;
2110 }
2111 }
2112
2113 if (!*device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002114 btrfs_err(root->fs_info, "no missing device found");
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002115 return -ENOENT;
2116 }
2117
2118 return 0;
2119 } else {
2120 return btrfs_find_device_by_path(root, device_path, device);
2121 }
2122}
2123
Yan Zheng2b820322008-11-17 21:11:30 -05002124/*
2125 * does all the dirty work required for changing file system's UUID.
2126 */
Li Zefan125ccb02011-12-08 15:07:24 +08002127static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05002128{
2129 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2130 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002131 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02002132 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002133 struct btrfs_device *device;
2134 u64 super_flags;
2135
2136 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05002137 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002138 return -EINVAL;
2139
Ilya Dryomov2208a372013-08-12 14:33:03 +03002140 seed_devices = __alloc_fs_devices();
2141 if (IS_ERR(seed_devices))
2142 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002143
Yan Zhenge4404d62008-12-12 10:03:26 -05002144 old_devices = clone_fs_devices(fs_devices);
2145 if (IS_ERR(old_devices)) {
2146 kfree(seed_devices);
2147 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002148 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002149
Yan Zheng2b820322008-11-17 21:11:30 -05002150 list_add(&old_devices->list, &fs_uuids);
2151
Yan Zhenge4404d62008-12-12 10:03:26 -05002152 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2153 seed_devices->opened = 1;
2154 INIT_LIST_HEAD(&seed_devices->devices);
2155 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002156 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002157
2158 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002159 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2160 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002161 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002162 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002163
2164 lock_chunks(root);
2165 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2166 unlock_chunks(root);
Yan Zhenge4404d62008-12-12 10:03:26 -05002167
Yan Zheng2b820322008-11-17 21:11:30 -05002168 fs_devices->seeding = 0;
2169 fs_devices->num_devices = 0;
2170 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002171 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002172 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002173 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002174
2175 generate_random_uuid(fs_devices->fsid);
2176 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2177 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002178 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2179
Yan Zheng2b820322008-11-17 21:11:30 -05002180 super_flags = btrfs_super_flags(disk_super) &
2181 ~BTRFS_SUPER_FLAG_SEEDING;
2182 btrfs_set_super_flags(disk_super, super_flags);
2183
2184 return 0;
2185}
2186
2187/*
2188 * strore the expected generation for seed devices in device items.
2189 */
2190static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2191 struct btrfs_root *root)
2192{
2193 struct btrfs_path *path;
2194 struct extent_buffer *leaf;
2195 struct btrfs_dev_item *dev_item;
2196 struct btrfs_device *device;
2197 struct btrfs_key key;
2198 u8 fs_uuid[BTRFS_UUID_SIZE];
2199 u8 dev_uuid[BTRFS_UUID_SIZE];
2200 u64 devid;
2201 int ret;
2202
2203 path = btrfs_alloc_path();
2204 if (!path)
2205 return -ENOMEM;
2206
2207 root = root->fs_info->chunk_root;
2208 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2209 key.offset = 0;
2210 key.type = BTRFS_DEV_ITEM_KEY;
2211
2212 while (1) {
2213 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2214 if (ret < 0)
2215 goto error;
2216
2217 leaf = path->nodes[0];
2218next_slot:
2219 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2220 ret = btrfs_next_leaf(root, path);
2221 if (ret > 0)
2222 break;
2223 if (ret < 0)
2224 goto error;
2225 leaf = path->nodes[0];
2226 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002227 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002228 continue;
2229 }
2230
2231 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2232 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2233 key.type != BTRFS_DEV_ITEM_KEY)
2234 break;
2235
2236 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2237 struct btrfs_dev_item);
2238 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002239 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002240 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002241 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002242 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002243 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2244 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002245 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002246
2247 if (device->fs_devices->seeding) {
2248 btrfs_set_device_generation(leaf, dev_item,
2249 device->generation);
2250 btrfs_mark_buffer_dirty(leaf);
2251 }
2252
2253 path->slots[0]++;
2254 goto next_slot;
2255 }
2256 ret = 0;
2257error:
2258 btrfs_free_path(path);
2259 return ret;
2260}
2261
Chris Mason788f20e2008-04-28 15:29:42 -04002262int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2263{
Josef Bacikd5e20032011-08-04 14:52:27 +00002264 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002265 struct btrfs_trans_handle *trans;
2266 struct btrfs_device *device;
2267 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002268 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002269 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002270 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002271 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002272 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002273 int ret = 0;
2274
Yan Zheng2b820322008-11-17 21:11:30 -05002275 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002276 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002277
Li Zefana5d163332011-12-07 20:08:40 -05002278 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002279 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002280 if (IS_ERR(bdev))
2281 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002282
Yan Zheng2b820322008-11-17 21:11:30 -05002283 if (root->fs_info->fs_devices->seeding) {
2284 seeding_dev = 1;
2285 down_write(&sb->s_umount);
2286 mutex_lock(&uuid_mutex);
2287 }
2288
Chris Mason8c8bee12008-09-29 11:19:10 -04002289 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002290
Chris Mason788f20e2008-04-28 15:29:42 -04002291 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002292
2293 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002294 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002295 if (device->bdev == bdev) {
2296 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002297 mutex_unlock(
2298 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002299 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002300 }
2301 }
Liu Bod25628b2012-11-14 14:35:30 +00002302 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002303
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002304 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2305 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002306 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002307 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002308 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002309 }
2310
Josef Bacik606686e2012-06-04 14:03:51 -04002311 name = rcu_string_strdup(device_path, GFP_NOFS);
2312 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002313 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002314 ret = -ENOMEM;
2315 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002316 }
Josef Bacik606686e2012-06-04 14:03:51 -04002317 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002318
Yan, Zhenga22285a2010-05-16 10:48:46 -04002319 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002320 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002321 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002322 kfree(device);
2323 ret = PTR_ERR(trans);
2324 goto error;
2325 }
2326
Josef Bacikd5e20032011-08-04 14:52:27 +00002327 q = bdev_get_queue(bdev);
2328 if (blk_queue_discard(q))
2329 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002330 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002331 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002332 device->io_width = root->sectorsize;
2333 device->io_align = root->sectorsize;
2334 device->sector_size = root->sectorsize;
2335 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002336 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002337 device->commit_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002338 device->dev_root = root->fs_info->dev_root;
2339 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002340 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002341 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002342 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002343 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002344 set_blocksize(device->bdev, 4096);
2345
Yan Zheng2b820322008-11-17 21:11:30 -05002346 if (seeding_dev) {
2347 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002348 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002349 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002350 }
2351
2352 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002353
Chris Masone5e9a522009-06-10 15:17:02 -04002354 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002355 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002356 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002357 list_add(&device->dev_alloc_list,
2358 &root->fs_info->fs_devices->alloc_list);
2359 root->fs_info->fs_devices->num_devices++;
2360 root->fs_info->fs_devices->open_devices++;
2361 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002362 root->fs_info->fs_devices->total_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -05002363 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2364
Josef Bacik2bf64752011-09-26 17:12:22 -04002365 spin_lock(&root->fs_info->free_chunk_lock);
2366 root->fs_info->free_chunk_space += device->total_bytes;
2367 spin_unlock(&root->fs_info->free_chunk_lock);
2368
Chris Masonc2898112009-06-10 09:51:32 -04002369 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2370 root->fs_info->fs_devices->rotating = 1;
2371
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002372 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002373 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002374 tmp + device->total_bytes);
Chris Mason788f20e2008-04-28 15:29:42 -04002375
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002376 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002377 btrfs_set_super_num_devices(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002378 tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002379
2380 /* add sysfs device entry */
Anand Jain1ba43812015-03-10 06:38:33 +08002381 btrfs_kobj_add_device(root->fs_info->fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002382
Miao Xie2196d6e2014-09-03 21:35:41 +08002383 /*
2384 * we've got more storage, clear any full flags on the space
2385 * infos
2386 */
2387 btrfs_clear_space_info_full(root->fs_info);
2388
2389 unlock_chunks(root);
Chris Masone5e9a522009-06-10 15:17:02 -04002390 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002391
Yan Zheng2b820322008-11-17 21:11:30 -05002392 if (seeding_dev) {
Miao Xie2196d6e2014-09-03 21:35:41 +08002393 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05002394 ret = init_first_rw_device(trans, root, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002395 unlock_chunks(root);
David Sterba005d6422012-09-18 07:52:32 -06002396 if (ret) {
2397 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002398 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002399 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002400 }
2401
2402 ret = btrfs_add_device(trans, root, device);
2403 if (ret) {
2404 btrfs_abort_transaction(trans, root, ret);
2405 goto error_trans;
2406 }
2407
2408 if (seeding_dev) {
2409 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2410
Yan Zheng2b820322008-11-17 21:11:30 -05002411 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002412 if (ret) {
2413 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002414 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002415 }
Anand Jainb2373f22014-06-03 11:36:03 +08002416
2417 /* Sprouting would change fsid of the mounted root,
2418 * so rename the fsid on the sysfs
2419 */
2420 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2421 root->fs_info->fsid);
Anand Jain2e7910d2015-03-10 06:38:29 +08002422 if (kobject_rename(&root->fs_info->fs_devices->super_kobj,
2423 fsid_buf))
Anand Jain2421a8c2015-03-10 06:38:40 +08002424 pr_warn("BTRFS: sysfs: failed to create fsid for sprout\n");
Yan Zheng2b820322008-11-17 21:11:30 -05002425 }
2426
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002427 root->fs_info->num_tolerated_disk_barrier_failures =
2428 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002429 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002430
2431 if (seeding_dev) {
2432 mutex_unlock(&uuid_mutex);
2433 up_write(&sb->s_umount);
2434
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002435 if (ret) /* transaction commit */
2436 return ret;
2437
Yan Zheng2b820322008-11-17 21:11:30 -05002438 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002439 if (ret < 0)
2440 btrfs_error(root->fs_info, ret,
2441 "Failed to relocate sys chunks after "
2442 "device initialization. This can be fixed "
2443 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002444 trans = btrfs_attach_transaction(root);
2445 if (IS_ERR(trans)) {
2446 if (PTR_ERR(trans) == -ENOENT)
2447 return 0;
2448 return PTR_ERR(trans);
2449 }
2450 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002451 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002452
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002453 /* Update ctime/mtime for libblkid */
2454 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002455 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002456
2457error_trans:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002458 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002459 rcu_string_free(device->name);
Anand Jain6c14a162015-03-10 06:38:34 +08002460 btrfs_kobj_rm_device(root->fs_info->fs_devices, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002461 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002462error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002463 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002464 if (seeding_dev) {
2465 mutex_unlock(&uuid_mutex);
2466 up_write(&sb->s_umount);
2467 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002468 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002469}
2470
Stefan Behrense93c89c2012-11-05 17:33:06 +01002471int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002472 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002473 struct btrfs_device **device_out)
2474{
2475 struct request_queue *q;
2476 struct btrfs_device *device;
2477 struct block_device *bdev;
2478 struct btrfs_fs_info *fs_info = root->fs_info;
2479 struct list_head *devices;
2480 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002481 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002482 int ret = 0;
2483
2484 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002485 if (fs_info->fs_devices->seeding) {
2486 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002487 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002488 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002489
2490 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2491 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002492 if (IS_ERR(bdev)) {
2493 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002494 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002495 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002496
2497 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2498
2499 devices = &fs_info->fs_devices->devices;
2500 list_for_each_entry(device, devices, dev_list) {
2501 if (device->bdev == bdev) {
Miao Xie1c433662014-09-03 21:35:32 +08002502 btrfs_err(fs_info, "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002503 ret = -EEXIST;
2504 goto error;
2505 }
2506 }
2507
Miao Xie1c433662014-09-03 21:35:32 +08002508
Miao Xie7cc8e582014-09-03 21:35:38 +08002509 if (i_size_read(bdev->bd_inode) <
2510 btrfs_device_get_total_bytes(srcdev)) {
Miao Xie1c433662014-09-03 21:35:32 +08002511 btrfs_err(fs_info, "target device is smaller than source device!");
2512 ret = -EINVAL;
2513 goto error;
2514 }
2515
2516
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002517 device = btrfs_alloc_device(NULL, &devid, NULL);
2518 if (IS_ERR(device)) {
2519 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002520 goto error;
2521 }
2522
2523 name = rcu_string_strdup(device_path, GFP_NOFS);
2524 if (!name) {
2525 kfree(device);
2526 ret = -ENOMEM;
2527 goto error;
2528 }
2529 rcu_assign_pointer(device->name, name);
2530
2531 q = bdev_get_queue(bdev);
2532 if (blk_queue_discard(q))
2533 device->can_discard = 1;
2534 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2535 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002536 device->generation = 0;
2537 device->io_width = root->sectorsize;
2538 device->io_align = root->sectorsize;
2539 device->sector_size = root->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002540 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2541 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2542 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002543 ASSERT(list_empty(&srcdev->resized_list));
2544 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002545 device->commit_bytes_used = device->bytes_used;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002546 device->dev_root = fs_info->dev_root;
2547 device->bdev = bdev;
2548 device->in_fs_metadata = 1;
2549 device->is_tgtdev_for_dev_replace = 1;
2550 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002551 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002552 set_blocksize(device->bdev, 4096);
2553 device->fs_devices = fs_info->fs_devices;
2554 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2555 fs_info->fs_devices->num_devices++;
2556 fs_info->fs_devices->open_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002557 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2558
2559 *device_out = device;
2560 return ret;
2561
2562error:
2563 blkdev_put(bdev, FMODE_EXCL);
2564 return ret;
2565}
2566
2567void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2568 struct btrfs_device *tgtdev)
2569{
2570 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2571 tgtdev->io_width = fs_info->dev_root->sectorsize;
2572 tgtdev->io_align = fs_info->dev_root->sectorsize;
2573 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2574 tgtdev->dev_root = fs_info->dev_root;
2575 tgtdev->in_fs_metadata = 1;
2576}
2577
Chris Masond3977122009-01-05 21:25:51 -05002578static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2579 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002580{
2581 int ret;
2582 struct btrfs_path *path;
2583 struct btrfs_root *root;
2584 struct btrfs_dev_item *dev_item;
2585 struct extent_buffer *leaf;
2586 struct btrfs_key key;
2587
2588 root = device->dev_root->fs_info->chunk_root;
2589
2590 path = btrfs_alloc_path();
2591 if (!path)
2592 return -ENOMEM;
2593
2594 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2595 key.type = BTRFS_DEV_ITEM_KEY;
2596 key.offset = device->devid;
2597
2598 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2599 if (ret < 0)
2600 goto out;
2601
2602 if (ret > 0) {
2603 ret = -ENOENT;
2604 goto out;
2605 }
2606
2607 leaf = path->nodes[0];
2608 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2609
2610 btrfs_set_device_id(leaf, dev_item, device->devid);
2611 btrfs_set_device_type(leaf, dev_item, device->type);
2612 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2613 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2614 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002615 btrfs_set_device_total_bytes(leaf, dev_item,
2616 btrfs_device_get_disk_total_bytes(device));
2617 btrfs_set_device_bytes_used(leaf, dev_item,
2618 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002619 btrfs_mark_buffer_dirty(leaf);
2620
2621out:
2622 btrfs_free_path(path);
2623 return ret;
2624}
2625
Miao Xie2196d6e2014-09-03 21:35:41 +08002626int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002627 struct btrfs_device *device, u64 new_size)
2628{
2629 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002630 device->dev_root->fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002631 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002632 u64 old_total;
2633 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002634
Yan Zheng2b820322008-11-17 21:11:30 -05002635 if (!device->writeable)
2636 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002637
2638 lock_chunks(device->dev_root);
2639 old_total = btrfs_super_total_bytes(super_copy);
2640 diff = new_size - device->total_bytes;
2641
Stefan Behrens63a212a2012-11-05 18:29:28 +01002642 if (new_size <= device->total_bytes ||
Miao Xie2196d6e2014-09-03 21:35:41 +08002643 device->is_tgtdev_for_dev_replace) {
2644 unlock_chunks(device->dev_root);
Yan Zheng2b820322008-11-17 21:11:30 -05002645 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002646 }
Yan Zheng2b820322008-11-17 21:11:30 -05002647
Miao Xie935e5cc2014-09-03 21:35:33 +08002648 fs_devices = device->dev_root->fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002649
2650 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002651 device->fs_devices->total_rw_bytes += diff;
2652
Miao Xie7cc8e582014-09-03 21:35:38 +08002653 btrfs_device_set_total_bytes(device, new_size);
2654 btrfs_device_set_disk_total_bytes(device, new_size);
Chris Mason4184ea72009-03-10 12:39:20 -04002655 btrfs_clear_space_info_full(device->dev_root->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002656 if (list_empty(&device->resized_list))
2657 list_add_tail(&device->resized_list,
2658 &fs_devices->resized_devices);
Miao Xie2196d6e2014-09-03 21:35:41 +08002659 unlock_chunks(device->dev_root);
Chris Mason4184ea72009-03-10 12:39:20 -04002660
Chris Mason8f18cf12008-04-25 16:53:30 -04002661 return btrfs_update_device(trans, device);
2662}
2663
2664static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Zhao Leia688a042015-02-09 20:31:44 +08002665 struct btrfs_root *root, u64 chunk_objectid,
Chris Mason8f18cf12008-04-25 16:53:30 -04002666 u64 chunk_offset)
2667{
2668 int ret;
2669 struct btrfs_path *path;
2670 struct btrfs_key key;
2671
2672 root = root->fs_info->chunk_root;
2673 path = btrfs_alloc_path();
2674 if (!path)
2675 return -ENOMEM;
2676
2677 key.objectid = chunk_objectid;
2678 key.offset = chunk_offset;
2679 key.type = BTRFS_CHUNK_ITEM_KEY;
2680
2681 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002682 if (ret < 0)
2683 goto out;
2684 else if (ret > 0) { /* Logic error or corruption */
2685 btrfs_error(root->fs_info, -ENOENT,
2686 "Failed lookup while freeing chunk.");
2687 ret = -ENOENT;
2688 goto out;
2689 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002690
2691 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002692 if (ret < 0)
2693 btrfs_error(root->fs_info, ret,
2694 "Failed to delete chunk item.");
2695out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002696 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002697 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002698}
2699
Christoph Hellwigb2950862008-12-02 09:54:17 -05002700static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002701 chunk_offset)
2702{
David Sterba6c417612011-04-13 15:41:04 +02002703 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002704 struct btrfs_disk_key *disk_key;
2705 struct btrfs_chunk *chunk;
2706 u8 *ptr;
2707 int ret = 0;
2708 u32 num_stripes;
2709 u32 array_size;
2710 u32 len = 0;
2711 u32 cur;
2712 struct btrfs_key key;
2713
Miao Xie2196d6e2014-09-03 21:35:41 +08002714 lock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002715 array_size = btrfs_super_sys_array_size(super_copy);
2716
2717 ptr = super_copy->sys_chunk_array;
2718 cur = 0;
2719
2720 while (cur < array_size) {
2721 disk_key = (struct btrfs_disk_key *)ptr;
2722 btrfs_disk_key_to_cpu(&key, disk_key);
2723
2724 len = sizeof(*disk_key);
2725
2726 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2727 chunk = (struct btrfs_chunk *)(ptr + len);
2728 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2729 len += btrfs_chunk_item_size(num_stripes);
2730 } else {
2731 ret = -EIO;
2732 break;
2733 }
2734 if (key.objectid == chunk_objectid &&
2735 key.offset == chunk_offset) {
2736 memmove(ptr, ptr + len, array_size - (cur + len));
2737 array_size -= len;
2738 btrfs_set_super_sys_array_size(super_copy, array_size);
2739 } else {
2740 ptr += len;
2741 cur += len;
2742 }
2743 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002744 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002745 return ret;
2746}
2747
Josef Bacik47ab2a62014-09-18 11:20:02 -04002748int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2749 struct btrfs_root *root, u64 chunk_offset)
2750{
2751 struct extent_map_tree *em_tree;
2752 struct extent_map *em;
2753 struct btrfs_root *extent_root = root->fs_info->extent_root;
2754 struct map_lookup *map;
2755 u64 dev_extent_len = 0;
2756 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002757 int i, ret = 0;
2758
2759 /* Just in case */
2760 root = root->fs_info->chunk_root;
2761 em_tree = &root->fs_info->mapping_tree.map_tree;
2762
2763 read_lock(&em_tree->lock);
2764 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2765 read_unlock(&em_tree->lock);
2766
2767 if (!em || em->start > chunk_offset ||
2768 em->start + em->len < chunk_offset) {
2769 /*
2770 * This is a logic error, but we don't want to just rely on the
2771 * user having built with ASSERT enabled, so if ASSERT doens't
2772 * do anything we still error out.
2773 */
2774 ASSERT(0);
2775 if (em)
2776 free_extent_map(em);
2777 return -EINVAL;
2778 }
2779 map = (struct map_lookup *)em->bdev;
Filipe Manana39c2d7f2015-05-20 14:01:55 +01002780 lock_chunks(root->fs_info->chunk_root);
Filipe Manana4617ea32015-06-09 17:48:21 +01002781 check_system_chunk(trans, extent_root, map->type);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01002782 unlock_chunks(root->fs_info->chunk_root);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002783
2784 for (i = 0; i < map->num_stripes; i++) {
2785 struct btrfs_device *device = map->stripes[i].dev;
2786 ret = btrfs_free_dev_extent(trans, device,
2787 map->stripes[i].physical,
2788 &dev_extent_len);
2789 if (ret) {
2790 btrfs_abort_transaction(trans, root, ret);
2791 goto out;
2792 }
2793
2794 if (device->bytes_used > 0) {
2795 lock_chunks(root);
2796 btrfs_device_set_bytes_used(device,
2797 device->bytes_used - dev_extent_len);
2798 spin_lock(&root->fs_info->free_chunk_lock);
2799 root->fs_info->free_chunk_space += dev_extent_len;
2800 spin_unlock(&root->fs_info->free_chunk_lock);
2801 btrfs_clear_space_info_full(root->fs_info);
2802 unlock_chunks(root);
2803 }
2804
2805 if (map->stripes[i].dev) {
2806 ret = btrfs_update_device(trans, map->stripes[i].dev);
2807 if (ret) {
2808 btrfs_abort_transaction(trans, root, ret);
2809 goto out;
2810 }
2811 }
2812 }
Zhao Leia688a042015-02-09 20:31:44 +08002813 ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002814 if (ret) {
2815 btrfs_abort_transaction(trans, root, ret);
2816 goto out;
2817 }
2818
2819 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2820
2821 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2822 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2823 if (ret) {
2824 btrfs_abort_transaction(trans, root, ret);
2825 goto out;
2826 }
2827 }
2828
Filipe Manana04216822014-11-27 21:14:15 +00002829 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002830 if (ret) {
2831 btrfs_abort_transaction(trans, extent_root, ret);
2832 goto out;
2833 }
2834
Josef Bacik47ab2a62014-09-18 11:20:02 -04002835out:
2836 /* once for us */
2837 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002838 return ret;
2839}
2840
Zhaoleidc2ee4e2015-08-05 18:00:04 +08002841static int btrfs_relocate_chunk(struct btrfs_root *root, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002842{
Chris Mason8f18cf12008-04-25 16:53:30 -04002843 struct btrfs_root *extent_root;
2844 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002845 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002846
2847 root = root->fs_info->chunk_root;
2848 extent_root = root->fs_info->extent_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002849
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002850 /*
2851 * Prevent races with automatic removal of unused block groups.
2852 * After we relocate and before we remove the chunk with offset
2853 * chunk_offset, automatic removal of the block group can kick in,
2854 * resulting in a failure when calling btrfs_remove_chunk() below.
2855 *
2856 * Make sure to acquire this mutex before doing a tree search (dev
2857 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2858 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2859 * we release the path used to search the chunk/dev tree and before
2860 * the current task acquires this mutex and calls us.
2861 */
2862 ASSERT(mutex_is_locked(&root->fs_info->delete_unused_bgs_mutex));
2863
Josef Bacikba1bf482009-09-11 16:11:19 -04002864 ret = btrfs_can_relocate(extent_root, chunk_offset);
2865 if (ret)
2866 return -ENOSPC;
2867
Chris Mason8f18cf12008-04-25 16:53:30 -04002868 /* step one, relocate all the extents inside this chunk */
Zhaolei55e3a602015-08-05 16:43:30 +08002869 btrfs_scrub_pause(root);
Zheng Yan1a40e232008-09-26 10:09:34 -04002870 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Zhaolei55e3a602015-08-05 16:43:30 +08002871 btrfs_scrub_continue(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002872 if (ret)
2873 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002874
Yan, Zhenga22285a2010-05-16 10:48:46 -04002875 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002876 if (IS_ERR(trans)) {
2877 ret = PTR_ERR(trans);
2878 btrfs_std_error(root->fs_info, ret);
2879 return ret;
2880 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002881
2882 /*
2883 * step two, delete the device extents and the
2884 * chunk tree entries
2885 */
Josef Bacik47ab2a62014-09-18 11:20:02 -04002886 ret = btrfs_remove_chunk(trans, root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002887 btrfs_end_transaction(trans, root);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002888 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002889}
2890
Yan Zheng2b820322008-11-17 21:11:30 -05002891static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2892{
2893 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2894 struct btrfs_path *path;
2895 struct extent_buffer *leaf;
2896 struct btrfs_chunk *chunk;
2897 struct btrfs_key key;
2898 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05002899 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002900 bool retried = false;
2901 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002902 int ret;
2903
2904 path = btrfs_alloc_path();
2905 if (!path)
2906 return -ENOMEM;
2907
Josef Bacikba1bf482009-09-11 16:11:19 -04002908again:
Yan Zheng2b820322008-11-17 21:11:30 -05002909 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2910 key.offset = (u64)-1;
2911 key.type = BTRFS_CHUNK_ITEM_KEY;
2912
2913 while (1) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002914 mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002915 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002916 if (ret < 0) {
2917 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002918 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002919 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002920 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002921
2922 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2923 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002924 if (ret)
2925 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002926 if (ret < 0)
2927 goto error;
2928 if (ret > 0)
2929 break;
2930
2931 leaf = path->nodes[0];
2932 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2933
2934 chunk = btrfs_item_ptr(leaf, path->slots[0],
2935 struct btrfs_chunk);
2936 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002937 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002938
2939 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Zhao Leia688a042015-02-09 20:31:44 +08002940 ret = btrfs_relocate_chunk(chunk_root,
Yan Zheng2b820322008-11-17 21:11:30 -05002941 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002942 if (ret == -ENOSPC)
2943 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302944 else
2945 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002946 }
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002947 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002948
2949 if (found_key.offset == 0)
2950 break;
2951 key.offset = found_key.offset - 1;
2952 }
2953 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002954 if (failed && !retried) {
2955 failed = 0;
2956 retried = true;
2957 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302958 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002959 ret = -ENOSPC;
2960 }
Yan Zheng2b820322008-11-17 21:11:30 -05002961error:
2962 btrfs_free_path(path);
2963 return ret;
2964}
2965
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002966static int insert_balance_item(struct btrfs_root *root,
2967 struct btrfs_balance_control *bctl)
2968{
2969 struct btrfs_trans_handle *trans;
2970 struct btrfs_balance_item *item;
2971 struct btrfs_disk_balance_args disk_bargs;
2972 struct btrfs_path *path;
2973 struct extent_buffer *leaf;
2974 struct btrfs_key key;
2975 int ret, err;
2976
2977 path = btrfs_alloc_path();
2978 if (!path)
2979 return -ENOMEM;
2980
2981 trans = btrfs_start_transaction(root, 0);
2982 if (IS_ERR(trans)) {
2983 btrfs_free_path(path);
2984 return PTR_ERR(trans);
2985 }
2986
2987 key.objectid = BTRFS_BALANCE_OBJECTID;
2988 key.type = BTRFS_BALANCE_ITEM_KEY;
2989 key.offset = 0;
2990
2991 ret = btrfs_insert_empty_item(trans, root, path, &key,
2992 sizeof(*item));
2993 if (ret)
2994 goto out;
2995
2996 leaf = path->nodes[0];
2997 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2998
2999 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
3000
3001 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3002 btrfs_set_balance_data(leaf, item, &disk_bargs);
3003 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3004 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3005 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3006 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3007
3008 btrfs_set_balance_flags(leaf, item, bctl->flags);
3009
3010 btrfs_mark_buffer_dirty(leaf);
3011out:
3012 btrfs_free_path(path);
3013 err = btrfs_commit_transaction(trans, root);
3014 if (err && !ret)
3015 ret = err;
3016 return ret;
3017}
3018
3019static int del_balance_item(struct btrfs_root *root)
3020{
3021 struct btrfs_trans_handle *trans;
3022 struct btrfs_path *path;
3023 struct btrfs_key key;
3024 int ret, err;
3025
3026 path = btrfs_alloc_path();
3027 if (!path)
3028 return -ENOMEM;
3029
3030 trans = btrfs_start_transaction(root, 0);
3031 if (IS_ERR(trans)) {
3032 btrfs_free_path(path);
3033 return PTR_ERR(trans);
3034 }
3035
3036 key.objectid = BTRFS_BALANCE_OBJECTID;
3037 key.type = BTRFS_BALANCE_ITEM_KEY;
3038 key.offset = 0;
3039
3040 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3041 if (ret < 0)
3042 goto out;
3043 if (ret > 0) {
3044 ret = -ENOENT;
3045 goto out;
3046 }
3047
3048 ret = btrfs_del_item(trans, root, path);
3049out:
3050 btrfs_free_path(path);
3051 err = btrfs_commit_transaction(trans, root);
3052 if (err && !ret)
3053 ret = err;
3054 return ret;
3055}
3056
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003057/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003058 * This is a heuristic used to reduce the number of chunks balanced on
3059 * resume after balance was interrupted.
3060 */
3061static void update_balance_args(struct btrfs_balance_control *bctl)
3062{
3063 /*
3064 * Turn on soft mode for chunk types that were being converted.
3065 */
3066 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3067 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3068 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3069 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3070 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3071 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3072
3073 /*
3074 * Turn on usage filter if is not already used. The idea is
3075 * that chunks that we have already balanced should be
3076 * reasonably full. Don't do it for chunks that are being
3077 * converted - that will keep us from relocating unconverted
3078 * (albeit full) chunks.
3079 */
3080 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3081 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3082 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3083 bctl->data.usage = 90;
3084 }
3085 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3086 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3087 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3088 bctl->sys.usage = 90;
3089 }
3090 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3091 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3092 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3093 bctl->meta.usage = 90;
3094 }
3095}
3096
3097/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003098 * Should be called with both balance and volume mutexes held to
3099 * serialize other volume operations (add_dev/rm_dev/resize) with
3100 * restriper. Same goes for unset_balance_control.
3101 */
3102static void set_balance_control(struct btrfs_balance_control *bctl)
3103{
3104 struct btrfs_fs_info *fs_info = bctl->fs_info;
3105
3106 BUG_ON(fs_info->balance_ctl);
3107
3108 spin_lock(&fs_info->balance_lock);
3109 fs_info->balance_ctl = bctl;
3110 spin_unlock(&fs_info->balance_lock);
3111}
3112
3113static void unset_balance_control(struct btrfs_fs_info *fs_info)
3114{
3115 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3116
3117 BUG_ON(!fs_info->balance_ctl);
3118
3119 spin_lock(&fs_info->balance_lock);
3120 fs_info->balance_ctl = NULL;
3121 spin_unlock(&fs_info->balance_lock);
3122
3123 kfree(bctl);
3124}
3125
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003126/*
3127 * Balance filters. Return 1 if chunk should be filtered out
3128 * (should not be balanced).
3129 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003130static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003131 struct btrfs_balance_args *bargs)
3132{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003133 chunk_type = chunk_to_extended(chunk_type) &
3134 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003135
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003136 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003137 return 0;
3138
3139 return 1;
3140}
3141
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003142static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
3143 struct btrfs_balance_args *bargs)
3144{
3145 struct btrfs_block_group_cache *cache;
3146 u64 chunk_used, user_thresh;
3147 int ret = 1;
3148
3149 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3150 chunk_used = btrfs_block_group_used(&cache->item);
3151
Ilya Dryomova105bb82013-01-21 15:15:56 +02003152 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003153 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003154 else if (bargs->usage > 100)
3155 user_thresh = cache->key.offset;
3156 else
3157 user_thresh = div_factor_fine(cache->key.offset,
3158 bargs->usage);
3159
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003160 if (chunk_used < user_thresh)
3161 ret = 0;
3162
3163 btrfs_put_block_group(cache);
3164 return ret;
3165}
3166
Ilya Dryomov409d4042012-01-16 22:04:47 +02003167static int chunk_devid_filter(struct extent_buffer *leaf,
3168 struct btrfs_chunk *chunk,
3169 struct btrfs_balance_args *bargs)
3170{
3171 struct btrfs_stripe *stripe;
3172 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3173 int i;
3174
3175 for (i = 0; i < num_stripes; i++) {
3176 stripe = btrfs_stripe_nr(chunk, i);
3177 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3178 return 0;
3179 }
3180
3181 return 1;
3182}
3183
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003184/* [pstart, pend) */
3185static int chunk_drange_filter(struct extent_buffer *leaf,
3186 struct btrfs_chunk *chunk,
3187 u64 chunk_offset,
3188 struct btrfs_balance_args *bargs)
3189{
3190 struct btrfs_stripe *stripe;
3191 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3192 u64 stripe_offset;
3193 u64 stripe_length;
3194 int factor;
3195 int i;
3196
3197 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3198 return 0;
3199
3200 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003201 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3202 factor = num_stripes / 2;
3203 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3204 factor = num_stripes - 1;
3205 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3206 factor = num_stripes - 2;
3207 } else {
3208 factor = num_stripes;
3209 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003210
3211 for (i = 0; i < num_stripes; i++) {
3212 stripe = btrfs_stripe_nr(chunk, i);
3213 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3214 continue;
3215
3216 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3217 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003218 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003219
3220 if (stripe_offset < bargs->pend &&
3221 stripe_offset + stripe_length > bargs->pstart)
3222 return 0;
3223 }
3224
3225 return 1;
3226}
3227
Ilya Dryomovea671762012-01-16 22:04:48 +02003228/* [vstart, vend) */
3229static int chunk_vrange_filter(struct extent_buffer *leaf,
3230 struct btrfs_chunk *chunk,
3231 u64 chunk_offset,
3232 struct btrfs_balance_args *bargs)
3233{
3234 if (chunk_offset < bargs->vend &&
3235 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3236 /* at least part of the chunk is inside this vrange */
3237 return 0;
3238
3239 return 1;
3240}
3241
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003242static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003243 struct btrfs_balance_args *bargs)
3244{
3245 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3246 return 0;
3247
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003248 chunk_type = chunk_to_extended(chunk_type) &
3249 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003250
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003251 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003252 return 1;
3253
3254 return 0;
3255}
3256
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003257static int should_balance_chunk(struct btrfs_root *root,
3258 struct extent_buffer *leaf,
3259 struct btrfs_chunk *chunk, u64 chunk_offset)
3260{
3261 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3262 struct btrfs_balance_args *bargs = NULL;
3263 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3264
3265 /* type filter */
3266 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3267 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3268 return 0;
3269 }
3270
3271 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3272 bargs = &bctl->data;
3273 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3274 bargs = &bctl->sys;
3275 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3276 bargs = &bctl->meta;
3277
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003278 /* profiles filter */
3279 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3280 chunk_profiles_filter(chunk_type, bargs)) {
3281 return 0;
3282 }
3283
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003284 /* usage filter */
3285 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3286 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3287 return 0;
3288 }
3289
Ilya Dryomov409d4042012-01-16 22:04:47 +02003290 /* devid filter */
3291 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3292 chunk_devid_filter(leaf, chunk, bargs)) {
3293 return 0;
3294 }
3295
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003296 /* drange filter, makes sense only with devid filter */
3297 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3298 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3299 return 0;
3300 }
3301
Ilya Dryomovea671762012-01-16 22:04:48 +02003302 /* vrange filter */
3303 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3304 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3305 return 0;
3306 }
3307
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003308 /* soft profile changing mode */
3309 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3310 chunk_soft_convert_filter(chunk_type, bargs)) {
3311 return 0;
3312 }
3313
David Sterba7d824b62014-05-07 17:37:51 +02003314 /*
3315 * limited by count, must be the last filter
3316 */
3317 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3318 if (bargs->limit == 0)
3319 return 0;
3320 else
3321 bargs->limit--;
3322 }
3323
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003324 return 1;
3325}
3326
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003327static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003328{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003329 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003330 struct btrfs_root *chunk_root = fs_info->chunk_root;
3331 struct btrfs_root *dev_root = fs_info->dev_root;
3332 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003333 struct btrfs_device *device;
3334 u64 old_size;
3335 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003336 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003337 struct btrfs_path *path;
3338 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003339 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003340 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003341 struct extent_buffer *leaf;
3342 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003343 int ret;
3344 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003345 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003346 u64 limit_data = bctl->data.limit;
3347 u64 limit_meta = bctl->meta.limit;
3348 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003349
Chris Masonec44a352008-04-28 15:29:52 -04003350 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003351 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003352 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003353 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003354 size_to_free = div_factor(old_size, 1);
3355 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003356 if (!device->writeable ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003357 btrfs_device_get_total_bytes(device) -
3358 btrfs_device_get_bytes_used(device) > size_to_free ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003359 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003360 continue;
3361
3362 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003363 if (ret == -ENOSPC)
3364 break;
Chris Masonec44a352008-04-28 15:29:52 -04003365 BUG_ON(ret);
3366
Yan, Zhenga22285a2010-05-16 10:48:46 -04003367 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003368 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003369
3370 ret = btrfs_grow_device(trans, device, old_size);
3371 BUG_ON(ret);
3372
3373 btrfs_end_transaction(trans, dev_root);
3374 }
3375
3376 /* step two, relocate all the chunks */
3377 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003378 if (!path) {
3379 ret = -ENOMEM;
3380 goto error;
3381 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003382
3383 /* zero out stat counters */
3384 spin_lock(&fs_info->balance_lock);
3385 memset(&bctl->stat, 0, sizeof(bctl->stat));
3386 spin_unlock(&fs_info->balance_lock);
3387again:
David Sterba7d824b62014-05-07 17:37:51 +02003388 if (!counting) {
3389 bctl->data.limit = limit_data;
3390 bctl->meta.limit = limit_meta;
3391 bctl->sys.limit = limit_sys;
3392 }
Chris Masonec44a352008-04-28 15:29:52 -04003393 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3394 key.offset = (u64)-1;
3395 key.type = BTRFS_CHUNK_ITEM_KEY;
3396
Chris Masond3977122009-01-05 21:25:51 -05003397 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003398 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003399 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003400 ret = -ECANCELED;
3401 goto error;
3402 }
3403
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003404 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003405 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003406 if (ret < 0) {
3407 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003408 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003409 }
Chris Masonec44a352008-04-28 15:29:52 -04003410
3411 /*
3412 * this shouldn't happen, it means the last relocate
3413 * failed
3414 */
3415 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003416 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003417
3418 ret = btrfs_previous_item(chunk_root, path, 0,
3419 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003420 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003421 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003422 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003423 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003424 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003425
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003426 leaf = path->nodes[0];
3427 slot = path->slots[0];
3428 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3429
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003430 if (found_key.objectid != key.objectid) {
3431 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003432 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003433 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003434
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003435 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3436
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003437 if (!counting) {
3438 spin_lock(&fs_info->balance_lock);
3439 bctl->stat.considered++;
3440 spin_unlock(&fs_info->balance_lock);
3441 }
3442
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003443 ret = should_balance_chunk(chunk_root, leaf, chunk,
3444 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003445 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003446 if (!ret) {
3447 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003448 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003449 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003450
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003451 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003452 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003453 spin_lock(&fs_info->balance_lock);
3454 bctl->stat.expected++;
3455 spin_unlock(&fs_info->balance_lock);
3456 goto loop;
3457 }
3458
Chris Masonec44a352008-04-28 15:29:52 -04003459 ret = btrfs_relocate_chunk(chunk_root,
Chris Masonec44a352008-04-28 15:29:52 -04003460 found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003461 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik508794e2011-07-02 21:24:41 +00003462 if (ret && ret != -ENOSPC)
3463 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003464 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003465 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003466 } else {
3467 spin_lock(&fs_info->balance_lock);
3468 bctl->stat.completed++;
3469 spin_unlock(&fs_info->balance_lock);
3470 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003471loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003472 if (found_key.offset == 0)
3473 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003474 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003475 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003476
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003477 if (counting) {
3478 btrfs_release_path(path);
3479 counting = false;
3480 goto again;
3481 }
Chris Masonec44a352008-04-28 15:29:52 -04003482error:
3483 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003484 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003485 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003486 enospc_errors);
3487 if (!ret)
3488 ret = -ENOSPC;
3489 }
3490
Chris Masonec44a352008-04-28 15:29:52 -04003491 return ret;
3492}
3493
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003494/**
3495 * alloc_profile_is_valid - see if a given profile is valid and reduced
3496 * @flags: profile to validate
3497 * @extended: if true @flags is treated as an extended profile
3498 */
3499static int alloc_profile_is_valid(u64 flags, int extended)
3500{
3501 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3502 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3503
3504 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3505
3506 /* 1) check that all other bits are zeroed */
3507 if (flags & ~mask)
3508 return 0;
3509
3510 /* 2) see if profile is reduced */
3511 if (flags == 0)
3512 return !extended; /* "0" is valid for usual profiles */
3513
3514 /* true if exactly one bit set */
3515 return (flags & (flags - 1)) == 0;
3516}
3517
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003518static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3519{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003520 /* cancel requested || normal exit path */
3521 return atomic_read(&fs_info->balance_cancel_req) ||
3522 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3523 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003524}
3525
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003526static void __cancel_balance(struct btrfs_fs_info *fs_info)
3527{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003528 int ret;
3529
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003530 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003531 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003532 if (ret)
3533 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003534
3535 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003536}
3537
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003538/*
3539 * Should be called with both balance and volume mutexes held
3540 */
3541int btrfs_balance(struct btrfs_balance_control *bctl,
3542 struct btrfs_ioctl_balance_args *bargs)
3543{
3544 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003545 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003546 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003547 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003548 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003549 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003550
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003551 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003552 atomic_read(&fs_info->balance_pause_req) ||
3553 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003554 ret = -EINVAL;
3555 goto out;
3556 }
3557
Ilya Dryomove4837f82012-03-27 17:09:17 +03003558 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3559 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3560 mixed = 1;
3561
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003562 /*
3563 * In case of mixed groups both data and meta should be picked,
3564 * and identical options should be given for both of them.
3565 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003566 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3567 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003568 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3569 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3570 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003571 btrfs_err(fs_info, "with mixed groups data and "
3572 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003573 ret = -EINVAL;
3574 goto out;
3575 }
3576 }
3577
Stefan Behrens8dabb742012-11-06 13:15:27 +01003578 num_devices = fs_info->fs_devices->num_devices;
3579 btrfs_dev_replace_lock(&fs_info->dev_replace);
3580 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3581 BUG_ON(num_devices < 1);
3582 num_devices--;
3583 }
3584 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003585 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003586 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003587 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003588 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003589 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003590 if (num_devices > 2)
3591 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3592 if (num_devices > 3)
3593 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3594 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003595 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3596 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3597 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003598 btrfs_err(fs_info, "unable to start balance with target "
3599 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003600 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003601 ret = -EINVAL;
3602 goto out;
3603 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003604 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3605 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3606 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003607 btrfs_err(fs_info,
3608 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003609 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003610 ret = -EINVAL;
3611 goto out;
3612 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003613 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3614 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3615 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003616 btrfs_err(fs_info,
3617 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003618 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003619 ret = -EINVAL;
3620 goto out;
3621 }
3622
Ilya Dryomove4837f82012-03-27 17:09:17 +03003623 /* allow dup'ed data chunks only in mixed mode */
3624 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003625 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003626 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003627 ret = -EINVAL;
3628 goto out;
3629 }
3630
3631 /* allow to reduce meta or sys integrity only if force set */
3632 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003633 BTRFS_BLOCK_GROUP_RAID10 |
3634 BTRFS_BLOCK_GROUP_RAID5 |
3635 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003636 do {
3637 seq = read_seqbegin(&fs_info->profiles_lock);
3638
3639 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3640 (fs_info->avail_system_alloc_bits & allowed) &&
3641 !(bctl->sys.target & allowed)) ||
3642 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3643 (fs_info->avail_metadata_alloc_bits & allowed) &&
3644 !(bctl->meta.target & allowed))) {
3645 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003646 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003647 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003648 btrfs_err(fs_info, "balance will reduce metadata "
3649 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003650 ret = -EINVAL;
3651 goto out;
3652 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003653 }
Miao Xiede98ced2013-01-29 10:13:12 +00003654 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003655
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003656 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
Zhao Lei943c6e92015-08-19 15:54:15 +08003657 fs_info->num_tolerated_disk_barrier_failures = min(
3658 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info),
3659 btrfs_get_num_tolerated_disk_barrier_failures(
3660 bctl->sys.target));
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003661 }
3662
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003663 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003664 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003665 goto out;
3666
Ilya Dryomov59641012012-01-16 22:04:48 +02003667 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3668 BUG_ON(ret == -EEXIST);
3669 set_balance_control(bctl);
3670 } else {
3671 BUG_ON(ret != -EEXIST);
3672 spin_lock(&fs_info->balance_lock);
3673 update_balance_args(bctl);
3674 spin_unlock(&fs_info->balance_lock);
3675 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003676
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003677 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003678 mutex_unlock(&fs_info->balance_mutex);
3679
3680 ret = __btrfs_balance(fs_info);
3681
3682 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003683 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003684
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003685 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3686 fs_info->num_tolerated_disk_barrier_failures =
3687 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3688 }
3689
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003690 if (bargs) {
3691 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003692 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003693 }
3694
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003695 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3696 balance_need_close(fs_info)) {
3697 __cancel_balance(fs_info);
3698 }
3699
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003700 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003701
3702 return ret;
3703out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003704 if (bctl->flags & BTRFS_BALANCE_RESUME)
3705 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003706 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003707 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003708 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3709 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003710 return ret;
3711}
3712
3713static int balance_kthread(void *data)
3714{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003715 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003716 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003717
3718 mutex_lock(&fs_info->volume_mutex);
3719 mutex_lock(&fs_info->balance_mutex);
3720
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003721 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003722 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003723 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003724 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003725
3726 mutex_unlock(&fs_info->balance_mutex);
3727 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003728
Ilya Dryomov59641012012-01-16 22:04:48 +02003729 return ret;
3730}
3731
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003732int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3733{
3734 struct task_struct *tsk;
3735
3736 spin_lock(&fs_info->balance_lock);
3737 if (!fs_info->balance_ctl) {
3738 spin_unlock(&fs_info->balance_lock);
3739 return 0;
3740 }
3741 spin_unlock(&fs_info->balance_lock);
3742
3743 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003744 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003745 return 0;
3746 }
3747
3748 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303749 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003750}
3751
Ilya Dryomov68310a52012-06-22 12:24:12 -06003752int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003753{
Ilya Dryomov59641012012-01-16 22:04:48 +02003754 struct btrfs_balance_control *bctl;
3755 struct btrfs_balance_item *item;
3756 struct btrfs_disk_balance_args disk_bargs;
3757 struct btrfs_path *path;
3758 struct extent_buffer *leaf;
3759 struct btrfs_key key;
3760 int ret;
3761
3762 path = btrfs_alloc_path();
3763 if (!path)
3764 return -ENOMEM;
3765
Ilya Dryomov68310a52012-06-22 12:24:12 -06003766 key.objectid = BTRFS_BALANCE_OBJECTID;
3767 key.type = BTRFS_BALANCE_ITEM_KEY;
3768 key.offset = 0;
3769
3770 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3771 if (ret < 0)
3772 goto out;
3773 if (ret > 0) { /* ret = -ENOENT; */
3774 ret = 0;
3775 goto out;
3776 }
3777
Ilya Dryomov59641012012-01-16 22:04:48 +02003778 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3779 if (!bctl) {
3780 ret = -ENOMEM;
3781 goto out;
3782 }
3783
Ilya Dryomov59641012012-01-16 22:04:48 +02003784 leaf = path->nodes[0];
3785 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3786
Ilya Dryomov68310a52012-06-22 12:24:12 -06003787 bctl->fs_info = fs_info;
3788 bctl->flags = btrfs_balance_flags(leaf, item);
3789 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003790
3791 btrfs_balance_data(leaf, item, &disk_bargs);
3792 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3793 btrfs_balance_meta(leaf, item, &disk_bargs);
3794 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3795 btrfs_balance_sys(leaf, item, &disk_bargs);
3796 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3797
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003798 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3799
Ilya Dryomov68310a52012-06-22 12:24:12 -06003800 mutex_lock(&fs_info->volume_mutex);
3801 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003802
Ilya Dryomov68310a52012-06-22 12:24:12 -06003803 set_balance_control(bctl);
3804
3805 mutex_unlock(&fs_info->balance_mutex);
3806 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003807out:
3808 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003809 return ret;
3810}
3811
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003812int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3813{
3814 int ret = 0;
3815
3816 mutex_lock(&fs_info->balance_mutex);
3817 if (!fs_info->balance_ctl) {
3818 mutex_unlock(&fs_info->balance_mutex);
3819 return -ENOTCONN;
3820 }
3821
3822 if (atomic_read(&fs_info->balance_running)) {
3823 atomic_inc(&fs_info->balance_pause_req);
3824 mutex_unlock(&fs_info->balance_mutex);
3825
3826 wait_event(fs_info->balance_wait_q,
3827 atomic_read(&fs_info->balance_running) == 0);
3828
3829 mutex_lock(&fs_info->balance_mutex);
3830 /* we are good with balance_ctl ripped off from under us */
3831 BUG_ON(atomic_read(&fs_info->balance_running));
3832 atomic_dec(&fs_info->balance_pause_req);
3833 } else {
3834 ret = -ENOTCONN;
3835 }
3836
3837 mutex_unlock(&fs_info->balance_mutex);
3838 return ret;
3839}
3840
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003841int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3842{
Ilya Dryomove649e582013-10-10 20:40:21 +03003843 if (fs_info->sb->s_flags & MS_RDONLY)
3844 return -EROFS;
3845
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003846 mutex_lock(&fs_info->balance_mutex);
3847 if (!fs_info->balance_ctl) {
3848 mutex_unlock(&fs_info->balance_mutex);
3849 return -ENOTCONN;
3850 }
3851
3852 atomic_inc(&fs_info->balance_cancel_req);
3853 /*
3854 * if we are running just wait and return, balance item is
3855 * deleted in btrfs_balance in this case
3856 */
3857 if (atomic_read(&fs_info->balance_running)) {
3858 mutex_unlock(&fs_info->balance_mutex);
3859 wait_event(fs_info->balance_wait_q,
3860 atomic_read(&fs_info->balance_running) == 0);
3861 mutex_lock(&fs_info->balance_mutex);
3862 } else {
3863 /* __cancel_balance needs volume_mutex */
3864 mutex_unlock(&fs_info->balance_mutex);
3865 mutex_lock(&fs_info->volume_mutex);
3866 mutex_lock(&fs_info->balance_mutex);
3867
3868 if (fs_info->balance_ctl)
3869 __cancel_balance(fs_info);
3870
3871 mutex_unlock(&fs_info->volume_mutex);
3872 }
3873
3874 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3875 atomic_dec(&fs_info->balance_cancel_req);
3876 mutex_unlock(&fs_info->balance_mutex);
3877 return 0;
3878}
3879
Stefan Behrens803b2f52013-08-15 17:11:21 +02003880static int btrfs_uuid_scan_kthread(void *data)
3881{
3882 struct btrfs_fs_info *fs_info = data;
3883 struct btrfs_root *root = fs_info->tree_root;
3884 struct btrfs_key key;
3885 struct btrfs_key max_key;
3886 struct btrfs_path *path = NULL;
3887 int ret = 0;
3888 struct extent_buffer *eb;
3889 int slot;
3890 struct btrfs_root_item root_item;
3891 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003892 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003893
3894 path = btrfs_alloc_path();
3895 if (!path) {
3896 ret = -ENOMEM;
3897 goto out;
3898 }
3899
3900 key.objectid = 0;
3901 key.type = BTRFS_ROOT_ITEM_KEY;
3902 key.offset = 0;
3903
3904 max_key.objectid = (u64)-1;
3905 max_key.type = BTRFS_ROOT_ITEM_KEY;
3906 max_key.offset = (u64)-1;
3907
Stefan Behrens803b2f52013-08-15 17:11:21 +02003908 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003909 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003910 if (ret) {
3911 if (ret > 0)
3912 ret = 0;
3913 break;
3914 }
3915
3916 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3917 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3918 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3919 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3920 goto skip;
3921
3922 eb = path->nodes[0];
3923 slot = path->slots[0];
3924 item_size = btrfs_item_size_nr(eb, slot);
3925 if (item_size < sizeof(root_item))
3926 goto skip;
3927
Stefan Behrens803b2f52013-08-15 17:11:21 +02003928 read_extent_buffer(eb, &root_item,
3929 btrfs_item_ptr_offset(eb, slot),
3930 (int)sizeof(root_item));
3931 if (btrfs_root_refs(&root_item) == 0)
3932 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003933
3934 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3935 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3936 if (trans)
3937 goto update_tree;
3938
3939 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003940 /*
3941 * 1 - subvol uuid item
3942 * 1 - received_subvol uuid item
3943 */
3944 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3945 if (IS_ERR(trans)) {
3946 ret = PTR_ERR(trans);
3947 break;
3948 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003949 continue;
3950 } else {
3951 goto skip;
3952 }
3953update_tree:
3954 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003955 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3956 root_item.uuid,
3957 BTRFS_UUID_KEY_SUBVOL,
3958 key.objectid);
3959 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003960 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003961 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003962 break;
3963 }
3964 }
3965
3966 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003967 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3968 root_item.received_uuid,
3969 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3970 key.objectid);
3971 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003972 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003973 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003974 break;
3975 }
3976 }
3977
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003978skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003979 if (trans) {
3980 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003981 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003982 if (ret)
3983 break;
3984 }
3985
Stefan Behrens803b2f52013-08-15 17:11:21 +02003986 btrfs_release_path(path);
3987 if (key.offset < (u64)-1) {
3988 key.offset++;
3989 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3990 key.offset = 0;
3991 key.type = BTRFS_ROOT_ITEM_KEY;
3992 } else if (key.objectid < (u64)-1) {
3993 key.offset = 0;
3994 key.type = BTRFS_ROOT_ITEM_KEY;
3995 key.objectid++;
3996 } else {
3997 break;
3998 }
3999 cond_resched();
4000 }
4001
4002out:
4003 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004004 if (trans && !IS_ERR(trans))
4005 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004006 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004007 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004008 else
4009 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004010 up(&fs_info->uuid_tree_rescan_sem);
4011 return 0;
4012}
4013
Stefan Behrens70f80172013-08-15 17:11:23 +02004014/*
4015 * Callback for btrfs_uuid_tree_iterate().
4016 * returns:
4017 * 0 check succeeded, the entry is not outdated.
4018 * < 0 if an error occured.
4019 * > 0 if the check failed, which means the caller shall remove the entry.
4020 */
4021static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4022 u8 *uuid, u8 type, u64 subid)
4023{
4024 struct btrfs_key key;
4025 int ret = 0;
4026 struct btrfs_root *subvol_root;
4027
4028 if (type != BTRFS_UUID_KEY_SUBVOL &&
4029 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4030 goto out;
4031
4032 key.objectid = subid;
4033 key.type = BTRFS_ROOT_ITEM_KEY;
4034 key.offset = (u64)-1;
4035 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4036 if (IS_ERR(subvol_root)) {
4037 ret = PTR_ERR(subvol_root);
4038 if (ret == -ENOENT)
4039 ret = 1;
4040 goto out;
4041 }
4042
4043 switch (type) {
4044 case BTRFS_UUID_KEY_SUBVOL:
4045 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4046 ret = 1;
4047 break;
4048 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4049 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4050 BTRFS_UUID_SIZE))
4051 ret = 1;
4052 break;
4053 }
4054
4055out:
4056 return ret;
4057}
4058
4059static int btrfs_uuid_rescan_kthread(void *data)
4060{
4061 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4062 int ret;
4063
4064 /*
4065 * 1st step is to iterate through the existing UUID tree and
4066 * to delete all entries that contain outdated data.
4067 * 2nd step is to add all missing entries to the UUID tree.
4068 */
4069 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4070 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004071 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004072 up(&fs_info->uuid_tree_rescan_sem);
4073 return ret;
4074 }
4075 return btrfs_uuid_scan_kthread(data);
4076}
4077
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004078int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4079{
4080 struct btrfs_trans_handle *trans;
4081 struct btrfs_root *tree_root = fs_info->tree_root;
4082 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004083 struct task_struct *task;
4084 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004085
4086 /*
4087 * 1 - root node
4088 * 1 - root item
4089 */
4090 trans = btrfs_start_transaction(tree_root, 2);
4091 if (IS_ERR(trans))
4092 return PTR_ERR(trans);
4093
4094 uuid_root = btrfs_create_tree(trans, fs_info,
4095 BTRFS_UUID_TREE_OBJECTID);
4096 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004097 ret = PTR_ERR(uuid_root);
4098 btrfs_abort_transaction(trans, tree_root, ret);
4099 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004100 }
4101
4102 fs_info->uuid_root = uuid_root;
4103
Stefan Behrens803b2f52013-08-15 17:11:21 +02004104 ret = btrfs_commit_transaction(trans, tree_root);
4105 if (ret)
4106 return ret;
4107
4108 down(&fs_info->uuid_tree_rescan_sem);
4109 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4110 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004111 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004112 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004113 up(&fs_info->uuid_tree_rescan_sem);
4114 return PTR_ERR(task);
4115 }
4116
4117 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004118}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004119
Stefan Behrens70f80172013-08-15 17:11:23 +02004120int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4121{
4122 struct task_struct *task;
4123
4124 down(&fs_info->uuid_tree_rescan_sem);
4125 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4126 if (IS_ERR(task)) {
4127 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004128 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004129 up(&fs_info->uuid_tree_rescan_sem);
4130 return PTR_ERR(task);
4131 }
4132
4133 return 0;
4134}
4135
Chris Mason8f18cf12008-04-25 16:53:30 -04004136/*
4137 * shrinking a device means finding all of the device extents past
4138 * the new size, and then following the back refs to the chunks.
4139 * The chunk relocation code actually frees the device extent
4140 */
4141int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4142{
4143 struct btrfs_trans_handle *trans;
4144 struct btrfs_root *root = device->dev_root;
4145 struct btrfs_dev_extent *dev_extent = NULL;
4146 struct btrfs_path *path;
4147 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004148 u64 chunk_offset;
4149 int ret;
4150 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004151 int failed = 0;
4152 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004153 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004154 struct extent_buffer *l;
4155 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02004156 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004157 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004158 u64 old_size = btrfs_device_get_total_bytes(device);
4159 u64 diff = old_size - new_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04004160
Stefan Behrens63a212a2012-11-05 18:29:28 +01004161 if (device->is_tgtdev_for_dev_replace)
4162 return -EINVAL;
4163
Chris Mason8f18cf12008-04-25 16:53:30 -04004164 path = btrfs_alloc_path();
4165 if (!path)
4166 return -ENOMEM;
4167
Chris Mason8f18cf12008-04-25 16:53:30 -04004168 path->reada = 2;
4169
Chris Mason7d9eb122008-07-08 14:19:17 -04004170 lock_chunks(root);
4171
Miao Xie7cc8e582014-09-03 21:35:38 +08004172 btrfs_device_set_total_bytes(device, new_size);
Josef Bacik2bf64752011-09-26 17:12:22 -04004173 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004174 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04004175 spin_lock(&root->fs_info->free_chunk_lock);
4176 root->fs_info->free_chunk_space -= diff;
4177 spin_unlock(&root->fs_info->free_chunk_lock);
4178 }
Chris Mason7d9eb122008-07-08 14:19:17 -04004179 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004180
Josef Bacikba1bf482009-09-11 16:11:19 -04004181again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004182 key.objectid = device->devid;
4183 key.offset = (u64)-1;
4184 key.type = BTRFS_DEV_EXTENT_KEY;
4185
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004186 do {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004187 mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004188 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004189 if (ret < 0) {
4190 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004191 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004192 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004193
4194 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004195 if (ret)
4196 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004197 if (ret < 0)
4198 goto done;
4199 if (ret) {
4200 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004201 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004202 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004203 }
4204
4205 l = path->nodes[0];
4206 slot = path->slots[0];
4207 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4208
Josef Bacikba1bf482009-09-11 16:11:19 -04004209 if (key.objectid != device->devid) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004210 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004211 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004212 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004213 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004214
4215 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4216 length = btrfs_dev_extent_length(l, dev_extent);
4217
Josef Bacikba1bf482009-09-11 16:11:19 -04004218 if (key.offset + length <= new_size) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004219 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004220 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004221 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004222 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004223
Chris Mason8f18cf12008-04-25 16:53:30 -04004224 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004225 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004226
Zhaoleidc2ee4e2015-08-05 18:00:04 +08004227 ret = btrfs_relocate_chunk(root, chunk_offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004228 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04004229 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004230 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004231 if (ret == -ENOSPC)
4232 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004233 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004234
4235 if (failed && !retried) {
4236 failed = 0;
4237 retried = true;
4238 goto again;
4239 } else if (failed && retried) {
4240 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004241 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004242 }
4243
Chris Balld6397ba2009-04-27 07:29:03 -04004244 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004245 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004246 if (IS_ERR(trans)) {
4247 ret = PTR_ERR(trans);
4248 goto done;
4249 }
4250
Chris Balld6397ba2009-04-27 07:29:03 -04004251 lock_chunks(root);
Filipe Manana53e489b2015-06-02 14:43:21 +01004252
4253 /*
4254 * We checked in the above loop all device extents that were already in
4255 * the device tree. However before we have updated the device's
4256 * total_bytes to the new size, we might have had chunk allocations that
4257 * have not complete yet (new block groups attached to transaction
4258 * handles), and therefore their device extents were not yet in the
4259 * device tree and we missed them in the loop above. So if we have any
4260 * pending chunk using a device extent that overlaps the device range
4261 * that we can not use anymore, commit the current transaction and
4262 * repeat the search on the device tree - this way we guarantee we will
4263 * not have chunks using device extents that end beyond 'new_size'.
4264 */
4265 if (!checked_pending_chunks) {
4266 u64 start = new_size;
4267 u64 len = old_size - new_size;
4268
Jeff Mahoney499f3772015-06-15 09:41:17 -04004269 if (contains_pending_extent(trans->transaction, device,
4270 &start, len)) {
Filipe Manana53e489b2015-06-02 14:43:21 +01004271 unlock_chunks(root);
4272 checked_pending_chunks = true;
4273 failed = 0;
4274 retried = false;
4275 ret = btrfs_commit_transaction(trans, root);
4276 if (ret)
4277 goto done;
4278 goto again;
4279 }
4280 }
4281
Miao Xie7cc8e582014-09-03 21:35:38 +08004282 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004283 if (list_empty(&device->resized_list))
4284 list_add_tail(&device->resized_list,
4285 &root->fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004286
Chris Balld6397ba2009-04-27 07:29:03 -04004287 WARN_ON(diff > old_total);
4288 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4289 unlock_chunks(root);
Miao Xie2196d6e2014-09-03 21:35:41 +08004290
4291 /* Now btrfs_update_device() will change the on-disk size. */
4292 ret = btrfs_update_device(trans, device);
Chris Balld6397ba2009-04-27 07:29:03 -04004293 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004294done:
4295 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004296 if (ret) {
4297 lock_chunks(root);
4298 btrfs_device_set_total_bytes(device, old_size);
4299 if (device->writeable)
4300 device->fs_devices->total_rw_bytes += diff;
4301 spin_lock(&root->fs_info->free_chunk_lock);
4302 root->fs_info->free_chunk_space += diff;
4303 spin_unlock(&root->fs_info->free_chunk_lock);
4304 unlock_chunks(root);
4305 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004306 return ret;
4307}
4308
Li Zefan125ccb02011-12-08 15:07:24 +08004309static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004310 struct btrfs_key *key,
4311 struct btrfs_chunk *chunk, int item_size)
4312{
David Sterba6c417612011-04-13 15:41:04 +02004313 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004314 struct btrfs_disk_key disk_key;
4315 u32 array_size;
4316 u8 *ptr;
4317
Miao Xiefe48a5c2014-09-03 21:35:39 +08004318 lock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004319 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004320 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004321 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4322 unlock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004323 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004324 }
Chris Mason0b86a832008-03-24 15:01:56 -04004325
4326 ptr = super_copy->sys_chunk_array + array_size;
4327 btrfs_cpu_key_to_disk(&disk_key, key);
4328 memcpy(ptr, &disk_key, sizeof(disk_key));
4329 ptr += sizeof(disk_key);
4330 memcpy(ptr, chunk, item_size);
4331 item_size += sizeof(disk_key);
4332 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004333 unlock_chunks(root);
4334
Chris Mason0b86a832008-03-24 15:01:56 -04004335 return 0;
4336}
4337
Miao Xieb2117a32011-01-05 10:07:28 +00004338/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004339 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004340 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004341static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004342{
Arne Jansen73c5de02011-04-12 12:07:57 +02004343 const struct btrfs_device_info *di_a = a;
4344 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004345
Arne Jansen73c5de02011-04-12 12:07:57 +02004346 if (di_a->max_avail > di_b->max_avail)
4347 return -1;
4348 if (di_a->max_avail < di_b->max_avail)
4349 return 1;
4350 if (di_a->total_avail > di_b->total_avail)
4351 return -1;
4352 if (di_a->total_avail < di_b->total_avail)
4353 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004354 return 0;
4355}
4356
David Woodhouse53b381b2013-01-29 18:40:14 -05004357static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4358{
4359 /* TODO allow them to set a preferred stripe size */
4360 return 64 * 1024;
4361}
4362
4363static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4364{
Zhao Leiffe2d202015-01-20 15:11:44 +08004365 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004366 return;
4367
Miao Xieceda0862013-04-11 10:30:16 +00004368 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004369}
4370
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004371#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4372 - sizeof(struct btrfs_item) \
4373 - sizeof(struct btrfs_chunk)) \
4374 / sizeof(struct btrfs_stripe) + 1)
4375
4376#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4377 - 2 * sizeof(struct btrfs_disk_key) \
4378 - 2 * sizeof(struct btrfs_chunk)) \
4379 / sizeof(struct btrfs_stripe) + 1)
4380
Miao Xieb2117a32011-01-05 10:07:28 +00004381static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004382 struct btrfs_root *extent_root, u64 start,
4383 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004384{
4385 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004386 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4387 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004388 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004389 struct extent_map_tree *em_tree;
4390 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004391 struct btrfs_device_info *devices_info = NULL;
4392 u64 total_avail;
4393 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004394 int data_stripes; /* number of stripes that count for
4395 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004396 int sub_stripes; /* sub_stripes info for map */
4397 int dev_stripes; /* stripes per dev */
4398 int devs_max; /* max devs to use */
4399 int devs_min; /* min devs needed */
4400 int devs_increment; /* ndevs has to be a multiple of this */
4401 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004402 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004403 u64 max_stripe_size;
4404 u64 max_chunk_size;
4405 u64 stripe_size;
4406 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004407 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004408 int ndevs;
4409 int i;
4410 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004411 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004412
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004413 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004414
Miao Xieb2117a32011-01-05 10:07:28 +00004415 if (list_empty(&fs_devices->alloc_list))
4416 return -ENOSPC;
4417
Liu Bo31e50222012-11-21 14:18:10 +00004418 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004419
Liu Bo31e50222012-11-21 14:18:10 +00004420 sub_stripes = btrfs_raid_array[index].sub_stripes;
4421 dev_stripes = btrfs_raid_array[index].dev_stripes;
4422 devs_max = btrfs_raid_array[index].devs_max;
4423 devs_min = btrfs_raid_array[index].devs_min;
4424 devs_increment = btrfs_raid_array[index].devs_increment;
4425 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004426
4427 if (type & BTRFS_BLOCK_GROUP_DATA) {
4428 max_stripe_size = 1024 * 1024 * 1024;
4429 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004430 if (!devs_max)
4431 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004432 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004433 /* for larger filesystems, use larger metadata chunks */
4434 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4435 max_stripe_size = 1024 * 1024 * 1024;
4436 else
4437 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004438 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004439 if (!devs_max)
4440 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004441 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004442 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004443 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004444 if (!devs_max)
4445 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004446 } else {
David Sterba351fd352014-05-15 16:48:20 +02004447 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004448 type);
4449 BUG_ON(1);
4450 }
4451
4452 /* we don't want a chunk larger than 10% of writeable space */
4453 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4454 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004455
David Sterba31e818f2015-02-20 18:00:26 +01004456 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004457 GFP_NOFS);
4458 if (!devices_info)
4459 return -ENOMEM;
4460
Arne Jansen73c5de02011-04-12 12:07:57 +02004461 cur = fs_devices->alloc_list.next;
4462
4463 /*
4464 * in the first pass through the devices list, we gather information
4465 * about the available holes on each device.
4466 */
4467 ndevs = 0;
4468 while (cur != &fs_devices->alloc_list) {
4469 struct btrfs_device *device;
4470 u64 max_avail;
4471 u64 dev_offset;
4472
4473 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4474
4475 cur = cur->next;
4476
4477 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004478 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004479 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004480 continue;
4481 }
4482
Stefan Behrens63a212a2012-11-05 18:29:28 +01004483 if (!device->in_fs_metadata ||
4484 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004485 continue;
4486
4487 if (device->total_bytes > device->bytes_used)
4488 total_avail = device->total_bytes - device->bytes_used;
4489 else
4490 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004491
4492 /* If there is no space on this device, skip it. */
4493 if (total_avail == 0)
4494 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004495
Josef Bacik6df9a952013-06-27 13:22:46 -04004496 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004497 max_stripe_size * dev_stripes,
4498 &dev_offset, &max_avail);
4499 if (ret && ret != -ENOSPC)
4500 goto error;
4501
4502 if (ret == 0)
4503 max_avail = max_stripe_size * dev_stripes;
4504
4505 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4506 continue;
4507
Eric Sandeen063d0062013-01-31 00:55:01 +00004508 if (ndevs == fs_devices->rw_devices) {
4509 WARN(1, "%s: found more than %llu devices\n",
4510 __func__, fs_devices->rw_devices);
4511 break;
4512 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004513 devices_info[ndevs].dev_offset = dev_offset;
4514 devices_info[ndevs].max_avail = max_avail;
4515 devices_info[ndevs].total_avail = total_avail;
4516 devices_info[ndevs].dev = device;
4517 ++ndevs;
4518 }
4519
4520 /*
4521 * now sort the devices by hole size / available space
4522 */
4523 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4524 btrfs_cmp_device_info, NULL);
4525
4526 /* round down to number of usable stripes */
4527 ndevs -= ndevs % devs_increment;
4528
4529 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4530 ret = -ENOSPC;
4531 goto error;
4532 }
4533
4534 if (devs_max && ndevs > devs_max)
4535 ndevs = devs_max;
4536 /*
4537 * the primary goal is to maximize the number of stripes, so use as many
4538 * devices as possible, even if the stripes are not maximum sized.
4539 */
4540 stripe_size = devices_info[ndevs-1].max_avail;
4541 num_stripes = ndevs * dev_stripes;
4542
David Woodhouse53b381b2013-01-29 18:40:14 -05004543 /*
4544 * this will have to be fixed for RAID1 and RAID10 over
4545 * more drives
4546 */
4547 data_stripes = num_stripes / ncopies;
4548
David Woodhouse53b381b2013-01-29 18:40:14 -05004549 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4550 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4551 btrfs_super_stripesize(info->super_copy));
4552 data_stripes = num_stripes - 1;
4553 }
4554 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4555 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4556 btrfs_super_stripesize(info->super_copy));
4557 data_stripes = num_stripes - 2;
4558 }
Chris Mason86db2572013-02-20 16:23:40 -05004559
4560 /*
4561 * Use the number of data stripes to figure out how big this chunk
4562 * is really going to be in terms of logical address space,
4563 * and compare that answer with the max chunk size
4564 */
4565 if (stripe_size * data_stripes > max_chunk_size) {
4566 u64 mask = (1ULL << 24) - 1;
David Sterbab8b93ad2015-01-16 17:26:13 +01004567
4568 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004569
4570 /* bump the answer up to a 16MB boundary */
4571 stripe_size = (stripe_size + mask) & ~mask;
4572
4573 /* but don't go higher than the limits we found
4574 * while searching for free extents
4575 */
4576 if (stripe_size > devices_info[ndevs-1].max_avail)
4577 stripe_size = devices_info[ndevs-1].max_avail;
4578 }
4579
David Sterbab8b93ad2015-01-16 17:26:13 +01004580 stripe_size = div_u64(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004581
4582 /* align to BTRFS_STRIPE_LEN */
David Sterbab8b93ad2015-01-16 17:26:13 +01004583 stripe_size = div_u64(stripe_size, raid_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05004584 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004585
Miao Xieb2117a32011-01-05 10:07:28 +00004586 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4587 if (!map) {
4588 ret = -ENOMEM;
4589 goto error;
4590 }
4591 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004592
Arne Jansen73c5de02011-04-12 12:07:57 +02004593 for (i = 0; i < ndevs; ++i) {
4594 for (j = 0; j < dev_stripes; ++j) {
4595 int s = i * dev_stripes + j;
4596 map->stripes[s].dev = devices_info[i].dev;
4597 map->stripes[s].physical = devices_info[i].dev_offset +
4598 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004599 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004600 }
Chris Mason593060d2008-03-25 16:50:33 -04004601 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004602 map->stripe_len = raid_stripe_len;
4603 map->io_align = raid_stripe_len;
4604 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004605 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004606 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004607
David Woodhouse53b381b2013-01-29 18:40:14 -05004608 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004609
Arne Jansen73c5de02011-04-12 12:07:57 +02004610 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004611
David Sterba172ddd62011-04-21 00:48:27 +02004612 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004613 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004614 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004615 ret = -ENOMEM;
4616 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004617 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004618 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004619 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004620 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004621 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004622 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004623 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004624 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004625
Chris Mason0b86a832008-03-24 15:01:56 -04004626 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004627 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004628 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004629 if (!ret) {
4630 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4631 atomic_inc(&em->refs);
4632 }
Chris Mason890871b2009-09-02 16:24:52 -04004633 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004634 if (ret) {
4635 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004636 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004637 }
Yan Zheng2b820322008-11-17 21:11:30 -05004638
Josef Bacik04487482013-01-29 15:03:37 -05004639 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4640 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4641 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004642 if (ret)
4643 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004644
Miao Xie7cc8e582014-09-03 21:35:38 +08004645 for (i = 0; i < map->num_stripes; i++) {
4646 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4647 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4648 }
Miao Xie43530c42014-09-03 21:35:36 +08004649
Miao Xie1c116182014-09-03 21:35:37 +08004650 spin_lock(&extent_root->fs_info->free_chunk_lock);
4651 extent_root->fs_info->free_chunk_space -= (stripe_size *
4652 map->num_stripes);
4653 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4654
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004655 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004656 check_raid56_incompat_flag(extent_root->fs_info, type);
4657
Miao Xieb2117a32011-01-05 10:07:28 +00004658 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004659 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004660
Josef Bacik6df9a952013-06-27 13:22:46 -04004661error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004662 write_lock(&em_tree->lock);
4663 remove_extent_mapping(em_tree, em);
4664 write_unlock(&em_tree->lock);
4665
4666 /* One for our allocation */
4667 free_extent_map(em);
4668 /* One for the tree reference */
4669 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004670 /* One for the pending_chunks list reference */
4671 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004672error:
Miao Xieb2117a32011-01-05 10:07:28 +00004673 kfree(devices_info);
4674 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004675}
4676
Josef Bacik6df9a952013-06-27 13:22:46 -04004677int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004678 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004679 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004680{
Yan Zheng2b820322008-11-17 21:11:30 -05004681 struct btrfs_key key;
4682 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4683 struct btrfs_device *device;
4684 struct btrfs_chunk *chunk;
4685 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004686 struct extent_map_tree *em_tree;
4687 struct extent_map *em;
4688 struct map_lookup *map;
4689 size_t item_size;
4690 u64 dev_offset;
4691 u64 stripe_size;
4692 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004693 int ret;
4694
Josef Bacik6df9a952013-06-27 13:22:46 -04004695 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4696 read_lock(&em_tree->lock);
4697 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4698 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004699
Josef Bacik6df9a952013-06-27 13:22:46 -04004700 if (!em) {
4701 btrfs_crit(extent_root->fs_info, "unable to find logical "
4702 "%Lu len %Lu", chunk_offset, chunk_size);
4703 return -EINVAL;
4704 }
4705
4706 if (em->start != chunk_offset || em->len != chunk_size) {
4707 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004708 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004709 chunk_size, em->start, em->len);
4710 free_extent_map(em);
4711 return -EINVAL;
4712 }
4713
4714 map = (struct map_lookup *)em->bdev;
4715 item_size = btrfs_chunk_item_size(map->num_stripes);
4716 stripe_size = em->orig_block_len;
4717
4718 chunk = kzalloc(item_size, GFP_NOFS);
4719 if (!chunk) {
4720 ret = -ENOMEM;
4721 goto out;
4722 }
4723
4724 for (i = 0; i < map->num_stripes; i++) {
4725 device = map->stripes[i].dev;
4726 dev_offset = map->stripes[i].physical;
4727
Yan Zheng2b820322008-11-17 21:11:30 -05004728 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004729 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004730 goto out;
4731 ret = btrfs_alloc_dev_extent(trans, device,
4732 chunk_root->root_key.objectid,
4733 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4734 chunk_offset, dev_offset,
4735 stripe_size);
4736 if (ret)
4737 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004738 }
4739
Yan Zheng2b820322008-11-17 21:11:30 -05004740 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004741 for (i = 0; i < map->num_stripes; i++) {
4742 device = map->stripes[i].dev;
4743 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004744
4745 btrfs_set_stack_stripe_devid(stripe, device->devid);
4746 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4747 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4748 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004749 }
4750
4751 btrfs_set_stack_chunk_length(chunk, chunk_size);
4752 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4753 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4754 btrfs_set_stack_chunk_type(chunk, map->type);
4755 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4756 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4757 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4758 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4759 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4760
4761 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4762 key.type = BTRFS_CHUNK_ITEM_KEY;
4763 key.offset = chunk_offset;
4764
4765 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004766 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4767 /*
4768 * TODO: Cleanup of inserted chunk root in case of
4769 * failure.
4770 */
Li Zefan125ccb02011-12-08 15:07:24 +08004771 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004772 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004773 }
liubo1abe9b82011-03-24 11:18:59 +00004774
Josef Bacik6df9a952013-06-27 13:22:46 -04004775out:
Yan Zheng2b820322008-11-17 21:11:30 -05004776 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004777 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004778 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004779}
4780
4781/*
4782 * Chunk allocation falls into two parts. The first part does works
4783 * that make the new allocated chunk useable, but not do any operation
4784 * that modifies the chunk tree. The second part does the works that
4785 * require modifying the chunk tree. This division is important for the
4786 * bootstrap process of adding storage to a seed btrfs.
4787 */
4788int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4789 struct btrfs_root *extent_root, u64 type)
4790{
4791 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004792
Filipe Mananaa9629592015-05-18 19:11:40 +01004793 ASSERT(mutex_is_locked(&extent_root->fs_info->chunk_mutex));
Josef Bacik6df9a952013-06-27 13:22:46 -04004794 chunk_offset = find_next_chunk(extent_root->fs_info);
4795 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004796}
4797
Chris Masond3977122009-01-05 21:25:51 -05004798static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004799 struct btrfs_root *root,
4800 struct btrfs_device *device)
4801{
4802 u64 chunk_offset;
4803 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004804 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004805 struct btrfs_fs_info *fs_info = root->fs_info;
4806 struct btrfs_root *extent_root = fs_info->extent_root;
4807 int ret;
4808
Josef Bacik6df9a952013-06-27 13:22:46 -04004809 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004810 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004811 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4812 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004813 if (ret)
4814 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004815
Josef Bacik6df9a952013-06-27 13:22:46 -04004816 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004817 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004818 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4819 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004820 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004821}
4822
Miao Xied20983b2014-07-03 18:22:13 +08004823static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4824{
4825 int max_errors;
4826
4827 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4828 BTRFS_BLOCK_GROUP_RAID10 |
4829 BTRFS_BLOCK_GROUP_RAID5 |
4830 BTRFS_BLOCK_GROUP_DUP)) {
4831 max_errors = 1;
4832 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4833 max_errors = 2;
4834 } else {
4835 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004836 }
4837
Miao Xied20983b2014-07-03 18:22:13 +08004838 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05004839}
4840
4841int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4842{
4843 struct extent_map *em;
4844 struct map_lookup *map;
4845 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4846 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004847 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004848 int i;
4849
Chris Mason890871b2009-09-02 16:24:52 -04004850 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004851 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004852 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004853 if (!em)
4854 return 1;
4855
4856 map = (struct map_lookup *)em->bdev;
4857 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004858 if (map->stripes[i].dev->missing) {
4859 miss_ndevs++;
4860 continue;
4861 }
4862
Yan Zheng2b820322008-11-17 21:11:30 -05004863 if (!map->stripes[i].dev->writeable) {
4864 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004865 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004866 }
4867 }
Miao Xied20983b2014-07-03 18:22:13 +08004868
4869 /*
4870 * If the number of missing devices is larger than max errors,
4871 * we can not write the data into that chunk successfully, so
4872 * set it readonly.
4873 */
4874 if (miss_ndevs > btrfs_chunk_max_errors(map))
4875 readonly = 1;
4876end:
Yan Zheng2b820322008-11-17 21:11:30 -05004877 free_extent_map(em);
4878 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004879}
4880
4881void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4882{
David Sterbaa8067e02011-04-21 00:34:43 +02004883 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004884}
4885
4886void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4887{
4888 struct extent_map *em;
4889
Chris Masond3977122009-01-05 21:25:51 -05004890 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004891 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004892 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4893 if (em)
4894 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004895 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004896 if (!em)
4897 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004898 /* once for us */
4899 free_extent_map(em);
4900 /* once for the tree */
4901 free_extent_map(em);
4902 }
4903}
4904
Stefan Behrens5d964052012-11-05 14:59:07 +01004905int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004906{
Stefan Behrens5d964052012-11-05 14:59:07 +01004907 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004908 struct extent_map *em;
4909 struct map_lookup *map;
4910 struct extent_map_tree *em_tree = &map_tree->map_tree;
4911 int ret;
4912
Chris Mason890871b2009-09-02 16:24:52 -04004913 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004914 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004915 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004916
Josef Bacikfb7669b2013-04-23 10:53:18 -04004917 /*
4918 * We could return errors for these cases, but that could get ugly and
4919 * we'd probably do the same thing which is just not do anything else
4920 * and exit, so return 1 so the callers don't try to use other copies.
4921 */
4922 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004923 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004924 logical+len);
4925 return 1;
4926 }
4927
4928 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004929 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004930 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004931 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004932 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004933 return 1;
4934 }
4935
Chris Masonf1885912008-04-09 16:28:12 -04004936 map = (struct map_lookup *)em->bdev;
4937 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4938 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004939 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4940 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004941 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4942 ret = 2;
4943 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4944 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004945 else
4946 ret = 1;
4947 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004948
4949 btrfs_dev_replace_lock(&fs_info->dev_replace);
4950 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4951 ret++;
4952 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4953
Chris Masonf1885912008-04-09 16:28:12 -04004954 return ret;
4955}
4956
David Woodhouse53b381b2013-01-29 18:40:14 -05004957unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4958 struct btrfs_mapping_tree *map_tree,
4959 u64 logical)
4960{
4961 struct extent_map *em;
4962 struct map_lookup *map;
4963 struct extent_map_tree *em_tree = &map_tree->map_tree;
4964 unsigned long len = root->sectorsize;
4965
4966 read_lock(&em_tree->lock);
4967 em = lookup_extent_mapping(em_tree, logical, len);
4968 read_unlock(&em_tree->lock);
4969 BUG_ON(!em);
4970
4971 BUG_ON(em->start > logical || em->start + em->len < logical);
4972 map = (struct map_lookup *)em->bdev;
Zhao Leiffe2d202015-01-20 15:11:44 +08004973 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05004974 len = map->stripe_len * nr_data_stripes(map);
David Woodhouse53b381b2013-01-29 18:40:14 -05004975 free_extent_map(em);
4976 return len;
4977}
4978
4979int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4980 u64 logical, u64 len, int mirror_num)
4981{
4982 struct extent_map *em;
4983 struct map_lookup *map;
4984 struct extent_map_tree *em_tree = &map_tree->map_tree;
4985 int ret = 0;
4986
4987 read_lock(&em_tree->lock);
4988 em = lookup_extent_mapping(em_tree, logical, len);
4989 read_unlock(&em_tree->lock);
4990 BUG_ON(!em);
4991
4992 BUG_ON(em->start > logical || em->start + em->len < logical);
4993 map = (struct map_lookup *)em->bdev;
Zhao Leiffe2d202015-01-20 15:11:44 +08004994 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05004995 ret = 1;
4996 free_extent_map(em);
4997 return ret;
4998}
4999
Stefan Behrens30d98612012-11-06 14:52:18 +01005000static int find_live_mirror(struct btrfs_fs_info *fs_info,
5001 struct map_lookup *map, int first, int num,
5002 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005003{
5004 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01005005 int tolerance;
5006 struct btrfs_device *srcdev;
5007
5008 if (dev_replace_is_ongoing &&
5009 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5010 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5011 srcdev = fs_info->dev_replace.srcdev;
5012 else
5013 srcdev = NULL;
5014
5015 /*
5016 * try to avoid the drive that is the source drive for a
5017 * dev-replace procedure, only choose it if no other non-missing
5018 * mirror is available
5019 */
5020 for (tolerance = 0; tolerance < 2; tolerance++) {
5021 if (map->stripes[optimal].dev->bdev &&
5022 (tolerance || map->stripes[optimal].dev != srcdev))
5023 return optimal;
5024 for (i = first; i < first + num; i++) {
5025 if (map->stripes[i].dev->bdev &&
5026 (tolerance || map->stripes[i].dev != srcdev))
5027 return i;
5028 }
Chris Masondfe25022008-05-13 13:46:40 -04005029 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005030
Chris Masondfe25022008-05-13 13:46:40 -04005031 /* we couldn't find one that doesn't fail. Just return something
5032 * and the io error handling code will clean up eventually
5033 */
5034 return optimal;
5035}
5036
David Woodhouse53b381b2013-01-29 18:40:14 -05005037static inline int parity_smaller(u64 a, u64 b)
5038{
5039 return a > b;
5040}
5041
5042/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005043static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005044{
5045 struct btrfs_bio_stripe s;
5046 int i;
5047 u64 l;
5048 int again = 1;
5049
5050 while (again) {
5051 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005052 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005053 if (parity_smaller(bbio->raid_map[i],
5054 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005055 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005056 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005057 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005058 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005059 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005060 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005061
David Woodhouse53b381b2013-01-29 18:40:14 -05005062 again = 1;
5063 }
5064 }
5065 }
5066}
5067
Zhao Lei6e9606d2015-01-20 15:11:34 +08005068static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5069{
5070 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005071 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005072 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005073 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005074 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005075 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005076 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005077 /*
5078 * plus the raid_map, which includes both the tgt dev
5079 * and the stripes
5080 */
5081 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005082 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005083
5084 atomic_set(&bbio->error, 0);
5085 atomic_set(&bbio->refs, 1);
5086
5087 return bbio;
5088}
5089
5090void btrfs_get_bbio(struct btrfs_bio *bbio)
5091{
5092 WARN_ON(!atomic_read(&bbio->refs));
5093 atomic_inc(&bbio->refs);
5094}
5095
5096void btrfs_put_bbio(struct btrfs_bio *bbio)
5097{
5098 if (!bbio)
5099 return;
5100 if (atomic_dec_and_test(&bbio->refs))
5101 kfree(bbio);
5102}
5103
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005104static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005105 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005106 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005107 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005108{
5109 struct extent_map *em;
5110 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005111 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04005112 struct extent_map_tree *em_tree = &map_tree->map_tree;
5113 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005114 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005115 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04005116 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005117 u64 stripe_nr_orig;
5118 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05005119 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005120 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005121 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005122 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005123 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005124 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005125 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005126 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005127 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5128 int dev_replace_is_ongoing = 0;
5129 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005130 int patch_the_first_stripe_for_dev_replace = 0;
5131 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005132 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005133
Chris Mason890871b2009-09-02 16:24:52 -04005134 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005135 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04005136 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04005137
Chris Mason3b951512008-04-17 11:29:12 -04005138 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005139 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005140 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04005141 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04005142 }
Chris Mason0b86a832008-03-24 15:01:56 -04005143
Josef Bacik9bb91872013-04-19 23:45:33 -04005144 if (em->start > logical || em->start + em->len < logical) {
5145 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02005146 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04005147 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08005148 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005149 return -EINVAL;
5150 }
5151
Chris Mason0b86a832008-03-24 15:01:56 -04005152 map = (struct map_lookup *)em->bdev;
5153 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005154
David Woodhouse53b381b2013-01-29 18:40:14 -05005155 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005156 stripe_nr = offset;
5157 /*
5158 * stripe_nr counts the total number of stripes we have to stride
5159 * to get to this block
5160 */
David Sterba47c57132015-02-20 18:43:47 +01005161 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005162
David Woodhouse53b381b2013-01-29 18:40:14 -05005163 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005164 BUG_ON(offset < stripe_offset);
5165
5166 /* stripe_offset is the offset of this block in its stripe*/
5167 stripe_offset = offset - stripe_offset;
5168
David Woodhouse53b381b2013-01-29 18:40:14 -05005169 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005170 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005171 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5172 raid56_full_stripe_start = offset;
5173
5174 /* allow a write of a full stripe, but make sure we don't
5175 * allow straddling of stripes
5176 */
David Sterba47c57132015-02-20 18:43:47 +01005177 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5178 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005179 raid56_full_stripe_start *= full_stripe_len;
5180 }
5181
5182 if (rw & REQ_DISCARD) {
5183 /* we don't discard raid56 yet */
Zhao Leiffe2d202015-01-20 15:11:44 +08005184 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005185 ret = -EOPNOTSUPP;
5186 goto out;
5187 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005188 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005189 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5190 u64 max_len;
5191 /* For writes to RAID[56], allow a full stripeset across all disks.
5192 For other RAID types and for RAID[56] reads, just allow a single
5193 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005194 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
David Woodhouse53b381b2013-01-29 18:40:14 -05005195 (rw & REQ_WRITE)) {
5196 max_len = stripe_len * nr_data_stripes(map) -
5197 (offset - raid56_full_stripe_start);
5198 } else {
5199 /* we limit the length of each bio to what fits in a stripe */
5200 max_len = stripe_len - stripe_offset;
5201 }
5202 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005203 } else {
5204 *length = em->len - offset;
5205 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005206
David Woodhouse53b381b2013-01-29 18:40:14 -05005207 /* This is for when we're called from btrfs_merge_bio_hook() and all
5208 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005209 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005210 goto out;
5211
Stefan Behrens472262f2012-11-06 14:43:46 +01005212 btrfs_dev_replace_lock(dev_replace);
5213 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5214 if (!dev_replace_is_ongoing)
5215 btrfs_dev_replace_unlock(dev_replace);
5216
Stefan Behrensad6d6202012-11-06 15:06:47 +01005217 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
5218 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
5219 dev_replace->tgtdev != NULL) {
5220 /*
5221 * in dev-replace case, for repair case (that's the only
5222 * case where the mirror is selected explicitly when
5223 * calling btrfs_map_block), blocks left of the left cursor
5224 * can also be read from the target drive.
5225 * For REQ_GET_READ_MIRRORS, the target drive is added as
5226 * the last one to the array of stripes. For READ, it also
5227 * needs to be supported using the same mirror number.
5228 * If the requested block is not left of the left cursor,
5229 * EIO is returned. This can happen because btrfs_num_copies()
5230 * returns one more in the dev-replace case.
5231 */
5232 u64 tmp_length = *length;
5233 struct btrfs_bio *tmp_bbio = NULL;
5234 int tmp_num_stripes;
5235 u64 srcdev_devid = dev_replace->srcdev->devid;
5236 int index_srcdev = 0;
5237 int found = 0;
5238 u64 physical_of_found = 0;
5239
5240 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005241 logical, &tmp_length, &tmp_bbio, 0, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005242 if (ret) {
5243 WARN_ON(tmp_bbio != NULL);
5244 goto out;
5245 }
5246
5247 tmp_num_stripes = tmp_bbio->num_stripes;
5248 if (mirror_num > tmp_num_stripes) {
5249 /*
5250 * REQ_GET_READ_MIRRORS does not contain this
5251 * mirror, that means that the requested area
5252 * is not left of the left cursor
5253 */
5254 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005255 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005256 goto out;
5257 }
5258
5259 /*
5260 * process the rest of the function using the mirror_num
5261 * of the source drive. Therefore look it up first.
5262 * At the end, patch the device pointer to the one of the
5263 * target drive.
5264 */
5265 for (i = 0; i < tmp_num_stripes; i++) {
5266 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
5267 /*
5268 * In case of DUP, in order to keep it
5269 * simple, only add the mirror with the
5270 * lowest physical address
5271 */
5272 if (found &&
5273 physical_of_found <=
5274 tmp_bbio->stripes[i].physical)
5275 continue;
5276 index_srcdev = i;
5277 found = 1;
5278 physical_of_found =
5279 tmp_bbio->stripes[i].physical;
5280 }
5281 }
5282
5283 if (found) {
5284 mirror_num = index_srcdev + 1;
5285 patch_the_first_stripe_for_dev_replace = 1;
5286 physical_to_patch_in_first_stripe = physical_of_found;
5287 } else {
5288 WARN_ON(1);
5289 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005290 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005291 goto out;
5292 }
5293
Zhao Lei6e9606d2015-01-20 15:11:34 +08005294 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005295 } else if (mirror_num > map->num_stripes) {
5296 mirror_num = 0;
5297 }
5298
Chris Masonf2d8d742008-04-21 10:03:05 -04005299 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005300 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005301 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005302 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
David Sterbab8b93ad2015-01-16 17:26:13 +01005303 stripe_nr_end = div_u64(stripe_nr_end, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005304 stripe_end_offset = stripe_nr_end * map->stripe_len -
5305 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005306
Li Dongyangfce3bb92011-03-24 10:24:26 +00005307 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5308 if (rw & REQ_DISCARD)
5309 num_stripes = min_t(u64, map->num_stripes,
5310 stripe_nr_end - stripe_nr_orig);
David Sterba47c57132015-02-20 18:43:47 +01005311 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5312 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005313 if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)))
5314 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005315 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005316 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005317 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005318 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005319 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005320 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005321 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005322 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005323 current->pid % map->num_stripes,
5324 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005325 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005326 }
Chris Mason2fff7342008-04-29 14:12:09 -04005327
Chris Mason611f0e02008-04-03 16:29:03 -04005328 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005329 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005330 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005331 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005332 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005333 } else {
5334 mirror_num = 1;
5335 }
Chris Mason2fff7342008-04-29 14:12:09 -04005336
Chris Mason321aecc2008-04-16 10:49:51 -04005337 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005338 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005339
David Sterba47c57132015-02-20 18:43:47 +01005340 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005341 stripe_index *= map->sub_stripes;
5342
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005343 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005344 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005345 else if (rw & REQ_DISCARD)
5346 num_stripes = min_t(u64, map->sub_stripes *
5347 (stripe_nr_end - stripe_nr_orig),
5348 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005349 else if (mirror_num)
5350 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005351 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005352 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005353 stripe_index = find_live_mirror(fs_info, map,
5354 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005355 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005356 current->pid % map->sub_stripes,
5357 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005358 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005359 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005360
Zhao Leiffe2d202015-01-20 15:11:44 +08005361 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005362 if (need_raid_map &&
Miao Xieaf8e2d12014-10-23 14:42:50 +08005363 ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5364 mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005365 /* push stripe_nr back to the start of the full stripe */
David Sterbab8b93ad2015-01-16 17:26:13 +01005366 stripe_nr = div_u64(raid56_full_stripe_start,
5367 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005368
5369 /* RAID[56] write or recovery. Return all stripes */
5370 num_stripes = map->num_stripes;
5371 max_errors = nr_parity_stripes(map);
5372
David Woodhouse53b381b2013-01-29 18:40:14 -05005373 *length = map->stripe_len;
5374 stripe_index = 0;
5375 stripe_offset = 0;
5376 } else {
5377 /*
5378 * Mirror #0 or #1 means the original data block.
5379 * Mirror #2 is RAID5 parity block.
5380 * Mirror #3 is RAID6 Q block.
5381 */
David Sterba47c57132015-02-20 18:43:47 +01005382 stripe_nr = div_u64_rem(stripe_nr,
5383 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005384 if (mirror_num > 1)
5385 stripe_index = nr_data_stripes(map) +
5386 mirror_num - 2;
5387
5388 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005389 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5390 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005391 if (!(rw & (REQ_WRITE | REQ_DISCARD |
5392 REQ_GET_READ_MIRRORS)) && mirror_num <= 1)
5393 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005394 }
Chris Mason8790d502008-04-03 16:29:03 -04005395 } else {
5396 /*
David Sterba47c57132015-02-20 18:43:47 +01005397 * after this, stripe_nr is the number of stripes on this
5398 * device we have to walk to find the data, and stripe_index is
5399 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005400 */
David Sterba47c57132015-02-20 18:43:47 +01005401 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5402 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005403 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005404 }
Chris Mason593060d2008-03-25 16:50:33 -04005405 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005406
Stefan Behrens472262f2012-11-06 14:43:46 +01005407 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005408 if (dev_replace_is_ongoing) {
5409 if (rw & (REQ_WRITE | REQ_DISCARD))
5410 num_alloc_stripes <<= 1;
5411 if (rw & REQ_GET_READ_MIRRORS)
5412 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005413 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005414 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005415
Zhao Lei6e9606d2015-01-20 15:11:34 +08005416 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005417 if (!bbio) {
5418 ret = -ENOMEM;
5419 goto out;
5420 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005421 if (dev_replace_is_ongoing)
5422 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005423
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005424 /* build raid_map */
Zhao Leiffe2d202015-01-20 15:11:44 +08005425 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK &&
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005426 need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5427 mirror_num > 1)) {
5428 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005429 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005430
5431 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5432 sizeof(struct btrfs_bio_stripe) *
5433 num_alloc_stripes +
5434 sizeof(int) * tgtdev_indexes);
5435
5436 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005437 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005438
5439 /* Fill in the logical address of each stripe */
5440 tmp = stripe_nr * nr_data_stripes(map);
5441 for (i = 0; i < nr_data_stripes(map); i++)
5442 bbio->raid_map[(i+rot) % num_stripes] =
5443 em->start + (tmp + i) * map->stripe_len;
5444
5445 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5446 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5447 bbio->raid_map[(i+rot+1) % num_stripes] =
5448 RAID6_Q_STRIPE;
5449 }
5450
Li Dongyangfce3bb92011-03-24 10:24:26 +00005451 if (rw & REQ_DISCARD) {
David Sterba9d644a62015-02-20 18:42:11 +01005452 u32 factor = 0;
5453 u32 sub_stripes = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005454 u64 stripes_per_dev = 0;
5455 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005456 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005457
5458 if (map->type &
5459 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5460 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5461 sub_stripes = 1;
5462 else
5463 sub_stripes = map->sub_stripes;
5464
5465 factor = map->num_stripes / sub_stripes;
5466 stripes_per_dev = div_u64_rem(stripe_nr_end -
5467 stripe_nr_orig,
5468 factor,
5469 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005470 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5471 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005472 }
5473
Li Dongyangfce3bb92011-03-24 10:24:26 +00005474 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005475 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005476 map->stripes[stripe_index].physical +
5477 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005478 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005479
Li Zefanec9ef7a2011-12-01 14:06:42 +08005480 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5481 BTRFS_BLOCK_GROUP_RAID10)) {
5482 bbio->stripes[i].length = stripes_per_dev *
5483 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005484
Li Zefanec9ef7a2011-12-01 14:06:42 +08005485 if (i / sub_stripes < remaining_stripes)
5486 bbio->stripes[i].length +=
5487 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005488
5489 /*
5490 * Special for the first stripe and
5491 * the last stripe:
5492 *
5493 * |-------|...|-------|
5494 * |----------|
5495 * off end_off
5496 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005497 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005498 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005499 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005500
5501 if (stripe_index >= last_stripe &&
5502 stripe_index <= (last_stripe +
5503 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005504 bbio->stripes[i].length -=
5505 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005506
Li Zefanec9ef7a2011-12-01 14:06:42 +08005507 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005508 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005509 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005510 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005511
5512 stripe_index++;
5513 if (stripe_index == map->num_stripes) {
5514 /* This could only happen for RAID0/10 */
5515 stripe_index = 0;
5516 stripe_nr++;
5517 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005518 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005519 } else {
5520 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005521 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005522 map->stripes[stripe_index].physical +
5523 stripe_offset +
5524 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005525 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005526 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005527 stripe_index++;
5528 }
Chris Mason593060d2008-03-25 16:50:33 -04005529 }
Li Zefande11cc12011-12-01 12:55:47 +08005530
Miao Xied20983b2014-07-03 18:22:13 +08005531 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5532 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005533
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005534 if (bbio->raid_map)
5535 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005536
Miao Xie2c8cdd62014-11-14 16:06:25 +08005537 tgtdev_indexes = 0;
Stefan Behrens472262f2012-11-06 14:43:46 +01005538 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5539 dev_replace->tgtdev != NULL) {
5540 int index_where_to_add;
5541 u64 srcdev_devid = dev_replace->srcdev->devid;
5542
5543 /*
5544 * duplicate the write operations while the dev replace
5545 * procedure is running. Since the copying of the old disk
5546 * to the new disk takes place at run time while the
5547 * filesystem is mounted writable, the regular write
5548 * operations to the old disk have to be duplicated to go
5549 * to the new disk as well.
5550 * Note that device->missing is handled by the caller, and
5551 * that the write to the old disk is already set up in the
5552 * stripes array.
5553 */
5554 index_where_to_add = num_stripes;
5555 for (i = 0; i < num_stripes; i++) {
5556 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5557 /* write to new disk, too */
5558 struct btrfs_bio_stripe *new =
5559 bbio->stripes + index_where_to_add;
5560 struct btrfs_bio_stripe *old =
5561 bbio->stripes + i;
5562
5563 new->physical = old->physical;
5564 new->length = old->length;
5565 new->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005566 bbio->tgtdev_map[i] = index_where_to_add;
Stefan Behrens472262f2012-11-06 14:43:46 +01005567 index_where_to_add++;
5568 max_errors++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005569 tgtdev_indexes++;
Stefan Behrens472262f2012-11-06 14:43:46 +01005570 }
5571 }
5572 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005573 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5574 dev_replace->tgtdev != NULL) {
5575 u64 srcdev_devid = dev_replace->srcdev->devid;
5576 int index_srcdev = 0;
5577 int found = 0;
5578 u64 physical_of_found = 0;
5579
5580 /*
5581 * During the dev-replace procedure, the target drive can
5582 * also be used to read data in case it is needed to repair
5583 * a corrupt block elsewhere. This is possible if the
5584 * requested area is left of the left cursor. In this area,
5585 * the target drive is a full copy of the source drive.
5586 */
5587 for (i = 0; i < num_stripes; i++) {
5588 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5589 /*
5590 * In case of DUP, in order to keep it
5591 * simple, only add the mirror with the
5592 * lowest physical address
5593 */
5594 if (found &&
5595 physical_of_found <=
5596 bbio->stripes[i].physical)
5597 continue;
5598 index_srcdev = i;
5599 found = 1;
5600 physical_of_found = bbio->stripes[i].physical;
5601 }
5602 }
5603 if (found) {
David Sterba258ece02015-02-24 19:45:15 +01005604 if (physical_of_found + map->stripe_len <=
Stefan Behrensad6d6202012-11-06 15:06:47 +01005605 dev_replace->cursor_left) {
5606 struct btrfs_bio_stripe *tgtdev_stripe =
5607 bbio->stripes + num_stripes;
5608
5609 tgtdev_stripe->physical = physical_of_found;
5610 tgtdev_stripe->length =
5611 bbio->stripes[index_srcdev].length;
5612 tgtdev_stripe->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005613 bbio->tgtdev_map[index_srcdev] = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005614
Miao Xie2c8cdd62014-11-14 16:06:25 +08005615 tgtdev_indexes++;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005616 num_stripes++;
5617 }
5618 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005619 }
5620
Li Zefande11cc12011-12-01 12:55:47 +08005621 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005622 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005623 bbio->num_stripes = num_stripes;
5624 bbio->max_errors = max_errors;
5625 bbio->mirror_num = mirror_num;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005626 bbio->num_tgtdevs = tgtdev_indexes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005627
5628 /*
5629 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5630 * mirror_num == num_stripes + 1 && dev_replace target drive is
5631 * available as a mirror
5632 */
5633 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5634 WARN_ON(num_stripes > 1);
5635 bbio->stripes[0].dev = dev_replace->tgtdev;
5636 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5637 bbio->mirror_num = map->num_stripes + 1;
5638 }
Chris Masoncea9e442008-04-09 16:28:12 -04005639out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005640 if (dev_replace_is_ongoing)
5641 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005642 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005643 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005644}
5645
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005646int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005647 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005648 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005649{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005650 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005651 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005652}
5653
Miao Xieaf8e2d12014-10-23 14:42:50 +08005654/* For Scrub/replace */
5655int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw,
5656 u64 logical, u64 *length,
5657 struct btrfs_bio **bbio_ret, int mirror_num,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005658 int need_raid_map)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005659{
5660 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005661 mirror_num, need_raid_map);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005662}
5663
Yan Zhenga512bbf2008-12-08 16:46:26 -05005664int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5665 u64 chunk_start, u64 physical, u64 devid,
5666 u64 **logical, int *naddrs, int *stripe_len)
5667{
5668 struct extent_map_tree *em_tree = &map_tree->map_tree;
5669 struct extent_map *em;
5670 struct map_lookup *map;
5671 u64 *buf;
5672 u64 bytenr;
5673 u64 length;
5674 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005675 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005676 int i, j, nr = 0;
5677
Chris Mason890871b2009-09-02 16:24:52 -04005678 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005679 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005680 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005681
Josef Bacik835d9742013-03-19 12:13:25 -04005682 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005683 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005684 chunk_start);
5685 return -EIO;
5686 }
5687
5688 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005689 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005690 em->start, chunk_start);
5691 free_extent_map(em);
5692 return -EIO;
5693 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005694 map = (struct map_lookup *)em->bdev;
5695
5696 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005697 rmap_len = map->stripe_len;
5698
Yan Zhenga512bbf2008-12-08 16:46:26 -05005699 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01005700 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005701 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01005702 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08005703 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01005704 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005705 rmap_len = map->stripe_len * nr_data_stripes(map);
5706 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005707
David Sterba31e818f2015-02-20 18:00:26 +01005708 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005709 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005710
5711 for (i = 0; i < map->num_stripes; i++) {
5712 if (devid && map->stripes[i].dev->devid != devid)
5713 continue;
5714 if (map->stripes[i].physical > physical ||
5715 map->stripes[i].physical + length <= physical)
5716 continue;
5717
5718 stripe_nr = physical - map->stripes[i].physical;
David Sterbab8b93ad2015-01-16 17:26:13 +01005719 stripe_nr = div_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005720
5721 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5722 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01005723 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005724 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5725 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005726 } /* else if RAID[56], multiply by nr_data_stripes().
5727 * Alternatively, just use rmap_len below instead of
5728 * map->stripe_len */
5729
5730 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005731 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005732 for (j = 0; j < nr; j++) {
5733 if (buf[j] == bytenr)
5734 break;
5735 }
Chris Mason934d3752008-12-08 16:43:10 -05005736 if (j == nr) {
5737 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005738 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005739 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005740 }
5741
Yan Zhenga512bbf2008-12-08 16:46:26 -05005742 *logical = buf;
5743 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005744 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005745
5746 free_extent_map(em);
5747 return 0;
5748}
5749
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005750static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08005751{
Mike Snitzer326e1db2015-05-22 09:14:03 -04005752 bio->bi_private = bbio->private;
5753 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005754 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04005755
Zhao Lei6e9606d2015-01-20 15:11:34 +08005756 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08005757}
5758
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005759static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04005760{
Chris Mason9be33952013-05-17 18:30:14 -04005761 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005762 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005763
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005764 if (bio->bi_error) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005765 atomic_inc(&bbio->error);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005766 if (bio->bi_error == -EIO || bio->bi_error == -EREMOTEIO) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02005767 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005768 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08005769 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005770
5771 BUG_ON(stripe_index >= bbio->num_stripes);
5772 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005773 if (dev->bdev) {
5774 if (bio->bi_rw & WRITE)
5775 btrfs_dev_stat_inc(dev,
5776 BTRFS_DEV_STAT_WRITE_ERRS);
5777 else
5778 btrfs_dev_stat_inc(dev,
5779 BTRFS_DEV_STAT_READ_ERRS);
5780 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5781 btrfs_dev_stat_inc(dev,
5782 BTRFS_DEV_STAT_FLUSH_ERRS);
5783 btrfs_dev_stat_print_on_error(dev);
5784 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005785 }
5786 }
Chris Mason8790d502008-04-03 16:29:03 -04005787
Jan Schmidta1d3c472011-08-04 17:15:33 +02005788 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005789 is_orig_bio = 1;
5790
Miao Xiec404e0d2014-01-30 16:46:55 +08005791 btrfs_bio_counter_dec(bbio->fs_info);
5792
Jan Schmidta1d3c472011-08-04 17:15:33 +02005793 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005794 if (!is_orig_bio) {
5795 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005796 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005797 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005798
Chris Mason9be33952013-05-17 18:30:14 -04005799 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005800 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005801 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005802 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005803 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005804 bio->bi_error = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005805 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005806 /*
5807 * this bio is actually up to date, we didn't
5808 * go over the max number of errors
5809 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005810 bio->bi_error = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005811 }
Miao Xiec55f1392014-06-19 10:42:54 +08005812
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005813 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005814 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005815 bio_put(bio);
5816 }
Chris Mason8790d502008-04-03 16:29:03 -04005817}
5818
Chris Mason8b712842008-06-11 16:50:36 -04005819/*
5820 * see run_scheduled_bios for a description of why bios are collected for
5821 * async submit.
5822 *
5823 * This will add one bio to the pending list for a device and make sure
5824 * the work struct is scheduled.
5825 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005826static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5827 struct btrfs_device *device,
5828 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005829{
5830 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005831 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005832
David Woodhouse53b381b2013-01-29 18:40:14 -05005833 if (device->missing || !device->bdev) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005834 bio_io_error(bio);
David Woodhouse53b381b2013-01-29 18:40:14 -05005835 return;
5836 }
5837
Chris Mason8b712842008-06-11 16:50:36 -04005838 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005839 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005840 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005841 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005842 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005843 return;
Chris Mason8b712842008-06-11 16:50:36 -04005844 }
5845
5846 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005847 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005848 * higher layers. Otherwise, the async bio makes it appear we have
5849 * made progress against dirty pages when we've really just put it
5850 * on a queue for later
5851 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005852 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005853 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005854 bio->bi_next = NULL;
5855 bio->bi_rw |= rw;
5856
5857 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005858 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005859 pending_bios = &device->pending_sync_bios;
5860 else
5861 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005862
Chris Masonffbd5172009-04-20 15:50:09 -04005863 if (pending_bios->tail)
5864 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005865
Chris Masonffbd5172009-04-20 15:50:09 -04005866 pending_bios->tail = bio;
5867 if (!pending_bios->head)
5868 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005869 if (device->running_pending)
5870 should_queue = 0;
5871
5872 spin_unlock(&device->io_lock);
5873
5874 if (should_queue)
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +08005875 btrfs_queue_work(root->fs_info->submit_workers,
5876 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005877}
5878
Josef Bacikde1ee922012-10-19 16:50:56 -04005879static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5880 struct bio *bio, u64 physical, int dev_nr,
5881 int rw, int async)
5882{
5883 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5884
5885 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005886 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005887 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005888 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005889#ifdef DEBUG
5890 {
5891 struct rcu_string *name;
5892
5893 rcu_read_lock();
5894 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005895 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005896 "(%s id %llu), size=%u\n", rw,
Fabian Frederick1b6e4462014-09-24 20:23:05 +02005897 (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev,
5898 name->str, dev->devid, bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04005899 rcu_read_unlock();
5900 }
5901#endif
5902 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005903
5904 btrfs_bio_counter_inc_noblocked(root->fs_info);
5905
Josef Bacikde1ee922012-10-19 16:50:56 -04005906 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005907 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005908 else
5909 btrfsic_submit_bio(rw, bio);
5910}
5911
Josef Bacikde1ee922012-10-19 16:50:56 -04005912static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5913{
5914 atomic_inc(&bbio->error);
5915 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005916 /* Shoud be the original bio. */
5917 WARN_ON(bio != bbio->orig_bio);
5918
Chris Mason9be33952013-05-17 18:30:14 -04005919 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005920 bio->bi_iter.bi_sector = logical >> 9;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005921 bio->bi_error = -EIO;
5922 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005923 }
5924}
5925
Chris Masonf1885912008-04-09 16:28:12 -04005926int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005927 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005928{
Chris Mason0b86a832008-03-24 15:01:56 -04005929 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005930 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005931 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005932 u64 length = 0;
5933 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04005934 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08005935 int dev_nr;
5936 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005937 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005938
Kent Overstreet4f024f32013-10-11 15:44:27 -07005939 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005940 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005941
Miao Xiec404e0d2014-01-30 16:46:55 +08005942 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005943 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005944 mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08005945 if (ret) {
5946 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005947 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005948 }
Chris Masoncea9e442008-04-09 16:28:12 -04005949
Jan Schmidta1d3c472011-08-04 17:15:33 +02005950 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005951 bbio->orig_bio = first_bio;
5952 bbio->private = first_bio->bi_private;
5953 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005954 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005955 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5956
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005957 if (bbio->raid_map) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005958 /* In this case, map_length has been set to the length of
5959 a single stripe; not the whole write */
5960 if (rw & WRITE) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005961 ret = raid56_parity_write(root, bio, bbio, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005962 } else {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005963 ret = raid56_parity_recover(root, bio, bbio, map_length,
Miao Xie42452152014-11-25 16:39:28 +08005964 mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05005965 }
Miao Xie42452152014-11-25 16:39:28 +08005966
Miao Xiec404e0d2014-01-30 16:46:55 +08005967 btrfs_bio_counter_dec(root->fs_info);
5968 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005969 }
5970
Chris Masoncea9e442008-04-09 16:28:12 -04005971 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005972 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005973 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005974 BUG();
5975 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005976
Zhao Lei08da7572015-02-12 15:42:16 +08005977 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005978 dev = bbio->stripes[dev_nr].dev;
5979 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5980 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04005981 continue;
5982 }
5983
Jan Schmidta1d3c472011-08-04 17:15:33 +02005984 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005985 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005986 BUG_ON(!bio); /* -ENOMEM */
Mike Snitzer326e1db2015-05-22 09:14:03 -04005987 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005988 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04005989
Josef Bacikde1ee922012-10-19 16:50:56 -04005990 submit_stripe_bio(root, bbio, bio,
5991 bbio->stripes[dev_nr].physical, dev_nr, rw,
5992 async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04005993 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005994 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04005995 return 0;
5996}
5997
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005998struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005999 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006000{
Yan Zheng2b820322008-11-17 21:11:30 -05006001 struct btrfs_device *device;
6002 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006003
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006004 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006005 while (cur_devices) {
6006 if (!fsid ||
6007 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
6008 device = __find_device(&cur_devices->devices,
6009 devid, uuid);
6010 if (device)
6011 return device;
6012 }
6013 cur_devices = cur_devices->seed;
6014 }
6015 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006016}
6017
Chris Masondfe25022008-05-13 13:46:40 -04006018static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
Miao Xie5f375832014-09-03 21:35:46 +08006019 struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006020 u64 devid, u8 *dev_uuid)
6021{
6022 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006023
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006024 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6025 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05006026 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006027
6028 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006029 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006030 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006031
6032 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05006033 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006034
Chris Masondfe25022008-05-13 13:46:40 -04006035 return device;
6036}
6037
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006038/**
6039 * btrfs_alloc_device - allocate struct btrfs_device
6040 * @fs_info: used only for generating a new devid, can be NULL if
6041 * devid is provided (i.e. @devid != NULL).
6042 * @devid: a pointer to devid for this device. If NULL a new devid
6043 * is generated.
6044 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6045 * is generated.
6046 *
6047 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6048 * on error. Returned struct is not linked onto any lists and can be
6049 * destroyed with kfree() right away.
6050 */
6051struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6052 const u64 *devid,
6053 const u8 *uuid)
6054{
6055 struct btrfs_device *dev;
6056 u64 tmp;
6057
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306058 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006059 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006060
6061 dev = __alloc_device();
6062 if (IS_ERR(dev))
6063 return dev;
6064
6065 if (devid)
6066 tmp = *devid;
6067 else {
6068 int ret;
6069
6070 ret = find_next_devid(fs_info, &tmp);
6071 if (ret) {
6072 kfree(dev);
6073 return ERR_PTR(ret);
6074 }
6075 }
6076 dev->devid = tmp;
6077
6078 if (uuid)
6079 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6080 else
6081 generate_random_uuid(dev->uuid);
6082
Liu Bo9e0af232014-08-15 23:36:53 +08006083 btrfs_init_work(&dev->work, btrfs_submit_helper,
6084 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006085
6086 return dev;
6087}
6088
Chris Mason0b86a832008-03-24 15:01:56 -04006089static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6090 struct extent_buffer *leaf,
6091 struct btrfs_chunk *chunk)
6092{
6093 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6094 struct map_lookup *map;
6095 struct extent_map *em;
6096 u64 logical;
6097 u64 length;
6098 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006099 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006100 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006101 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006102 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006103
Chris Masone17cade2008-04-15 15:41:47 -04006104 logical = key->offset;
6105 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04006106
Chris Mason890871b2009-09-02 16:24:52 -04006107 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006108 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006109 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006110
6111 /* already mapped? */
6112 if (em && em->start <= logical && em->start + em->len > logical) {
6113 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006114 return 0;
6115 } else if (em) {
6116 free_extent_map(em);
6117 }
Chris Mason0b86a832008-03-24 15:01:56 -04006118
David Sterba172ddd62011-04-21 00:48:27 +02006119 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006120 if (!em)
6121 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006122 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6123 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006124 if (!map) {
6125 free_extent_map(em);
6126 return -ENOMEM;
6127 }
6128
Wang Shilong298a8f92014-06-19 10:42:52 +08006129 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04006130 em->bdev = (struct block_device *)map;
6131 em->start = logical;
6132 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006133 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006134 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006135 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006136
Chris Mason593060d2008-03-25 16:50:33 -04006137 map->num_stripes = num_stripes;
6138 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6139 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6140 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
6141 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6142 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006143 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006144 for (i = 0; i < num_stripes; i++) {
6145 map->stripes[i].physical =
6146 btrfs_stripe_offset_nr(leaf, chunk, i);
6147 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006148 read_extent_buffer(leaf, uuid, (unsigned long)
6149 btrfs_stripe_dev_uuid_nr(chunk, i),
6150 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006151 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
6152 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04006153 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006154 free_extent_map(em);
6155 return -EIO;
6156 }
Chris Masondfe25022008-05-13 13:46:40 -04006157 if (!map->stripes[i].dev) {
6158 map->stripes[i].dev =
Miao Xie5f375832014-09-03 21:35:46 +08006159 add_missing_dev(root, root->fs_info->fs_devices,
6160 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006161 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04006162 free_extent_map(em);
6163 return -EIO;
6164 }
Anand Jain816fceb2015-04-27 12:46:18 +08006165 btrfs_warn(root->fs_info, "devid %llu uuid %pU is missing",
6166 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006167 }
6168 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04006169 }
6170
Chris Mason890871b2009-09-02 16:24:52 -04006171 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006172 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006173 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006174 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006175 free_extent_map(em);
6176
6177 return 0;
6178}
6179
Jeff Mahoney143bede2012-03-01 14:56:26 +01006180static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006181 struct btrfs_dev_item *dev_item,
6182 struct btrfs_device *device)
6183{
6184 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006185
6186 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006187 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6188 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006189 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006190 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006191 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006192 device->type = btrfs_device_type(leaf, dev_item);
6193 device->io_align = btrfs_device_io_align(leaf, dev_item);
6194 device->io_width = btrfs_device_io_width(leaf, dev_item);
6195 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006196 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006197 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006198
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006199 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006200 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006201}
6202
Miao Xie5f375832014-09-03 21:35:46 +08006203static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6204 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006205{
6206 struct btrfs_fs_devices *fs_devices;
6207 int ret;
6208
Li Zefanb367e472011-12-07 11:38:24 +08006209 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05006210
6211 fs_devices = root->fs_info->fs_devices->seed;
6212 while (fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006213 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE))
6214 return fs_devices;
6215
Yan Zheng2b820322008-11-17 21:11:30 -05006216 fs_devices = fs_devices->seed;
6217 }
6218
6219 fs_devices = find_fsid(fsid);
6220 if (!fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006221 if (!btrfs_test_opt(root, DEGRADED))
6222 return ERR_PTR(-ENOENT);
6223
6224 fs_devices = alloc_fs_devices(fsid);
6225 if (IS_ERR(fs_devices))
6226 return fs_devices;
6227
6228 fs_devices->seeding = 1;
6229 fs_devices->opened = 1;
6230 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006231 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006232
6233 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006234 if (IS_ERR(fs_devices))
6235 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006236
Christoph Hellwig97288f22008-12-02 06:36:09 -05006237 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006238 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006239 if (ret) {
6240 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006241 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006242 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006243 }
Yan Zheng2b820322008-11-17 21:11:30 -05006244
6245 if (!fs_devices->seeding) {
6246 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006247 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006248 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006249 goto out;
6250 }
6251
6252 fs_devices->seed = root->fs_info->fs_devices->seed;
6253 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006254out:
Miao Xie5f375832014-09-03 21:35:46 +08006255 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006256}
6257
Chris Mason0d81ba52008-03-24 15:02:07 -04006258static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006259 struct extent_buffer *leaf,
6260 struct btrfs_dev_item *dev_item)
6261{
Miao Xie5f375832014-09-03 21:35:46 +08006262 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006263 struct btrfs_device *device;
6264 u64 devid;
6265 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006266 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006267 u8 dev_uuid[BTRFS_UUID_SIZE];
6268
Chris Mason0b86a832008-03-24 15:01:56 -04006269 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006270 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006271 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006272 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006273 BTRFS_UUID_SIZE);
6274
6275 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
Miao Xie5f375832014-09-03 21:35:46 +08006276 fs_devices = open_seed_devices(root, fs_uuid);
6277 if (IS_ERR(fs_devices))
6278 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006279 }
6280
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006281 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006282 if (!device) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006283 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006284 return -EIO;
6285
Miao Xie5f375832014-09-03 21:35:46 +08006286 device = add_missing_dev(root, fs_devices, devid, dev_uuid);
6287 if (!device)
6288 return -ENOMEM;
Anand Jain33b97e42015-05-08 04:34:35 +08006289 btrfs_warn(root->fs_info, "devid %llu uuid %pU missing",
6290 devid, dev_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006291 } else {
6292 if (!device->bdev && !btrfs_test_opt(root, DEGRADED))
6293 return -EIO;
6294
6295 if(!device->bdev && !device->missing) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006296 /*
6297 * this happens when a device that was properly setup
6298 * in the device info lists suddenly goes bad.
6299 * device->bdev is NULL, and so we have to set
6300 * device->missing to one here
6301 */
Miao Xie5f375832014-09-03 21:35:46 +08006302 device->fs_devices->missing_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05006303 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006304 }
Miao Xie5f375832014-09-03 21:35:46 +08006305
6306 /* Move the device to its own fs_devices */
6307 if (device->fs_devices != fs_devices) {
6308 ASSERT(device->missing);
6309
6310 list_move(&device->dev_list, &fs_devices->devices);
6311 device->fs_devices->num_devices--;
6312 fs_devices->num_devices++;
6313
6314 device->fs_devices->missing_devices--;
6315 fs_devices->missing_devices++;
6316
6317 device->fs_devices = fs_devices;
6318 }
Yan Zheng2b820322008-11-17 21:11:30 -05006319 }
6320
6321 if (device->fs_devices != root->fs_info->fs_devices) {
6322 BUG_ON(device->writeable);
6323 if (device->generation !=
6324 btrfs_device_generation(leaf, dev_item))
6325 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006326 }
Chris Mason0b86a832008-03-24 15:01:56 -04006327
6328 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006329 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006330 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006331 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006332 spin_lock(&root->fs_info->free_chunk_lock);
6333 root->fs_info->free_chunk_space += device->total_bytes -
6334 device->bytes_used;
6335 spin_unlock(&root->fs_info->free_chunk_lock);
6336 }
Chris Mason0b86a832008-03-24 15:01:56 -04006337 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006338 return ret;
6339}
6340
Yan Zhenge4404d62008-12-12 10:03:26 -05006341int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006342{
David Sterba6c417612011-04-13 15:41:04 +02006343 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006344 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006345 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006346 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006347 u8 *array_ptr;
6348 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006349 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006350 u32 num_stripes;
6351 u32 array_size;
6352 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006353 u32 cur_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006354 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006355
David Sterbaa83fffb2014-06-15 02:39:54 +02006356 ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize);
6357 /*
6358 * This will create extent buffer of nodesize, superblock size is
6359 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6360 * overallocate but we can keep it as-is, only the first page is used.
6361 */
6362 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET);
Chris Masona061fc82008-05-07 11:43:44 -04006363 if (!sb)
6364 return -ENOMEM;
6365 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006366 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006367 /*
6368 * The sb extent buffer is artifical and just used to read the system array.
6369 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6370 * pages up-to-date when the page is larger: extent does not cover the
6371 * whole page and consequently check_page_uptodate does not find all
6372 * the page's extents up-to-date (the hole beyond sb),
6373 * write_extent_buffer then triggers a WARN_ON.
6374 *
6375 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6376 * but sb spans only this function. Add an explicit SetPageUptodate call
6377 * to silence the warning eg. on PowerPC 64.
6378 */
6379 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006380 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006381
Chris Masona061fc82008-05-07 11:43:44 -04006382 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006383 array_size = btrfs_super_sys_array_size(super_copy);
6384
David Sterba1ffb22c2014-10-31 19:02:42 +01006385 array_ptr = super_copy->sys_chunk_array;
6386 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6387 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006388
David Sterba1ffb22c2014-10-31 19:02:42 +01006389 while (cur_offset < array_size) {
6390 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006391 len = sizeof(*disk_key);
6392 if (cur_offset + len > array_size)
6393 goto out_short_read;
6394
Chris Mason0b86a832008-03-24 15:01:56 -04006395 btrfs_disk_key_to_cpu(&key, disk_key);
6396
David Sterba1ffb22c2014-10-31 19:02:42 +01006397 array_ptr += len;
6398 sb_array_offset += len;
6399 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006400
Chris Mason0d81ba52008-03-24 15:02:07 -04006401 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006402 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006403 /*
6404 * At least one btrfs_chunk with one stripe must be
6405 * present, exact stripe count check comes afterwards
6406 */
6407 len = btrfs_chunk_item_size(1);
6408 if (cur_offset + len > array_size)
6409 goto out_short_read;
6410
6411 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6412 len = btrfs_chunk_item_size(num_stripes);
6413 if (cur_offset + len > array_size)
6414 goto out_short_read;
6415
Chris Mason0d81ba52008-03-24 15:02:07 -04006416 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006417 if (ret)
6418 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006419 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006420 ret = -EIO;
6421 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006422 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006423 array_ptr += len;
6424 sb_array_offset += len;
6425 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006426 }
Chris Masona061fc82008-05-07 11:43:44 -04006427 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006428 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006429
6430out_short_read:
6431 printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n",
6432 len, cur_offset);
6433 free_extent_buffer(sb);
6434 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006435}
6436
6437int btrfs_read_chunk_tree(struct btrfs_root *root)
6438{
6439 struct btrfs_path *path;
6440 struct extent_buffer *leaf;
6441 struct btrfs_key key;
6442 struct btrfs_key found_key;
6443 int ret;
6444 int slot;
6445
6446 root = root->fs_info->chunk_root;
6447
6448 path = btrfs_alloc_path();
6449 if (!path)
6450 return -ENOMEM;
6451
Li Zefanb367e472011-12-07 11:38:24 +08006452 mutex_lock(&uuid_mutex);
6453 lock_chunks(root);
6454
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006455 /*
6456 * Read all device items, and then all the chunk items. All
6457 * device items are found before any chunk item (their object id
6458 * is smaller than the lowest possible object id for a chunk
6459 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006460 */
6461 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6462 key.offset = 0;
6463 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006464 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006465 if (ret < 0)
6466 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006467 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006468 leaf = path->nodes[0];
6469 slot = path->slots[0];
6470 if (slot >= btrfs_header_nritems(leaf)) {
6471 ret = btrfs_next_leaf(root, path);
6472 if (ret == 0)
6473 continue;
6474 if (ret < 0)
6475 goto error;
6476 break;
6477 }
6478 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006479 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6480 struct btrfs_dev_item *dev_item;
6481 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006482 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006483 ret = read_one_dev(root, leaf, dev_item);
6484 if (ret)
6485 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006486 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6487 struct btrfs_chunk *chunk;
6488 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6489 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006490 if (ret)
6491 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006492 }
6493 path->slots[0]++;
6494 }
Chris Mason0b86a832008-03-24 15:01:56 -04006495 ret = 0;
6496error:
Li Zefanb367e472011-12-07 11:38:24 +08006497 unlock_chunks(root);
6498 mutex_unlock(&uuid_mutex);
6499
Yan Zheng2b820322008-11-17 21:11:30 -05006500 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006501 return ret;
6502}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006503
Miao Xiecb517ea2013-05-15 07:48:19 +00006504void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6505{
6506 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6507 struct btrfs_device *device;
6508
Liu Bo29cc83f2014-05-11 23:14:59 +08006509 while (fs_devices) {
6510 mutex_lock(&fs_devices->device_list_mutex);
6511 list_for_each_entry(device, &fs_devices->devices, dev_list)
6512 device->dev_root = fs_info->dev_root;
6513 mutex_unlock(&fs_devices->device_list_mutex);
6514
6515 fs_devices = fs_devices->seed;
6516 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006517}
6518
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006519static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6520{
6521 int i;
6522
6523 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6524 btrfs_dev_stat_reset(dev, i);
6525}
6526
6527int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6528{
6529 struct btrfs_key key;
6530 struct btrfs_key found_key;
6531 struct btrfs_root *dev_root = fs_info->dev_root;
6532 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6533 struct extent_buffer *eb;
6534 int slot;
6535 int ret = 0;
6536 struct btrfs_device *device;
6537 struct btrfs_path *path = NULL;
6538 int i;
6539
6540 path = btrfs_alloc_path();
6541 if (!path) {
6542 ret = -ENOMEM;
6543 goto out;
6544 }
6545
6546 mutex_lock(&fs_devices->device_list_mutex);
6547 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6548 int item_size;
6549 struct btrfs_dev_stats_item *ptr;
6550
6551 key.objectid = 0;
6552 key.type = BTRFS_DEV_STATS_KEY;
6553 key.offset = device->devid;
6554 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6555 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006556 __btrfs_reset_dev_stats(device);
6557 device->dev_stats_valid = 1;
6558 btrfs_release_path(path);
6559 continue;
6560 }
6561 slot = path->slots[0];
6562 eb = path->nodes[0];
6563 btrfs_item_key_to_cpu(eb, &found_key, slot);
6564 item_size = btrfs_item_size_nr(eb, slot);
6565
6566 ptr = btrfs_item_ptr(eb, slot,
6567 struct btrfs_dev_stats_item);
6568
6569 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6570 if (item_size >= (1 + i) * sizeof(__le64))
6571 btrfs_dev_stat_set(device, i,
6572 btrfs_dev_stats_value(eb, ptr, i));
6573 else
6574 btrfs_dev_stat_reset(device, i);
6575 }
6576
6577 device->dev_stats_valid = 1;
6578 btrfs_dev_stat_print_on_load(device);
6579 btrfs_release_path(path);
6580 }
6581 mutex_unlock(&fs_devices->device_list_mutex);
6582
6583out:
6584 btrfs_free_path(path);
6585 return ret < 0 ? ret : 0;
6586}
6587
6588static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6589 struct btrfs_root *dev_root,
6590 struct btrfs_device *device)
6591{
6592 struct btrfs_path *path;
6593 struct btrfs_key key;
6594 struct extent_buffer *eb;
6595 struct btrfs_dev_stats_item *ptr;
6596 int ret;
6597 int i;
6598
6599 key.objectid = 0;
6600 key.type = BTRFS_DEV_STATS_KEY;
6601 key.offset = device->devid;
6602
6603 path = btrfs_alloc_path();
6604 BUG_ON(!path);
6605 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6606 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006607 printk_in_rcu(KERN_WARNING "BTRFS: "
6608 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006609 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006610 goto out;
6611 }
6612
6613 if (ret == 0 &&
6614 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6615 /* need to delete old one and insert a new one */
6616 ret = btrfs_del_item(trans, dev_root, path);
6617 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006618 printk_in_rcu(KERN_WARNING "BTRFS: "
6619 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006620 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006621 goto out;
6622 }
6623 ret = 1;
6624 }
6625
6626 if (ret == 1) {
6627 /* need to insert a new item */
6628 btrfs_release_path(path);
6629 ret = btrfs_insert_empty_item(trans, dev_root, path,
6630 &key, sizeof(*ptr));
6631 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006632 printk_in_rcu(KERN_WARNING "BTRFS: "
6633 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006634 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006635 goto out;
6636 }
6637 }
6638
6639 eb = path->nodes[0];
6640 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6641 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6642 btrfs_set_dev_stats_value(eb, ptr, i,
6643 btrfs_dev_stat_read(device, i));
6644 btrfs_mark_buffer_dirty(eb);
6645
6646out:
6647 btrfs_free_path(path);
6648 return ret;
6649}
6650
6651/*
6652 * called from commit_transaction. Writes all changed device stats to disk.
6653 */
6654int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6655 struct btrfs_fs_info *fs_info)
6656{
6657 struct btrfs_root *dev_root = fs_info->dev_root;
6658 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6659 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006660 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006661 int ret = 0;
6662
6663 mutex_lock(&fs_devices->device_list_mutex);
6664 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006665 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006666 continue;
6667
Miao Xieaddc3fa2014-07-24 11:37:11 +08006668 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006669 ret = update_dev_stat_item(trans, dev_root, device);
6670 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006671 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006672 }
6673 mutex_unlock(&fs_devices->device_list_mutex);
6674
6675 return ret;
6676}
6677
Stefan Behrens442a4f62012-05-25 16:06:08 +02006678void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6679{
6680 btrfs_dev_stat_inc(dev, index);
6681 btrfs_dev_stat_print_on_error(dev);
6682}
6683
Eric Sandeen48a3b632013-04-25 20:41:01 +00006684static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006685{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006686 if (!dev->dev_stats_valid)
6687 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006688 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6689 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006690 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006691 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6692 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6693 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006694 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6695 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006696}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006697
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006698static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6699{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006700 int i;
6701
6702 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6703 if (btrfs_dev_stat_read(dev, i) != 0)
6704 break;
6705 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6706 return; /* all values == 0, suppress message */
6707
Frank Holtonefe120a2013-12-20 11:37:06 -05006708 printk_in_rcu(KERN_INFO "BTRFS: "
6709 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006710 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006711 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6712 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6713 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6714 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6715 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6716}
6717
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006718int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006719 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006720{
6721 struct btrfs_device *dev;
6722 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6723 int i;
6724
6725 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006726 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006727 mutex_unlock(&fs_devices->device_list_mutex);
6728
6729 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006730 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006731 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006732 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006733 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006734 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006735 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006736 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6737 if (stats->nr_items > i)
6738 stats->values[i] =
6739 btrfs_dev_stat_read_and_reset(dev, i);
6740 else
6741 btrfs_dev_stat_reset(dev, i);
6742 }
6743 } else {
6744 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6745 if (stats->nr_items > i)
6746 stats->values[i] = btrfs_dev_stat_read(dev, i);
6747 }
6748 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6749 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6750 return 0;
6751}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006752
6753int btrfs_scratch_superblock(struct btrfs_device *device)
6754{
6755 struct buffer_head *bh;
6756 struct btrfs_super_block *disk_super;
6757
6758 bh = btrfs_read_dev_super(device->bdev);
6759 if (!bh)
6760 return -EINVAL;
6761 disk_super = (struct btrfs_super_block *)bh->b_data;
6762
6763 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6764 set_buffer_dirty(bh);
6765 sync_dirty_buffer(bh);
6766 brelse(bh);
6767
6768 return 0;
6769}
Miao Xie935e5cc2014-09-03 21:35:33 +08006770
6771/*
6772 * Update the size of all devices, which is used for writing out the
6773 * super blocks.
6774 */
6775void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
6776{
6777 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6778 struct btrfs_device *curr, *next;
6779
6780 if (list_empty(&fs_devices->resized_devices))
6781 return;
6782
6783 mutex_lock(&fs_devices->device_list_mutex);
6784 lock_chunks(fs_info->dev_root);
6785 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
6786 resized_list) {
6787 list_del_init(&curr->resized_list);
6788 curr->commit_total_bytes = curr->disk_total_bytes;
6789 }
6790 unlock_chunks(fs_info->dev_root);
6791 mutex_unlock(&fs_devices->device_list_mutex);
6792}
Miao Xiece7213c2014-09-03 21:35:34 +08006793
6794/* Must be invoked during the transaction commit */
6795void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
6796 struct btrfs_transaction *transaction)
6797{
6798 struct extent_map *em;
6799 struct map_lookup *map;
6800 struct btrfs_device *dev;
6801 int i;
6802
6803 if (list_empty(&transaction->pending_chunks))
6804 return;
6805
6806 /* In order to kick the device replace finish process */
6807 lock_chunks(root);
6808 list_for_each_entry(em, &transaction->pending_chunks, list) {
6809 map = (struct map_lookup *)em->bdev;
6810
6811 for (i = 0; i < map->num_stripes; i++) {
6812 dev = map->stripes[i].dev;
6813 dev->commit_bytes_used = dev->bytes_used;
6814 }
6815 }
6816 unlock_chunks(root);
6817}
Anand Jain5a13f432015-03-10 06:38:31 +08006818
6819void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
6820{
6821 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6822 while (fs_devices) {
6823 fs_devices->fs_info = fs_info;
6824 fs_devices = fs_devices->seed;
6825 }
6826}
6827
6828void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
6829{
6830 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6831 while (fs_devices) {
6832 fs_devices->fs_info = NULL;
6833 fs_devices = fs_devices->seed;
6834 }
6835}