blob: d48b24e54366f3c2c6892d6a7bb0f286c02cb8c2 [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 Masonb765ead2009-04-03 10:27:10 -040023#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000024#include <linux/capability.h>
Stefan Behrens442a4f62012-05-25 16:06:08 +020025#include <linux/ratelimit.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020026#include <linux/kthread.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050027#include <linux/raid/pq.h>
Stefan Behrens803b2f52013-08-15 17:11:21 +020028#include <linux/semaphore.h>
Andy Shevchenko8da4b8c2016-05-20 17:01:00 -070029#include <linux/uuid.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,
Zhao Lei8789f4f2015-09-15 21:08:07 +080051 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080052 .devs_increment = 2,
53 .ncopies = 2,
54 },
55 [BTRFS_RAID_RAID1] = {
56 .sub_stripes = 1,
57 .dev_stripes = 1,
58 .devs_max = 2,
59 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080060 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080061 .devs_increment = 2,
62 .ncopies = 2,
63 },
64 [BTRFS_RAID_DUP] = {
65 .sub_stripes = 1,
66 .dev_stripes = 2,
67 .devs_max = 1,
68 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080069 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080070 .devs_increment = 1,
71 .ncopies = 2,
72 },
73 [BTRFS_RAID_RAID0] = {
74 .sub_stripes = 1,
75 .dev_stripes = 1,
76 .devs_max = 0,
77 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080078 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080079 .devs_increment = 1,
80 .ncopies = 1,
81 },
82 [BTRFS_RAID_SINGLE] = {
83 .sub_stripes = 1,
84 .dev_stripes = 1,
85 .devs_max = 1,
86 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080087 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080088 .devs_increment = 1,
89 .ncopies = 1,
90 },
91 [BTRFS_RAID_RAID5] = {
92 .sub_stripes = 1,
93 .dev_stripes = 1,
94 .devs_max = 0,
95 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080096 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080097 .devs_increment = 1,
98 .ncopies = 2,
99 },
100 [BTRFS_RAID_RAID6] = {
101 .sub_stripes = 1,
102 .dev_stripes = 1,
103 .devs_max = 0,
104 .devs_min = 3,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800105 .tolerated_failures = 2,
Zhao Leiaf902042015-09-15 21:08:06 +0800106 .devs_increment = 1,
107 .ncopies = 3,
108 },
109};
110
Colin Ian Kingfb75d852016-01-19 00:05:28 +0000111const u64 btrfs_raid_group[BTRFS_NR_RAID_TYPES] = {
Zhao Leiaf902042015-09-15 21:08:06 +0800112 [BTRFS_RAID_RAID10] = BTRFS_BLOCK_GROUP_RAID10,
113 [BTRFS_RAID_RAID1] = BTRFS_BLOCK_GROUP_RAID1,
114 [BTRFS_RAID_DUP] = BTRFS_BLOCK_GROUP_DUP,
115 [BTRFS_RAID_RAID0] = BTRFS_BLOCK_GROUP_RAID0,
116 [BTRFS_RAID_SINGLE] = 0,
117 [BTRFS_RAID_RAID5] = BTRFS_BLOCK_GROUP_RAID5,
118 [BTRFS_RAID_RAID6] = BTRFS_BLOCK_GROUP_RAID6,
119};
120
David Sterba621292b2016-02-15 16:28:03 +0100121/*
122 * Table to convert BTRFS_RAID_* to the error code if minimum number of devices
123 * condition is not met. Zero means there's no corresponding
124 * BTRFS_ERROR_DEV_*_NOT_MET value.
125 */
126const int btrfs_raid_mindev_error[BTRFS_NR_RAID_TYPES] = {
127 [BTRFS_RAID_RAID10] = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
128 [BTRFS_RAID_RAID1] = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
129 [BTRFS_RAID_DUP] = 0,
130 [BTRFS_RAID_RAID0] = 0,
131 [BTRFS_RAID_SINGLE] = 0,
132 [BTRFS_RAID_RAID5] = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
133 [BTRFS_RAID_RAID6] = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
134};
135
Yan Zheng2b820322008-11-17 21:11:30 -0500136static int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +0100137 struct btrfs_fs_info *fs_info);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400138static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200139static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000140static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200141static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Liu Bo5ab56092017-03-14 13:33:57 -0700142static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
143 enum btrfs_map_op op,
144 u64 logical, u64 *length,
145 struct btrfs_bio **bbio_ret,
146 int mirror_num, int need_raid_map);
Yan Zheng2b820322008-11-17 21:11:30 -0500147
Miao Xie67a2c452014-09-03 21:35:43 +0800148DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400149static LIST_HEAD(fs_uuids);
Anand Jainc73eccf2015-03-10 06:38:30 +0800150struct list_head *btrfs_get_fs_uuids(void)
151{
152 return &fs_uuids;
153}
Chris Mason8a4b83c2008-03-24 15:02:07 -0400154
David Sterba2dfeca92017-06-14 02:48:07 +0200155/*
156 * alloc_fs_devices - allocate struct btrfs_fs_devices
157 * @fsid: if not NULL, copy the uuid to fs_devices::fsid
158 *
159 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
160 * The returned struct is not linked onto any lists and can be destroyed with
161 * kfree() right away.
162 */
163static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
Ilya Dryomov2208a372013-08-12 14:33:03 +0300164{
165 struct btrfs_fs_devices *fs_devs;
166
David Sterba78f2c9e2016-02-11 14:25:38 +0100167 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300168 if (!fs_devs)
169 return ERR_PTR(-ENOMEM);
170
171 mutex_init(&fs_devs->device_list_mutex);
172
173 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +0800174 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300175 INIT_LIST_HEAD(&fs_devs->alloc_list);
176 INIT_LIST_HEAD(&fs_devs->list);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300177 if (fsid)
178 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300179
180 return fs_devs;
181}
182
Yan Zhenge4404d62008-12-12 10:03:26 -0500183static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
184{
185 struct btrfs_device *device;
186 WARN_ON(fs_devices->opened);
187 while (!list_empty(&fs_devices->devices)) {
188 device = list_entry(fs_devices->devices.next,
189 struct btrfs_device, dev_list);
190 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -0400191 rcu_string_free(device->name);
David Sterba3065ae52017-10-30 18:36:08 +0100192 bio_put(device->flush_bio);
Yan Zhenge4404d62008-12-12 10:03:26 -0500193 kfree(device);
194 }
195 kfree(fs_devices);
196}
197
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000198static void btrfs_kobject_uevent(struct block_device *bdev,
199 enum kobject_action action)
200{
201 int ret;
202
203 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
204 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500205 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000206 action,
207 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
208 &disk_to_dev(bdev->bd_disk)->kobj);
209}
210
Jeff Mahoney143bede2012-03-01 14:56:26 +0100211void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400212{
213 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400214
Yan Zheng2b820322008-11-17 21:11:30 -0500215 while (!list_empty(&fs_uuids)) {
216 fs_devices = list_entry(fs_uuids.next,
217 struct btrfs_fs_devices, list);
218 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500219 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400220 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400221}
222
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300223static struct btrfs_device *__alloc_device(void)
224{
225 struct btrfs_device *dev;
226
David Sterba78f2c9e2016-02-11 14:25:38 +0100227 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300228 if (!dev)
229 return ERR_PTR(-ENOMEM);
230
David Sterbae0ae9992017-06-06 17:06:06 +0200231 /*
232 * Preallocate a bio that's always going to be used for flushing device
233 * barriers and matches the device lifespan
234 */
235 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
236 if (!dev->flush_bio) {
237 kfree(dev);
238 return ERR_PTR(-ENOMEM);
239 }
240 bio_get(dev->flush_bio);
241
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300242 INIT_LIST_HEAD(&dev->dev_list);
243 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800244 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300245
246 spin_lock_init(&dev->io_lock);
247
248 spin_lock_init(&dev->reada_lock);
249 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800250 atomic_set(&dev->dev_stats_ccnt, 0);
Sebastian Andrzej Siewior546bed62016-01-15 14:37:15 +0100251 btrfs_device_data_ordered_init(dev);
Chris Mason9bcaaea2017-05-04 16:08:08 -0700252 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Mel Gormand0164ad2015-11-06 16:28:21 -0800253 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300254
255 return dev;
256}
257
David Sterba35c70102017-06-15 19:51:51 +0200258/*
259 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
260 * return NULL.
261 *
262 * If devid and uuid are both specified, the match must be exact, otherwise
263 * only devid is used.
264 */
265static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
266 u64 devid, const u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400267{
David Sterba35c70102017-06-15 19:51:51 +0200268 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400269 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400270
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500271 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400272 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400273 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400274 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400275 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400276 }
277 return NULL;
278}
279
Chris Masona1b32a52008-09-05 16:09:51 -0400280static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400281{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400282 struct btrfs_fs_devices *fs_devices;
283
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500284 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400285 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
286 return fs_devices;
287 }
288 return NULL;
289}
290
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100291static int
292btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
293 int flush, struct block_device **bdev,
294 struct buffer_head **bh)
295{
296 int ret;
297
298 *bdev = blkdev_get_by_path(device_path, flags, holder);
299
300 if (IS_ERR(*bdev)) {
301 ret = PTR_ERR(*bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100302 goto error;
303 }
304
305 if (flush)
306 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
David Sterba9f6d2512017-06-16 01:48:05 +0200307 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100308 if (ret) {
309 blkdev_put(*bdev, flags);
310 goto error;
311 }
312 invalidate_bdev(*bdev);
313 *bh = btrfs_read_dev_super(*bdev);
Anand Jain92fc03f2015-08-14 18:32:51 +0800314 if (IS_ERR(*bh)) {
315 ret = PTR_ERR(*bh);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100316 blkdev_put(*bdev, flags);
317 goto error;
318 }
319
320 return 0;
321
322error:
323 *bdev = NULL;
324 *bh = NULL;
325 return ret;
326}
327
Chris Masonffbd5172009-04-20 15:50:09 -0400328static void requeue_list(struct btrfs_pending_bios *pending_bios,
329 struct bio *head, struct bio *tail)
330{
331
332 struct bio *old_head;
333
334 old_head = pending_bios->head;
335 pending_bios->head = head;
336 if (pending_bios->tail)
337 tail->bi_next = old_head;
338 else
339 pending_bios->tail = tail;
340}
341
Chris Mason8b712842008-06-11 16:50:36 -0400342/*
343 * we try to collect pending bios for a device so we don't get a large
344 * number of procs sending bios down to the same device. This greatly
345 * improves the schedulers ability to collect and merge the bios.
346 *
347 * But, it also turns into a long list of bios to process and that is sure
348 * to eventually make the worker thread block. The solution here is to
349 * make some progress and then put this work struct back at the end of
350 * the list if the block device is congested. This way, multiple devices
351 * can make progress from a single worker thread.
352 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100353static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400354{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400355 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -0400356 struct bio *pending;
357 struct backing_dev_info *bdi;
Chris Masonffbd5172009-04-20 15:50:09 -0400358 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400359 struct bio *tail;
360 struct bio *cur;
361 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400362 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400363 unsigned long batch_run = 0;
Chris Masonb765ead2009-04-03 10:27:10 -0400364 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400365 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000366 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400367 struct blk_plug plug;
368
369 /*
370 * this function runs all the bios we've collected for
371 * a particular device. We don't want to wander off to
372 * another device without first sending all of these down.
373 * So, setup a plug here and finish it off before we return
374 */
375 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400376
Jan Karaefa7c9f2017-02-02 15:56:53 +0100377 bdi = device->bdev->bd_bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400378
Chris Mason8b712842008-06-11 16:50:36 -0400379loop:
380 spin_lock(&device->io_lock);
381
Chris Masona6837052009-02-04 09:19:41 -0500382loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400383 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400384
Chris Mason8b712842008-06-11 16:50:36 -0400385 /* take all the bios off the list at once and process them
386 * later on (without the lock held). But, remember the
387 * tail and other pointers so the bios can be properly reinserted
388 * into the list if we hit congestion
389 */
Chris Masond84275c2009-06-09 15:39:08 -0400390 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400391 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400392 force_reg = 1;
393 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400394 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400395 force_reg = 0;
396 }
Chris Masonffbd5172009-04-20 15:50:09 -0400397
398 pending = pending_bios->head;
399 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400400 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400401
402 /*
403 * if pending was null this time around, no bios need processing
404 * at all and we can stop. Otherwise it'll loop back up again
405 * and do an additional check so no bios are missed.
406 *
407 * device->running_pending is used to synchronize with the
408 * schedule_bio code.
409 */
Chris Masonffbd5172009-04-20 15:50:09 -0400410 if (device->pending_sync_bios.head == NULL &&
411 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400412 again = 0;
413 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400414 } else {
415 again = 1;
416 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400417 }
Chris Masonffbd5172009-04-20 15:50:09 -0400418
419 pending_bios->head = NULL;
420 pending_bios->tail = NULL;
421
Chris Mason8b712842008-06-11 16:50:36 -0400422 spin_unlock(&device->io_lock);
423
Chris Masond3977122009-01-05 21:25:51 -0500424 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400425
426 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400427 /* we want to work on both lists, but do more bios on the
428 * sync list than the regular list
429 */
430 if ((num_run > 32 &&
431 pending_bios != &device->pending_sync_bios &&
432 device->pending_sync_bios.head) ||
433 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
434 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400435 spin_lock(&device->io_lock);
436 requeue_list(pending_bios, pending, tail);
437 goto loop_lock;
438 }
439
Chris Mason8b712842008-06-11 16:50:36 -0400440 cur = pending;
441 pending = pending->bi_next;
442 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400443
Jens Axboedac56212015-04-17 16:23:59 -0600444 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400445
Chris Mason2ab1ba62011-08-04 14:28:36 -0400446 /*
447 * if we're doing the sync list, record that our
448 * plug has some sync requests on it
449 *
450 * If we're doing the regular list and there are
451 * sync requests sitting around, unplug before
452 * we add more
453 */
454 if (pending_bios == &device->pending_sync_bios) {
455 sync_pending = 1;
456 } else if (sync_pending) {
457 blk_finish_plug(&plug);
458 blk_start_plug(&plug);
459 sync_pending = 0;
460 }
461
Mike Christie4e49ea42016-06-05 14:31:41 -0500462 btrfsic_submit_bio(cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400463 num_run++;
464 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100465
466 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400467
468 /*
469 * we made progress, there is more work to do and the bdi
470 * is now congested. Back off and let other work structs
471 * run instead
472 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400473 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500474 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400475 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400476
Chris Masonb765ead2009-04-03 10:27:10 -0400477 ioc = current->io_context;
478
479 /*
480 * the main goal here is that we don't want to
481 * block if we're going to be able to submit
482 * more requests without blocking.
483 *
484 * This code does two great things, it pokes into
485 * the elevator code from a filesystem _and_
486 * it makes assumptions about how batching works.
487 */
488 if (ioc && ioc->nr_batch_requests > 0 &&
489 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
490 (last_waited == 0 ||
491 ioc->last_waited == last_waited)) {
492 /*
493 * we want to go through our batch of
494 * requests and stop. So, we copy out
495 * the ioc->last_waited time and test
496 * against it before looping
497 */
498 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100499 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400500 continue;
501 }
Chris Mason8b712842008-06-11 16:50:36 -0400502 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400503 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500504 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400505
506 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800507 btrfs_queue_work(fs_info->submit_workers,
508 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400509 goto done;
510 }
511 }
Chris Masonffbd5172009-04-20 15:50:09 -0400512
Chris Mason51684082010-03-10 15:33:32 -0500513 cond_resched();
514 if (again)
515 goto loop;
516
517 spin_lock(&device->io_lock);
518 if (device->pending_bios.head || device->pending_sync_bios.head)
519 goto loop_lock;
520 spin_unlock(&device->io_lock);
521
Chris Mason8b712842008-06-11 16:50:36 -0400522done:
Chris Mason211588a2011-04-19 20:12:40 -0400523 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400524}
525
Christoph Hellwigb2950862008-12-02 09:54:17 -0500526static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400527{
528 struct btrfs_device *device;
529
530 device = container_of(work, struct btrfs_device, work);
531 run_scheduled_bios(device);
532}
533
Anand Jain4fde46f2015-06-17 21:10:48 +0800534
Omar Sandovalc9162bd2017-08-22 23:46:04 -0700535static void btrfs_free_stale_device(struct btrfs_device *cur_dev)
Anand Jain4fde46f2015-06-17 21:10:48 +0800536{
537 struct btrfs_fs_devices *fs_devs;
538 struct btrfs_device *dev;
539
540 if (!cur_dev->name)
541 return;
542
543 list_for_each_entry(fs_devs, &fs_uuids, list) {
544 int del = 1;
545
546 if (fs_devs->opened)
547 continue;
548 if (fs_devs->seeding)
549 continue;
550
551 list_for_each_entry(dev, &fs_devs->devices, dev_list) {
552
553 if (dev == cur_dev)
554 continue;
555 if (!dev->name)
556 continue;
557
558 /*
559 * Todo: This won't be enough. What if the same device
560 * comes back (with new uuid and) with its mapper path?
561 * But for now, this does help as mostly an admin will
562 * either use mapper or non mapper path throughout.
563 */
564 rcu_read_lock();
565 del = strcmp(rcu_str_deref(dev->name),
566 rcu_str_deref(cur_dev->name));
567 rcu_read_unlock();
568 if (!del)
569 break;
570 }
571
572 if (!del) {
573 /* delete the stale device */
574 if (fs_devs->num_devices == 1) {
575 btrfs_sysfs_remove_fsid(fs_devs);
576 list_del(&fs_devs->list);
577 free_fs_devices(fs_devs);
578 } else {
579 fs_devs->num_devices--;
580 list_del(&dev->dev_list);
581 rcu_string_free(dev->name);
David Sterba3065ae52017-10-30 18:36:08 +0100582 bio_put(dev->flush_bio);
Anand Jain4fde46f2015-06-17 21:10:48 +0800583 kfree(dev);
584 }
585 break;
586 }
587 }
588}
589
David Sterba60999ca2014-03-26 18:26:36 +0100590/*
591 * Add new device to list of registered devices
592 *
593 * Returns:
594 * 1 - first time device is seen
595 * 0 - device already known
596 * < 0 - error
597 */
Chris Masona1b32a52008-09-05 16:09:51 -0400598static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400599 struct btrfs_super_block *disk_super,
600 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
601{
602 struct btrfs_device *device;
603 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400604 struct rcu_string *name;
David Sterba60999ca2014-03-26 18:26:36 +0100605 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400606 u64 found_transid = btrfs_super_generation(disk_super);
607
608 fs_devices = find_fsid(disk_super->fsid);
609 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300610 fs_devices = alloc_fs_devices(disk_super->fsid);
611 if (IS_ERR(fs_devices))
612 return PTR_ERR(fs_devices);
613
Chris Mason8a4b83c2008-03-24 15:02:07 -0400614 list_add(&fs_devices->list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300615
Chris Mason8a4b83c2008-03-24 15:02:07 -0400616 device = NULL;
617 } else {
David Sterba35c70102017-06-15 19:51:51 +0200618 device = find_device(fs_devices, devid,
619 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400620 }
Miao Xie443f24f2014-07-24 11:37:15 +0800621
Chris Mason8a4b83c2008-03-24 15:02:07 -0400622 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500623 if (fs_devices->opened)
624 return -EBUSY;
625
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300626 device = btrfs_alloc_device(NULL, &devid,
627 disk_super->dev_item.uuid);
628 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400629 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300630 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400631 }
Josef Bacik606686e2012-06-04 14:03:51 -0400632
633 name = rcu_string_strdup(path, GFP_NOFS);
634 if (!name) {
David Sterba3065ae52017-10-30 18:36:08 +0100635 bio_put(device->flush_bio);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400636 kfree(device);
637 return -ENOMEM;
638 }
Josef Bacik606686e2012-06-04 14:03:51 -0400639 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200640
Chris Masone5e9a522009-06-10 15:17:02 -0400641 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000642 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100643 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400644 mutex_unlock(&fs_devices->device_list_mutex);
645
David Sterba60999ca2014-03-26 18:26:36 +0100646 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500647 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400648 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800649 /*
650 * When FS is already mounted.
651 * 1. If you are here and if the device->name is NULL that
652 * means this device was missing at time of FS mount.
653 * 2. If you are here and if the device->name is different
654 * from 'path' that means either
655 * a. The same device disappeared and reappeared with
656 * different name. or
657 * b. The missing-disk-which-was-replaced, has
658 * reappeared now.
659 *
660 * We must allow 1 and 2a above. But 2b would be a spurious
661 * and unintentional.
662 *
663 * Further in case of 1 and 2a above, the disk at 'path'
664 * would have missed some transaction when it was away and
665 * in case of 2a the stale bdev has to be updated as well.
666 * 2b must not be allowed at all time.
667 */
668
669 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700670 * For now, we do allow update to btrfs_fs_device through the
671 * btrfs dev scan cli after FS has been mounted. We're still
672 * tracking a problem where systems fail mount by subvolume id
673 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800674 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700675 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800676 /*
677 * That is if the FS is _not_ mounted and if you
678 * are here, that means there is more than one
679 * disk with same uuid and devid.We keep the one
680 * with larger generation number or the last-in if
681 * generation are equal.
682 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700683 return -EEXIST;
Anand Jain77bdae42014-07-03 18:22:06 +0800684 }
Anand Jainb96de002014-07-03 18:22:05 +0800685
Josef Bacik606686e2012-06-04 14:03:51 -0400686 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000687 if (!name)
688 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400689 rcu_string_free(device->name);
690 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500691 if (device->missing) {
692 fs_devices->missing_devices--;
693 device->missing = 0;
694 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400695 }
696
Anand Jain77bdae42014-07-03 18:22:06 +0800697 /*
698 * Unmount does not free the btrfs_device struct but would zero
699 * generation along with most of the other members. So just update
700 * it back. We need it to pick the disk with largest generation
701 * (as above).
702 */
703 if (!fs_devices->opened)
704 device->generation = found_transid;
705
Anand Jain4fde46f2015-06-17 21:10:48 +0800706 /*
707 * if there is new btrfs on an already registered device,
708 * then remove the stale device entry.
709 */
Anand Jain02feae32016-02-13 10:01:40 +0800710 if (ret > 0)
711 btrfs_free_stale_device(device);
Anand Jain4fde46f2015-06-17 21:10:48 +0800712
Chris Mason8a4b83c2008-03-24 15:02:07 -0400713 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100714
715 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400716}
717
Yan Zhenge4404d62008-12-12 10:03:26 -0500718static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
719{
720 struct btrfs_fs_devices *fs_devices;
721 struct btrfs_device *device;
722 struct btrfs_device *orig_dev;
723
Ilya Dryomov2208a372013-08-12 14:33:03 +0300724 fs_devices = alloc_fs_devices(orig->fsid);
725 if (IS_ERR(fs_devices))
726 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500727
Miao Xieadbbb862014-09-03 21:35:42 +0800728 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400729 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500730
Xiao Guangrong46224702011-04-20 10:08:47 +0000731 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500732 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400733 struct rcu_string *name;
734
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300735 device = btrfs_alloc_device(NULL, &orig_dev->devid,
736 orig_dev->uuid);
737 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500738 goto error;
739
Josef Bacik606686e2012-06-04 14:03:51 -0400740 /*
741 * This is ok to do without rcu read locked because we hold the
742 * uuid mutex so nothing we touch in here is going to disappear.
743 */
Anand Jaine755f782014-06-30 17:12:47 +0800744 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +0100745 name = rcu_string_strdup(orig_dev->name->str,
746 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +0800747 if (!name) {
David Sterba3065ae52017-10-30 18:36:08 +0100748 bio_put(device->flush_bio);
Anand Jaine755f782014-06-30 17:12:47 +0800749 kfree(device);
750 goto error;
751 }
752 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400753 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500754
Yan Zhenge4404d62008-12-12 10:03:26 -0500755 list_add(&device->dev_list, &fs_devices->devices);
756 device->fs_devices = fs_devices;
757 fs_devices->num_devices++;
758 }
Miao Xieadbbb862014-09-03 21:35:42 +0800759 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500760 return fs_devices;
761error:
Miao Xieadbbb862014-09-03 21:35:42 +0800762 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500763 free_fs_devices(fs_devices);
764 return ERR_PTR(-ENOMEM);
765}
766
Eric Sandeen9eaed212014-08-01 18:12:35 -0500767void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400768{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500769 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800770 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500771
Chris Masondfe25022008-05-13 13:46:40 -0400772 mutex_lock(&uuid_mutex);
773again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000774 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500775 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500776 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100777 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800778 (!latest_dev ||
779 device->generation > latest_dev->generation)) {
780 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500781 }
Yan Zheng2b820322008-11-17 21:11:30 -0500782 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500783 }
Yan Zheng2b820322008-11-17 21:11:30 -0500784
Stefan Behrens8dabb742012-11-06 13:15:27 +0100785 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
786 /*
787 * In the first step, keep the device which has
788 * the correct fsid and the devid that is used
789 * for the dev_replace procedure.
790 * In the second step, the dev_replace state is
791 * read from the device tree and it is known
792 * whether the procedure is really active or
793 * not, which means whether this device is
794 * used or whether it should be removed.
795 */
796 if (step == 0 || device->is_tgtdev_for_dev_replace) {
797 continue;
798 }
799 }
Yan Zheng2b820322008-11-17 21:11:30 -0500800 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100801 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500802 device->bdev = NULL;
803 fs_devices->open_devices--;
804 }
805 if (device->writeable) {
806 list_del_init(&device->dev_alloc_list);
807 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100808 if (!device->is_tgtdev_for_dev_replace)
809 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500810 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500811 list_del_init(&device->dev_list);
812 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400813 rcu_string_free(device->name);
David Sterba3065ae52017-10-30 18:36:08 +0100814 bio_put(device->flush_bio);
Yan Zhenge4404d62008-12-12 10:03:26 -0500815 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400816 }
Yan Zheng2b820322008-11-17 21:11:30 -0500817
818 if (fs_devices->seed) {
819 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500820 goto again;
821 }
822
Miao Xie443f24f2014-07-24 11:37:15 +0800823 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500824
Chris Masondfe25022008-05-13 13:46:40 -0400825 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400826}
Chris Masona0af4692008-05-13 16:03:06 -0400827
Xiao Guangrong1f781602011-04-20 10:09:16 +0000828static void __free_device(struct work_struct *work)
829{
830 struct btrfs_device *device;
831
832 device = container_of(work, struct btrfs_device, rcu_work);
Josef Bacik606686e2012-06-04 14:03:51 -0400833 rcu_string_free(device->name);
David Sterbae0ae9992017-06-06 17:06:06 +0200834 bio_put(device->flush_bio);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000835 kfree(device);
836}
837
838static void free_device(struct rcu_head *head)
839{
840 struct btrfs_device *device;
841
842 device = container_of(head, struct btrfs_device, rcu);
843
844 INIT_WORK(&device->rcu_work, __free_device);
845 schedule_work(&device->rcu_work);
846}
847
Anand Jain14238812016-07-22 06:04:53 +0800848static void btrfs_close_bdev(struct btrfs_device *device)
849{
850 if (device->bdev && device->writeable) {
851 sync_blockdev(device->bdev);
852 invalidate_bdev(device->bdev);
853 }
854
855 if (device->bdev)
856 blkdev_put(device->bdev, device->mode);
857}
858
Anand Jain0ccd0522016-09-22 12:56:13 +0800859static void btrfs_prepare_close_one_device(struct btrfs_device *device)
Anand Jainf448341a2016-06-14 18:55:25 +0800860{
861 struct btrfs_fs_devices *fs_devices = device->fs_devices;
862 struct btrfs_device *new_device;
863 struct rcu_string *name;
864
865 if (device->bdev)
866 fs_devices->open_devices--;
867
868 if (device->writeable &&
869 device->devid != BTRFS_DEV_REPLACE_DEVID) {
870 list_del_init(&device->dev_alloc_list);
871 fs_devices->rw_devices--;
872 }
873
874 if (device->missing)
875 fs_devices->missing_devices--;
876
877 new_device = btrfs_alloc_device(NULL, &device->devid,
878 device->uuid);
879 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
880
881 /* Safe because we are under uuid_mutex */
882 if (device->name) {
883 name = rcu_string_strdup(device->name->str, GFP_NOFS);
884 BUG_ON(!name); /* -ENOMEM */
885 rcu_assign_pointer(new_device->name, name);
886 }
887
888 list_replace_rcu(&device->dev_list, &new_device->dev_list);
889 new_device->fs_devices = device->fs_devices;
Anand Jainf448341a2016-06-14 18:55:25 +0800890}
891
Yan Zheng2b820322008-11-17 21:11:30 -0500892static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400893{
Sasha Levin2037a092015-05-12 19:31:37 -0400894 struct btrfs_device *device, *tmp;
Anand Jain0ccd0522016-09-22 12:56:13 +0800895 struct list_head pending_put;
896
897 INIT_LIST_HEAD(&pending_put);
Yan Zhenge4404d62008-12-12 10:03:26 -0500898
Yan Zheng2b820322008-11-17 21:11:30 -0500899 if (--fs_devices->opened > 0)
900 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400901
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000902 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -0400903 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Anand Jain0ccd0522016-09-22 12:56:13 +0800904 btrfs_prepare_close_one_device(device);
905 list_add(&device->dev_list, &pending_put);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400906 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000907 mutex_unlock(&fs_devices->device_list_mutex);
908
Anand Jain0ccd0522016-09-22 12:56:13 +0800909 /*
910 * btrfs_show_devname() is using the device_list_mutex,
911 * sometimes call to blkdev_put() leads vfs calling
912 * into this func. So do put outside of device_list_mutex,
913 * as of now.
914 */
915 while (!list_empty(&pending_put)) {
916 device = list_first_entry(&pending_put,
917 struct btrfs_device, dev_list);
918 list_del(&device->dev_list);
919 btrfs_close_bdev(device);
920 call_rcu(&device->rcu, free_device);
921 }
922
Yan Zhenge4404d62008-12-12 10:03:26 -0500923 WARN_ON(fs_devices->open_devices);
924 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500925 fs_devices->opened = 0;
926 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500927
Chris Mason8a4b83c2008-03-24 15:02:07 -0400928 return 0;
929}
930
Yan Zheng2b820322008-11-17 21:11:30 -0500931int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
932{
Yan Zhenge4404d62008-12-12 10:03:26 -0500933 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500934 int ret;
935
936 mutex_lock(&uuid_mutex);
937 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500938 if (!fs_devices->opened) {
939 seed_devices = fs_devices->seed;
940 fs_devices->seed = NULL;
941 }
Yan Zheng2b820322008-11-17 21:11:30 -0500942 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500943
944 while (seed_devices) {
945 fs_devices = seed_devices;
946 seed_devices = fs_devices->seed;
947 __btrfs_close_devices(fs_devices);
948 free_fs_devices(fs_devices);
949 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000950 /*
951 * Wait for rcu kworkers under __btrfs_close_devices
952 * to finish all blkdev_puts so device is really
953 * free when umount is done.
954 */
955 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500956 return ret;
957}
958
Yan Zhenge4404d62008-12-12 10:03:26 -0500959static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
960 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400961{
Josef Bacikd5e20032011-08-04 14:52:27 +0000962 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400963 struct block_device *bdev;
964 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400965 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +0800966 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -0400967 struct buffer_head *bh;
968 struct btrfs_super_block *disk_super;
Chris Masona0af4692008-05-13 16:03:06 -0400969 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500970 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400971 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400972
Tejun Heod4d77622010-11-13 11:55:18 +0100973 flags |= FMODE_EXCL;
974
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500975 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400976 if (device->bdev)
977 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400978 if (!device->name)
979 continue;
980
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000981 /* Just open everything we can; ignore failures here */
982 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
983 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100984 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400985
986 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000987 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400988 if (devid != device->devid)
989 goto error_brelse;
990
Yan Zheng2b820322008-11-17 21:11:30 -0500991 if (memcmp(device->uuid, disk_super->dev_item.uuid,
992 BTRFS_UUID_SIZE))
993 goto error_brelse;
994
995 device->generation = btrfs_super_generation(disk_super);
Miao Xie443f24f2014-07-24 11:37:15 +0800996 if (!latest_dev ||
997 device->generation > latest_dev->generation)
998 latest_dev = device;
Chris Masona0af4692008-05-13 16:03:06 -0400999
Yan Zheng2b820322008-11-17 21:11:30 -05001000 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
1001 device->writeable = 0;
1002 } else {
1003 device->writeable = !bdev_read_only(bdev);
1004 seeding = 0;
1005 }
1006
Josef Bacikd5e20032011-08-04 14:52:27 +00001007 q = bdev_get_queue(bdev);
Miao Xie90180da2014-09-03 21:35:30 +08001008 if (blk_queue_discard(q))
Josef Bacikd5e20032011-08-04 14:52:27 +00001009 device->can_discard = 1;
Anand Jaine884f4f2017-04-04 18:40:19 +08001010 if (!blk_queue_nonrot(q))
1011 fs_devices->rotating = 1;
Josef Bacikd5e20032011-08-04 14:52:27 +00001012
Chris Mason8a4b83c2008-03-24 15:02:07 -04001013 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001014 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -05001015 device->mode = flags;
1016
Chris Masona0af4692008-05-13 16:03:06 -04001017 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +03001018 if (device->writeable &&
1019 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -05001020 fs_devices->rw_devices++;
1021 list_add(&device->dev_alloc_list,
1022 &fs_devices->alloc_list);
1023 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +00001024 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -04001025 continue;
Chris Masona061fc82008-05-07 11:43:44 -04001026
Chris Masona0af4692008-05-13 16:03:06 -04001027error_brelse:
1028 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +01001029 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -04001030 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001031 }
Chris Masona0af4692008-05-13 16:03:06 -04001032 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +03001033 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -04001034 goto out;
1035 }
Yan Zheng2b820322008-11-17 21:11:30 -05001036 fs_devices->seeding = seeding;
1037 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +08001038 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -05001039 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -04001040out:
Yan Zheng2b820322008-11-17 21:11:30 -05001041 return ret;
1042}
1043
1044int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001045 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001046{
1047 int ret;
1048
1049 mutex_lock(&uuid_mutex);
1050 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001051 fs_devices->opened++;
1052 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001053 } else {
Chris Mason15916de2008-11-19 21:17:22 -05001054 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001055 }
Chris Mason8a4b83c2008-03-24 15:02:07 -04001056 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001057 return ret;
1058}
1059
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001060static void btrfs_release_disk_super(struct page *page)
Anand Jain6cf86a002016-02-13 10:01:29 +08001061{
1062 kunmap(page);
1063 put_page(page);
1064}
1065
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001066static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1067 struct page **page,
1068 struct btrfs_super_block **disk_super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001069{
1070 void *p;
1071 pgoff_t index;
1072
1073 /* make sure our super fits in the device */
1074 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1075 return 1;
1076
1077 /* make sure our super fits in the page */
1078 if (sizeof(**disk_super) > PAGE_SIZE)
1079 return 1;
1080
1081 /* make sure our super doesn't straddle pages on disk */
1082 index = bytenr >> PAGE_SHIFT;
1083 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1084 return 1;
1085
1086 /* pull in the page with our super */
1087 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1088 index, GFP_KERNEL);
1089
1090 if (IS_ERR_OR_NULL(*page))
1091 return 1;
1092
1093 p = kmap(*page);
1094
1095 /* align our pointer to the offset of the super block */
1096 *disk_super = p + (bytenr & ~PAGE_MASK);
1097
1098 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1099 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1100 btrfs_release_disk_super(*page);
1101 return 1;
1102 }
1103
1104 if ((*disk_super)->label[0] &&
1105 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1106 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1107
1108 return 0;
1109}
1110
David Sterba6f60cbd2013-02-15 11:31:02 -07001111/*
1112 * Look for a btrfs signature on a device. This may be called out of the mount path
1113 * and we are not allowed to call set_blocksize during the scan. The superblock
1114 * is read via pagecache
1115 */
Christoph Hellwig97288f22008-12-02 06:36:09 -05001116int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -04001117 struct btrfs_fs_devices **fs_devices_ret)
1118{
1119 struct btrfs_super_block *disk_super;
1120 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001121 struct page *page;
David Sterba6f60cbd2013-02-15 11:31:02 -07001122 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001123 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -04001124 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -04001125 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001126 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001127
David Sterba6f60cbd2013-02-15 11:31:02 -07001128 /*
1129 * we would like to check all the supers, but that would make
1130 * a btrfs mount succeed after a mkfs from a different FS.
1131 * So, we need to add a special mount option to scan for
1132 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1133 */
1134 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001135 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -05001136 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -07001137
1138 bdev = blkdev_get_by_path(path, flags, holder);
David Sterba6f60cbd2013-02-15 11:31:02 -07001139 if (IS_ERR(bdev)) {
1140 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001141 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -07001142 }
1143
Anand Jain6cf86a002016-02-13 10:01:29 +08001144 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super))
David Sterba6f60cbd2013-02-15 11:31:02 -07001145 goto error_bdev_put;
1146
Xiao Guangronga3438322010-01-06 11:48:18 +00001147 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -04001148 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -04001149 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -07001150
Chris Mason8a4b83c2008-03-24 15:02:07 -04001151 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +01001152 if (ret > 0) {
1153 if (disk_super->label[0]) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001154 pr_info("BTRFS: device label %s ", disk_super->label);
David Sterba60999ca2014-03-26 18:26:36 +01001155 } else {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001156 pr_info("BTRFS: device fsid %pU ", disk_super->fsid);
David Sterba60999ca2014-03-26 18:26:36 +01001157 }
1158
Jeff Mahoney62e85572016-09-20 10:05:01 -04001159 pr_cont("devid %llu transid %llu %s\n", devid, transid, path);
David Sterba60999ca2014-03-26 18:26:36 +01001160 ret = 0;
1161 }
Josef Bacik02db0842012-06-21 16:03:58 -04001162 if (!ret && fs_devices_ret)
1163 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001164
Anand Jain6cf86a002016-02-13 10:01:29 +08001165 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001166
1167error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001168 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001169error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001170 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001171 return ret;
1172}
Chris Mason0b86a832008-03-24 15:01:56 -04001173
Miao Xie6d07bce2011-01-05 10:07:31 +00001174/* helper to account the used device space in the range */
1175int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1176 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -04001177{
1178 struct btrfs_key key;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001179 struct btrfs_root *root = device->fs_info->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +00001180 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001181 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +00001182 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001183 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +00001184 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001185 struct extent_buffer *l;
1186
Miao Xie6d07bce2011-01-05 10:07:31 +00001187 *length = 0;
1188
Stefan Behrens63a212a2012-11-05 18:29:28 +01001189 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +00001190 return 0;
1191
Yan Zheng2b820322008-11-17 21:11:30 -05001192 path = btrfs_alloc_path();
1193 if (!path)
1194 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01001195 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04001196
Chris Mason0b86a832008-03-24 15:01:56 -04001197 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001198 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001199 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001200
1201 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001202 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001203 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001204 if (ret > 0) {
1205 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1206 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001207 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001208 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001209
Chris Mason0b86a832008-03-24 15:01:56 -04001210 while (1) {
1211 l = path->nodes[0];
1212 slot = path->slots[0];
1213 if (slot >= btrfs_header_nritems(l)) {
1214 ret = btrfs_next_leaf(root, path);
1215 if (ret == 0)
1216 continue;
1217 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001218 goto out;
1219
1220 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001221 }
1222 btrfs_item_key_to_cpu(l, &key, slot);
1223
1224 if (key.objectid < device->devid)
1225 goto next;
1226
1227 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001228 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001229
David Sterba962a2982014-06-04 18:41:45 +02001230 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001231 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001232
Chris Mason0b86a832008-03-24 15:01:56 -04001233 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001234 extent_end = key.offset + btrfs_dev_extent_length(l,
1235 dev_extent);
1236 if (key.offset <= start && extent_end > end) {
1237 *length = end - start + 1;
1238 break;
1239 } else if (key.offset <= start && extent_end > start)
1240 *length += extent_end - start;
1241 else if (key.offset > start && extent_end <= end)
1242 *length += extent_end - key.offset;
1243 else if (key.offset > start && key.offset <= end) {
1244 *length += end - key.offset + 1;
1245 break;
1246 } else if (key.offset > end)
1247 break;
1248
1249next:
1250 path->slots[0]++;
1251 }
1252 ret = 0;
1253out:
1254 btrfs_free_path(path);
1255 return ret;
1256}
1257
Jeff Mahoney499f3772015-06-15 09:41:17 -04001258static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001259 struct btrfs_device *device,
1260 u64 *start, u64 len)
1261{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001262 struct btrfs_fs_info *fs_info = device->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001263 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001264 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001265 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001266 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001267
Jeff Mahoney499f3772015-06-15 09:41:17 -04001268 if (transaction)
1269 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001270again:
1271 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001272 struct map_lookup *map;
1273 int i;
1274
Jeff Mahoney95617d62015-06-03 10:55:48 -04001275 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04001276 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001277 u64 end;
1278
Josef Bacik6df9a952013-06-27 13:22:46 -04001279 if (map->stripes[i].dev != device)
1280 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001281 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001282 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001283 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001284 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001285 /*
1286 * Make sure that while processing the pinned list we do
1287 * not override our *start with a lower value, because
1288 * we can have pinned chunks that fall within this
1289 * device hole and that have lower physical addresses
1290 * than the pending chunks we processed before. If we
1291 * do not take this special care we can end up getting
1292 * 2 pending chunks that start at the same physical
1293 * device offsets because the end offset of a pinned
1294 * chunk can be equal to the start offset of some
1295 * pending chunk.
1296 */
1297 end = map->stripes[i].physical + em->orig_block_len;
1298 if (end > *start) {
1299 *start = end;
1300 ret = 1;
1301 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001302 }
1303 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001304 if (search_list != &fs_info->pinned_chunks) {
1305 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001306 goto again;
1307 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001308
1309 return ret;
1310}
1311
1312
Chris Mason0b86a832008-03-24 15:01:56 -04001313/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001314 * find_free_dev_extent_start - find free space in the specified device
1315 * @device: the device which we search the free space in
1316 * @num_bytes: the size of the free space that we need
1317 * @search_start: the position from which to begin the search
1318 * @start: store the start of the free space.
1319 * @len: the size of the free space. that we find, or the size
1320 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001321 *
Chris Mason0b86a832008-03-24 15:01:56 -04001322 * this uses a pretty simple search, the expectation is that it is
1323 * called very infrequently and that a given device has a small number
1324 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001325 *
1326 * @start is used to store the start of the free space if we find. But if we
1327 * don't find suitable free space, it will be used to store the start position
1328 * of the max free space.
1329 *
1330 * @len is used to store the size of the free space that we find.
1331 * But if we don't find suitable free space, it is used to store the size of
1332 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001333 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001334int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1335 struct btrfs_device *device, u64 num_bytes,
1336 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001337{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001338 struct btrfs_fs_info *fs_info = device->fs_info;
1339 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001340 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001341 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001342 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001343 u64 hole_size;
1344 u64 max_hole_start;
1345 u64 max_hole_size;
1346 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001347 u64 search_end = device->total_bytes;
1348 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001349 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001350 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001351
1352 /*
1353 * We don't want to overwrite the superblock on the drive nor any area
1354 * used by the boot loader (grub for example), so we make sure to start
1355 * at an offset of at least 1MB.
1356 */
David Sterba0d0c71b2017-06-15 01:30:06 +02001357 search_start = max_t(u64, search_start, SZ_1M);
Chris Mason0b86a832008-03-24 15:01:56 -04001358
Josef Bacik6df9a952013-06-27 13:22:46 -04001359 path = btrfs_alloc_path();
1360 if (!path)
1361 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001362
Miao Xie7bfc8372011-01-05 10:07:26 +00001363 max_hole_start = search_start;
1364 max_hole_size = 0;
1365
Zhao Leif2ab7612015-02-16 18:52:17 +08001366again:
Stefan Behrens63a212a2012-11-05 18:29:28 +01001367 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001368 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001369 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001370 }
1371
David Sterbae4058b52015-11-27 16:31:35 +01001372 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001373 path->search_commit_root = 1;
1374 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001375
Chris Mason0b86a832008-03-24 15:01:56 -04001376 key.objectid = device->devid;
1377 key.offset = search_start;
1378 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001379
Li Zefan125ccb02011-12-08 15:07:24 +08001380 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001381 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001382 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001383 if (ret > 0) {
1384 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1385 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001386 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001387 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001388
Chris Mason0b86a832008-03-24 15:01:56 -04001389 while (1) {
1390 l = path->nodes[0];
1391 slot = path->slots[0];
1392 if (slot >= btrfs_header_nritems(l)) {
1393 ret = btrfs_next_leaf(root, path);
1394 if (ret == 0)
1395 continue;
1396 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001397 goto out;
1398
1399 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001400 }
1401 btrfs_item_key_to_cpu(l, &key, slot);
1402
1403 if (key.objectid < device->devid)
1404 goto next;
1405
1406 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001407 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001408
David Sterba962a2982014-06-04 18:41:45 +02001409 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001410 goto next;
1411
Miao Xie7bfc8372011-01-05 10:07:26 +00001412 if (key.offset > search_start) {
1413 hole_size = key.offset - search_start;
1414
Josef Bacik6df9a952013-06-27 13:22:46 -04001415 /*
1416 * Have to check before we set max_hole_start, otherwise
1417 * we could end up sending back this offset anyway.
1418 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001419 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001420 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001421 hole_size)) {
1422 if (key.offset >= search_start) {
1423 hole_size = key.offset - search_start;
1424 } else {
1425 WARN_ON_ONCE(1);
1426 hole_size = 0;
1427 }
1428 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001429
Miao Xie7bfc8372011-01-05 10:07:26 +00001430 if (hole_size > max_hole_size) {
1431 max_hole_start = search_start;
1432 max_hole_size = hole_size;
1433 }
1434
1435 /*
1436 * If this free space is greater than which we need,
1437 * it must be the max free space that we have found
1438 * until now, so max_hole_start must point to the start
1439 * of this free space and the length of this free space
1440 * is stored in max_hole_size. Thus, we return
1441 * max_hole_start and max_hole_size and go back to the
1442 * caller.
1443 */
1444 if (hole_size >= num_bytes) {
1445 ret = 0;
1446 goto out;
1447 }
1448 }
1449
Chris Mason0b86a832008-03-24 15:01:56 -04001450 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001451 extent_end = key.offset + btrfs_dev_extent_length(l,
1452 dev_extent);
1453 if (extent_end > search_start)
1454 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001455next:
1456 path->slots[0]++;
1457 cond_resched();
1458 }
Chris Mason0b86a832008-03-24 15:01:56 -04001459
liubo38c01b92011-08-02 02:39:03 +00001460 /*
1461 * At this point, search_start should be the end of
1462 * allocated dev extents, and when shrinking the device,
1463 * search_end may be smaller than search_start.
1464 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001465 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001466 hole_size = search_end - search_start;
1467
Jeff Mahoney499f3772015-06-15 09:41:17 -04001468 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001469 hole_size)) {
1470 btrfs_release_path(path);
1471 goto again;
1472 }
Chris Mason0b86a832008-03-24 15:01:56 -04001473
Zhao Leif2ab7612015-02-16 18:52:17 +08001474 if (hole_size > max_hole_size) {
1475 max_hole_start = search_start;
1476 max_hole_size = hole_size;
1477 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001478 }
1479
Miao Xie7bfc8372011-01-05 10:07:26 +00001480 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001481 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001482 ret = -ENOSPC;
1483 else
1484 ret = 0;
1485
1486out:
Yan Zheng2b820322008-11-17 21:11:30 -05001487 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001488 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001489 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001490 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001491 return ret;
1492}
1493
Jeff Mahoney499f3772015-06-15 09:41:17 -04001494int find_free_dev_extent(struct btrfs_trans_handle *trans,
1495 struct btrfs_device *device, u64 num_bytes,
1496 u64 *start, u64 *len)
1497{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001498 /* FIXME use last free of some kind */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001499 return find_free_dev_extent_start(trans->transaction, device,
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001500 num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001501}
1502
Christoph Hellwigb2950862008-12-02 09:54:17 -05001503static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001504 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001505 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001506{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001507 struct btrfs_fs_info *fs_info = device->fs_info;
1508 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001509 int ret;
1510 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001511 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001512 struct btrfs_key found_key;
1513 struct extent_buffer *leaf = NULL;
1514 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001515
1516 path = btrfs_alloc_path();
1517 if (!path)
1518 return -ENOMEM;
1519
1520 key.objectid = device->devid;
1521 key.offset = start;
1522 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001523again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001524 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001525 if (ret > 0) {
1526 ret = btrfs_previous_item(root, path, key.objectid,
1527 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001528 if (ret)
1529 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001530 leaf = path->nodes[0];
1531 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1532 extent = btrfs_item_ptr(leaf, path->slots[0],
1533 struct btrfs_dev_extent);
1534 BUG_ON(found_key.offset > start || found_key.offset +
1535 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001536 key = found_key;
1537 btrfs_release_path(path);
1538 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001539 } else if (ret == 0) {
1540 leaf = path->nodes[0];
1541 extent = btrfs_item_ptr(leaf, path->slots[0],
1542 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001543 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001544 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001545 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001546 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001547
Miao Xie2196d6e2014-09-03 21:35:41 +08001548 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1549
Chris Mason8f18cf12008-04-25 16:53:30 -04001550 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001551 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001552 btrfs_handle_fs_error(fs_info, ret,
1553 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001554 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001555 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001556 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001557out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001558 btrfs_free_path(path);
1559 return ret;
1560}
1561
Eric Sandeen48a3b632013-04-25 20:41:01 +00001562static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1563 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001564 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001565{
1566 int ret;
1567 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001568 struct btrfs_fs_info *fs_info = device->fs_info;
1569 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001570 struct btrfs_dev_extent *extent;
1571 struct extent_buffer *leaf;
1572 struct btrfs_key key;
1573
Chris Masondfe25022008-05-13 13:46:40 -04001574 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001575 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001576 path = btrfs_alloc_path();
1577 if (!path)
1578 return -ENOMEM;
1579
Chris Mason0b86a832008-03-24 15:01:56 -04001580 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001581 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001582 key.type = BTRFS_DEV_EXTENT_KEY;
1583 ret = btrfs_insert_empty_item(trans, root, path, &key,
1584 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001585 if (ret)
1586 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001587
1588 leaf = path->nodes[0];
1589 extent = btrfs_item_ptr(leaf, path->slots[0],
1590 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001591 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1592 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001593 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1594 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001595 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1596
Chris Mason0b86a832008-03-24 15:01:56 -04001597 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1598 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001599out:
Chris Mason0b86a832008-03-24 15:01:56 -04001600 btrfs_free_path(path);
1601 return ret;
1602}
1603
Josef Bacik6df9a952013-06-27 13:22:46 -04001604static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001605{
Josef Bacik6df9a952013-06-27 13:22:46 -04001606 struct extent_map_tree *em_tree;
1607 struct extent_map *em;
1608 struct rb_node *n;
1609 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001610
Josef Bacik6df9a952013-06-27 13:22:46 -04001611 em_tree = &fs_info->mapping_tree.map_tree;
1612 read_lock(&em_tree->lock);
1613 n = rb_last(&em_tree->map);
1614 if (n) {
1615 em = rb_entry(n, struct extent_map, rb_node);
1616 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001617 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001618 read_unlock(&em_tree->lock);
1619
Chris Mason0b86a832008-03-24 15:01:56 -04001620 return ret;
1621}
1622
Ilya Dryomov53f10652013-08-12 14:33:01 +03001623static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1624 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001625{
1626 int ret;
1627 struct btrfs_key key;
1628 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001629 struct btrfs_path *path;
1630
Yan Zheng2b820322008-11-17 21:11:30 -05001631 path = btrfs_alloc_path();
1632 if (!path)
1633 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001634
1635 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1636 key.type = BTRFS_DEV_ITEM_KEY;
1637 key.offset = (u64)-1;
1638
Ilya Dryomov53f10652013-08-12 14:33:01 +03001639 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001640 if (ret < 0)
1641 goto error;
1642
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001643 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001644
Ilya Dryomov53f10652013-08-12 14:33:01 +03001645 ret = btrfs_previous_item(fs_info->chunk_root, path,
1646 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001647 BTRFS_DEV_ITEM_KEY);
1648 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001649 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001650 } else {
1651 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1652 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001653 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001654 }
1655 ret = 0;
1656error:
Yan Zheng2b820322008-11-17 21:11:30 -05001657 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001658 return ret;
1659}
1660
1661/*
1662 * the device information is stored in the chunk root
1663 * the btrfs_device struct should be fully filled in
1664 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001665static int btrfs_add_device(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001666 struct btrfs_fs_info *fs_info,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001667 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001668{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001669 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001670 int ret;
1671 struct btrfs_path *path;
1672 struct btrfs_dev_item *dev_item;
1673 struct extent_buffer *leaf;
1674 struct btrfs_key key;
1675 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001676
Chris Mason0b86a832008-03-24 15:01:56 -04001677 path = btrfs_alloc_path();
1678 if (!path)
1679 return -ENOMEM;
1680
Chris Mason0b86a832008-03-24 15:01:56 -04001681 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1682 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001683 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001684
1685 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001686 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001687 if (ret)
1688 goto out;
1689
1690 leaf = path->nodes[0];
1691 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1692
1693 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001694 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001695 btrfs_set_device_type(leaf, dev_item, device->type);
1696 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1697 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1698 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001699 btrfs_set_device_total_bytes(leaf, dev_item,
1700 btrfs_device_get_disk_total_bytes(device));
1701 btrfs_set_device_bytes_used(leaf, dev_item,
1702 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001703 btrfs_set_device_group(leaf, dev_item, 0);
1704 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1705 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001706 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001707
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001708 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001709 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001710 ptr = btrfs_device_fsid(dev_item);
Anand Jain44880fd2017-07-29 17:50:09 +08001711 write_extent_buffer(leaf, fs_info->fsid, ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001712 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001713
Yan Zheng2b820322008-11-17 21:11:30 -05001714 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001715out:
1716 btrfs_free_path(path);
1717 return ret;
1718}
Chris Mason8f18cf12008-04-25 16:53:30 -04001719
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001720/*
1721 * Function to update ctime/mtime for a given device path.
1722 * Mainly used for ctime/mtime based probe like libblkid.
1723 */
David Sterbada353f62017-02-14 17:55:53 +01001724static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001725{
1726 struct file *filp;
1727
1728 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001729 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001730 return;
1731 file_update_time(filp);
1732 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001733}
1734
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001735static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
Chris Masona061fc82008-05-07 11:43:44 -04001736 struct btrfs_device *device)
1737{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001738 struct btrfs_root *root = fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001739 int ret;
1740 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001741 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001742 struct btrfs_trans_handle *trans;
1743
Chris Masona061fc82008-05-07 11:43:44 -04001744 path = btrfs_alloc_path();
1745 if (!path)
1746 return -ENOMEM;
1747
Yan, Zhenga22285a2010-05-16 10:48:46 -04001748 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001749 if (IS_ERR(trans)) {
1750 btrfs_free_path(path);
1751 return PTR_ERR(trans);
1752 }
Chris Masona061fc82008-05-07 11:43:44 -04001753 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1754 key.type = BTRFS_DEV_ITEM_KEY;
1755 key.offset = device->devid;
1756
1757 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001758 if (ret) {
1759 if (ret > 0)
1760 ret = -ENOENT;
1761 btrfs_abort_transaction(trans, ret);
1762 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001763 goto out;
1764 }
1765
1766 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001767 if (ret) {
1768 btrfs_abort_transaction(trans, ret);
1769 btrfs_end_transaction(trans);
1770 }
1771
Chris Masona061fc82008-05-07 11:43:44 -04001772out:
1773 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001774 if (!ret)
1775 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001776 return ret;
1777}
1778
David Sterba3cc31a02016-02-15 16:00:26 +01001779/*
1780 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1781 * filesystem. It's up to the caller to adjust that number regarding eg. device
1782 * replace.
1783 */
1784static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1785 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001786{
Chris Masona061fc82008-05-07 11:43:44 -04001787 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00001788 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01001789 int i;
Chris Masona061fc82008-05-07 11:43:44 -04001790
Miao Xiede98ced2013-01-29 10:13:12 +00001791 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001792 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00001793
Anand Jainbd45ffb2016-02-13 10:01:34 +08001794 all_avail = fs_info->avail_data_alloc_bits |
1795 fs_info->avail_system_alloc_bits |
1796 fs_info->avail_metadata_alloc_bits;
1797 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08001798
David Sterba418775a2016-02-15 16:28:14 +01001799 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1800 if (!(all_avail & btrfs_raid_group[i]))
1801 continue;
Chris Masona061fc82008-05-07 11:43:44 -04001802
David Sterba418775a2016-02-15 16:28:14 +01001803 if (num_devices < btrfs_raid_array[i].devs_min) {
1804 int ret = btrfs_raid_mindev_error[i];
Chris Masona061fc82008-05-07 11:43:44 -04001805
David Sterba418775a2016-02-15 16:28:14 +01001806 if (ret)
1807 return ret;
1808 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08001809 }
1810
1811 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001812}
1813
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001814static struct btrfs_device * btrfs_find_next_active_device(
1815 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08001816{
1817 struct btrfs_device *next_device;
1818
1819 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1820 if (next_device != device &&
1821 !next_device->missing && next_device->bdev)
1822 return next_device;
1823 }
1824
1825 return NULL;
1826}
1827
1828/*
1829 * Helper function to check if the given device is part of s_bdev / latest_bdev
1830 * and replace it with the provided or the next active device, in the context
1831 * where this function called, there should be always be another device (or
1832 * this_dev) which is active.
1833 */
1834void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info,
1835 struct btrfs_device *device, struct btrfs_device *this_dev)
1836{
1837 struct btrfs_device *next_device;
1838
1839 if (this_dev)
1840 next_device = this_dev;
1841 else
1842 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1843 device);
1844 ASSERT(next_device);
1845
1846 if (fs_info->sb->s_bdev &&
1847 (fs_info->sb->s_bdev == device->bdev))
1848 fs_info->sb->s_bdev = next_device->bdev;
1849
1850 if (fs_info->fs_devices->latest_bdev == device->bdev)
1851 fs_info->fs_devices->latest_bdev = next_device->bdev;
1852}
1853
David Sterbada353f62017-02-14 17:55:53 +01001854int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
1855 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08001856{
1857 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001858 struct btrfs_fs_devices *cur_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001859 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001860 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001861
1862 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001863
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001864 num_devices = fs_info->fs_devices->num_devices;
1865 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
1866 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
Chris Masona061fc82008-05-07 11:43:44 -04001867 WARN_ON(num_devices < 1);
1868 num_devices--;
1869 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001870 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Chris Masona061fc82008-05-07 11:43:44 -04001871
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001872 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08001873 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001874 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001875
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001876 ret = btrfs_find_device_by_devspec(fs_info, devid, device_path,
1877 &device);
Anand Jain24fc5722016-02-13 10:01:36 +08001878 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001879 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001880
Stefan Behrens63a212a2012-11-05 18:29:28 +01001881 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001882 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08001883 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001884 }
1885
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001886 if (device->writeable && fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001887 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08001888 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001889 }
1890
1891 if (device->writeable) {
David Sterba34441362016-10-04 19:34:27 +02001892 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001893 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001894 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02001895 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001896 }
Chris Masona061fc82008-05-07 11:43:44 -04001897
Carey Underwoodd7901552013-03-04 16:37:06 -06001898 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001899 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001900 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001901 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001902 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001903
Stefan Behrens63a212a2012-11-05 18:29:28 +01001904 /*
1905 * TODO: the superblock still includes this device in its num_devices
1906 * counter although write_all_supers() is not locked out. This
1907 * could give a filesystem state which requires a degraded mount.
1908 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001909 ret = btrfs_rm_dev_item(fs_info, device);
Chris Masona061fc82008-05-07 11:43:44 -04001910 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001911 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001912
Yan Zheng2b820322008-11-17 21:11:30 -05001913 device->in_fs_metadata = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001914 btrfs_scrub_cancel_dev(fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001915
1916 /*
1917 * the device list mutex makes sure that we don't change
1918 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001919 * the device supers. Whoever is writing all supers, should
1920 * lock the device list mutex before getting the number of
1921 * devices in the super block (super_copy). Conversely,
1922 * whoever updates the number of devices in the super block
1923 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001924 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001925
1926 cur_devices = device->fs_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001927 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001928 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001929
Yan Zhenge4404d62008-12-12 10:03:26 -05001930 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001931 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001932
Chris Masoncd02dca2010-12-13 14:56:23 -05001933 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001934 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001935
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001936 btrfs_assign_next_active_device(fs_info, device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05001937
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001938 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001939 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001940 /* remove sysfs entry */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001941 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001942 }
Anand Jain99994cd2014-06-03 11:36:00 +08001943
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001944 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
1945 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
1946 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001947
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04001948 /*
1949 * at this point, the device is zero sized and detached from
1950 * the devices list. All that's left is to zero out the old
1951 * supers and free the device.
1952 */
1953 if (device->writeable)
1954 btrfs_scratch_superblocks(device->bdev, device->name->str);
1955
1956 btrfs_close_bdev(device);
1957 call_rcu(&device->rcu, free_device);
1958
Xiao Guangrong1f781602011-04-20 10:09:16 +00001959 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001960 struct btrfs_fs_devices *fs_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001961 fs_devices = fs_info->fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001962 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001963 if (fs_devices->seed == cur_devices) {
1964 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001965 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001966 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001967 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001968 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001969 cur_devices->seed = NULL;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001970 __btrfs_close_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001971 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001972 }
1973
Chris Masona061fc82008-05-07 11:43:44 -04001974out:
1975 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001976 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08001977
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001978error_undo:
1979 if (device->writeable) {
David Sterba34441362016-10-04 19:34:27 +02001980 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001981 list_add(&device->dev_alloc_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001982 &fs_info->fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001983 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02001984 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001985 }
Anand Jain24fc5722016-02-13 10:01:36 +08001986 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001987}
1988
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001989void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
1990 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01001991{
Anand Jaind51908c2014-08-13 14:24:19 +08001992 struct btrfs_fs_devices *fs_devices;
1993
Stefan Behrense93c89c2012-11-05 17:33:06 +01001994 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001995
Anand Jain25e8e912014-08-20 10:56:56 +08001996 /*
1997 * in case of fs with no seed, srcdev->fs_devices will point
1998 * to fs_devices of fs_info. However when the dev being replaced is
1999 * a seed dev it will point to the seed's local fs_devices. In short
2000 * srcdev will have its correct fs_devices in both the cases.
2001 */
2002 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002003
Stefan Behrense93c89c2012-11-05 17:33:06 +01002004 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002005 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002006 fs_devices->num_devices--;
Miao Xie82372bc2014-09-03 21:35:44 +08002007 if (srcdev->missing)
Anand Jaind51908c2014-08-13 14:24:19 +08002008 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002009
Anand Jain48b3b9d2016-04-12 21:36:16 +08002010 if (srcdev->writeable)
Miao Xie82372bc2014-09-03 21:35:44 +08002011 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002012
Miao Xie82372bc2014-09-03 21:35:44 +08002013 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002014 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002015}
2016
2017void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2018 struct btrfs_device *srcdev)
2019{
2020 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002021
Anand Jain48b3b9d2016-04-12 21:36:16 +08002022 if (srcdev->writeable) {
2023 /* zero out the old super if it is writable */
2024 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2025 }
Anand Jain14238812016-07-22 06:04:53 +08002026
2027 btrfs_close_bdev(srcdev);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002028 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002029
Anand Jain94d5f0c2014-08-13 14:24:22 +08002030 /* if this is no devs we rather delete the fs_devices */
2031 if (!fs_devices->num_devices) {
2032 struct btrfs_fs_devices *tmp_fs_devices;
2033
Anand Jain6dd38f82017-10-17 06:53:50 +08002034 /*
2035 * On a mounted FS, num_devices can't be zero unless it's a
2036 * seed. In case of a seed device being replaced, the replace
2037 * target added to the sprout FS, so there will be no more
2038 * device left under the seed FS.
2039 */
2040 ASSERT(fs_devices->seeding);
2041
Anand Jain94d5f0c2014-08-13 14:24:22 +08002042 tmp_fs_devices = fs_info->fs_devices;
2043 while (tmp_fs_devices) {
2044 if (tmp_fs_devices->seed == fs_devices) {
2045 tmp_fs_devices->seed = fs_devices->seed;
2046 break;
2047 }
2048 tmp_fs_devices = tmp_fs_devices->seed;
2049 }
2050 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08002051 __btrfs_close_devices(fs_devices);
2052 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002053 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002054}
2055
2056void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2057 struct btrfs_device *tgtdev)
2058{
Miao Xie67a2c452014-09-03 21:35:43 +08002059 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002060 WARN_ON(!tgtdev);
2061 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002062
Anand Jain32576042015-08-14 18:32:49 +08002063 btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002064
Anand Jain779bf3fe2016-04-18 16:51:23 +08002065 if (tgtdev->bdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002066 fs_info->fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002067
Stefan Behrense93c89c2012-11-05 17:33:06 +01002068 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002069
Anand Jain88acff62016-05-03 17:44:43 +08002070 btrfs_assign_next_active_device(fs_info, tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002071
Stefan Behrense93c89c2012-11-05 17:33:06 +01002072 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002073
2074 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08002075 mutex_unlock(&uuid_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002076
2077 /*
2078 * The update_dev_time() with in btrfs_scratch_superblocks()
2079 * may lead to a call to btrfs_show_devname() which will try
2080 * to hold device_list_mutex. And here this device
2081 * is already out of device list, so we don't have to hold
2082 * the device_list_mutex lock.
2083 */
2084 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002085
2086 btrfs_close_bdev(tgtdev);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002087 call_rcu(&tgtdev->rcu, free_device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002088}
2089
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002090static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002091 const char *device_path,
Eric Sandeen48a3b632013-04-25 20:41:01 +00002092 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002093{
2094 int ret = 0;
2095 struct btrfs_super_block *disk_super;
2096 u64 devid;
2097 u8 *dev_uuid;
2098 struct block_device *bdev;
2099 struct buffer_head *bh;
2100
2101 *device = NULL;
2102 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002103 fs_info->bdev_holder, 0, &bdev, &bh);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002104 if (ret)
2105 return ret;
2106 disk_super = (struct btrfs_super_block *)bh->b_data;
2107 devid = btrfs_stack_device_id(&disk_super->dev_item);
2108 dev_uuid = disk_super->dev_item.uuid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002109 *device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002110 brelse(bh);
2111 if (!*device)
2112 ret = -ENOENT;
2113 blkdev_put(bdev, FMODE_READ);
2114 return ret;
2115}
2116
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002117int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002118 const char *device_path,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002119 struct btrfs_device **device)
2120{
2121 *device = NULL;
2122 if (strcmp(device_path, "missing") == 0) {
2123 struct list_head *devices;
2124 struct btrfs_device *tmp;
2125
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002126 devices = &fs_info->fs_devices->devices;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002127 /*
2128 * It is safe to read the devices since the volume_mutex
2129 * is held by the caller.
2130 */
2131 list_for_each_entry(tmp, devices, dev_list) {
2132 if (tmp->in_fs_metadata && !tmp->bdev) {
2133 *device = tmp;
2134 break;
2135 }
2136 }
2137
Anand Jaind74a6252015-08-14 18:32:56 +08002138 if (!*device)
2139 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002140
2141 return 0;
2142 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002143 return btrfs_find_device_by_path(fs_info, device_path, device);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002144 }
2145}
2146
Yan Zheng2b820322008-11-17 21:11:30 -05002147/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002148 * Lookup a device given by device id, or the path if the id is 0.
2149 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002150int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid,
David Sterbada353f62017-02-14 17:55:53 +01002151 const char *devpath,
2152 struct btrfs_device **device)
Anand Jain24e04742016-02-13 10:01:35 +08002153{
2154 int ret;
2155
David Sterba5c5c0df2016-02-15 16:39:55 +01002156 if (devid) {
Anand Jain24e04742016-02-13 10:01:35 +08002157 ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002158 *device = btrfs_find_device(fs_info, devid, NULL, NULL);
Anand Jain24e04742016-02-13 10:01:35 +08002159 if (!*device)
2160 ret = -ENOENT;
2161 } else {
David Sterba5c5c0df2016-02-15 16:39:55 +01002162 if (!devpath || !devpath[0])
Anand Jainb3d1b152016-02-13 10:01:37 +08002163 return -EINVAL;
2164
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002165 ret = btrfs_find_device_missing_or_by_path(fs_info, devpath,
Anand Jain24e04742016-02-13 10:01:35 +08002166 device);
2167 }
2168 return ret;
2169}
2170
Yan Zheng2b820322008-11-17 21:11:30 -05002171/*
2172 * does all the dirty work required for changing file system's UUID.
2173 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002174static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002175{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002176 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002177 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002178 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002179 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002180 struct btrfs_device *device;
2181 u64 super_flags;
2182
2183 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05002184 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002185 return -EINVAL;
2186
David Sterba2dfeca92017-06-14 02:48:07 +02002187 seed_devices = alloc_fs_devices(NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002188 if (IS_ERR(seed_devices))
2189 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002190
Yan Zhenge4404d62008-12-12 10:03:26 -05002191 old_devices = clone_fs_devices(fs_devices);
2192 if (IS_ERR(old_devices)) {
2193 kfree(seed_devices);
2194 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002195 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002196
Yan Zheng2b820322008-11-17 21:11:30 -05002197 list_add(&old_devices->list, &fs_uuids);
2198
Yan Zhenge4404d62008-12-12 10:03:26 -05002199 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2200 seed_devices->opened = 1;
2201 INIT_LIST_HEAD(&seed_devices->devices);
2202 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002203 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002204
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002205 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002206 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2207 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002208 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002209 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002210
David Sterba34441362016-10-04 19:34:27 +02002211 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002212 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002213 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002214
Yan Zheng2b820322008-11-17 21:11:30 -05002215 fs_devices->seeding = 0;
2216 fs_devices->num_devices = 0;
2217 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002218 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002219 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002220 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002221
2222 generate_random_uuid(fs_devices->fsid);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002223 memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002224 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002225 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002226
Yan Zheng2b820322008-11-17 21:11:30 -05002227 super_flags = btrfs_super_flags(disk_super) &
2228 ~BTRFS_SUPER_FLAG_SEEDING;
2229 btrfs_set_super_flags(disk_super, super_flags);
2230
2231 return 0;
2232}
2233
2234/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002235 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002236 */
2237static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002238 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002239{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002240 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002241 struct btrfs_path *path;
2242 struct extent_buffer *leaf;
2243 struct btrfs_dev_item *dev_item;
2244 struct btrfs_device *device;
2245 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002246 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002247 u8 dev_uuid[BTRFS_UUID_SIZE];
2248 u64 devid;
2249 int ret;
2250
2251 path = btrfs_alloc_path();
2252 if (!path)
2253 return -ENOMEM;
2254
Yan Zheng2b820322008-11-17 21:11:30 -05002255 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2256 key.offset = 0;
2257 key.type = BTRFS_DEV_ITEM_KEY;
2258
2259 while (1) {
2260 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2261 if (ret < 0)
2262 goto error;
2263
2264 leaf = path->nodes[0];
2265next_slot:
2266 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2267 ret = btrfs_next_leaf(root, path);
2268 if (ret > 0)
2269 break;
2270 if (ret < 0)
2271 goto error;
2272 leaf = path->nodes[0];
2273 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002274 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002275 continue;
2276 }
2277
2278 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2279 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2280 key.type != BTRFS_DEV_ITEM_KEY)
2281 break;
2282
2283 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2284 struct btrfs_dev_item);
2285 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002286 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002287 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002288 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002289 BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002290 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002291 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002292
2293 if (device->fs_devices->seeding) {
2294 btrfs_set_device_generation(leaf, dev_item,
2295 device->generation);
2296 btrfs_mark_buffer_dirty(leaf);
2297 }
2298
2299 path->slots[0]++;
2300 goto next_slot;
2301 }
2302 ret = 0;
2303error:
2304 btrfs_free_path(path);
2305 return ret;
2306}
2307
David Sterbada353f62017-02-14 17:55:53 +01002308int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002309{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002310 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002311 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002312 struct btrfs_trans_handle *trans;
2313 struct btrfs_device *device;
2314 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002315 struct list_head *devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002316 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002317 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002318 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002319 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002320 int ret = 0;
Anand Jain7132a262017-09-28 14:51:11 +08002321 bool unlocked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002322
David Howellsbc98a422017-07-17 08:45:34 +01002323 if (sb_rdonly(sb) && !fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002324 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002325
Li Zefana5d16332011-12-07 20:08:40 -05002326 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002327 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002328 if (IS_ERR(bdev))
2329 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002330
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002331 if (fs_info->fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002332 seeding_dev = 1;
2333 down_write(&sb->s_umount);
2334 mutex_lock(&uuid_mutex);
2335 }
2336
Chris Mason8c8bee12008-09-29 11:19:10 -04002337 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002338
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002339 devices = &fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002340
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002341 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002342 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002343 if (device->bdev == bdev) {
2344 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002345 mutex_unlock(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002346 &fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002347 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002348 }
2349 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002350 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002351
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002352 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002353 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002354 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002355 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002356 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002357 }
2358
David Sterba78f2c9e2016-02-11 14:25:38 +01002359 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002360 if (!name) {
David Sterba3065ae52017-10-30 18:36:08 +01002361 bio_put(device->flush_bio);
Chris Mason788f20e2008-04-28 15:29:42 -04002362 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002363 ret = -ENOMEM;
2364 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002365 }
Josef Bacik606686e2012-06-04 14:03:51 -04002366 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002367
Yan, Zhenga22285a2010-05-16 10:48:46 -04002368 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002369 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002370 rcu_string_free(device->name);
David Sterba3065ae52017-10-30 18:36:08 +01002371 bio_put(device->flush_bio);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002372 kfree(device);
2373 ret = PTR_ERR(trans);
2374 goto error;
2375 }
2376
Josef Bacikd5e20032011-08-04 14:52:27 +00002377 q = bdev_get_queue(bdev);
2378 if (blk_queue_discard(q))
2379 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002380 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002381 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002382 device->io_width = fs_info->sectorsize;
2383 device->io_align = fs_info->sectorsize;
2384 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002385 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2386 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002387 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002388 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002389 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002390 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002391 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002392 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002393 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002394 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002395 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002396
Yan Zheng2b820322008-11-17 21:11:30 -05002397 if (seeding_dev) {
2398 sb->s_flags &= ~MS_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002399 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002400 if (ret) {
2401 btrfs_abort_transaction(trans, ret);
2402 goto error_trans;
2403 }
Yan Zheng2b820322008-11-17 21:11:30 -05002404 }
2405
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002406 device->fs_devices = fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002407
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002408 mutex_lock(&fs_info->fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002409 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002410 list_add_rcu(&device->dev_list, &fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002411 list_add(&device->dev_alloc_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002412 &fs_info->fs_devices->alloc_list);
2413 fs_info->fs_devices->num_devices++;
2414 fs_info->fs_devices->open_devices++;
2415 fs_info->fs_devices->rw_devices++;
2416 fs_info->fs_devices->total_devices++;
2417 fs_info->fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002418
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002419 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002420
Anand Jaine884f4f2017-04-04 18:40:19 +08002421 if (!blk_queue_nonrot(q))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002422 fs_info->fs_devices->rotating = 1;
Chris Masonc2898112009-06-10 09:51:32 -04002423
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002424 tmp = btrfs_super_total_bytes(fs_info->super_copy);
2425 btrfs_set_super_total_bytes(fs_info->super_copy,
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002426 round_down(tmp + device->total_bytes, fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002427
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002428 tmp = btrfs_super_num_devices(fs_info->super_copy);
2429 btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002430
2431 /* add sysfs device entry */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002432 btrfs_sysfs_add_device_link(fs_info->fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002433
Miao Xie2196d6e2014-09-03 21:35:41 +08002434 /*
2435 * we've got more storage, clear any full flags on the space
2436 * infos
2437 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002438 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002439
David Sterba34441362016-10-04 19:34:27 +02002440 mutex_unlock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002441 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002442
Yan Zheng2b820322008-11-17 21:11:30 -05002443 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002444 mutex_lock(&fs_info->chunk_mutex);
David Sterbae4a4dce2017-02-10 19:49:01 +01002445 ret = init_first_rw_device(trans, fs_info);
David Sterba34441362016-10-04 19:34:27 +02002446 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002447 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002448 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002449 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002450 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002451 }
2452
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002453 ret = btrfs_add_device(trans, fs_info, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002454 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002455 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002456 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002457 }
2458
2459 if (seeding_dev) {
2460 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2461
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002462 ret = btrfs_finish_sprout(trans, fs_info);
David Sterba005d6422012-09-18 07:52:32 -06002463 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002464 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002465 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002466 }
Anand Jainb2373f22014-06-03 11:36:03 +08002467
2468 /* Sprouting would change fsid of the mounted root,
2469 * so rename the fsid on the sysfs
2470 */
2471 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002472 fs_info->fsid);
2473 if (kobject_rename(&fs_info->fs_devices->fsid_kobj, fsid_buf))
2474 btrfs_warn(fs_info,
2475 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002476 }
2477
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002478 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002479
2480 if (seeding_dev) {
2481 mutex_unlock(&uuid_mutex);
2482 up_write(&sb->s_umount);
Anand Jain7132a262017-09-28 14:51:11 +08002483 unlocked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002484
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002485 if (ret) /* transaction commit */
2486 return ret;
2487
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002488 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002489 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002490 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002491 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002492 trans = btrfs_attach_transaction(root);
2493 if (IS_ERR(trans)) {
2494 if (PTR_ERR(trans) == -ENOENT)
2495 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002496 ret = PTR_ERR(trans);
2497 trans = NULL;
2498 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002499 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002500 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002501 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002502
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002503 /* Update ctime/mtime for libblkid */
2504 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002505 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002506
Anand Jaind31c32f2017-09-28 14:51:10 +08002507error_sysfs:
2508 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002509error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002510 if (seeding_dev)
2511 sb->s_flags |= MS_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002512 if (trans)
2513 btrfs_end_transaction(trans);
Josef Bacik606686e2012-06-04 14:03:51 -04002514 rcu_string_free(device->name);
David Sterba3065ae52017-10-30 18:36:08 +01002515 bio_put(device->flush_bio);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002516 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002517error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002518 blkdev_put(bdev, FMODE_EXCL);
Anand Jain7132a262017-09-28 14:51:11 +08002519 if (seeding_dev && !unlocked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002520 mutex_unlock(&uuid_mutex);
2521 up_write(&sb->s_umount);
2522 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002523 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002524}
2525
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002526int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002527 const char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002528 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002529 struct btrfs_device **device_out)
2530{
2531 struct request_queue *q;
2532 struct btrfs_device *device;
2533 struct block_device *bdev;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002534 struct list_head *devices;
2535 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002536 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002537 int ret = 0;
2538
2539 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002540 if (fs_info->fs_devices->seeding) {
2541 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002542 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002543 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002544
2545 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2546 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002547 if (IS_ERR(bdev)) {
2548 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002549 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002550 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002551
2552 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2553
2554 devices = &fs_info->fs_devices->devices;
2555 list_for_each_entry(device, devices, dev_list) {
2556 if (device->bdev == bdev) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002557 btrfs_err(fs_info,
2558 "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002559 ret = -EEXIST;
2560 goto error;
2561 }
2562 }
2563
Miao Xie1c433662014-09-03 21:35:32 +08002564
Miao Xie7cc8e582014-09-03 21:35:38 +08002565 if (i_size_read(bdev->bd_inode) <
2566 btrfs_device_get_total_bytes(srcdev)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002567 btrfs_err(fs_info,
2568 "target device is smaller than source device!");
Miao Xie1c433662014-09-03 21:35:32 +08002569 ret = -EINVAL;
2570 goto error;
2571 }
2572
2573
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002574 device = btrfs_alloc_device(NULL, &devid, NULL);
2575 if (IS_ERR(device)) {
2576 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002577 goto error;
2578 }
2579
David Sterba61655722017-06-15 17:16:43 +02002580 name = rcu_string_strdup(device_path, GFP_KERNEL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002581 if (!name) {
David Sterba3065ae52017-10-30 18:36:08 +01002582 bio_put(device->flush_bio);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002583 kfree(device);
2584 ret = -ENOMEM;
2585 goto error;
2586 }
2587 rcu_assign_pointer(device->name, name);
2588
2589 q = bdev_get_queue(bdev);
2590 if (blk_queue_discard(q))
2591 device->can_discard = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002592 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002593 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002594 device->generation = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002595 device->io_width = fs_info->sectorsize;
2596 device->io_align = fs_info->sectorsize;
2597 device->sector_size = fs_info->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002598 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2599 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2600 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002601 ASSERT(list_empty(&srcdev->resized_list));
2602 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002603 device->commit_bytes_used = device->bytes_used;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002604 device->fs_info = fs_info;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002605 device->bdev = bdev;
2606 device->in_fs_metadata = 1;
2607 device->is_tgtdev_for_dev_replace = 1;
2608 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002609 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002610 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002611 device->fs_devices = fs_info->fs_devices;
2612 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2613 fs_info->fs_devices->num_devices++;
2614 fs_info->fs_devices->open_devices++;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002615 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002616
2617 *device_out = device;
2618 return ret;
2619
2620error:
2621 blkdev_put(bdev, FMODE_EXCL);
2622 return ret;
2623}
2624
2625void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2626 struct btrfs_device *tgtdev)
2627{
Jeff Mahoneyda170662016-06-15 09:22:56 -04002628 u32 sectorsize = fs_info->sectorsize;
2629
Stefan Behrense93c89c2012-11-05 17:33:06 +01002630 WARN_ON(fs_info->fs_devices->rw_devices == 0);
Jeff Mahoneyda170662016-06-15 09:22:56 -04002631 tgtdev->io_width = sectorsize;
2632 tgtdev->io_align = sectorsize;
2633 tgtdev->sector_size = sectorsize;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002634 tgtdev->fs_info = fs_info;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002635 tgtdev->in_fs_metadata = 1;
2636}
2637
Chris Masond3977122009-01-05 21:25:51 -05002638static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2639 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002640{
2641 int ret;
2642 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002643 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002644 struct btrfs_dev_item *dev_item;
2645 struct extent_buffer *leaf;
2646 struct btrfs_key key;
2647
Chris Mason0b86a832008-03-24 15:01:56 -04002648 path = btrfs_alloc_path();
2649 if (!path)
2650 return -ENOMEM;
2651
2652 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2653 key.type = BTRFS_DEV_ITEM_KEY;
2654 key.offset = device->devid;
2655
2656 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2657 if (ret < 0)
2658 goto out;
2659
2660 if (ret > 0) {
2661 ret = -ENOENT;
2662 goto out;
2663 }
2664
2665 leaf = path->nodes[0];
2666 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2667
2668 btrfs_set_device_id(leaf, dev_item, device->devid);
2669 btrfs_set_device_type(leaf, dev_item, device->type);
2670 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2671 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2672 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002673 btrfs_set_device_total_bytes(leaf, dev_item,
2674 btrfs_device_get_disk_total_bytes(device));
2675 btrfs_set_device_bytes_used(leaf, dev_item,
2676 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002677 btrfs_mark_buffer_dirty(leaf);
2678
2679out:
2680 btrfs_free_path(path);
2681 return ret;
2682}
2683
Miao Xie2196d6e2014-09-03 21:35:41 +08002684int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002685 struct btrfs_device *device, u64 new_size)
2686{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002687 struct btrfs_fs_info *fs_info = device->fs_info;
2688 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002689 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002690 u64 old_total;
2691 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002692
Yan Zheng2b820322008-11-17 21:11:30 -05002693 if (!device->writeable)
2694 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002695
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002696 new_size = round_down(new_size, fs_info->sectorsize);
2697
David Sterba34441362016-10-04 19:34:27 +02002698 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002699 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002700 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002701
Stefan Behrens63a212a2012-11-05 18:29:28 +01002702 if (new_size <= device->total_bytes ||
Miao Xie2196d6e2014-09-03 21:35:41 +08002703 device->is_tgtdev_for_dev_replace) {
David Sterba34441362016-10-04 19:34:27 +02002704 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002705 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002706 }
Yan Zheng2b820322008-11-17 21:11:30 -05002707
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002708 fs_devices = fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002709
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002710 btrfs_set_super_total_bytes(super_copy,
2711 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002712 device->fs_devices->total_rw_bytes += diff;
2713
Miao Xie7cc8e582014-09-03 21:35:38 +08002714 btrfs_device_set_total_bytes(device, new_size);
2715 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002716 btrfs_clear_space_info_full(device->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002717 if (list_empty(&device->resized_list))
2718 list_add_tail(&device->resized_list,
2719 &fs_devices->resized_devices);
David Sterba34441362016-10-04 19:34:27 +02002720 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002721
Chris Mason8f18cf12008-04-25 16:53:30 -04002722 return btrfs_update_device(trans, device);
2723}
2724
2725static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002726 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002727{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002728 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002729 int ret;
2730 struct btrfs_path *path;
2731 struct btrfs_key key;
2732
Chris Mason8f18cf12008-04-25 16:53:30 -04002733 path = btrfs_alloc_path();
2734 if (!path)
2735 return -ENOMEM;
2736
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002737 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002738 key.offset = chunk_offset;
2739 key.type = BTRFS_CHUNK_ITEM_KEY;
2740
2741 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002742 if (ret < 0)
2743 goto out;
2744 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002745 btrfs_handle_fs_error(fs_info, -ENOENT,
2746 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002747 ret = -ENOENT;
2748 goto out;
2749 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002750
2751 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002752 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002753 btrfs_handle_fs_error(fs_info, ret,
2754 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002755out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002756 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002757 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002758}
2759
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002760static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002761{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002762 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002763 struct btrfs_disk_key *disk_key;
2764 struct btrfs_chunk *chunk;
2765 u8 *ptr;
2766 int ret = 0;
2767 u32 num_stripes;
2768 u32 array_size;
2769 u32 len = 0;
2770 u32 cur;
2771 struct btrfs_key key;
2772
David Sterba34441362016-10-04 19:34:27 +02002773 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002774 array_size = btrfs_super_sys_array_size(super_copy);
2775
2776 ptr = super_copy->sys_chunk_array;
2777 cur = 0;
2778
2779 while (cur < array_size) {
2780 disk_key = (struct btrfs_disk_key *)ptr;
2781 btrfs_disk_key_to_cpu(&key, disk_key);
2782
2783 len = sizeof(*disk_key);
2784
2785 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2786 chunk = (struct btrfs_chunk *)(ptr + len);
2787 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2788 len += btrfs_chunk_item_size(num_stripes);
2789 } else {
2790 ret = -EIO;
2791 break;
2792 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002793 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002794 key.offset == chunk_offset) {
2795 memmove(ptr, ptr + len, array_size - (cur + len));
2796 array_size -= len;
2797 btrfs_set_super_sys_array_size(super_copy, array_size);
2798 } else {
2799 ptr += len;
2800 cur += len;
2801 }
2802 }
David Sterba34441362016-10-04 19:34:27 +02002803 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002804 return ret;
2805}
2806
Liu Bo592d92e2017-03-14 13:33:55 -07002807static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info,
2808 u64 logical, u64 length)
2809{
2810 struct extent_map_tree *em_tree;
2811 struct extent_map *em;
2812
2813 em_tree = &fs_info->mapping_tree.map_tree;
2814 read_lock(&em_tree->lock);
2815 em = lookup_extent_mapping(em_tree, logical, length);
2816 read_unlock(&em_tree->lock);
2817
2818 if (!em) {
2819 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2820 logical, length);
2821 return ERR_PTR(-EINVAL);
2822 }
2823
2824 if (em->start > logical || em->start + em->len < logical) {
2825 btrfs_crit(fs_info,
2826 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2827 logical, length, em->start, em->start + em->len);
2828 free_extent_map(em);
2829 return ERR_PTR(-EINVAL);
2830 }
2831
2832 /* callers are responsible for dropping em's ref. */
2833 return em;
2834}
2835
Josef Bacik47ab2a62014-09-18 11:20:02 -04002836int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002837 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04002838{
Josef Bacik47ab2a62014-09-18 11:20:02 -04002839 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002840 struct map_lookup *map;
2841 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002842 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002843 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002844
Liu Bo592d92e2017-03-14 13:33:55 -07002845 em = get_chunk_map(fs_info, chunk_offset, 1);
2846 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04002847 /*
2848 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002849 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002850 * do anything we still error out.
2851 */
2852 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07002853 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002854 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002855 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02002856 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002857 check_system_chunk(trans, fs_info, map->type);
David Sterba34441362016-10-04 19:34:27 +02002858 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002859
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002860 /*
2861 * Take the device list mutex to prevent races with the final phase of
2862 * a device replace operation that replaces the device object associated
2863 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2864 */
2865 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002866 for (i = 0; i < map->num_stripes; i++) {
2867 struct btrfs_device *device = map->stripes[i].dev;
2868 ret = btrfs_free_dev_extent(trans, device,
2869 map->stripes[i].physical,
2870 &dev_extent_len);
2871 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002872 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002873 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002874 goto out;
2875 }
2876
2877 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02002878 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002879 btrfs_device_set_bytes_used(device,
2880 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002881 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002882 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02002883 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002884 }
2885
2886 if (map->stripes[i].dev) {
2887 ret = btrfs_update_device(trans, map->stripes[i].dev);
2888 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002889 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002890 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002891 goto out;
2892 }
2893 }
2894 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002895 mutex_unlock(&fs_devices->device_list_mutex);
2896
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002897 ret = btrfs_free_chunk(trans, fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002898 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002899 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002900 goto out;
2901 }
2902
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002903 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002904
2905 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002906 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002907 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002908 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002909 goto out;
2910 }
2911 }
2912
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002913 ret = btrfs_remove_block_group(trans, fs_info, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002914 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002915 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002916 goto out;
2917 }
2918
Josef Bacik47ab2a62014-09-18 11:20:02 -04002919out:
2920 /* once for us */
2921 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002922 return ret;
2923}
2924
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002925static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002926{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002927 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07002928 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002929 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002930
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002931 /*
2932 * Prevent races with automatic removal of unused block groups.
2933 * After we relocate and before we remove the chunk with offset
2934 * chunk_offset, automatic removal of the block group can kick in,
2935 * resulting in a failure when calling btrfs_remove_chunk() below.
2936 *
2937 * Make sure to acquire this mutex before doing a tree search (dev
2938 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2939 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2940 * we release the path used to search the chunk/dev tree and before
2941 * the current task acquires this mutex and calls us.
2942 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002943 ASSERT(mutex_is_locked(&fs_info->delete_unused_bgs_mutex));
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002944
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002945 ret = btrfs_can_relocate(fs_info, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002946 if (ret)
2947 return -ENOSPC;
2948
Chris Mason8f18cf12008-04-25 16:53:30 -04002949 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002950 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002951 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002952 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002953 if (ret)
2954 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002955
Chris Mason19c4d2f2016-10-10 13:43:31 -07002956 trans = btrfs_start_trans_remove_block_group(root->fs_info,
2957 chunk_offset);
2958 if (IS_ERR(trans)) {
2959 ret = PTR_ERR(trans);
2960 btrfs_handle_fs_error(root->fs_info, ret, NULL);
2961 return ret;
2962 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09002963
Chris Mason19c4d2f2016-10-10 13:43:31 -07002964 /*
2965 * step two, delete the device extents and the
2966 * chunk tree entries
2967 */
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002968 ret = btrfs_remove_chunk(trans, fs_info, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002969 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07002970 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002971}
2972
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002973static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002974{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002975 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002976 struct btrfs_path *path;
2977 struct extent_buffer *leaf;
2978 struct btrfs_chunk *chunk;
2979 struct btrfs_key key;
2980 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05002981 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002982 bool retried = false;
2983 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002984 int ret;
2985
2986 path = btrfs_alloc_path();
2987 if (!path)
2988 return -ENOMEM;
2989
Josef Bacikba1bf482009-09-11 16:11:19 -04002990again:
Yan Zheng2b820322008-11-17 21:11:30 -05002991 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2992 key.offset = (u64)-1;
2993 key.type = BTRFS_CHUNK_ITEM_KEY;
2994
2995 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002996 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002997 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002998 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002999 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003000 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003001 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003002 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05003003
3004 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3005 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003006 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003007 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003008 if (ret < 0)
3009 goto error;
3010 if (ret > 0)
3011 break;
3012
3013 leaf = path->nodes[0];
3014 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3015
3016 chunk = btrfs_item_ptr(leaf, path->slots[0],
3017 struct btrfs_chunk);
3018 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003019 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003020
3021 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003022 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003023 if (ret == -ENOSPC)
3024 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303025 else
3026 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003027 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003028 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003029
3030 if (found_key.offset == 0)
3031 break;
3032 key.offset = found_key.offset - 1;
3033 }
3034 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003035 if (failed && !retried) {
3036 failed = 0;
3037 retried = true;
3038 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303039 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003040 ret = -ENOSPC;
3041 }
Yan Zheng2b820322008-11-17 21:11:30 -05003042error:
3043 btrfs_free_path(path);
3044 return ret;
3045}
3046
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003047static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003048 struct btrfs_balance_control *bctl)
3049{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003050 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003051 struct btrfs_trans_handle *trans;
3052 struct btrfs_balance_item *item;
3053 struct btrfs_disk_balance_args disk_bargs;
3054 struct btrfs_path *path;
3055 struct extent_buffer *leaf;
3056 struct btrfs_key key;
3057 int ret, err;
3058
3059 path = btrfs_alloc_path();
3060 if (!path)
3061 return -ENOMEM;
3062
3063 trans = btrfs_start_transaction(root, 0);
3064 if (IS_ERR(trans)) {
3065 btrfs_free_path(path);
3066 return PTR_ERR(trans);
3067 }
3068
3069 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003070 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003071 key.offset = 0;
3072
3073 ret = btrfs_insert_empty_item(trans, root, path, &key,
3074 sizeof(*item));
3075 if (ret)
3076 goto out;
3077
3078 leaf = path->nodes[0];
3079 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3080
David Sterbab159fa22016-11-08 18:09:03 +01003081 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003082
3083 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3084 btrfs_set_balance_data(leaf, item, &disk_bargs);
3085 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3086 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3087 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3088 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3089
3090 btrfs_set_balance_flags(leaf, item, bctl->flags);
3091
3092 btrfs_mark_buffer_dirty(leaf);
3093out:
3094 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003095 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003096 if (err && !ret)
3097 ret = err;
3098 return ret;
3099}
3100
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003101static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003102{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003103 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003104 struct btrfs_trans_handle *trans;
3105 struct btrfs_path *path;
3106 struct btrfs_key key;
3107 int ret, err;
3108
3109 path = btrfs_alloc_path();
3110 if (!path)
3111 return -ENOMEM;
3112
3113 trans = btrfs_start_transaction(root, 0);
3114 if (IS_ERR(trans)) {
3115 btrfs_free_path(path);
3116 return PTR_ERR(trans);
3117 }
3118
3119 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003120 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003121 key.offset = 0;
3122
3123 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3124 if (ret < 0)
3125 goto out;
3126 if (ret > 0) {
3127 ret = -ENOENT;
3128 goto out;
3129 }
3130
3131 ret = btrfs_del_item(trans, root, path);
3132out:
3133 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003134 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003135 if (err && !ret)
3136 ret = err;
3137 return ret;
3138}
3139
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003140/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003141 * This is a heuristic used to reduce the number of chunks balanced on
3142 * resume after balance was interrupted.
3143 */
3144static void update_balance_args(struct btrfs_balance_control *bctl)
3145{
3146 /*
3147 * Turn on soft mode for chunk types that were being converted.
3148 */
3149 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3150 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3151 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3152 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3153 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3154 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3155
3156 /*
3157 * Turn on usage filter if is not already used. The idea is
3158 * that chunks that we have already balanced should be
3159 * reasonably full. Don't do it for chunks that are being
3160 * converted - that will keep us from relocating unconverted
3161 * (albeit full) chunks.
3162 */
3163 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003164 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003165 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3166 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3167 bctl->data.usage = 90;
3168 }
3169 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003170 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003171 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3172 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3173 bctl->sys.usage = 90;
3174 }
3175 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003176 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003177 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3178 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3179 bctl->meta.usage = 90;
3180 }
3181}
3182
3183/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003184 * Should be called with both balance and volume mutexes held to
3185 * serialize other volume operations (add_dev/rm_dev/resize) with
3186 * restriper. Same goes for unset_balance_control.
3187 */
3188static void set_balance_control(struct btrfs_balance_control *bctl)
3189{
3190 struct btrfs_fs_info *fs_info = bctl->fs_info;
3191
3192 BUG_ON(fs_info->balance_ctl);
3193
3194 spin_lock(&fs_info->balance_lock);
3195 fs_info->balance_ctl = bctl;
3196 spin_unlock(&fs_info->balance_lock);
3197}
3198
3199static void unset_balance_control(struct btrfs_fs_info *fs_info)
3200{
3201 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3202
3203 BUG_ON(!fs_info->balance_ctl);
3204
3205 spin_lock(&fs_info->balance_lock);
3206 fs_info->balance_ctl = NULL;
3207 spin_unlock(&fs_info->balance_lock);
3208
3209 kfree(bctl);
3210}
3211
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003212/*
3213 * Balance filters. Return 1 if chunk should be filtered out
3214 * (should not be balanced).
3215 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003216static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003217 struct btrfs_balance_args *bargs)
3218{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003219 chunk_type = chunk_to_extended(chunk_type) &
3220 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003221
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003222 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003223 return 0;
3224
3225 return 1;
3226}
3227
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003228static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003229 struct btrfs_balance_args *bargs)
3230{
3231 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003232 u64 chunk_used;
3233 u64 user_thresh_min;
3234 u64 user_thresh_max;
3235 int ret = 1;
3236
3237 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3238 chunk_used = btrfs_block_group_used(&cache->item);
3239
3240 if (bargs->usage_min == 0)
3241 user_thresh_min = 0;
3242 else
3243 user_thresh_min = div_factor_fine(cache->key.offset,
3244 bargs->usage_min);
3245
3246 if (bargs->usage_max == 0)
3247 user_thresh_max = 1;
3248 else if (bargs->usage_max > 100)
3249 user_thresh_max = cache->key.offset;
3250 else
3251 user_thresh_max = div_factor_fine(cache->key.offset,
3252 bargs->usage_max);
3253
3254 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3255 ret = 0;
3256
3257 btrfs_put_block_group(cache);
3258 return ret;
3259}
3260
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003261static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003262 u64 chunk_offset, struct btrfs_balance_args *bargs)
3263{
3264 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003265 u64 chunk_used, user_thresh;
3266 int ret = 1;
3267
3268 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3269 chunk_used = btrfs_block_group_used(&cache->item);
3270
David Sterbabc309462015-10-20 18:22:13 +02003271 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003272 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003273 else if (bargs->usage > 100)
3274 user_thresh = cache->key.offset;
3275 else
3276 user_thresh = div_factor_fine(cache->key.offset,
3277 bargs->usage);
3278
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003279 if (chunk_used < user_thresh)
3280 ret = 0;
3281
3282 btrfs_put_block_group(cache);
3283 return ret;
3284}
3285
Ilya Dryomov409d4042012-01-16 22:04:47 +02003286static int chunk_devid_filter(struct extent_buffer *leaf,
3287 struct btrfs_chunk *chunk,
3288 struct btrfs_balance_args *bargs)
3289{
3290 struct btrfs_stripe *stripe;
3291 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3292 int i;
3293
3294 for (i = 0; i < num_stripes; i++) {
3295 stripe = btrfs_stripe_nr(chunk, i);
3296 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3297 return 0;
3298 }
3299
3300 return 1;
3301}
3302
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003303/* [pstart, pend) */
3304static int chunk_drange_filter(struct extent_buffer *leaf,
3305 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003306 struct btrfs_balance_args *bargs)
3307{
3308 struct btrfs_stripe *stripe;
3309 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3310 u64 stripe_offset;
3311 u64 stripe_length;
3312 int factor;
3313 int i;
3314
3315 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3316 return 0;
3317
3318 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003319 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3320 factor = num_stripes / 2;
3321 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3322 factor = num_stripes - 1;
3323 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3324 factor = num_stripes - 2;
3325 } else {
3326 factor = num_stripes;
3327 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003328
3329 for (i = 0; i < num_stripes; i++) {
3330 stripe = btrfs_stripe_nr(chunk, i);
3331 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3332 continue;
3333
3334 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3335 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003336 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003337
3338 if (stripe_offset < bargs->pend &&
3339 stripe_offset + stripe_length > bargs->pstart)
3340 return 0;
3341 }
3342
3343 return 1;
3344}
3345
Ilya Dryomovea671762012-01-16 22:04:48 +02003346/* [vstart, vend) */
3347static int chunk_vrange_filter(struct extent_buffer *leaf,
3348 struct btrfs_chunk *chunk,
3349 u64 chunk_offset,
3350 struct btrfs_balance_args *bargs)
3351{
3352 if (chunk_offset < bargs->vend &&
3353 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3354 /* at least part of the chunk is inside this vrange */
3355 return 0;
3356
3357 return 1;
3358}
3359
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003360static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3361 struct btrfs_chunk *chunk,
3362 struct btrfs_balance_args *bargs)
3363{
3364 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3365
3366 if (bargs->stripes_min <= num_stripes
3367 && num_stripes <= bargs->stripes_max)
3368 return 0;
3369
3370 return 1;
3371}
3372
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003373static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003374 struct btrfs_balance_args *bargs)
3375{
3376 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3377 return 0;
3378
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003379 chunk_type = chunk_to_extended(chunk_type) &
3380 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003381
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003382 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003383 return 1;
3384
3385 return 0;
3386}
3387
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003388static int should_balance_chunk(struct btrfs_fs_info *fs_info,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003389 struct extent_buffer *leaf,
3390 struct btrfs_chunk *chunk, u64 chunk_offset)
3391{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003392 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003393 struct btrfs_balance_args *bargs = NULL;
3394 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3395
3396 /* type filter */
3397 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3398 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3399 return 0;
3400 }
3401
3402 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3403 bargs = &bctl->data;
3404 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3405 bargs = &bctl->sys;
3406 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3407 bargs = &bctl->meta;
3408
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003409 /* profiles filter */
3410 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3411 chunk_profiles_filter(chunk_type, bargs)) {
3412 return 0;
3413 }
3414
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003415 /* usage filter */
3416 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003417 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003418 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003419 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003420 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003421 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003422 }
3423
Ilya Dryomov409d4042012-01-16 22:04:47 +02003424 /* devid filter */
3425 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3426 chunk_devid_filter(leaf, chunk, bargs)) {
3427 return 0;
3428 }
3429
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003430 /* drange filter, makes sense only with devid filter */
3431 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003432 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003433 return 0;
3434 }
3435
Ilya Dryomovea671762012-01-16 22:04:48 +02003436 /* vrange filter */
3437 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3438 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3439 return 0;
3440 }
3441
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003442 /* stripes filter */
3443 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3444 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3445 return 0;
3446 }
3447
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003448 /* soft profile changing mode */
3449 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3450 chunk_soft_convert_filter(chunk_type, bargs)) {
3451 return 0;
3452 }
3453
David Sterba7d824b62014-05-07 17:37:51 +02003454 /*
3455 * limited by count, must be the last filter
3456 */
3457 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3458 if (bargs->limit == 0)
3459 return 0;
3460 else
3461 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003462 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3463 /*
3464 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003465 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003466 * about the count of all chunks that satisfy the filters.
3467 */
3468 if (bargs->limit_max == 0)
3469 return 0;
3470 else
3471 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003472 }
3473
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003474 return 1;
3475}
3476
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003477static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003478{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003479 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003480 struct btrfs_root *chunk_root = fs_info->chunk_root;
3481 struct btrfs_root *dev_root = fs_info->dev_root;
3482 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003483 struct btrfs_device *device;
3484 u64 old_size;
3485 u64 size_to_free;
David Sterba12907fc2015-10-10 17:16:50 +02003486 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003487 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003488 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003489 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003490 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003491 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003492 struct extent_buffer *leaf;
3493 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003494 int ret;
3495 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003496 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003497 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003498 u64 limit_data = bctl->data.limit;
3499 u64 limit_meta = bctl->meta.limit;
3500 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003501 u32 count_data = 0;
3502 u32 count_meta = 0;
3503 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003504 int chunk_reserved = 0;
Liu Bocf25ce52015-12-14 18:29:32 -08003505 u64 bytes_used = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003506
Chris Masonec44a352008-04-28 15:29:52 -04003507 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003508 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003509 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003510 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003511 size_to_free = div_factor(old_size, 1);
Byongho Leeee221842015-12-15 01:42:10 +09003512 size_to_free = min_t(u64, size_to_free, SZ_1M);
Yan Zheng2b820322008-11-17 21:11:30 -05003513 if (!device->writeable ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003514 btrfs_device_get_total_bytes(device) -
3515 btrfs_device_get_bytes_used(device) > size_to_free ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003516 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003517 continue;
3518
3519 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003520 if (ret == -ENOSPC)
3521 break;
Liu Bo5a488b92016-07-12 11:24:21 -07003522 if (ret) {
3523 /* btrfs_shrink_device never returns ret > 0 */
3524 WARN_ON(ret > 0);
3525 goto error;
3526 }
Chris Masonec44a352008-04-28 15:29:52 -04003527
Yan, Zhenga22285a2010-05-16 10:48:46 -04003528 trans = btrfs_start_transaction(dev_root, 0);
Liu Bo5a488b92016-07-12 11:24:21 -07003529 if (IS_ERR(trans)) {
3530 ret = PTR_ERR(trans);
3531 btrfs_info_in_rcu(fs_info,
3532 "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu",
3533 rcu_str_deref(device->name), ret,
3534 old_size, old_size - size_to_free);
3535 goto error;
3536 }
Chris Masonec44a352008-04-28 15:29:52 -04003537
3538 ret = btrfs_grow_device(trans, device, old_size);
Liu Bo5a488b92016-07-12 11:24:21 -07003539 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003540 btrfs_end_transaction(trans);
Liu Bo5a488b92016-07-12 11:24:21 -07003541 /* btrfs_grow_device never returns ret > 0 */
3542 WARN_ON(ret > 0);
3543 btrfs_info_in_rcu(fs_info,
3544 "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu",
3545 rcu_str_deref(device->name), ret,
3546 old_size, old_size - size_to_free);
3547 goto error;
3548 }
Chris Masonec44a352008-04-28 15:29:52 -04003549
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003550 btrfs_end_transaction(trans);
Chris Masonec44a352008-04-28 15:29:52 -04003551 }
3552
3553 /* step two, relocate all the chunks */
3554 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003555 if (!path) {
3556 ret = -ENOMEM;
3557 goto error;
3558 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003559
3560 /* zero out stat counters */
3561 spin_lock(&fs_info->balance_lock);
3562 memset(&bctl->stat, 0, sizeof(bctl->stat));
3563 spin_unlock(&fs_info->balance_lock);
3564again:
David Sterba7d824b62014-05-07 17:37:51 +02003565 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003566 /*
3567 * The single value limit and min/max limits use the same bytes
3568 * in the
3569 */
David Sterba7d824b62014-05-07 17:37:51 +02003570 bctl->data.limit = limit_data;
3571 bctl->meta.limit = limit_meta;
3572 bctl->sys.limit = limit_sys;
3573 }
Chris Masonec44a352008-04-28 15:29:52 -04003574 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3575 key.offset = (u64)-1;
3576 key.type = BTRFS_CHUNK_ITEM_KEY;
3577
Chris Masond3977122009-01-05 21:25:51 -05003578 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003579 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003580 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003581 ret = -ECANCELED;
3582 goto error;
3583 }
3584
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003585 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003586 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003587 if (ret < 0) {
3588 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003589 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003590 }
Chris Masonec44a352008-04-28 15:29:52 -04003591
3592 /*
3593 * this shouldn't happen, it means the last relocate
3594 * failed
3595 */
3596 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003597 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003598
3599 ret = btrfs_previous_item(chunk_root, path, 0,
3600 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003601 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003602 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003603 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003604 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003605 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003606
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003607 leaf = path->nodes[0];
3608 slot = path->slots[0];
3609 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3610
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003611 if (found_key.objectid != key.objectid) {
3612 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003613 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003614 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003615
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003616 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003617 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003618
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003619 if (!counting) {
3620 spin_lock(&fs_info->balance_lock);
3621 bctl->stat.considered++;
3622 spin_unlock(&fs_info->balance_lock);
3623 }
3624
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003625 ret = should_balance_chunk(fs_info, leaf, chunk,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003626 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003627
David Sterbab3b4aa72011-04-21 01:20:15 +02003628 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003629 if (!ret) {
3630 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003631 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003632 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003633
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003634 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003635 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003636 spin_lock(&fs_info->balance_lock);
3637 bctl->stat.expected++;
3638 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003639
3640 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3641 count_data++;
3642 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3643 count_sys++;
3644 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3645 count_meta++;
3646
3647 goto loop;
3648 }
3649
3650 /*
3651 * Apply limit_min filter, no need to check if the LIMITS
3652 * filter is used, limit_min is 0 by default
3653 */
3654 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3655 count_data < bctl->data.limit_min)
3656 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3657 count_meta < bctl->meta.limit_min)
3658 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3659 count_sys < bctl->sys.limit_min)) {
3660 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003661 goto loop;
3662 }
3663
Liu Bocf25ce52015-12-14 18:29:32 -08003664 ASSERT(fs_info->data_sinfo);
3665 spin_lock(&fs_info->data_sinfo->lock);
3666 bytes_used = fs_info->data_sinfo->bytes_used;
3667 spin_unlock(&fs_info->data_sinfo->lock);
3668
3669 if ((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3670 !chunk_reserved && !bytes_used) {
Zhao Lei2c9fe832015-11-09 11:51:32 +08003671 trans = btrfs_start_transaction(chunk_root, 0);
3672 if (IS_ERR(trans)) {
3673 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3674 ret = PTR_ERR(trans);
3675 goto error;
3676 }
3677
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003678 ret = btrfs_force_chunk_alloc(trans, fs_info,
Zhao Lei2c9fe832015-11-09 11:51:32 +08003679 BTRFS_BLOCK_GROUP_DATA);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003680 btrfs_end_transaction(trans);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003681 if (ret < 0) {
3682 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3683 goto error;
3684 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003685 chunk_reserved = 1;
3686 }
3687
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003688 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003689 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik508794e2011-07-02 21:24:41 +00003690 if (ret && ret != -ENOSPC)
3691 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003692 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003693 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003694 } else {
3695 spin_lock(&fs_info->balance_lock);
3696 bctl->stat.completed++;
3697 spin_unlock(&fs_info->balance_lock);
3698 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003699loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003700 if (found_key.offset == 0)
3701 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003702 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003703 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003704
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003705 if (counting) {
3706 btrfs_release_path(path);
3707 counting = false;
3708 goto again;
3709 }
Chris Masonec44a352008-04-28 15:29:52 -04003710error:
3711 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003712 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003713 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003714 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003715 if (!ret)
3716 ret = -ENOSPC;
3717 }
3718
Chris Masonec44a352008-04-28 15:29:52 -04003719 return ret;
3720}
3721
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003722/**
3723 * alloc_profile_is_valid - see if a given profile is valid and reduced
3724 * @flags: profile to validate
3725 * @extended: if true @flags is treated as an extended profile
3726 */
3727static int alloc_profile_is_valid(u64 flags, int extended)
3728{
3729 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3730 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3731
3732 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3733
3734 /* 1) check that all other bits are zeroed */
3735 if (flags & ~mask)
3736 return 0;
3737
3738 /* 2) see if profile is reduced */
3739 if (flags == 0)
3740 return !extended; /* "0" is valid for usual profiles */
3741
3742 /* true if exactly one bit set */
3743 return (flags & (flags - 1)) == 0;
3744}
3745
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003746static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3747{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003748 /* cancel requested || normal exit path */
3749 return atomic_read(&fs_info->balance_cancel_req) ||
3750 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3751 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003752}
3753
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003754static void __cancel_balance(struct btrfs_fs_info *fs_info)
3755{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003756 int ret;
3757
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003758 unset_balance_control(fs_info);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003759 ret = del_balance_item(fs_info);
Liu Bo0f788c52013-03-04 16:25:40 +00003760 if (ret)
Anand Jain34d97002016-03-16 16:43:06 +08003761 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003762
David Sterba171938e2017-03-28 14:44:21 +02003763 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003764}
3765
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003766/* Non-zero return value signifies invalidity */
3767static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3768 u64 allowed)
3769{
3770 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3771 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3772 (bctl_arg->target & ~allowed)));
3773}
3774
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003775/*
3776 * Should be called with both balance and volume mutexes held
3777 */
3778int btrfs_balance(struct btrfs_balance_control *bctl,
3779 struct btrfs_ioctl_balance_args *bargs)
3780{
3781 struct btrfs_fs_info *fs_info = bctl->fs_info;
Adam Borowski14506122017-03-07 23:34:44 +01003782 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003783 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003784 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003785 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003786 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003787 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003788
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003789 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003790 atomic_read(&fs_info->balance_pause_req) ||
3791 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003792 ret = -EINVAL;
3793 goto out;
3794 }
3795
Ilya Dryomove4837f82012-03-27 17:09:17 +03003796 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3797 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3798 mixed = 1;
3799
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003800 /*
3801 * In case of mixed groups both data and meta should be picked,
3802 * and identical options should be given for both of them.
3803 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003804 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3805 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003806 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3807 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3808 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003809 btrfs_err(fs_info,
3810 "with mixed groups data and metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003811 ret = -EINVAL;
3812 goto out;
3813 }
3814 }
3815
Stefan Behrens8dabb742012-11-06 13:15:27 +01003816 num_devices = fs_info->fs_devices->num_devices;
Liu Bo73beece2015-07-17 16:49:19 +08003817 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Stefan Behrens8dabb742012-11-06 13:15:27 +01003818 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3819 BUG_ON(num_devices < 1);
3820 num_devices--;
3821 }
Liu Bo73beece2015-07-17 16:49:19 +08003822 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Austin S. Hemmelgarn88be1592016-03-23 14:22:59 -04003823 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
3824 if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003825 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003826 if (num_devices > 2)
3827 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3828 if (num_devices > 3)
3829 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3830 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003831 if (validate_convert_profile(&bctl->data, allowed)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003832 btrfs_err(fs_info,
3833 "unable to start balance with target data profile %llu",
3834 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003835 ret = -EINVAL;
3836 goto out;
3837 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003838 if (validate_convert_profile(&bctl->meta, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003839 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003840 "unable to start balance with target metadata profile %llu",
3841 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003842 ret = -EINVAL;
3843 goto out;
3844 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003845 if (validate_convert_profile(&bctl->sys, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003846 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003847 "unable to start balance with target system profile %llu",
3848 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003849 ret = -EINVAL;
3850 goto out;
3851 }
3852
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003853 /* allow to reduce meta or sys integrity only if force set */
3854 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003855 BTRFS_BLOCK_GROUP_RAID10 |
3856 BTRFS_BLOCK_GROUP_RAID5 |
3857 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003858 do {
3859 seq = read_seqbegin(&fs_info->profiles_lock);
3860
3861 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3862 (fs_info->avail_system_alloc_bits & allowed) &&
3863 !(bctl->sys.target & allowed)) ||
3864 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3865 (fs_info->avail_metadata_alloc_bits & allowed) &&
3866 !(bctl->meta.target & allowed))) {
3867 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003868 btrfs_info(fs_info,
3869 "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003870 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003871 btrfs_err(fs_info,
3872 "balance will reduce metadata integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003873 ret = -EINVAL;
3874 goto out;
3875 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003876 }
Miao Xiede98ced2013-01-29 10:13:12 +00003877 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003878
Adam Borowski14506122017-03-07 23:34:44 +01003879 /* if we're not converting, the target field is uninitialized */
3880 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3881 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
3882 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3883 bctl->data.target : fs_info->avail_data_alloc_bits;
3884 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
3885 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Sam Tygieree592d02016-01-06 08:46:12 +00003886 btrfs_warn(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003887 "metadata profile 0x%llx has lower redundancy than data profile 0x%llx",
Adam Borowski14506122017-03-07 23:34:44 +01003888 meta_target, data_target);
Sam Tygieree592d02016-01-06 08:46:12 +00003889 }
3890
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003891 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003892 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003893 goto out;
3894
Ilya Dryomov59641012012-01-16 22:04:48 +02003895 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3896 BUG_ON(ret == -EEXIST);
3897 set_balance_control(bctl);
3898 } else {
3899 BUG_ON(ret != -EEXIST);
3900 spin_lock(&fs_info->balance_lock);
3901 update_balance_args(bctl);
3902 spin_unlock(&fs_info->balance_lock);
3903 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003904
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003905 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003906 mutex_unlock(&fs_info->balance_mutex);
3907
3908 ret = __btrfs_balance(fs_info);
3909
3910 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003911 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003912
3913 if (bargs) {
3914 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003915 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003916 }
3917
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003918 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3919 balance_need_close(fs_info)) {
3920 __cancel_balance(fs_info);
3921 }
3922
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003923 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003924
3925 return ret;
3926out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003927 if (bctl->flags & BTRFS_BALANCE_RESUME)
3928 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003929 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003930 kfree(bctl);
David Sterba171938e2017-03-28 14:44:21 +02003931 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003932 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003933 return ret;
3934}
3935
3936static int balance_kthread(void *data)
3937{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003938 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003939 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003940
3941 mutex_lock(&fs_info->volume_mutex);
3942 mutex_lock(&fs_info->balance_mutex);
3943
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003944 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003945 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003946 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003947 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003948
3949 mutex_unlock(&fs_info->balance_mutex);
3950 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003951
Ilya Dryomov59641012012-01-16 22:04:48 +02003952 return ret;
3953}
3954
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003955int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3956{
3957 struct task_struct *tsk;
3958
3959 spin_lock(&fs_info->balance_lock);
3960 if (!fs_info->balance_ctl) {
3961 spin_unlock(&fs_info->balance_lock);
3962 return 0;
3963 }
3964 spin_unlock(&fs_info->balance_lock);
3965
Jeff Mahoney3cdde222016-06-09 21:38:35 -04003966 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003967 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003968 return 0;
3969 }
3970
3971 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303972 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003973}
3974
Ilya Dryomov68310a52012-06-22 12:24:12 -06003975int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003976{
Ilya Dryomov59641012012-01-16 22:04:48 +02003977 struct btrfs_balance_control *bctl;
3978 struct btrfs_balance_item *item;
3979 struct btrfs_disk_balance_args disk_bargs;
3980 struct btrfs_path *path;
3981 struct extent_buffer *leaf;
3982 struct btrfs_key key;
3983 int ret;
3984
3985 path = btrfs_alloc_path();
3986 if (!path)
3987 return -ENOMEM;
3988
Ilya Dryomov68310a52012-06-22 12:24:12 -06003989 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003990 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06003991 key.offset = 0;
3992
3993 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3994 if (ret < 0)
3995 goto out;
3996 if (ret > 0) { /* ret = -ENOENT; */
3997 ret = 0;
3998 goto out;
3999 }
4000
Ilya Dryomov59641012012-01-16 22:04:48 +02004001 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4002 if (!bctl) {
4003 ret = -ENOMEM;
4004 goto out;
4005 }
4006
Ilya Dryomov59641012012-01-16 22:04:48 +02004007 leaf = path->nodes[0];
4008 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4009
Ilya Dryomov68310a52012-06-22 12:24:12 -06004010 bctl->fs_info = fs_info;
4011 bctl->flags = btrfs_balance_flags(leaf, item);
4012 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004013
4014 btrfs_balance_data(leaf, item, &disk_bargs);
4015 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4016 btrfs_balance_meta(leaf, item, &disk_bargs);
4017 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4018 btrfs_balance_sys(leaf, item, &disk_bargs);
4019 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4020
David Sterba171938e2017-03-28 14:44:21 +02004021 WARN_ON(test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004022
Ilya Dryomov68310a52012-06-22 12:24:12 -06004023 mutex_lock(&fs_info->volume_mutex);
4024 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004025
Ilya Dryomov68310a52012-06-22 12:24:12 -06004026 set_balance_control(bctl);
4027
4028 mutex_unlock(&fs_info->balance_mutex);
4029 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004030out:
4031 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004032 return ret;
4033}
4034
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004035int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4036{
4037 int ret = 0;
4038
4039 mutex_lock(&fs_info->balance_mutex);
4040 if (!fs_info->balance_ctl) {
4041 mutex_unlock(&fs_info->balance_mutex);
4042 return -ENOTCONN;
4043 }
4044
4045 if (atomic_read(&fs_info->balance_running)) {
4046 atomic_inc(&fs_info->balance_pause_req);
4047 mutex_unlock(&fs_info->balance_mutex);
4048
4049 wait_event(fs_info->balance_wait_q,
4050 atomic_read(&fs_info->balance_running) == 0);
4051
4052 mutex_lock(&fs_info->balance_mutex);
4053 /* we are good with balance_ctl ripped off from under us */
4054 BUG_ON(atomic_read(&fs_info->balance_running));
4055 atomic_dec(&fs_info->balance_pause_req);
4056 } else {
4057 ret = -ENOTCONN;
4058 }
4059
4060 mutex_unlock(&fs_info->balance_mutex);
4061 return ret;
4062}
4063
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004064int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4065{
David Howellsbc98a422017-07-17 08:45:34 +01004066 if (sb_rdonly(fs_info->sb))
Ilya Dryomove649e582013-10-10 20:40:21 +03004067 return -EROFS;
4068
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004069 mutex_lock(&fs_info->balance_mutex);
4070 if (!fs_info->balance_ctl) {
4071 mutex_unlock(&fs_info->balance_mutex);
4072 return -ENOTCONN;
4073 }
4074
4075 atomic_inc(&fs_info->balance_cancel_req);
4076 /*
4077 * if we are running just wait and return, balance item is
4078 * deleted in btrfs_balance in this case
4079 */
4080 if (atomic_read(&fs_info->balance_running)) {
4081 mutex_unlock(&fs_info->balance_mutex);
4082 wait_event(fs_info->balance_wait_q,
4083 atomic_read(&fs_info->balance_running) == 0);
4084 mutex_lock(&fs_info->balance_mutex);
4085 } else {
4086 /* __cancel_balance needs volume_mutex */
4087 mutex_unlock(&fs_info->balance_mutex);
4088 mutex_lock(&fs_info->volume_mutex);
4089 mutex_lock(&fs_info->balance_mutex);
4090
4091 if (fs_info->balance_ctl)
4092 __cancel_balance(fs_info);
4093
4094 mutex_unlock(&fs_info->volume_mutex);
4095 }
4096
4097 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
4098 atomic_dec(&fs_info->balance_cancel_req);
4099 mutex_unlock(&fs_info->balance_mutex);
4100 return 0;
4101}
4102
Stefan Behrens803b2f52013-08-15 17:11:21 +02004103static int btrfs_uuid_scan_kthread(void *data)
4104{
4105 struct btrfs_fs_info *fs_info = data;
4106 struct btrfs_root *root = fs_info->tree_root;
4107 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004108 struct btrfs_path *path = NULL;
4109 int ret = 0;
4110 struct extent_buffer *eb;
4111 int slot;
4112 struct btrfs_root_item root_item;
4113 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004114 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004115
4116 path = btrfs_alloc_path();
4117 if (!path) {
4118 ret = -ENOMEM;
4119 goto out;
4120 }
4121
4122 key.objectid = 0;
4123 key.type = BTRFS_ROOT_ITEM_KEY;
4124 key.offset = 0;
4125
Stefan Behrens803b2f52013-08-15 17:11:21 +02004126 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004127 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004128 if (ret) {
4129 if (ret > 0)
4130 ret = 0;
4131 break;
4132 }
4133
4134 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4135 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4136 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4137 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4138 goto skip;
4139
4140 eb = path->nodes[0];
4141 slot = path->slots[0];
4142 item_size = btrfs_item_size_nr(eb, slot);
4143 if (item_size < sizeof(root_item))
4144 goto skip;
4145
Stefan Behrens803b2f52013-08-15 17:11:21 +02004146 read_extent_buffer(eb, &root_item,
4147 btrfs_item_ptr_offset(eb, slot),
4148 (int)sizeof(root_item));
4149 if (btrfs_root_refs(&root_item) == 0)
4150 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004151
4152 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4153 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4154 if (trans)
4155 goto update_tree;
4156
4157 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004158 /*
4159 * 1 - subvol uuid item
4160 * 1 - received_subvol uuid item
4161 */
4162 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4163 if (IS_ERR(trans)) {
4164 ret = PTR_ERR(trans);
4165 break;
4166 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004167 continue;
4168 } else {
4169 goto skip;
4170 }
4171update_tree:
4172 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004173 ret = btrfs_uuid_tree_add(trans, fs_info,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004174 root_item.uuid,
4175 BTRFS_UUID_KEY_SUBVOL,
4176 key.objectid);
4177 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004178 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004179 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004180 break;
4181 }
4182 }
4183
4184 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004185 ret = btrfs_uuid_tree_add(trans, fs_info,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004186 root_item.received_uuid,
4187 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4188 key.objectid);
4189 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004190 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004191 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004192 break;
4193 }
4194 }
4195
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004196skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004197 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004198 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004199 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004200 if (ret)
4201 break;
4202 }
4203
Stefan Behrens803b2f52013-08-15 17:11:21 +02004204 btrfs_release_path(path);
4205 if (key.offset < (u64)-1) {
4206 key.offset++;
4207 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4208 key.offset = 0;
4209 key.type = BTRFS_ROOT_ITEM_KEY;
4210 } else if (key.objectid < (u64)-1) {
4211 key.offset = 0;
4212 key.type = BTRFS_ROOT_ITEM_KEY;
4213 key.objectid++;
4214 } else {
4215 break;
4216 }
4217 cond_resched();
4218 }
4219
4220out:
4221 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004222 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004223 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004224 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004225 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004226 else
Josef Bacikafcdd122016-09-02 15:40:02 -04004227 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004228 up(&fs_info->uuid_tree_rescan_sem);
4229 return 0;
4230}
4231
Stefan Behrens70f80172013-08-15 17:11:23 +02004232/*
4233 * Callback for btrfs_uuid_tree_iterate().
4234 * returns:
4235 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004236 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004237 * > 0 if the check failed, which means the caller shall remove the entry.
4238 */
4239static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4240 u8 *uuid, u8 type, u64 subid)
4241{
4242 struct btrfs_key key;
4243 int ret = 0;
4244 struct btrfs_root *subvol_root;
4245
4246 if (type != BTRFS_UUID_KEY_SUBVOL &&
4247 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4248 goto out;
4249
4250 key.objectid = subid;
4251 key.type = BTRFS_ROOT_ITEM_KEY;
4252 key.offset = (u64)-1;
4253 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4254 if (IS_ERR(subvol_root)) {
4255 ret = PTR_ERR(subvol_root);
4256 if (ret == -ENOENT)
4257 ret = 1;
4258 goto out;
4259 }
4260
4261 switch (type) {
4262 case BTRFS_UUID_KEY_SUBVOL:
4263 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4264 ret = 1;
4265 break;
4266 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4267 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4268 BTRFS_UUID_SIZE))
4269 ret = 1;
4270 break;
4271 }
4272
4273out:
4274 return ret;
4275}
4276
4277static int btrfs_uuid_rescan_kthread(void *data)
4278{
4279 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4280 int ret;
4281
4282 /*
4283 * 1st step is to iterate through the existing UUID tree and
4284 * to delete all entries that contain outdated data.
4285 * 2nd step is to add all missing entries to the UUID tree.
4286 */
4287 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4288 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004289 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004290 up(&fs_info->uuid_tree_rescan_sem);
4291 return ret;
4292 }
4293 return btrfs_uuid_scan_kthread(data);
4294}
4295
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004296int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4297{
4298 struct btrfs_trans_handle *trans;
4299 struct btrfs_root *tree_root = fs_info->tree_root;
4300 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004301 struct task_struct *task;
4302 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004303
4304 /*
4305 * 1 - root node
4306 * 1 - root item
4307 */
4308 trans = btrfs_start_transaction(tree_root, 2);
4309 if (IS_ERR(trans))
4310 return PTR_ERR(trans);
4311
4312 uuid_root = btrfs_create_tree(trans, fs_info,
4313 BTRFS_UUID_TREE_OBJECTID);
4314 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004315 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004316 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004317 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004318 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004319 }
4320
4321 fs_info->uuid_root = uuid_root;
4322
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004323 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004324 if (ret)
4325 return ret;
4326
4327 down(&fs_info->uuid_tree_rescan_sem);
4328 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4329 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004330 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004331 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004332 up(&fs_info->uuid_tree_rescan_sem);
4333 return PTR_ERR(task);
4334 }
4335
4336 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004337}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004338
Stefan Behrens70f80172013-08-15 17:11:23 +02004339int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4340{
4341 struct task_struct *task;
4342
4343 down(&fs_info->uuid_tree_rescan_sem);
4344 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4345 if (IS_ERR(task)) {
4346 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004347 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004348 up(&fs_info->uuid_tree_rescan_sem);
4349 return PTR_ERR(task);
4350 }
4351
4352 return 0;
4353}
4354
Chris Mason8f18cf12008-04-25 16:53:30 -04004355/*
4356 * shrinking a device means finding all of the device extents past
4357 * the new size, and then following the back refs to the chunks.
4358 * The chunk relocation code actually frees the device extent
4359 */
4360int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4361{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004362 struct btrfs_fs_info *fs_info = device->fs_info;
4363 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004364 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004365 struct btrfs_dev_extent *dev_extent = NULL;
4366 struct btrfs_path *path;
4367 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004368 u64 chunk_offset;
4369 int ret;
4370 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004371 int failed = 0;
4372 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004373 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004374 struct extent_buffer *l;
4375 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004376 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004377 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004378 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004379 u64 diff;
4380
4381 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004382 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004383
Stefan Behrens63a212a2012-11-05 18:29:28 +01004384 if (device->is_tgtdev_for_dev_replace)
4385 return -EINVAL;
4386
Chris Mason8f18cf12008-04-25 16:53:30 -04004387 path = btrfs_alloc_path();
4388 if (!path)
4389 return -ENOMEM;
4390
David Sterbae4058b52015-11-27 16:31:35 +01004391 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04004392
David Sterba34441362016-10-04 19:34:27 +02004393 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004394
Miao Xie7cc8e582014-09-03 21:35:38 +08004395 btrfs_device_set_total_bytes(device, new_size);
Josef Bacik2bf64752011-09-26 17:12:22 -04004396 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004397 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004398 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004399 }
David Sterba34441362016-10-04 19:34:27 +02004400 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004401
Josef Bacikba1bf482009-09-11 16:11:19 -04004402again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004403 key.objectid = device->devid;
4404 key.offset = (u64)-1;
4405 key.type = BTRFS_DEV_EXTENT_KEY;
4406
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004407 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004408 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004409 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004410 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004411 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004412 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004413 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004414
4415 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004416 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004417 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004418 if (ret < 0)
4419 goto done;
4420 if (ret) {
4421 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004422 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004423 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004424 }
4425
4426 l = path->nodes[0];
4427 slot = path->slots[0];
4428 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4429
Josef Bacikba1bf482009-09-11 16:11:19 -04004430 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004431 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004432 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004433 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004434 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004435
4436 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4437 length = btrfs_dev_extent_length(l, dev_extent);
4438
Josef Bacikba1bf482009-09-11 16:11:19 -04004439 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004440 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004441 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004442 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004443 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004444
Chris Mason8f18cf12008-04-25 16:53:30 -04004445 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004446 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004447
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004448 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4449 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04004450 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004451 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004452 if (ret == -ENOSPC)
4453 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004454 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004455
4456 if (failed && !retried) {
4457 failed = 0;
4458 retried = true;
4459 goto again;
4460 } else if (failed && retried) {
4461 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004462 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004463 }
4464
Chris Balld6397ba2009-04-27 07:29:03 -04004465 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004466 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004467 if (IS_ERR(trans)) {
4468 ret = PTR_ERR(trans);
4469 goto done;
4470 }
4471
David Sterba34441362016-10-04 19:34:27 +02004472 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004473
4474 /*
4475 * We checked in the above loop all device extents that were already in
4476 * the device tree. However before we have updated the device's
4477 * total_bytes to the new size, we might have had chunk allocations that
4478 * have not complete yet (new block groups attached to transaction
4479 * handles), and therefore their device extents were not yet in the
4480 * device tree and we missed them in the loop above. So if we have any
4481 * pending chunk using a device extent that overlaps the device range
4482 * that we can not use anymore, commit the current transaction and
4483 * repeat the search on the device tree - this way we guarantee we will
4484 * not have chunks using device extents that end beyond 'new_size'.
4485 */
4486 if (!checked_pending_chunks) {
4487 u64 start = new_size;
4488 u64 len = old_size - new_size;
4489
Jeff Mahoney499f3772015-06-15 09:41:17 -04004490 if (contains_pending_extent(trans->transaction, device,
4491 &start, len)) {
David Sterba34441362016-10-04 19:34:27 +02004492 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004493 checked_pending_chunks = true;
4494 failed = 0;
4495 retried = false;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004496 ret = btrfs_commit_transaction(trans);
Filipe Manana53e489b2015-06-02 14:43:21 +01004497 if (ret)
4498 goto done;
4499 goto again;
4500 }
4501 }
4502
Miao Xie7cc8e582014-09-03 21:35:38 +08004503 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004504 if (list_empty(&device->resized_list))
4505 list_add_tail(&device->resized_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004506 &fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004507
Chris Balld6397ba2009-04-27 07:29:03 -04004508 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004509 btrfs_set_super_total_bytes(super_copy,
4510 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004511 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004512
4513 /* Now btrfs_update_device() will change the on-disk size. */
4514 ret = btrfs_update_device(trans, device);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004515 btrfs_end_transaction(trans);
Chris Mason8f18cf12008-04-25 16:53:30 -04004516done:
4517 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004518 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004519 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004520 btrfs_device_set_total_bytes(device, old_size);
4521 if (device->writeable)
4522 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004523 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004524 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004525 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004526 return ret;
4527}
4528
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004529static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004530 struct btrfs_key *key,
4531 struct btrfs_chunk *chunk, int item_size)
4532{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004533 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004534 struct btrfs_disk_key disk_key;
4535 u32 array_size;
4536 u8 *ptr;
4537
David Sterba34441362016-10-04 19:34:27 +02004538 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004539 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004540 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004541 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004542 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004543 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004544 }
Chris Mason0b86a832008-03-24 15:01:56 -04004545
4546 ptr = super_copy->sys_chunk_array + array_size;
4547 btrfs_cpu_key_to_disk(&disk_key, key);
4548 memcpy(ptr, &disk_key, sizeof(disk_key));
4549 ptr += sizeof(disk_key);
4550 memcpy(ptr, chunk, item_size);
4551 item_size += sizeof(disk_key);
4552 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004553 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004554
Chris Mason0b86a832008-03-24 15:01:56 -04004555 return 0;
4556}
4557
Miao Xieb2117a32011-01-05 10:07:28 +00004558/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004559 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004560 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004561static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004562{
Arne Jansen73c5de02011-04-12 12:07:57 +02004563 const struct btrfs_device_info *di_a = a;
4564 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004565
Arne Jansen73c5de02011-04-12 12:07:57 +02004566 if (di_a->max_avail > di_b->max_avail)
4567 return -1;
4568 if (di_a->max_avail < di_b->max_avail)
4569 return 1;
4570 if (di_a->total_avail > di_b->total_avail)
4571 return -1;
4572 if (di_a->total_avail < di_b->total_avail)
4573 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004574 return 0;
4575}
4576
David Woodhouse53b381b2013-01-29 18:40:14 -05004577static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4578{
Zhao Leiffe2d202015-01-20 15:11:44 +08004579 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004580 return;
4581
Miao Xieceda0862013-04-11 10:30:16 +00004582 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004583}
4584
Jeff Mahoneyda170662016-06-15 09:22:56 -04004585#define BTRFS_MAX_DEVS(r) ((BTRFS_MAX_ITEM_SIZE(r->fs_info) \
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004586 - sizeof(struct btrfs_chunk)) \
4587 / sizeof(struct btrfs_stripe) + 1)
4588
4589#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4590 - 2 * sizeof(struct btrfs_disk_key) \
4591 - 2 * sizeof(struct btrfs_chunk)) \
4592 / sizeof(struct btrfs_stripe) + 1)
4593
Miao Xieb2117a32011-01-05 10:07:28 +00004594static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
David Sterba72b468c2017-02-10 19:46:27 +01004595 u64 start, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004596{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004597 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004598 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004599 struct btrfs_device *device;
Arne Jansen73c5de02011-04-12 12:07:57 +02004600 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004601 struct extent_map_tree *em_tree;
4602 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004603 struct btrfs_device_info *devices_info = NULL;
4604 u64 total_avail;
4605 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004606 int data_stripes; /* number of stripes that count for
4607 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004608 int sub_stripes; /* sub_stripes info for map */
4609 int dev_stripes; /* stripes per dev */
4610 int devs_max; /* max devs to use */
4611 int devs_min; /* min devs needed */
4612 int devs_increment; /* ndevs has to be a multiple of this */
4613 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004614 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004615 u64 max_stripe_size;
4616 u64 max_chunk_size;
4617 u64 stripe_size;
4618 u64 num_bytes;
4619 int ndevs;
4620 int i;
4621 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004622 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004623
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004624 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004625
Miao Xieb2117a32011-01-05 10:07:28 +00004626 if (list_empty(&fs_devices->alloc_list))
4627 return -ENOSPC;
4628
Liu Bo31e50222012-11-21 14:18:10 +00004629 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004630
Liu Bo31e50222012-11-21 14:18:10 +00004631 sub_stripes = btrfs_raid_array[index].sub_stripes;
4632 dev_stripes = btrfs_raid_array[index].dev_stripes;
4633 devs_max = btrfs_raid_array[index].devs_max;
4634 devs_min = btrfs_raid_array[index].devs_min;
4635 devs_increment = btrfs_raid_array[index].devs_increment;
4636 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004637
4638 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09004639 max_stripe_size = SZ_1G;
Arne Jansen73c5de02011-04-12 12:07:57 +02004640 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004641 if (!devs_max)
4642 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004643 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004644 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09004645 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4646 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05004647 else
Byongho Leeee221842015-12-15 01:42:10 +09004648 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004649 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004650 if (!devs_max)
4651 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004652 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09004653 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004654 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004655 if (!devs_max)
4656 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004657 } else {
David Sterba351fd352014-05-15 16:48:20 +02004658 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004659 type);
4660 BUG_ON(1);
4661 }
4662
4663 /* we don't want a chunk larger than 10% of writeable space */
4664 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4665 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004666
David Sterba31e818f2015-02-20 18:00:26 +01004667 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004668 GFP_NOFS);
4669 if (!devices_info)
4670 return -ENOMEM;
4671
Arne Jansen73c5de02011-04-12 12:07:57 +02004672 /*
4673 * in the first pass through the devices list, we gather information
4674 * about the available holes on each device.
4675 */
4676 ndevs = 0;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004677 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004678 u64 max_avail;
4679 u64 dev_offset;
4680
Arne Jansen73c5de02011-04-12 12:07:57 +02004681 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004682 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004683 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004684 continue;
4685 }
4686
Stefan Behrens63a212a2012-11-05 18:29:28 +01004687 if (!device->in_fs_metadata ||
4688 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004689 continue;
4690
4691 if (device->total_bytes > device->bytes_used)
4692 total_avail = device->total_bytes - device->bytes_used;
4693 else
4694 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004695
4696 /* If there is no space on this device, skip it. */
4697 if (total_avail == 0)
4698 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004699
Josef Bacik6df9a952013-06-27 13:22:46 -04004700 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004701 max_stripe_size * dev_stripes,
4702 &dev_offset, &max_avail);
4703 if (ret && ret != -ENOSPC)
4704 goto error;
4705
4706 if (ret == 0)
4707 max_avail = max_stripe_size * dev_stripes;
4708
4709 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4710 continue;
4711
Eric Sandeen063d0062013-01-31 00:55:01 +00004712 if (ndevs == fs_devices->rw_devices) {
4713 WARN(1, "%s: found more than %llu devices\n",
4714 __func__, fs_devices->rw_devices);
4715 break;
4716 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004717 devices_info[ndevs].dev_offset = dev_offset;
4718 devices_info[ndevs].max_avail = max_avail;
4719 devices_info[ndevs].total_avail = total_avail;
4720 devices_info[ndevs].dev = device;
4721 ++ndevs;
4722 }
4723
4724 /*
4725 * now sort the devices by hole size / available space
4726 */
4727 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4728 btrfs_cmp_device_info, NULL);
4729
4730 /* round down to number of usable stripes */
Nikolay Borisove5600fd2017-06-27 10:02:25 +03004731 ndevs = round_down(ndevs, devs_increment);
Arne Jansen73c5de02011-04-12 12:07:57 +02004732
4733 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4734 ret = -ENOSPC;
4735 goto error;
4736 }
4737
Nikolay Borisovf148ef42017-06-27 10:02:26 +03004738 ndevs = min(ndevs, devs_max);
4739
Arne Jansen73c5de02011-04-12 12:07:57 +02004740 /*
4741 * the primary goal is to maximize the number of stripes, so use as many
4742 * devices as possible, even if the stripes are not maximum sized.
4743 */
4744 stripe_size = devices_info[ndevs-1].max_avail;
4745 num_stripes = ndevs * dev_stripes;
4746
David Woodhouse53b381b2013-01-29 18:40:14 -05004747 /*
4748 * this will have to be fixed for RAID1 and RAID10 over
4749 * more drives
4750 */
4751 data_stripes = num_stripes / ncopies;
4752
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004753 if (type & BTRFS_BLOCK_GROUP_RAID5)
David Woodhouse53b381b2013-01-29 18:40:14 -05004754 data_stripes = num_stripes - 1;
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004755
4756 if (type & BTRFS_BLOCK_GROUP_RAID6)
David Woodhouse53b381b2013-01-29 18:40:14 -05004757 data_stripes = num_stripes - 2;
Chris Mason86db2572013-02-20 16:23:40 -05004758
4759 /*
4760 * Use the number of data stripes to figure out how big this chunk
4761 * is really going to be in terms of logical address space,
4762 * and compare that answer with the max chunk size
4763 */
4764 if (stripe_size * data_stripes > max_chunk_size) {
4765 u64 mask = (1ULL << 24) - 1;
David Sterbab8b93ad2015-01-16 17:26:13 +01004766
4767 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004768
4769 /* bump the answer up to a 16MB boundary */
4770 stripe_size = (stripe_size + mask) & ~mask;
4771
4772 /* but don't go higher than the limits we found
4773 * while searching for free extents
4774 */
4775 if (stripe_size > devices_info[ndevs-1].max_avail)
4776 stripe_size = devices_info[ndevs-1].max_avail;
4777 }
4778
David Sterbab8b93ad2015-01-16 17:26:13 +01004779 stripe_size = div_u64(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004780
4781 /* align to BTRFS_STRIPE_LEN */
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004782 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
Arne Jansen73c5de02011-04-12 12:07:57 +02004783
Miao Xieb2117a32011-01-05 10:07:28 +00004784 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4785 if (!map) {
4786 ret = -ENOMEM;
4787 goto error;
4788 }
4789 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004790
Arne Jansen73c5de02011-04-12 12:07:57 +02004791 for (i = 0; i < ndevs; ++i) {
4792 for (j = 0; j < dev_stripes; ++j) {
4793 int s = i * dev_stripes + j;
4794 map->stripes[s].dev = devices_info[i].dev;
4795 map->stripes[s].physical = devices_info[i].dev_offset +
4796 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004797 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004798 }
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004799 map->stripe_len = BTRFS_STRIPE_LEN;
4800 map->io_align = BTRFS_STRIPE_LEN;
4801 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04004802 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004803 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004804
David Woodhouse53b381b2013-01-29 18:40:14 -05004805 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004806
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004807 trace_btrfs_chunk_alloc(info, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004808
David Sterba172ddd62011-04-21 00:48:27 +02004809 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004810 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004811 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004812 ret = -ENOMEM;
4813 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004814 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004815 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04004816 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05004817 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004818 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004819 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004820 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004821 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004822
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004823 em_tree = &info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004824 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004825 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004826 if (ret) {
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03004827 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004828 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004829 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004830 }
Yan Zheng2b820322008-11-17 21:11:30 -05004831
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03004832 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4833 refcount_inc(&em->refs);
4834 write_unlock(&em_tree->lock);
4835
Nikolay Borisov01744842017-07-27 14:22:11 +03004836 ret = btrfs_make_block_group(trans, info, 0, type, start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004837 if (ret)
4838 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004839
Miao Xie7cc8e582014-09-03 21:35:38 +08004840 for (i = 0; i < map->num_stripes; i++) {
4841 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4842 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4843 }
Miao Xie43530c42014-09-03 21:35:36 +08004844
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004845 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
Miao Xie1c116182014-09-03 21:35:37 +08004846
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004847 free_extent_map(em);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004848 check_raid56_incompat_flag(info, type);
David Woodhouse53b381b2013-01-29 18:40:14 -05004849
Miao Xieb2117a32011-01-05 10:07:28 +00004850 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004851 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004852
Josef Bacik6df9a952013-06-27 13:22:46 -04004853error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004854 write_lock(&em_tree->lock);
4855 remove_extent_mapping(em_tree, em);
4856 write_unlock(&em_tree->lock);
4857
4858 /* One for our allocation */
4859 free_extent_map(em);
4860 /* One for the tree reference */
4861 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004862 /* One for the pending_chunks list reference */
4863 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004864error:
Miao Xieb2117a32011-01-05 10:07:28 +00004865 kfree(devices_info);
4866 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004867}
4868
Josef Bacik6df9a952013-06-27 13:22:46 -04004869int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004870 struct btrfs_fs_info *fs_info,
Josef Bacik6df9a952013-06-27 13:22:46 -04004871 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004872{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004873 struct btrfs_root *extent_root = fs_info->extent_root;
4874 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05004875 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05004876 struct btrfs_device *device;
4877 struct btrfs_chunk *chunk;
4878 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004879 struct extent_map *em;
4880 struct map_lookup *map;
4881 size_t item_size;
4882 u64 dev_offset;
4883 u64 stripe_size;
4884 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08004885 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004886
Liu Bo592d92e2017-03-14 13:33:55 -07004887 em = get_chunk_map(fs_info, chunk_offset, chunk_size);
4888 if (IS_ERR(em))
4889 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04004890
Jeff Mahoney95617d62015-06-03 10:55:48 -04004891 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04004892 item_size = btrfs_chunk_item_size(map->num_stripes);
4893 stripe_size = em->orig_block_len;
4894
4895 chunk = kzalloc(item_size, GFP_NOFS);
4896 if (!chunk) {
4897 ret = -ENOMEM;
4898 goto out;
4899 }
4900
Filipe Manana50460e32015-11-20 10:42:47 +00004901 /*
4902 * Take the device list mutex to prevent races with the final phase of
4903 * a device replace operation that replaces the device object associated
4904 * with the map's stripes, because the device object's id can change
4905 * at any time during that final phase of the device replace operation
4906 * (dev-replace.c:btrfs_dev_replace_finishing()).
4907 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004908 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04004909 for (i = 0; i < map->num_stripes; i++) {
4910 device = map->stripes[i].dev;
4911 dev_offset = map->stripes[i].physical;
4912
Yan Zheng2b820322008-11-17 21:11:30 -05004913 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004914 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004915 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03004916 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
4917 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04004918 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004919 break;
4920 }
4921 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004922 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00004923 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004924 }
4925
Yan Zheng2b820322008-11-17 21:11:30 -05004926 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004927 for (i = 0; i < map->num_stripes; i++) {
4928 device = map->stripes[i].dev;
4929 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004930
4931 btrfs_set_stack_stripe_devid(stripe, device->devid);
4932 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4933 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4934 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004935 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004936 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05004937
4938 btrfs_set_stack_chunk_length(chunk, chunk_size);
4939 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4940 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4941 btrfs_set_stack_chunk_type(chunk, map->type);
4942 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4943 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4944 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004945 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05004946 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4947
4948 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4949 key.type = BTRFS_CHUNK_ITEM_KEY;
4950 key.offset = chunk_offset;
4951
4952 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004953 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4954 /*
4955 * TODO: Cleanup of inserted chunk root in case of
4956 * failure.
4957 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004958 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004959 }
liubo1abe9b82011-03-24 11:18:59 +00004960
Josef Bacik6df9a952013-06-27 13:22:46 -04004961out:
Yan Zheng2b820322008-11-17 21:11:30 -05004962 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004963 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004964 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004965}
4966
4967/*
4968 * Chunk allocation falls into two parts. The first part does works
4969 * that make the new allocated chunk useable, but not do any operation
4970 * that modifies the chunk tree. The second part does the works that
4971 * require modifying the chunk tree. This division is important for the
4972 * bootstrap process of adding storage to a seed btrfs.
4973 */
4974int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004975 struct btrfs_fs_info *fs_info, u64 type)
Yan Zheng2b820322008-11-17 21:11:30 -05004976{
4977 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004978
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004979 ASSERT(mutex_is_locked(&fs_info->chunk_mutex));
4980 chunk_offset = find_next_chunk(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01004981 return __btrfs_alloc_chunk(trans, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004982}
4983
Chris Masond3977122009-01-05 21:25:51 -05004984static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +01004985 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05004986{
4987 u64 chunk_offset;
4988 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004989 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004990 int ret;
4991
Josef Bacik6df9a952013-06-27 13:22:46 -04004992 chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04004993 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01004994 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004995 if (ret)
4996 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004997
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004998 sys_chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04004999 alloc_profile = btrfs_system_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005000 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005001 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005002}
5003
Miao Xied20983b2014-07-03 18:22:13 +08005004static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5005{
5006 int max_errors;
5007
5008 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5009 BTRFS_BLOCK_GROUP_RAID10 |
5010 BTRFS_BLOCK_GROUP_RAID5 |
5011 BTRFS_BLOCK_GROUP_DUP)) {
5012 max_errors = 1;
5013 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5014 max_errors = 2;
5015 } else {
5016 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005017 }
5018
Miao Xied20983b2014-07-03 18:22:13 +08005019 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05005020}
5021
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005022int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005023{
5024 struct extent_map *em;
5025 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005026 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005027 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005028 int i;
5029
Liu Bo592d92e2017-03-14 13:33:55 -07005030 em = get_chunk_map(fs_info, chunk_offset, 1);
5031 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005032 return 1;
5033
Jeff Mahoney95617d62015-06-03 10:55:48 -04005034 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005035 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08005036 if (map->stripes[i].dev->missing) {
5037 miss_ndevs++;
5038 continue;
5039 }
5040
Yan Zheng2b820322008-11-17 21:11:30 -05005041 if (!map->stripes[i].dev->writeable) {
5042 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005043 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005044 }
5045 }
Miao Xied20983b2014-07-03 18:22:13 +08005046
5047 /*
5048 * If the number of missing devices is larger than max errors,
5049 * we can not write the data into that chunk successfully, so
5050 * set it readonly.
5051 */
5052 if (miss_ndevs > btrfs_chunk_max_errors(map))
5053 readonly = 1;
5054end:
Yan Zheng2b820322008-11-17 21:11:30 -05005055 free_extent_map(em);
5056 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005057}
5058
5059void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
5060{
David Sterbaa8067e02011-04-21 00:34:43 +02005061 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04005062}
5063
5064void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
5065{
5066 struct extent_map *em;
5067
Chris Masond3977122009-01-05 21:25:51 -05005068 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04005069 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005070 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
5071 if (em)
5072 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005073 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005074 if (!em)
5075 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005076 /* once for us */
5077 free_extent_map(em);
5078 /* once for the tree */
5079 free_extent_map(em);
5080 }
5081}
5082
Stefan Behrens5d964052012-11-05 14:59:07 +01005083int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005084{
5085 struct extent_map *em;
5086 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005087 int ret;
5088
Liu Bo592d92e2017-03-14 13:33:55 -07005089 em = get_chunk_map(fs_info, logical, len);
5090 if (IS_ERR(em))
5091 /*
5092 * We could return errors for these cases, but that could get
5093 * ugly and we'd probably do the same thing which is just not do
5094 * anything else and exit, so return 1 so the callers don't try
5095 * to use other copies.
5096 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005097 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005098
Jeff Mahoney95617d62015-06-03 10:55:48 -04005099 map = em->map_lookup;
Chris Masonf1885912008-04-09 16:28:12 -04005100 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5101 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005102 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5103 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005104 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5105 ret = 2;
5106 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5107 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04005108 else
5109 ret = 1;
5110 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005111
Liu Bo73beece2015-07-17 16:49:19 +08005112 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Liu Bo6fad8232017-03-14 13:33:59 -07005113 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5114 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005115 ret++;
Liu Bo73beece2015-07-17 16:49:19 +08005116 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005117
Chris Masonf1885912008-04-09 16:28:12 -04005118 return ret;
5119}
5120
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005121unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005122 u64 logical)
5123{
5124 struct extent_map *em;
5125 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005126 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005127
Liu Bo592d92e2017-03-14 13:33:55 -07005128 em = get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005129
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005130 if (!WARN_ON(IS_ERR(em))) {
5131 map = em->map_lookup;
5132 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5133 len = map->stripe_len * nr_data_stripes(map);
5134 free_extent_map(em);
5135 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005136 return len;
5137}
5138
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005139int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005140{
5141 struct extent_map *em;
5142 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005143 int ret = 0;
5144
Liu Bo592d92e2017-03-14 13:33:55 -07005145 em = get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005146
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005147 if(!WARN_ON(IS_ERR(em))) {
5148 map = em->map_lookup;
5149 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5150 ret = 1;
5151 free_extent_map(em);
5152 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005153 return ret;
5154}
5155
Stefan Behrens30d98612012-11-06 14:52:18 +01005156static int find_live_mirror(struct btrfs_fs_info *fs_info,
5157 struct map_lookup *map, int first, int num,
5158 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005159{
5160 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01005161 int tolerance;
5162 struct btrfs_device *srcdev;
5163
5164 if (dev_replace_is_ongoing &&
5165 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5166 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5167 srcdev = fs_info->dev_replace.srcdev;
5168 else
5169 srcdev = NULL;
5170
5171 /*
5172 * try to avoid the drive that is the source drive for a
5173 * dev-replace procedure, only choose it if no other non-missing
5174 * mirror is available
5175 */
5176 for (tolerance = 0; tolerance < 2; tolerance++) {
5177 if (map->stripes[optimal].dev->bdev &&
5178 (tolerance || map->stripes[optimal].dev != srcdev))
5179 return optimal;
5180 for (i = first; i < first + num; i++) {
5181 if (map->stripes[i].dev->bdev &&
5182 (tolerance || map->stripes[i].dev != srcdev))
5183 return i;
5184 }
Chris Masondfe25022008-05-13 13:46:40 -04005185 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005186
Chris Masondfe25022008-05-13 13:46:40 -04005187 /* we couldn't find one that doesn't fail. Just return something
5188 * and the io error handling code will clean up eventually
5189 */
5190 return optimal;
5191}
5192
David Woodhouse53b381b2013-01-29 18:40:14 -05005193static inline int parity_smaller(u64 a, u64 b)
5194{
5195 return a > b;
5196}
5197
5198/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005199static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005200{
5201 struct btrfs_bio_stripe s;
5202 int i;
5203 u64 l;
5204 int again = 1;
5205
5206 while (again) {
5207 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005208 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005209 if (parity_smaller(bbio->raid_map[i],
5210 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005211 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005212 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005213 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005214 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005215 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005216 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005217
David Woodhouse53b381b2013-01-29 18:40:14 -05005218 again = 1;
5219 }
5220 }
5221 }
5222}
5223
Zhao Lei6e9606d2015-01-20 15:11:34 +08005224static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5225{
5226 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005227 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005228 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005229 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005230 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005231 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005232 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005233 /*
5234 * plus the raid_map, which includes both the tgt dev
5235 * and the stripes
5236 */
5237 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005238 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005239
5240 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005241 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005242
5243 return bbio;
5244}
5245
5246void btrfs_get_bbio(struct btrfs_bio *bbio)
5247{
Elena Reshetova140475a2017-03-03 10:55:10 +02005248 WARN_ON(!refcount_read(&bbio->refs));
5249 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005250}
5251
5252void btrfs_put_bbio(struct btrfs_bio *bbio)
5253{
5254 if (!bbio)
5255 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005256 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005257 kfree(bbio);
5258}
5259
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005260/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5261/*
5262 * Please note that, discard won't be sent to target device of device
5263 * replace.
5264 */
5265static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5266 u64 logical, u64 length,
5267 struct btrfs_bio **bbio_ret)
5268{
5269 struct extent_map *em;
5270 struct map_lookup *map;
5271 struct btrfs_bio *bbio;
5272 u64 offset;
5273 u64 stripe_nr;
5274 u64 stripe_nr_end;
5275 u64 stripe_end_offset;
5276 u64 stripe_cnt;
5277 u64 stripe_len;
5278 u64 stripe_offset;
5279 u64 num_stripes;
5280 u32 stripe_index;
5281 u32 factor = 0;
5282 u32 sub_stripes = 0;
5283 u64 stripes_per_dev = 0;
5284 u32 remaining_stripes = 0;
5285 u32 last_stripe = 0;
5286 int ret = 0;
5287 int i;
5288
5289 /* discard always return a bbio */
5290 ASSERT(bbio_ret);
5291
5292 em = get_chunk_map(fs_info, logical, length);
5293 if (IS_ERR(em))
5294 return PTR_ERR(em);
5295
5296 map = em->map_lookup;
5297 /* we don't discard raid56 yet */
5298 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5299 ret = -EOPNOTSUPP;
5300 goto out;
5301 }
5302
5303 offset = logical - em->start;
5304 length = min_t(u64, em->len - offset, length);
5305
5306 stripe_len = map->stripe_len;
5307 /*
5308 * stripe_nr counts the total number of stripes we have to stride
5309 * to get to this block
5310 */
5311 stripe_nr = div64_u64(offset, stripe_len);
5312
5313 /* stripe_offset is the offset of this block in its stripe */
5314 stripe_offset = offset - stripe_nr * stripe_len;
5315
5316 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005317 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005318 stripe_cnt = stripe_nr_end - stripe_nr;
5319 stripe_end_offset = stripe_nr_end * map->stripe_len -
5320 (offset + length);
5321 /*
5322 * after this, stripe_nr is the number of stripes on this
5323 * device we have to walk to find the data, and stripe_index is
5324 * the number of our device in the stripe array
5325 */
5326 num_stripes = 1;
5327 stripe_index = 0;
5328 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5329 BTRFS_BLOCK_GROUP_RAID10)) {
5330 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5331 sub_stripes = 1;
5332 else
5333 sub_stripes = map->sub_stripes;
5334
5335 factor = map->num_stripes / sub_stripes;
5336 num_stripes = min_t(u64, map->num_stripes,
5337 sub_stripes * stripe_cnt);
5338 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5339 stripe_index *= sub_stripes;
5340 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5341 &remaining_stripes);
5342 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5343 last_stripe *= sub_stripes;
5344 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5345 BTRFS_BLOCK_GROUP_DUP)) {
5346 num_stripes = map->num_stripes;
5347 } else {
5348 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5349 &stripe_index);
5350 }
5351
5352 bbio = alloc_btrfs_bio(num_stripes, 0);
5353 if (!bbio) {
5354 ret = -ENOMEM;
5355 goto out;
5356 }
5357
5358 for (i = 0; i < num_stripes; i++) {
5359 bbio->stripes[i].physical =
5360 map->stripes[stripe_index].physical +
5361 stripe_offset + stripe_nr * map->stripe_len;
5362 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5363
5364 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5365 BTRFS_BLOCK_GROUP_RAID10)) {
5366 bbio->stripes[i].length = stripes_per_dev *
5367 map->stripe_len;
5368
5369 if (i / sub_stripes < remaining_stripes)
5370 bbio->stripes[i].length +=
5371 map->stripe_len;
5372
5373 /*
5374 * Special for the first stripe and
5375 * the last stripe:
5376 *
5377 * |-------|...|-------|
5378 * |----------|
5379 * off end_off
5380 */
5381 if (i < sub_stripes)
5382 bbio->stripes[i].length -=
5383 stripe_offset;
5384
5385 if (stripe_index >= last_stripe &&
5386 stripe_index <= (last_stripe +
5387 sub_stripes - 1))
5388 bbio->stripes[i].length -=
5389 stripe_end_offset;
5390
5391 if (i == sub_stripes - 1)
5392 stripe_offset = 0;
5393 } else {
5394 bbio->stripes[i].length = length;
5395 }
5396
5397 stripe_index++;
5398 if (stripe_index == map->num_stripes) {
5399 stripe_index = 0;
5400 stripe_nr++;
5401 }
5402 }
5403
5404 *bbio_ret = bbio;
5405 bbio->map_type = map->type;
5406 bbio->num_stripes = num_stripes;
5407out:
5408 free_extent_map(em);
5409 return ret;
5410}
5411
Liu Bo5ab56092017-03-14 13:33:57 -07005412/*
5413 * In dev-replace case, for repair case (that's the only case where the mirror
5414 * is selected explicitly when calling btrfs_map_block), blocks left of the
5415 * left cursor can also be read from the target drive.
5416 *
5417 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5418 * array of stripes.
5419 * For READ, it also needs to be supported using the same mirror number.
5420 *
5421 * If the requested block is not left of the left cursor, EIO is returned. This
5422 * can happen because btrfs_num_copies() returns one more in the dev-replace
5423 * case.
5424 */
5425static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5426 u64 logical, u64 length,
5427 u64 srcdev_devid, int *mirror_num,
5428 u64 *physical)
5429{
5430 struct btrfs_bio *bbio = NULL;
5431 int num_stripes;
5432 int index_srcdev = 0;
5433 int found = 0;
5434 u64 physical_of_found = 0;
5435 int i;
5436 int ret = 0;
5437
5438 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5439 logical, &length, &bbio, 0, 0);
5440 if (ret) {
5441 ASSERT(bbio == NULL);
5442 return ret;
5443 }
5444
5445 num_stripes = bbio->num_stripes;
5446 if (*mirror_num > num_stripes) {
5447 /*
5448 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5449 * that means that the requested area is not left of the left
5450 * cursor
5451 */
5452 btrfs_put_bbio(bbio);
5453 return -EIO;
5454 }
5455
5456 /*
5457 * process the rest of the function using the mirror_num of the source
5458 * drive. Therefore look it up first. At the end, patch the device
5459 * pointer to the one of the target drive.
5460 */
5461 for (i = 0; i < num_stripes; i++) {
5462 if (bbio->stripes[i].dev->devid != srcdev_devid)
5463 continue;
5464
5465 /*
5466 * In case of DUP, in order to keep it simple, only add the
5467 * mirror with the lowest physical address
5468 */
5469 if (found &&
5470 physical_of_found <= bbio->stripes[i].physical)
5471 continue;
5472
5473 index_srcdev = i;
5474 found = 1;
5475 physical_of_found = bbio->stripes[i].physical;
5476 }
5477
5478 btrfs_put_bbio(bbio);
5479
5480 ASSERT(found);
5481 if (!found)
5482 return -EIO;
5483
5484 *mirror_num = index_srcdev + 1;
5485 *physical = physical_of_found;
5486 return ret;
5487}
5488
Liu Bo73c0f222017-03-14 13:33:58 -07005489static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5490 struct btrfs_bio **bbio_ret,
5491 struct btrfs_dev_replace *dev_replace,
5492 int *num_stripes_ret, int *max_errors_ret)
5493{
5494 struct btrfs_bio *bbio = *bbio_ret;
5495 u64 srcdev_devid = dev_replace->srcdev->devid;
5496 int tgtdev_indexes = 0;
5497 int num_stripes = *num_stripes_ret;
5498 int max_errors = *max_errors_ret;
5499 int i;
5500
5501 if (op == BTRFS_MAP_WRITE) {
5502 int index_where_to_add;
5503
5504 /*
5505 * duplicate the write operations while the dev replace
5506 * procedure is running. Since the copying of the old disk to
5507 * the new disk takes place at run time while the filesystem is
5508 * mounted writable, the regular write operations to the old
5509 * disk have to be duplicated to go to the new disk as well.
5510 *
5511 * Note that device->missing is handled by the caller, and that
5512 * the write to the old disk is already set up in the stripes
5513 * array.
5514 */
5515 index_where_to_add = num_stripes;
5516 for (i = 0; i < num_stripes; i++) {
5517 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5518 /* write to new disk, too */
5519 struct btrfs_bio_stripe *new =
5520 bbio->stripes + index_where_to_add;
5521 struct btrfs_bio_stripe *old =
5522 bbio->stripes + i;
5523
5524 new->physical = old->physical;
5525 new->length = old->length;
5526 new->dev = dev_replace->tgtdev;
5527 bbio->tgtdev_map[i] = index_where_to_add;
5528 index_where_to_add++;
5529 max_errors++;
5530 tgtdev_indexes++;
5531 }
5532 }
5533 num_stripes = index_where_to_add;
5534 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5535 int index_srcdev = 0;
5536 int found = 0;
5537 u64 physical_of_found = 0;
5538
5539 /*
5540 * During the dev-replace procedure, the target drive can also
5541 * be used to read data in case it is needed to repair a corrupt
5542 * block elsewhere. This is possible if the requested area is
5543 * left of the left cursor. In this area, the target drive is a
5544 * full copy of the source drive.
5545 */
5546 for (i = 0; i < num_stripes; i++) {
5547 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5548 /*
5549 * In case of DUP, in order to keep it simple,
5550 * only add the mirror with the lowest physical
5551 * address
5552 */
5553 if (found &&
5554 physical_of_found <=
5555 bbio->stripes[i].physical)
5556 continue;
5557 index_srcdev = i;
5558 found = 1;
5559 physical_of_found = bbio->stripes[i].physical;
5560 }
5561 }
5562 if (found) {
5563 struct btrfs_bio_stripe *tgtdev_stripe =
5564 bbio->stripes + num_stripes;
5565
5566 tgtdev_stripe->physical = physical_of_found;
5567 tgtdev_stripe->length =
5568 bbio->stripes[index_srcdev].length;
5569 tgtdev_stripe->dev = dev_replace->tgtdev;
5570 bbio->tgtdev_map[index_srcdev] = num_stripes;
5571
5572 tgtdev_indexes++;
5573 num_stripes++;
5574 }
5575 }
5576
5577 *num_stripes_ret = num_stripes;
5578 *max_errors_ret = max_errors;
5579 bbio->num_tgtdevs = tgtdev_indexes;
5580 *bbio_ret = bbio;
5581}
5582
Liu Bo2b19a1f2017-03-14 13:34:00 -07005583static bool need_full_stripe(enum btrfs_map_op op)
5584{
5585 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5586}
5587
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005588static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5589 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005590 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005591 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005592 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005593{
5594 struct extent_map *em;
5595 struct map_lookup *map;
Chris Mason0b86a832008-03-24 15:01:56 -04005596 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005597 u64 stripe_offset;
5598 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005599 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005600 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005601 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005602 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005603 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005604 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005605 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005606 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005607 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5608 int dev_replace_is_ongoing = 0;
5609 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005610 int patch_the_first_stripe_for_dev_replace = 0;
5611 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005612 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005613
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005614 if (op == BTRFS_MAP_DISCARD)
5615 return __btrfs_map_block_for_discard(fs_info, logical,
5616 *length, bbio_ret);
5617
Liu Bo592d92e2017-03-14 13:33:55 -07005618 em = get_chunk_map(fs_info, logical, *length);
5619 if (IS_ERR(em))
5620 return PTR_ERR(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005621
Jeff Mahoney95617d62015-06-03 10:55:48 -04005622 map = em->map_lookup;
Chris Mason0b86a832008-03-24 15:01:56 -04005623 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005624
David Woodhouse53b381b2013-01-29 18:40:14 -05005625 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005626 stripe_nr = offset;
5627 /*
5628 * stripe_nr counts the total number of stripes we have to stride
5629 * to get to this block
5630 */
David Sterba47c57132015-02-20 18:43:47 +01005631 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005632
David Woodhouse53b381b2013-01-29 18:40:14 -05005633 stripe_offset = stripe_nr * stripe_len;
Josef Bacike042d1e2016-04-12 12:54:40 -04005634 if (offset < stripe_offset) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005635 btrfs_crit(fs_info,
5636 "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
Josef Bacike042d1e2016-04-12 12:54:40 -04005637 stripe_offset, offset, em->start, logical,
5638 stripe_len);
5639 free_extent_map(em);
5640 return -EINVAL;
5641 }
Chris Mason593060d2008-03-25 16:50:33 -04005642
5643 /* stripe_offset is the offset of this block in its stripe*/
5644 stripe_offset = offset - stripe_offset;
5645
David Woodhouse53b381b2013-01-29 18:40:14 -05005646 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005647 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005648 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5649 raid56_full_stripe_start = offset;
5650
5651 /* allow a write of a full stripe, but make sure we don't
5652 * allow straddling of stripes
5653 */
David Sterba47c57132015-02-20 18:43:47 +01005654 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5655 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005656 raid56_full_stripe_start *= full_stripe_len;
5657 }
5658
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005659 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005660 u64 max_len;
5661 /* For writes to RAID[56], allow a full stripeset across all disks.
5662 For other RAID types and for RAID[56] reads, just allow a single
5663 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005664 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005665 (op == BTRFS_MAP_WRITE)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005666 max_len = stripe_len * nr_data_stripes(map) -
5667 (offset - raid56_full_stripe_start);
5668 } else {
5669 /* we limit the length of each bio to what fits in a stripe */
5670 max_len = stripe_len - stripe_offset;
5671 }
5672 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005673 } else {
5674 *length = em->len - offset;
5675 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005676
David Woodhouse53b381b2013-01-29 18:40:14 -05005677 /* This is for when we're called from btrfs_merge_bio_hook() and all
5678 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005679 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005680 goto out;
5681
Liu Bo73beece2015-07-17 16:49:19 +08005682 btrfs_dev_replace_lock(dev_replace, 0);
Stefan Behrens472262f2012-11-06 14:43:46 +01005683 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5684 if (!dev_replace_is_ongoing)
Liu Bo73beece2015-07-17 16:49:19 +08005685 btrfs_dev_replace_unlock(dev_replace, 0);
5686 else
5687 btrfs_dev_replace_set_lock_blocking(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005688
Stefan Behrensad6d6202012-11-06 15:06:47 +01005689 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005690 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07005691 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
5692 dev_replace->srcdev->devid,
5693 &mirror_num,
5694 &physical_to_patch_in_first_stripe);
5695 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005696 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07005697 else
5698 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005699 } else if (mirror_num > map->num_stripes) {
5700 mirror_num = 0;
5701 }
5702
Chris Masonf2d8d742008-04-21 10:03:05 -04005703 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005704 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005705 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01005706 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5707 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005708 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08005709 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005710 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Anand Jainde483732017-10-12 16:43:00 +08005711 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005712 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005713 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005714 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005715 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005716 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005717 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005718 current->pid % map->num_stripes,
5719 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005720 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005721 }
Chris Mason2fff7342008-04-29 14:12:09 -04005722
Chris Mason611f0e02008-04-03 16:29:03 -04005723 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Anand Jainde483732017-10-12 16:43:00 +08005724 if (need_full_stripe(op)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005725 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005726 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005727 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005728 } else {
5729 mirror_num = 1;
5730 }
Chris Mason2fff7342008-04-29 14:12:09 -04005731
Chris Mason321aecc2008-04-16 10:49:51 -04005732 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005733 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005734
David Sterba47c57132015-02-20 18:43:47 +01005735 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005736 stripe_index *= map->sub_stripes;
5737
Anand Jainde483732017-10-12 16:43:00 +08005738 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005739 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005740 else if (mirror_num)
5741 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005742 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005743 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005744 stripe_index = find_live_mirror(fs_info, map,
5745 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005746 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005747 current->pid % map->sub_stripes,
5748 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005749 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005750 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005751
Zhao Leiffe2d202015-01-20 15:11:44 +08005752 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08005753 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005754 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07005755 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbab8b93ad2015-01-16 17:26:13 +01005756 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005757
5758 /* RAID[56] write or recovery. Return all stripes */
5759 num_stripes = map->num_stripes;
5760 max_errors = nr_parity_stripes(map);
5761
David Woodhouse53b381b2013-01-29 18:40:14 -05005762 *length = map->stripe_len;
5763 stripe_index = 0;
5764 stripe_offset = 0;
5765 } else {
5766 /*
5767 * Mirror #0 or #1 means the original data block.
5768 * Mirror #2 is RAID5 parity block.
5769 * Mirror #3 is RAID6 Q block.
5770 */
David Sterba47c57132015-02-20 18:43:47 +01005771 stripe_nr = div_u64_rem(stripe_nr,
5772 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005773 if (mirror_num > 1)
5774 stripe_index = nr_data_stripes(map) +
5775 mirror_num - 2;
5776
5777 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005778 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5779 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005780 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08005781 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005782 }
Chris Mason8790d502008-04-03 16:29:03 -04005783 } else {
5784 /*
David Sterba47c57132015-02-20 18:43:47 +01005785 * after this, stripe_nr is the number of stripes on this
5786 * device we have to walk to find the data, and stripe_index is
5787 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005788 */
David Sterba47c57132015-02-20 18:43:47 +01005789 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5790 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005791 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005792 }
Josef Bacike042d1e2016-04-12 12:54:40 -04005793 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005794 btrfs_crit(fs_info,
5795 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04005796 stripe_index, map->num_stripes);
5797 ret = -EINVAL;
5798 goto out;
5799 }
Chris Mason593060d2008-03-25 16:50:33 -04005800
Stefan Behrens472262f2012-11-06 14:43:46 +01005801 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07005802 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005803 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005804 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005805 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005806 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005807 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005808 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005809
Zhao Lei6e9606d2015-01-20 15:11:34 +08005810 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005811 if (!bbio) {
5812 ret = -ENOMEM;
5813 goto out;
5814 }
Liu Bo6fad8232017-03-14 13:33:59 -07005815 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
Miao Xie2c8cdd62014-11-14 16:06:25 +08005816 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005817
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005818 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07005819 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
5820 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005821 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005822 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005823
5824 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5825 sizeof(struct btrfs_bio_stripe) *
5826 num_alloc_stripes +
5827 sizeof(int) * tgtdev_indexes);
5828
5829 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005830 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005831
5832 /* Fill in the logical address of each stripe */
5833 tmp = stripe_nr * nr_data_stripes(map);
5834 for (i = 0; i < nr_data_stripes(map); i++)
5835 bbio->raid_map[(i+rot) % num_stripes] =
5836 em->start + (tmp + i) * map->stripe_len;
5837
5838 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5839 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5840 bbio->raid_map[(i+rot+1) % num_stripes] =
5841 RAID6_Q_STRIPE;
5842 }
5843
Li Zefanec9ef7a2011-12-01 14:06:42 +08005844
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005845 for (i = 0; i < num_stripes; i++) {
5846 bbio->stripes[i].physical =
5847 map->stripes[stripe_index].physical +
5848 stripe_offset +
5849 stripe_nr * map->stripe_len;
5850 bbio->stripes[i].dev =
5851 map->stripes[stripe_index].dev;
5852 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04005853 }
Li Zefande11cc12011-12-01 12:55:47 +08005854
Liu Bo2b19a1f2017-03-14 13:34:00 -07005855 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08005856 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005857
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005858 if (bbio->raid_map)
5859 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005860
Liu Bo73c0f222017-03-14 13:33:58 -07005861 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005862 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07005863 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
5864 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01005865 }
5866
Li Zefande11cc12011-12-01 12:55:47 +08005867 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005868 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005869 bbio->num_stripes = num_stripes;
5870 bbio->max_errors = max_errors;
5871 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005872
5873 /*
5874 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5875 * mirror_num == num_stripes + 1 && dev_replace target drive is
5876 * available as a mirror
5877 */
5878 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5879 WARN_ON(num_stripes > 1);
5880 bbio->stripes[0].dev = dev_replace->tgtdev;
5881 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5882 bbio->mirror_num = map->num_stripes + 1;
5883 }
Chris Masoncea9e442008-04-09 16:28:12 -04005884out:
Liu Bo73beece2015-07-17 16:49:19 +08005885 if (dev_replace_is_ongoing) {
5886 btrfs_dev_replace_clear_lock_blocking(dev_replace);
5887 btrfs_dev_replace_unlock(dev_replace, 0);
5888 }
Chris Mason0b86a832008-03-24 15:01:56 -04005889 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005890 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005891}
5892
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005893int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005894 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005895 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005896{
Mike Christieb3d3fa52016-06-05 14:31:53 -05005897 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005898 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005899}
5900
Miao Xieaf8e2d12014-10-23 14:42:50 +08005901/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005902int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08005903 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02005904 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005905{
David Sterba825ad4c2017-03-28 14:45:22 +02005906 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005907}
5908
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005909int btrfs_rmap_block(struct btrfs_fs_info *fs_info,
Yan Zhenga512bbf2008-12-08 16:46:26 -05005910 u64 chunk_start, u64 physical, u64 devid,
5911 u64 **logical, int *naddrs, int *stripe_len)
5912{
Yan Zhenga512bbf2008-12-08 16:46:26 -05005913 struct extent_map *em;
5914 struct map_lookup *map;
5915 u64 *buf;
5916 u64 bytenr;
5917 u64 length;
5918 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005919 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005920 int i, j, nr = 0;
5921
Liu Bo592d92e2017-03-14 13:33:55 -07005922 em = get_chunk_map(fs_info, chunk_start, 1);
5923 if (IS_ERR(em))
Josef Bacik835d9742013-03-19 12:13:25 -04005924 return -EIO;
Josef Bacik835d9742013-03-19 12:13:25 -04005925
Jeff Mahoney95617d62015-06-03 10:55:48 -04005926 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005927 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005928 rmap_len = map->stripe_len;
5929
Yan Zhenga512bbf2008-12-08 16:46:26 -05005930 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01005931 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005932 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01005933 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08005934 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01005935 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005936 rmap_len = map->stripe_len * nr_data_stripes(map);
5937 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005938
David Sterba31e818f2015-02-20 18:00:26 +01005939 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005940 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005941
5942 for (i = 0; i < map->num_stripes; i++) {
5943 if (devid && map->stripes[i].dev->devid != devid)
5944 continue;
5945 if (map->stripes[i].physical > physical ||
5946 map->stripes[i].physical + length <= physical)
5947 continue;
5948
5949 stripe_nr = physical - map->stripes[i].physical;
Liu Bo42c61ab2017-04-03 13:45:24 -07005950 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005951
5952 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5953 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01005954 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005955 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5956 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005957 } /* else if RAID[56], multiply by nr_data_stripes().
5958 * Alternatively, just use rmap_len below instead of
5959 * map->stripe_len */
5960
5961 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005962 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005963 for (j = 0; j < nr; j++) {
5964 if (buf[j] == bytenr)
5965 break;
5966 }
Chris Mason934d3752008-12-08 16:43:10 -05005967 if (j == nr) {
5968 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005969 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005970 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005971 }
5972
Yan Zhenga512bbf2008-12-08 16:46:26 -05005973 *logical = buf;
5974 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005975 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005976
5977 free_extent_map(em);
5978 return 0;
5979}
5980
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005981static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08005982{
Mike Snitzer326e1db2015-05-22 09:14:03 -04005983 bio->bi_private = bbio->private;
5984 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005985 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04005986
Zhao Lei6e9606d2015-01-20 15:11:34 +08005987 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08005988}
5989
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005990static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04005991{
Chris Mason9be33952013-05-17 18:30:14 -04005992 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005993 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005994
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02005995 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005996 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02005997 if (bio->bi_status == BLK_STS_IOERR ||
5998 bio->bi_status == BLK_STS_TARGET) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02005999 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04006000 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08006001 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006002
6003 BUG_ON(stripe_index >= bbio->num_stripes);
6004 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02006005 if (dev->bdev) {
Mike Christie37226b22016-06-05 14:31:52 -05006006 if (bio_op(bio) == REQ_OP_WRITE)
Stefan Behrens597a60f2012-06-14 16:42:31 +02006007 btrfs_dev_stat_inc(dev,
6008 BTRFS_DEV_STAT_WRITE_ERRS);
6009 else
6010 btrfs_dev_stat_inc(dev,
6011 BTRFS_DEV_STAT_READ_ERRS);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06006012 if (bio->bi_opf & REQ_PREFLUSH)
Stefan Behrens597a60f2012-06-14 16:42:31 +02006013 btrfs_dev_stat_inc(dev,
6014 BTRFS_DEV_STAT_FLUSH_ERRS);
6015 btrfs_dev_stat_print_on_error(dev);
6016 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02006017 }
6018 }
Chris Mason8790d502008-04-03 16:29:03 -04006019
Jan Schmidta1d3c472011-08-04 17:15:33 +02006020 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006021 is_orig_bio = 1;
6022
Miao Xiec404e0d2014-01-30 16:46:55 +08006023 btrfs_bio_counter_dec(bbio->fs_info);
6024
Jan Schmidta1d3c472011-08-04 17:15:33 +02006025 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006026 if (!is_orig_bio) {
6027 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006028 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006029 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006030
Chris Mason9be33952013-05-17 18:30:14 -04006031 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006032 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006033 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006034 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006035 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006036 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006037 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006038 /*
6039 * this bio is actually up to date, we didn't
6040 * go over the max number of errors
6041 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006042 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006043 }
Miao Xiec55f1392014-06-19 10:42:54 +08006044
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006045 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006046 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006047 bio_put(bio);
6048 }
Chris Mason8790d502008-04-03 16:29:03 -04006049}
6050
Chris Mason8b712842008-06-11 16:50:36 -04006051/*
6052 * see run_scheduled_bios for a description of why bios are collected for
6053 * async submit.
6054 *
6055 * This will add one bio to the pending list for a device and make sure
6056 * the work struct is scheduled.
6057 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006058static noinline void btrfs_schedule_bio(struct btrfs_device *device,
Mike Christie4e49ea42016-06-05 14:31:41 -05006059 struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04006060{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006061 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -04006062 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04006063 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006064
David Woodhouse53b381b2013-01-29 18:40:14 -05006065 if (device->missing || !device->bdev) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006066 bio_io_error(bio);
David Woodhouse53b381b2013-01-29 18:40:14 -05006067 return;
6068 }
6069
Chris Mason8b712842008-06-11 16:50:36 -04006070 /* don't bother with additional async steps for reads, right now */
Mike Christie37226b22016-06-05 14:31:52 -05006071 if (bio_op(bio) == REQ_OP_READ) {
Chris Mason492bb6de2008-07-31 16:29:02 -04006072 bio_get(bio);
Mike Christie4e49ea42016-06-05 14:31:41 -05006073 btrfsic_submit_bio(bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04006074 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01006075 return;
Chris Mason8b712842008-06-11 16:50:36 -04006076 }
6077
Chris Mason492bb6de2008-07-31 16:29:02 -04006078 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04006079 bio->bi_next = NULL;
Chris Mason8b712842008-06-11 16:50:36 -04006080
6081 spin_lock(&device->io_lock);
Christoph Hellwig67f055c2016-11-01 07:40:06 -06006082 if (op_is_sync(bio->bi_opf))
Chris Masonffbd5172009-04-20 15:50:09 -04006083 pending_bios = &device->pending_sync_bios;
6084 else
6085 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006086
Chris Masonffbd5172009-04-20 15:50:09 -04006087 if (pending_bios->tail)
6088 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006089
Chris Masonffbd5172009-04-20 15:50:09 -04006090 pending_bios->tail = bio;
6091 if (!pending_bios->head)
6092 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006093 if (device->running_pending)
6094 should_queue = 0;
6095
6096 spin_unlock(&device->io_lock);
6097
6098 if (should_queue)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006099 btrfs_queue_work(fs_info->submit_workers, &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04006100}
6101
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006102static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6103 u64 physical, int dev_nr, int async)
Josef Bacikde1ee922012-10-19 16:50:56 -04006104{
6105 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006106 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006107
6108 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006109 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006110 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006111 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04006112#ifdef DEBUG
6113 {
6114 struct rcu_string *name;
6115
6116 rcu_read_lock();
6117 name = rcu_dereference(dev->name);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006118 btrfs_debug(fs_info,
6119 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6120 bio_op(bio), bio->bi_opf,
6121 (u64)bio->bi_iter.bi_sector,
6122 (u_long)dev->bdev->bd_dev, name->str, dev->devid,
6123 bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04006124 rcu_read_unlock();
6125 }
6126#endif
Christoph Hellwig74d46992017-08-23 19:10:32 +02006127 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006128
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006129 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006130
Josef Bacikde1ee922012-10-19 16:50:56 -04006131 if (async)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006132 btrfs_schedule_bio(dev, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006133 else
Mike Christie4e49ea42016-06-05 14:31:41 -05006134 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006135}
6136
Josef Bacikde1ee922012-10-19 16:50:56 -04006137static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6138{
6139 atomic_inc(&bbio->error);
6140 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006141 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006142 WARN_ON(bio != bbio->orig_bio);
6143
Chris Mason9be33952013-05-17 18:30:14 -04006144 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006145 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006146 if (atomic_read(&bbio->error) > bbio->max_errors)
6147 bio->bi_status = BLK_STS_IOERR;
6148 else
6149 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006150 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006151 }
6152}
6153
Omar Sandoval58efbc92017-08-22 23:45:59 -07006154blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6155 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006156{
Chris Mason0b86a832008-03-24 15:01:56 -04006157 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006158 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006159 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006160 u64 length = 0;
6161 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006162 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006163 int dev_nr;
6164 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006165 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006166
Kent Overstreet4f024f32013-10-11 15:44:27 -07006167 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006168 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006169
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006170 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006171 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006172 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006173 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006174 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006175 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006176 }
Chris Masoncea9e442008-04-09 16:28:12 -04006177
Jan Schmidta1d3c472011-08-04 17:15:33 +02006178 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006179 bbio->orig_bio = first_bio;
6180 bbio->private = first_bio->bi_private;
6181 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006182 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006183 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6184
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006185 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006186 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006187 /* In this case, map_length has been set to the length of
6188 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006189 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006190 ret = raid56_parity_write(fs_info, bio, bbio,
6191 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006192 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006193 ret = raid56_parity_recover(fs_info, bio, bbio,
6194 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006195 }
Miao Xie42452152014-11-25 16:39:28 +08006196
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006197 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006198 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006199 }
6200
Chris Masoncea9e442008-04-09 16:28:12 -04006201 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006202 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006203 "mapping failed logical %llu bio len %llu len %llu",
6204 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006205 BUG();
6206 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006207
Zhao Lei08da7572015-02-12 15:42:16 +08006208 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006209 dev = bbio->stripes[dev_nr].dev;
Mike Christie37226b22016-06-05 14:31:52 -05006210 if (!dev || !dev->bdev ||
Liu Boa967efb2017-04-10 12:36:26 -07006211 (bio_op(first_bio) == REQ_OP_WRITE && !dev->writeable)) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006212 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006213 continue;
6214 }
6215
David Sterba3aa8e072017-06-02 17:38:30 +02006216 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006217 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006218 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006219 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006220
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006221 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6222 dev_nr, async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006223 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006224 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006225 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006226}
6227
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006228struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006229 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006230{
Yan Zheng2b820322008-11-17 21:11:30 -05006231 struct btrfs_device *device;
6232 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006233
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006234 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006235 while (cur_devices) {
6236 if (!fsid ||
Anand Jain44880fd2017-07-29 17:50:09 +08006237 !memcmp(cur_devices->fsid, fsid, BTRFS_FSID_SIZE)) {
David Sterba35c70102017-06-15 19:51:51 +02006238 device = find_device(cur_devices, devid, uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006239 if (device)
6240 return device;
6241 }
6242 cur_devices = cur_devices->seed;
6243 }
6244 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006245}
6246
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006247static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006248 u64 devid, u8 *dev_uuid)
6249{
6250 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006251
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006252 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6253 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006254 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006255
6256 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006257 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006258 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006259
6260 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05006261 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006262
Chris Masondfe25022008-05-13 13:46:40 -04006263 return device;
6264}
6265
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006266/**
6267 * btrfs_alloc_device - allocate struct btrfs_device
6268 * @fs_info: used only for generating a new devid, can be NULL if
6269 * devid is provided (i.e. @devid != NULL).
6270 * @devid: a pointer to devid for this device. If NULL a new devid
6271 * is generated.
6272 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6273 * is generated.
6274 *
6275 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6276 * on error. Returned struct is not linked onto any lists and can be
6277 * destroyed with kfree() right away.
6278 */
6279struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6280 const u64 *devid,
6281 const u8 *uuid)
6282{
6283 struct btrfs_device *dev;
6284 u64 tmp;
6285
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306286 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006287 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006288
6289 dev = __alloc_device();
6290 if (IS_ERR(dev))
6291 return dev;
6292
6293 if (devid)
6294 tmp = *devid;
6295 else {
6296 int ret;
6297
6298 ret = find_next_devid(fs_info, &tmp);
6299 if (ret) {
David Sterba3065ae52017-10-30 18:36:08 +01006300 bio_put(dev->flush_bio);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006301 kfree(dev);
6302 return ERR_PTR(ret);
6303 }
6304 }
6305 dev->devid = tmp;
6306
6307 if (uuid)
6308 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6309 else
6310 generate_random_uuid(dev->uuid);
6311
Liu Bo9e0af232014-08-15 23:36:53 +08006312 btrfs_init_work(&dev->work, btrfs_submit_helper,
6313 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006314
6315 return dev;
6316}
6317
Liu Boe06cd3d2016-06-03 12:05:15 -07006318/* Return -EIO if any error, otherwise return 0. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006319static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006320 struct extent_buffer *leaf,
6321 struct btrfs_chunk *chunk, u64 logical)
6322{
6323 u64 length;
6324 u64 stripe_len;
6325 u16 num_stripes;
6326 u16 sub_stripes;
6327 u64 type;
6328
6329 length = btrfs_chunk_length(leaf, chunk);
6330 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6331 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6332 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6333 type = btrfs_chunk_type(leaf, chunk);
6334
6335 if (!num_stripes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006336 btrfs_err(fs_info, "invalid chunk num_stripes: %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006337 num_stripes);
6338 return -EIO;
6339 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006340 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
6341 btrfs_err(fs_info, "invalid chunk logical %llu", logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006342 return -EIO;
6343 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006344 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
6345 btrfs_err(fs_info, "invalid chunk sectorsize %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006346 btrfs_chunk_sector_size(leaf, chunk));
6347 return -EIO;
6348 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006349 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
6350 btrfs_err(fs_info, "invalid chunk length %llu", length);
Liu Boe06cd3d2016-06-03 12:05:15 -07006351 return -EIO;
6352 }
6353 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006354 btrfs_err(fs_info, "invalid chunk stripe length: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006355 stripe_len);
6356 return -EIO;
6357 }
6358 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6359 type) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006360 btrfs_err(fs_info, "unrecognized chunk type: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006361 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6362 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6363 btrfs_chunk_type(leaf, chunk));
6364 return -EIO;
6365 }
6366 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
6367 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
6368 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
6369 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
6370 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
6371 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
6372 num_stripes != 1)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006373 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006374 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
6375 num_stripes, sub_stripes,
6376 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
6377 return -EIO;
6378 }
6379
6380 return 0;
6381}
6382
Anand Jain5a2b8e62017-10-09 11:07:45 +08006383static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006384 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006385{
Anand Jain2b902df2017-10-09 11:07:46 +08006386 if (error)
6387 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6388 devid, uuid);
6389 else
6390 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6391 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006392}
6393
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006394static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
Chris Mason0b86a832008-03-24 15:01:56 -04006395 struct extent_buffer *leaf,
6396 struct btrfs_chunk *chunk)
6397{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006398 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006399 struct map_lookup *map;
6400 struct extent_map *em;
6401 u64 logical;
6402 u64 length;
6403 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006404 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006405 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006406 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006407 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006408
Chris Masone17cade2008-04-15 15:41:47 -04006409 logical = key->offset;
6410 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006411 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006412
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006413 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006414 if (ret)
6415 return ret;
Chris Masona061fc82008-05-07 11:43:44 -04006416
Chris Mason890871b2009-09-02 16:24:52 -04006417 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006418 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006419 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006420
6421 /* already mapped? */
6422 if (em && em->start <= logical && em->start + em->len > logical) {
6423 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006424 return 0;
6425 } else if (em) {
6426 free_extent_map(em);
6427 }
Chris Mason0b86a832008-03-24 15:01:56 -04006428
David Sterba172ddd62011-04-21 00:48:27 +02006429 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006430 if (!em)
6431 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006432 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006433 if (!map) {
6434 free_extent_map(em);
6435 return -ENOMEM;
6436 }
6437
Wang Shilong298a8f92014-06-19 10:42:52 +08006438 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006439 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006440 em->start = logical;
6441 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006442 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006443 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006444 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006445
Chris Mason593060d2008-03-25 16:50:33 -04006446 map->num_stripes = num_stripes;
6447 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6448 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006449 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6450 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006451 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006452 for (i = 0; i < num_stripes; i++) {
6453 map->stripes[i].physical =
6454 btrfs_stripe_offset_nr(leaf, chunk, i);
6455 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006456 read_extent_buffer(leaf, uuid, (unsigned long)
6457 btrfs_stripe_dev_uuid_nr(chunk, i),
6458 BTRFS_UUID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006459 map->stripes[i].dev = btrfs_find_device(fs_info, devid,
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006460 uuid, NULL);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006461 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006462 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006463 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006464 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006465 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006466 }
Chris Masondfe25022008-05-13 13:46:40 -04006467 if (!map->stripes[i].dev) {
6468 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006469 add_missing_dev(fs_info->fs_devices, devid,
6470 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006471 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006472 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006473 btrfs_err(fs_info,
6474 "failed to init missing dev %llu: %ld",
6475 devid, PTR_ERR(map->stripes[i].dev));
6476 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006477 }
Anand Jain2b902df2017-10-09 11:07:46 +08006478 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006479 }
6480 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04006481 }
6482
Chris Mason890871b2009-09-02 16:24:52 -04006483 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006484 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006485 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006486 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006487 free_extent_map(em);
6488
6489 return 0;
6490}
6491
Jeff Mahoney143bede2012-03-01 14:56:26 +01006492static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006493 struct btrfs_dev_item *dev_item,
6494 struct btrfs_device *device)
6495{
6496 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006497
6498 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006499 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6500 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006501 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006502 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006503 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006504 device->type = btrfs_device_type(leaf, dev_item);
6505 device->io_align = btrfs_device_io_align(leaf, dev_item);
6506 device->io_width = btrfs_device_io_width(leaf, dev_item);
6507 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006508 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006509 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006510
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006511 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006512 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006513}
6514
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006515static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006516 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006517{
6518 struct btrfs_fs_devices *fs_devices;
6519 int ret;
6520
Li Zefanb367e472011-12-07 11:38:24 +08006521 BUG_ON(!mutex_is_locked(&uuid_mutex));
David Sterba2dfeca92017-06-14 02:48:07 +02006522 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006523
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006524 fs_devices = fs_info->fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006525 while (fs_devices) {
Anand Jain44880fd2017-07-29 17:50:09 +08006526 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006527 return fs_devices;
6528
Yan Zheng2b820322008-11-17 21:11:30 -05006529 fs_devices = fs_devices->seed;
6530 }
6531
6532 fs_devices = find_fsid(fsid);
6533 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006534 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006535 return ERR_PTR(-ENOENT);
6536
6537 fs_devices = alloc_fs_devices(fsid);
6538 if (IS_ERR(fs_devices))
6539 return fs_devices;
6540
6541 fs_devices->seeding = 1;
6542 fs_devices->opened = 1;
6543 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006544 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006545
6546 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006547 if (IS_ERR(fs_devices))
6548 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006549
Christoph Hellwig97288f22008-12-02 06:36:09 -05006550 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006551 fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006552 if (ret) {
6553 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006554 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006555 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006556 }
Yan Zheng2b820322008-11-17 21:11:30 -05006557
6558 if (!fs_devices->seeding) {
6559 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006560 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006561 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006562 goto out;
6563 }
6564
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006565 fs_devices->seed = fs_info->fs_devices->seed;
6566 fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006567out:
Miao Xie5f375832014-09-03 21:35:46 +08006568 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006569}
6570
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006571static int read_one_dev(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04006572 struct extent_buffer *leaf,
6573 struct btrfs_dev_item *dev_item)
6574{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006575 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006576 struct btrfs_device *device;
6577 u64 devid;
6578 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08006579 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006580 u8 dev_uuid[BTRFS_UUID_SIZE];
6581
Chris Mason0b86a832008-03-24 15:01:56 -04006582 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006583 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006584 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006585 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08006586 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05006587
Anand Jain44880fd2017-07-29 17:50:09 +08006588 if (memcmp(fs_uuid, fs_info->fsid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006589 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006590 if (IS_ERR(fs_devices))
6591 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006592 }
6593
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006594 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006595 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08006596 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08006597 btrfs_report_missing_device(fs_info, devid,
6598 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006599 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08006600 }
Yan Zheng2b820322008-11-17 21:11:30 -05006601
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006602 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006603 if (IS_ERR(device)) {
6604 btrfs_err(fs_info,
6605 "failed to add missing dev %llu: %ld",
6606 devid, PTR_ERR(device));
6607 return PTR_ERR(device);
6608 }
Anand Jain2b902df2017-10-09 11:07:46 +08006609 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08006610 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08006611 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08006612 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6613 btrfs_report_missing_device(fs_info,
6614 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006615 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08006616 }
6617 btrfs_report_missing_device(fs_info, devid,
6618 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08006619 }
Miao Xie5f375832014-09-03 21:35:46 +08006620
6621 if(!device->bdev && !device->missing) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006622 /*
6623 * this happens when a device that was properly setup
6624 * in the device info lists suddenly goes bad.
6625 * device->bdev is NULL, and so we have to set
6626 * device->missing to one here
6627 */
Miao Xie5f375832014-09-03 21:35:46 +08006628 device->fs_devices->missing_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05006629 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006630 }
Miao Xie5f375832014-09-03 21:35:46 +08006631
6632 /* Move the device to its own fs_devices */
6633 if (device->fs_devices != fs_devices) {
6634 ASSERT(device->missing);
6635
6636 list_move(&device->dev_list, &fs_devices->devices);
6637 device->fs_devices->num_devices--;
6638 fs_devices->num_devices++;
6639
6640 device->fs_devices->missing_devices--;
6641 fs_devices->missing_devices++;
6642
6643 device->fs_devices = fs_devices;
6644 }
Yan Zheng2b820322008-11-17 21:11:30 -05006645 }
6646
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006647 if (device->fs_devices != fs_info->fs_devices) {
Yan Zheng2b820322008-11-17 21:11:30 -05006648 BUG_ON(device->writeable);
6649 if (device->generation !=
6650 btrfs_device_generation(leaf, dev_item))
6651 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006652 }
Chris Mason0b86a832008-03-24 15:01:56 -04006653
6654 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006655 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006656 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006657 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03006658 atomic64_add(device->total_bytes - device->bytes_used,
6659 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04006660 }
Chris Mason0b86a832008-03-24 15:01:56 -04006661 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006662 return ret;
6663}
6664
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006665int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006666{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006667 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006668 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006669 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006670 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006671 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006672 u8 *array_ptr;
6673 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006674 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006675 u32 num_stripes;
6676 u32 array_size;
6677 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006678 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07006679 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04006680 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006681
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006682 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02006683 /*
6684 * This will create extent buffer of nodesize, superblock size is
6685 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6686 * overallocate but we can keep it as-is, only the first page is used.
6687 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006688 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07006689 if (IS_ERR(sb))
6690 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01006691 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006692 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006693 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006694 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01006695 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02006696 * pages up-to-date when the page is larger: extent does not cover the
6697 * whole page and consequently check_page_uptodate does not find all
6698 * the page's extents up-to-date (the hole beyond sb),
6699 * write_extent_buffer then triggers a WARN_ON.
6700 *
6701 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6702 * but sb spans only this function. Add an explicit SetPageUptodate call
6703 * to silence the warning eg. on PowerPC 64.
6704 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006705 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006706 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006707
Chris Masona061fc82008-05-07 11:43:44 -04006708 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006709 array_size = btrfs_super_sys_array_size(super_copy);
6710
David Sterba1ffb22c2014-10-31 19:02:42 +01006711 array_ptr = super_copy->sys_chunk_array;
6712 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6713 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006714
David Sterba1ffb22c2014-10-31 19:02:42 +01006715 while (cur_offset < array_size) {
6716 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006717 len = sizeof(*disk_key);
6718 if (cur_offset + len > array_size)
6719 goto out_short_read;
6720
Chris Mason0b86a832008-03-24 15:01:56 -04006721 btrfs_disk_key_to_cpu(&key, disk_key);
6722
David Sterba1ffb22c2014-10-31 19:02:42 +01006723 array_ptr += len;
6724 sb_array_offset += len;
6725 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006726
Chris Mason0d81ba52008-03-24 15:02:07 -04006727 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006728 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006729 /*
6730 * At least one btrfs_chunk with one stripe must be
6731 * present, exact stripe count check comes afterwards
6732 */
6733 len = btrfs_chunk_item_size(1);
6734 if (cur_offset + len > array_size)
6735 goto out_short_read;
6736
6737 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01006738 if (!num_stripes) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006739 btrfs_err(fs_info,
6740 "invalid number of stripes %u in sys_array at offset %u",
David Sterbaf5cdedd2015-11-30 17:27:06 +01006741 num_stripes, cur_offset);
6742 ret = -EIO;
6743 break;
6744 }
6745
Liu Boe06cd3d2016-06-03 12:05:15 -07006746 type = btrfs_chunk_type(sb, chunk);
6747 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006748 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006749 "invalid chunk type %llu in sys_array at offset %u",
6750 type, cur_offset);
6751 ret = -EIO;
6752 break;
6753 }
6754
David Sterbae3540ea2014-11-05 15:24:51 +01006755 len = btrfs_chunk_item_size(num_stripes);
6756 if (cur_offset + len > array_size)
6757 goto out_short_read;
6758
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006759 ret = read_one_chunk(fs_info, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006760 if (ret)
6761 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006762 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006763 btrfs_err(fs_info,
6764 "unexpected item type %u in sys_array at offset %u",
6765 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04006766 ret = -EIO;
6767 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006768 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006769 array_ptr += len;
6770 sb_array_offset += len;
6771 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006772 }
Liu Bod8651772016-06-03 17:41:42 -07006773 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006774 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006775 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006776
6777out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006778 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01006779 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07006780 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006781 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01006782 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006783}
6784
Qu Wenruo21634a12017-03-09 09:34:36 +08006785/*
6786 * Check if all chunks in the fs are OK for read-write degraded mount
6787 *
6788 * Return true if all chunks meet the minimal RW mount requirements.
6789 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6790 */
6791bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info)
6792{
6793 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
6794 struct extent_map *em;
6795 u64 next_start = 0;
6796 bool ret = true;
6797
6798 read_lock(&map_tree->map_tree.lock);
6799 em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1);
6800 read_unlock(&map_tree->map_tree.lock);
6801 /* No chunk at all? Return false anyway */
6802 if (!em) {
6803 ret = false;
6804 goto out;
6805 }
6806 while (em) {
6807 struct map_lookup *map;
6808 int missing = 0;
6809 int max_tolerated;
6810 int i;
6811
6812 map = em->map_lookup;
6813 max_tolerated =
6814 btrfs_get_num_tolerated_disk_barrier_failures(
6815 map->type);
6816 for (i = 0; i < map->num_stripes; i++) {
6817 struct btrfs_device *dev = map->stripes[i].dev;
6818
6819 if (!dev || !dev->bdev || dev->missing ||
6820 dev->last_flush_error)
6821 missing++;
6822 }
6823 if (missing > max_tolerated) {
6824 btrfs_warn(fs_info,
6825 "chunk %llu missing %d devices, max tolerance is %d for writeable mount",
6826 em->start, missing, max_tolerated);
6827 free_extent_map(em);
6828 ret = false;
6829 goto out;
6830 }
6831 next_start = extent_map_end(em);
6832 free_extent_map(em);
6833
6834 read_lock(&map_tree->map_tree.lock);
6835 em = lookup_extent_mapping(&map_tree->map_tree, next_start,
6836 (u64)(-1) - next_start);
6837 read_unlock(&map_tree->map_tree.lock);
6838 }
6839out:
6840 return ret;
6841}
6842
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006843int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006844{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006845 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04006846 struct btrfs_path *path;
6847 struct extent_buffer *leaf;
6848 struct btrfs_key key;
6849 struct btrfs_key found_key;
6850 int ret;
6851 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006852 u64 total_dev = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006853
Chris Mason0b86a832008-03-24 15:01:56 -04006854 path = btrfs_alloc_path();
6855 if (!path)
6856 return -ENOMEM;
6857
Li Zefanb367e472011-12-07 11:38:24 +08006858 mutex_lock(&uuid_mutex);
David Sterba34441362016-10-04 19:34:27 +02006859 mutex_lock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08006860
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006861 /*
6862 * Read all device items, and then all the chunk items. All
6863 * device items are found before any chunk item (their object id
6864 * is smaller than the lowest possible object id for a chunk
6865 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006866 */
6867 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6868 key.offset = 0;
6869 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006870 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006871 if (ret < 0)
6872 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006873 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006874 leaf = path->nodes[0];
6875 slot = path->slots[0];
6876 if (slot >= btrfs_header_nritems(leaf)) {
6877 ret = btrfs_next_leaf(root, path);
6878 if (ret == 0)
6879 continue;
6880 if (ret < 0)
6881 goto error;
6882 break;
6883 }
6884 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006885 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6886 struct btrfs_dev_item *dev_item;
6887 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006888 struct btrfs_dev_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006889 ret = read_one_dev(fs_info, leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006890 if (ret)
6891 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006892 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04006893 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6894 struct btrfs_chunk *chunk;
6895 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006896 ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006897 if (ret)
6898 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006899 }
6900 path->slots[0]++;
6901 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07006902
6903 /*
6904 * After loading chunk tree, we've got all device information,
6905 * do another round of validation checks.
6906 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006907 if (total_dev != fs_info->fs_devices->total_devices) {
6908 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07006909 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006910 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07006911 total_dev);
6912 ret = -EINVAL;
6913 goto error;
6914 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006915 if (btrfs_super_total_bytes(fs_info->super_copy) <
6916 fs_info->fs_devices->total_rw_bytes) {
6917 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07006918 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006919 btrfs_super_total_bytes(fs_info->super_copy),
6920 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07006921 ret = -EINVAL;
6922 goto error;
6923 }
Chris Mason0b86a832008-03-24 15:01:56 -04006924 ret = 0;
6925error:
David Sterba34441362016-10-04 19:34:27 +02006926 mutex_unlock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08006927 mutex_unlock(&uuid_mutex);
6928
Yan Zheng2b820322008-11-17 21:11:30 -05006929 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006930 return ret;
6931}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006932
Miao Xiecb517ea2013-05-15 07:48:19 +00006933void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6934{
6935 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6936 struct btrfs_device *device;
6937
Liu Bo29cc83f2014-05-11 23:14:59 +08006938 while (fs_devices) {
6939 mutex_lock(&fs_devices->device_list_mutex);
6940 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04006941 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08006942 mutex_unlock(&fs_devices->device_list_mutex);
6943
6944 fs_devices = fs_devices->seed;
6945 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006946}
6947
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006948static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6949{
6950 int i;
6951
6952 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6953 btrfs_dev_stat_reset(dev, i);
6954}
6955
6956int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6957{
6958 struct btrfs_key key;
6959 struct btrfs_key found_key;
6960 struct btrfs_root *dev_root = fs_info->dev_root;
6961 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6962 struct extent_buffer *eb;
6963 int slot;
6964 int ret = 0;
6965 struct btrfs_device *device;
6966 struct btrfs_path *path = NULL;
6967 int i;
6968
6969 path = btrfs_alloc_path();
6970 if (!path) {
6971 ret = -ENOMEM;
6972 goto out;
6973 }
6974
6975 mutex_lock(&fs_devices->device_list_mutex);
6976 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6977 int item_size;
6978 struct btrfs_dev_stats_item *ptr;
6979
David Sterba242e2952016-01-25 17:51:31 +01006980 key.objectid = BTRFS_DEV_STATS_OBJECTID;
6981 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006982 key.offset = device->devid;
6983 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6984 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006985 __btrfs_reset_dev_stats(device);
6986 device->dev_stats_valid = 1;
6987 btrfs_release_path(path);
6988 continue;
6989 }
6990 slot = path->slots[0];
6991 eb = path->nodes[0];
6992 btrfs_item_key_to_cpu(eb, &found_key, slot);
6993 item_size = btrfs_item_size_nr(eb, slot);
6994
6995 ptr = btrfs_item_ptr(eb, slot,
6996 struct btrfs_dev_stats_item);
6997
6998 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6999 if (item_size >= (1 + i) * sizeof(__le64))
7000 btrfs_dev_stat_set(device, i,
7001 btrfs_dev_stats_value(eb, ptr, i));
7002 else
7003 btrfs_dev_stat_reset(device, i);
7004 }
7005
7006 device->dev_stats_valid = 1;
7007 btrfs_dev_stat_print_on_load(device);
7008 btrfs_release_path(path);
7009 }
7010 mutex_unlock(&fs_devices->device_list_mutex);
7011
7012out:
7013 btrfs_free_path(path);
7014 return ret < 0 ? ret : 0;
7015}
7016
7017static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007018 struct btrfs_fs_info *fs_info,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007019 struct btrfs_device *device)
7020{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007021 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007022 struct btrfs_path *path;
7023 struct btrfs_key key;
7024 struct extent_buffer *eb;
7025 struct btrfs_dev_stats_item *ptr;
7026 int ret;
7027 int i;
7028
David Sterba242e2952016-01-25 17:51:31 +01007029 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7030 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007031 key.offset = device->devid;
7032
7033 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007034 if (!path)
7035 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007036 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7037 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007038 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007039 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007040 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007041 goto out;
7042 }
7043
7044 if (ret == 0 &&
7045 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7046 /* need to delete old one and insert a new one */
7047 ret = btrfs_del_item(trans, dev_root, path);
7048 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007049 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007050 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007051 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007052 goto out;
7053 }
7054 ret = 1;
7055 }
7056
7057 if (ret == 1) {
7058 /* need to insert a new item */
7059 btrfs_release_path(path);
7060 ret = btrfs_insert_empty_item(trans, dev_root, path,
7061 &key, sizeof(*ptr));
7062 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007063 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007064 "insert dev_stats item for device %s failed %d",
7065 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007066 goto out;
7067 }
7068 }
7069
7070 eb = path->nodes[0];
7071 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7072 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7073 btrfs_set_dev_stats_value(eb, ptr, i,
7074 btrfs_dev_stat_read(device, i));
7075 btrfs_mark_buffer_dirty(eb);
7076
7077out:
7078 btrfs_free_path(path);
7079 return ret;
7080}
7081
7082/*
7083 * called from commit_transaction. Writes all changed device stats to disk.
7084 */
7085int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
7086 struct btrfs_fs_info *fs_info)
7087{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007088 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7089 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007090 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007091 int ret = 0;
7092
7093 mutex_lock(&fs_devices->device_list_mutex);
7094 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08007095 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007096 continue;
7097
Miao Xieaddc3fa2014-07-24 11:37:11 +08007098 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007099 ret = update_dev_stat_item(trans, fs_info, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007100 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007101 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007102 }
7103 mutex_unlock(&fs_devices->device_list_mutex);
7104
7105 return ret;
7106}
7107
Stefan Behrens442a4f62012-05-25 16:06:08 +02007108void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7109{
7110 btrfs_dev_stat_inc(dev, index);
7111 btrfs_dev_stat_print_on_error(dev);
7112}
7113
Eric Sandeen48a3b632013-04-25 20:41:01 +00007114static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007115{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007116 if (!dev->dev_stats_valid)
7117 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007118 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007119 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007120 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007121 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7122 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7123 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007124 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7125 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007126}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007127
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007128static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7129{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007130 int i;
7131
7132 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7133 if (btrfs_dev_stat_read(dev, i) != 0)
7134 break;
7135 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7136 return; /* all values == 0, suppress message */
7137
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007138 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007139 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007140 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007141 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7142 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7143 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7144 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7145 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7146}
7147
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007148int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007149 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007150{
7151 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007152 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007153 int i;
7154
7155 mutex_lock(&fs_devices->device_list_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007156 dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007157 mutex_unlock(&fs_devices->device_list_mutex);
7158
7159 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007160 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007161 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007162 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007163 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007164 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007165 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007166 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7167 if (stats->nr_items > i)
7168 stats->values[i] =
7169 btrfs_dev_stat_read_and_reset(dev, i);
7170 else
7171 btrfs_dev_stat_reset(dev, i);
7172 }
7173 } else {
7174 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7175 if (stats->nr_items > i)
7176 stats->values[i] = btrfs_dev_stat_read(dev, i);
7177 }
7178 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7179 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7180 return 0;
7181}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007182
David Sterbada353f62017-02-14 17:55:53 +01007183void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007184{
7185 struct buffer_head *bh;
7186 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08007187 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007188
Anand Jain12b1c262015-08-14 18:32:59 +08007189 if (!bdev)
7190 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007191
Anand Jain12b1c262015-08-14 18:32:59 +08007192 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7193 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007194
Anand Jain12b1c262015-08-14 18:32:59 +08007195 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7196 continue;
7197
7198 disk_super = (struct btrfs_super_block *)bh->b_data;
7199
7200 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7201 set_buffer_dirty(bh);
7202 sync_dirty_buffer(bh);
7203 brelse(bh);
7204 }
7205
7206 /* Notify udev that device has changed */
7207 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7208
7209 /* Update ctime/mtime for device path for libblkid */
7210 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007211}
Miao Xie935e5cc2014-09-03 21:35:33 +08007212
7213/*
7214 * Update the size of all devices, which is used for writing out the
7215 * super blocks.
7216 */
7217void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
7218{
7219 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7220 struct btrfs_device *curr, *next;
7221
7222 if (list_empty(&fs_devices->resized_devices))
7223 return;
7224
7225 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02007226 mutex_lock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007227 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
7228 resized_list) {
7229 list_del_init(&curr->resized_list);
7230 curr->commit_total_bytes = curr->disk_total_bytes;
7231 }
David Sterba34441362016-10-04 19:34:27 +02007232 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007233 mutex_unlock(&fs_devices->device_list_mutex);
7234}
Miao Xiece7213c2014-09-03 21:35:34 +08007235
7236/* Must be invoked during the transaction commit */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007237void btrfs_update_commit_device_bytes_used(struct btrfs_fs_info *fs_info,
Miao Xiece7213c2014-09-03 21:35:34 +08007238 struct btrfs_transaction *transaction)
7239{
7240 struct extent_map *em;
7241 struct map_lookup *map;
7242 struct btrfs_device *dev;
7243 int i;
7244
7245 if (list_empty(&transaction->pending_chunks))
7246 return;
7247
7248 /* In order to kick the device replace finish process */
David Sterba34441362016-10-04 19:34:27 +02007249 mutex_lock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007250 list_for_each_entry(em, &transaction->pending_chunks, list) {
Jeff Mahoney95617d62015-06-03 10:55:48 -04007251 map = em->map_lookup;
Miao Xiece7213c2014-09-03 21:35:34 +08007252
7253 for (i = 0; i < map->num_stripes; i++) {
7254 dev = map->stripes[i].dev;
7255 dev->commit_bytes_used = dev->bytes_used;
7256 }
7257 }
David Sterba34441362016-10-04 19:34:27 +02007258 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007259}
Anand Jain5a13f432015-03-10 06:38:31 +08007260
7261void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7262{
7263 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7264 while (fs_devices) {
7265 fs_devices->fs_info = fs_info;
7266 fs_devices = fs_devices->seed;
7267 }
7268}
7269
7270void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7271{
7272 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7273 while (fs_devices) {
7274 fs_devices->fs_info = NULL;
7275 fs_devices = fs_devices->seed;
7276 }
7277}