blob: b39737568c223c208d92b2f4ab73ac0263ad3ff4 [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);
Yan Zhenge4404d62008-12-12 10:03:26 -0500192 kfree(device);
193 }
194 kfree(fs_devices);
195}
196
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000197static void btrfs_kobject_uevent(struct block_device *bdev,
198 enum kobject_action action)
199{
200 int ret;
201
202 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
203 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500204 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000205 action,
206 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
207 &disk_to_dev(bdev->bd_disk)->kobj);
208}
209
Jeff Mahoney143bede2012-03-01 14:56:26 +0100210void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400211{
212 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400213
Yan Zheng2b820322008-11-17 21:11:30 -0500214 while (!list_empty(&fs_uuids)) {
215 fs_devices = list_entry(fs_uuids.next,
216 struct btrfs_fs_devices, list);
217 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500218 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400219 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400220}
221
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300222static struct btrfs_device *__alloc_device(void)
223{
224 struct btrfs_device *dev;
225
David Sterba78f2c9e2016-02-11 14:25:38 +0100226 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300227 if (!dev)
228 return ERR_PTR(-ENOMEM);
229
David Sterbae0ae9992017-06-06 17:06:06 +0200230 /*
231 * Preallocate a bio that's always going to be used for flushing device
232 * barriers and matches the device lifespan
233 */
234 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
235 if (!dev->flush_bio) {
236 kfree(dev);
237 return ERR_PTR(-ENOMEM);
238 }
239 bio_get(dev->flush_bio);
240
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300241 INIT_LIST_HEAD(&dev->dev_list);
242 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800243 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300244
245 spin_lock_init(&dev->io_lock);
246
247 spin_lock_init(&dev->reada_lock);
248 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800249 atomic_set(&dev->dev_stats_ccnt, 0);
Sebastian Andrzej Siewior546bed62016-01-15 14:37:15 +0100250 btrfs_device_data_ordered_init(dev);
Chris Mason9bcaaea2017-05-04 16:08:08 -0700251 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Mel Gormand0164ad2015-11-06 16:28:21 -0800252 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300253
254 return dev;
255}
256
David Sterba35c70102017-06-15 19:51:51 +0200257/*
258 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
259 * return NULL.
260 *
261 * If devid and uuid are both specified, the match must be exact, otherwise
262 * only devid is used.
263 */
264static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
265 u64 devid, const u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400266{
David Sterba35c70102017-06-15 19:51:51 +0200267 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400268 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400269
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500270 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400271 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400272 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400273 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400274 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400275 }
276 return NULL;
277}
278
Chris Masona1b32a52008-09-05 16:09:51 -0400279static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400280{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400281 struct btrfs_fs_devices *fs_devices;
282
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500283 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400284 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
285 return fs_devices;
286 }
287 return NULL;
288}
289
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100290static int
291btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
292 int flush, struct block_device **bdev,
293 struct buffer_head **bh)
294{
295 int ret;
296
297 *bdev = blkdev_get_by_path(device_path, flags, holder);
298
299 if (IS_ERR(*bdev)) {
300 ret = PTR_ERR(*bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100301 goto error;
302 }
303
304 if (flush)
305 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
David Sterba9f6d2512017-06-16 01:48:05 +0200306 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100307 if (ret) {
308 blkdev_put(*bdev, flags);
309 goto error;
310 }
311 invalidate_bdev(*bdev);
312 *bh = btrfs_read_dev_super(*bdev);
Anand Jain92fc03f2015-08-14 18:32:51 +0800313 if (IS_ERR(*bh)) {
314 ret = PTR_ERR(*bh);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100315 blkdev_put(*bdev, flags);
316 goto error;
317 }
318
319 return 0;
320
321error:
322 *bdev = NULL;
323 *bh = NULL;
324 return ret;
325}
326
Chris Masonffbd5172009-04-20 15:50:09 -0400327static void requeue_list(struct btrfs_pending_bios *pending_bios,
328 struct bio *head, struct bio *tail)
329{
330
331 struct bio *old_head;
332
333 old_head = pending_bios->head;
334 pending_bios->head = head;
335 if (pending_bios->tail)
336 tail->bi_next = old_head;
337 else
338 pending_bios->tail = tail;
339}
340
Chris Mason8b712842008-06-11 16:50:36 -0400341/*
342 * we try to collect pending bios for a device so we don't get a large
343 * number of procs sending bios down to the same device. This greatly
344 * improves the schedulers ability to collect and merge the bios.
345 *
346 * But, it also turns into a long list of bios to process and that is sure
347 * to eventually make the worker thread block. The solution here is to
348 * make some progress and then put this work struct back at the end of
349 * the list if the block device is congested. This way, multiple devices
350 * can make progress from a single worker thread.
351 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100352static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400353{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400354 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -0400355 struct bio *pending;
356 struct backing_dev_info *bdi;
Chris Masonffbd5172009-04-20 15:50:09 -0400357 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400358 struct bio *tail;
359 struct bio *cur;
360 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400361 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400362 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400363 unsigned long limit;
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 limit = btrfs_async_submit_limit(fs_info);
379 limit = limit * 2 / 3;
380
Chris Mason8b712842008-06-11 16:50:36 -0400381loop:
382 spin_lock(&device->io_lock);
383
Chris Masona6837052009-02-04 09:19:41 -0500384loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400385 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400386
Chris Mason8b712842008-06-11 16:50:36 -0400387 /* take all the bios off the list at once and process them
388 * later on (without the lock held). But, remember the
389 * tail and other pointers so the bios can be properly reinserted
390 * into the list if we hit congestion
391 */
Chris Masond84275c2009-06-09 15:39:08 -0400392 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400393 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400394 force_reg = 1;
395 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400396 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400397 force_reg = 0;
398 }
Chris Masonffbd5172009-04-20 15:50:09 -0400399
400 pending = pending_bios->head;
401 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400402 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400403
404 /*
405 * if pending was null this time around, no bios need processing
406 * at all and we can stop. Otherwise it'll loop back up again
407 * and do an additional check so no bios are missed.
408 *
409 * device->running_pending is used to synchronize with the
410 * schedule_bio code.
411 */
Chris Masonffbd5172009-04-20 15:50:09 -0400412 if (device->pending_sync_bios.head == NULL &&
413 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400414 again = 0;
415 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400416 } else {
417 again = 1;
418 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400419 }
Chris Masonffbd5172009-04-20 15:50:09 -0400420
421 pending_bios->head = NULL;
422 pending_bios->tail = NULL;
423
Chris Mason8b712842008-06-11 16:50:36 -0400424 spin_unlock(&device->io_lock);
425
Chris Masond3977122009-01-05 21:25:51 -0500426 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400427
428 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400429 /* we want to work on both lists, but do more bios on the
430 * sync list than the regular list
431 */
432 if ((num_run > 32 &&
433 pending_bios != &device->pending_sync_bios &&
434 device->pending_sync_bios.head) ||
435 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
436 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400437 spin_lock(&device->io_lock);
438 requeue_list(pending_bios, pending, tail);
439 goto loop_lock;
440 }
441
Chris Mason8b712842008-06-11 16:50:36 -0400442 cur = pending;
443 pending = pending->bi_next;
444 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400445
David Sterbaee863952015-02-16 19:41:40 +0100446 /*
447 * atomic_dec_return implies a barrier for waitqueue_active
448 */
Josef Bacik66657b32012-08-01 15:36:24 -0400449 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400450 waitqueue_active(&fs_info->async_submit_wait))
451 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400452
Jens Axboedac56212015-04-17 16:23:59 -0600453 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400454
Chris Mason2ab1ba62011-08-04 14:28:36 -0400455 /*
456 * if we're doing the sync list, record that our
457 * plug has some sync requests on it
458 *
459 * If we're doing the regular list and there are
460 * sync requests sitting around, unplug before
461 * we add more
462 */
463 if (pending_bios == &device->pending_sync_bios) {
464 sync_pending = 1;
465 } else if (sync_pending) {
466 blk_finish_plug(&plug);
467 blk_start_plug(&plug);
468 sync_pending = 0;
469 }
470
Mike Christie4e49ea42016-06-05 14:31:41 -0500471 btrfsic_submit_bio(cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400472 num_run++;
473 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100474
475 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400476
477 /*
478 * we made progress, there is more work to do and the bdi
479 * is now congested. Back off and let other work structs
480 * run instead
481 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400482 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500483 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400484 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400485
Chris Masonb765ead2009-04-03 10:27:10 -0400486 ioc = current->io_context;
487
488 /*
489 * the main goal here is that we don't want to
490 * block if we're going to be able to submit
491 * more requests without blocking.
492 *
493 * This code does two great things, it pokes into
494 * the elevator code from a filesystem _and_
495 * it makes assumptions about how batching works.
496 */
497 if (ioc && ioc->nr_batch_requests > 0 &&
498 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
499 (last_waited == 0 ||
500 ioc->last_waited == last_waited)) {
501 /*
502 * we want to go through our batch of
503 * requests and stop. So, we copy out
504 * the ioc->last_waited time and test
505 * against it before looping
506 */
507 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100508 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400509 continue;
510 }
Chris Mason8b712842008-06-11 16:50:36 -0400511 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400512 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500513 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400514
515 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d42014-02-28 10:46:08 +0800516 btrfs_queue_work(fs_info->submit_workers,
517 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400518 goto done;
519 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500520 /* unplug every 64 requests just for good measure */
521 if (batch_run % 64 == 0) {
522 blk_finish_plug(&plug);
523 blk_start_plug(&plug);
524 sync_pending = 0;
525 }
Chris Mason8b712842008-06-11 16:50:36 -0400526 }
Chris Masonffbd5172009-04-20 15:50:09 -0400527
Chris Mason51684082010-03-10 15:33:32 -0500528 cond_resched();
529 if (again)
530 goto loop;
531
532 spin_lock(&device->io_lock);
533 if (device->pending_bios.head || device->pending_sync_bios.head)
534 goto loop_lock;
535 spin_unlock(&device->io_lock);
536
Chris Mason8b712842008-06-11 16:50:36 -0400537done:
Chris Mason211588a2011-04-19 20:12:40 -0400538 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400539}
540
Christoph Hellwigb2950862008-12-02 09:54:17 -0500541static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400542{
543 struct btrfs_device *device;
544
545 device = container_of(work, struct btrfs_device, work);
546 run_scheduled_bios(device);
547}
548
Anand Jain4fde46f2015-06-17 21:10:48 +0800549
550void btrfs_free_stale_device(struct btrfs_device *cur_dev)
551{
552 struct btrfs_fs_devices *fs_devs;
553 struct btrfs_device *dev;
554
555 if (!cur_dev->name)
556 return;
557
558 list_for_each_entry(fs_devs, &fs_uuids, list) {
559 int del = 1;
560
561 if (fs_devs->opened)
562 continue;
563 if (fs_devs->seeding)
564 continue;
565
566 list_for_each_entry(dev, &fs_devs->devices, dev_list) {
567
568 if (dev == cur_dev)
569 continue;
570 if (!dev->name)
571 continue;
572
573 /*
574 * Todo: This won't be enough. What if the same device
575 * comes back (with new uuid and) with its mapper path?
576 * But for now, this does help as mostly an admin will
577 * either use mapper or non mapper path throughout.
578 */
579 rcu_read_lock();
580 del = strcmp(rcu_str_deref(dev->name),
581 rcu_str_deref(cur_dev->name));
582 rcu_read_unlock();
583 if (!del)
584 break;
585 }
586
587 if (!del) {
588 /* delete the stale device */
589 if (fs_devs->num_devices == 1) {
590 btrfs_sysfs_remove_fsid(fs_devs);
591 list_del(&fs_devs->list);
592 free_fs_devices(fs_devs);
593 } else {
594 fs_devs->num_devices--;
595 list_del(&dev->dev_list);
596 rcu_string_free(dev->name);
597 kfree(dev);
598 }
599 break;
600 }
601 }
602}
603
David Sterba60999ca2014-03-26 18:26:36 +0100604/*
605 * Add new device to list of registered devices
606 *
607 * Returns:
608 * 1 - first time device is seen
609 * 0 - device already known
610 * < 0 - error
611 */
Chris Masona1b32a52008-09-05 16:09:51 -0400612static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400613 struct btrfs_super_block *disk_super,
614 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
615{
616 struct btrfs_device *device;
617 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400618 struct rcu_string *name;
David Sterba60999ca2014-03-26 18:26:36 +0100619 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400620 u64 found_transid = btrfs_super_generation(disk_super);
621
622 fs_devices = find_fsid(disk_super->fsid);
623 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300624 fs_devices = alloc_fs_devices(disk_super->fsid);
625 if (IS_ERR(fs_devices))
626 return PTR_ERR(fs_devices);
627
Chris Mason8a4b83c2008-03-24 15:02:07 -0400628 list_add(&fs_devices->list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300629
Chris Mason8a4b83c2008-03-24 15:02:07 -0400630 device = NULL;
631 } else {
David Sterba35c70102017-06-15 19:51:51 +0200632 device = find_device(fs_devices, devid,
633 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400634 }
Miao Xie443f24f2014-07-24 11:37:15 +0800635
Chris Mason8a4b83c2008-03-24 15:02:07 -0400636 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500637 if (fs_devices->opened)
638 return -EBUSY;
639
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300640 device = btrfs_alloc_device(NULL, &devid,
641 disk_super->dev_item.uuid);
642 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400643 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300644 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400645 }
Josef Bacik606686e2012-06-04 14:03:51 -0400646
647 name = rcu_string_strdup(path, GFP_NOFS);
648 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400649 kfree(device);
650 return -ENOMEM;
651 }
Josef Bacik606686e2012-06-04 14:03:51 -0400652 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200653
Chris Masone5e9a522009-06-10 15:17:02 -0400654 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000655 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100656 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400657 mutex_unlock(&fs_devices->device_list_mutex);
658
David Sterba60999ca2014-03-26 18:26:36 +0100659 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500660 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400661 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800662 /*
663 * When FS is already mounted.
664 * 1. If you are here and if the device->name is NULL that
665 * means this device was missing at time of FS mount.
666 * 2. If you are here and if the device->name is different
667 * from 'path' that means either
668 * a. The same device disappeared and reappeared with
669 * different name. or
670 * b. The missing-disk-which-was-replaced, has
671 * reappeared now.
672 *
673 * We must allow 1 and 2a above. But 2b would be a spurious
674 * and unintentional.
675 *
676 * Further in case of 1 and 2a above, the disk at 'path'
677 * would have missed some transaction when it was away and
678 * in case of 2a the stale bdev has to be updated as well.
679 * 2b must not be allowed at all time.
680 */
681
682 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700683 * For now, we do allow update to btrfs_fs_device through the
684 * btrfs dev scan cli after FS has been mounted. We're still
685 * tracking a problem where systems fail mount by subvolume id
686 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800687 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700688 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800689 /*
690 * That is if the FS is _not_ mounted and if you
691 * are here, that means there is more than one
692 * disk with same uuid and devid.We keep the one
693 * with larger generation number or the last-in if
694 * generation are equal.
695 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700696 return -EEXIST;
Anand Jain77bdae42014-07-03 18:22:06 +0800697 }
Anand Jainb96de002014-07-03 18:22:05 +0800698
Josef Bacik606686e2012-06-04 14:03:51 -0400699 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000700 if (!name)
701 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400702 rcu_string_free(device->name);
703 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500704 if (device->missing) {
705 fs_devices->missing_devices--;
706 device->missing = 0;
707 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400708 }
709
Anand Jain77bdae42014-07-03 18:22:06 +0800710 /*
711 * Unmount does not free the btrfs_device struct but would zero
712 * generation along with most of the other members. So just update
713 * it back. We need it to pick the disk with largest generation
714 * (as above).
715 */
716 if (!fs_devices->opened)
717 device->generation = found_transid;
718
Anand Jain4fde46f2015-06-17 21:10:48 +0800719 /*
720 * if there is new btrfs on an already registered device,
721 * then remove the stale device entry.
722 */
Anand Jain02feae32016-02-13 10:01:40 +0800723 if (ret > 0)
724 btrfs_free_stale_device(device);
Anand Jain4fde46f2015-06-17 21:10:48 +0800725
Chris Mason8a4b83c2008-03-24 15:02:07 -0400726 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100727
728 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400729}
730
Yan Zhenge4404d62008-12-12 10:03:26 -0500731static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
732{
733 struct btrfs_fs_devices *fs_devices;
734 struct btrfs_device *device;
735 struct btrfs_device *orig_dev;
736
Ilya Dryomov2208a372013-08-12 14:33:03 +0300737 fs_devices = alloc_fs_devices(orig->fsid);
738 if (IS_ERR(fs_devices))
739 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500740
Miao Xieadbbb862014-09-03 21:35:42 +0800741 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400742 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500743
Xiao Guangrong46224702011-04-20 10:08:47 +0000744 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500745 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400746 struct rcu_string *name;
747
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300748 device = btrfs_alloc_device(NULL, &orig_dev->devid,
749 orig_dev->uuid);
750 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500751 goto error;
752
Josef Bacik606686e2012-06-04 14:03:51 -0400753 /*
754 * This is ok to do without rcu read locked because we hold the
755 * uuid mutex so nothing we touch in here is going to disappear.
756 */
Anand Jaine755f782014-06-30 17:12:47 +0800757 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +0100758 name = rcu_string_strdup(orig_dev->name->str,
759 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +0800760 if (!name) {
761 kfree(device);
762 goto error;
763 }
764 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400765 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500766
Yan Zhenge4404d62008-12-12 10:03:26 -0500767 list_add(&device->dev_list, &fs_devices->devices);
768 device->fs_devices = fs_devices;
769 fs_devices->num_devices++;
770 }
Miao Xieadbbb862014-09-03 21:35:42 +0800771 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500772 return fs_devices;
773error:
Miao Xieadbbb862014-09-03 21:35:42 +0800774 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500775 free_fs_devices(fs_devices);
776 return ERR_PTR(-ENOMEM);
777}
778
Eric Sandeen9eaed212014-08-01 18:12:35 -0500779void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400780{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500781 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800782 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500783
Chris Masondfe25022008-05-13 13:46:40 -0400784 mutex_lock(&uuid_mutex);
785again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000786 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500787 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500788 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100789 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800790 (!latest_dev ||
791 device->generation > latest_dev->generation)) {
792 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500793 }
Yan Zheng2b820322008-11-17 21:11:30 -0500794 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500795 }
Yan Zheng2b820322008-11-17 21:11:30 -0500796
Stefan Behrens8dabb742012-11-06 13:15:27 +0100797 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
798 /*
799 * In the first step, keep the device which has
800 * the correct fsid and the devid that is used
801 * for the dev_replace procedure.
802 * In the second step, the dev_replace state is
803 * read from the device tree and it is known
804 * whether the procedure is really active or
805 * not, which means whether this device is
806 * used or whether it should be removed.
807 */
808 if (step == 0 || device->is_tgtdev_for_dev_replace) {
809 continue;
810 }
811 }
Yan Zheng2b820322008-11-17 21:11:30 -0500812 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100813 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500814 device->bdev = NULL;
815 fs_devices->open_devices--;
816 }
817 if (device->writeable) {
818 list_del_init(&device->dev_alloc_list);
819 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100820 if (!device->is_tgtdev_for_dev_replace)
821 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500822 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500823 list_del_init(&device->dev_list);
824 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400825 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500826 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400827 }
Yan Zheng2b820322008-11-17 21:11:30 -0500828
829 if (fs_devices->seed) {
830 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500831 goto again;
832 }
833
Miao Xie443f24f2014-07-24 11:37:15 +0800834 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500835
Chris Masondfe25022008-05-13 13:46:40 -0400836 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400837}
Chris Masona0af4692008-05-13 16:03:06 -0400838
Xiao Guangrong1f781602011-04-20 10:09:16 +0000839static void __free_device(struct work_struct *work)
840{
841 struct btrfs_device *device;
842
843 device = container_of(work, struct btrfs_device, rcu_work);
Josef Bacik606686e2012-06-04 14:03:51 -0400844 rcu_string_free(device->name);
David Sterbae0ae9992017-06-06 17:06:06 +0200845 bio_put(device->flush_bio);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000846 kfree(device);
847}
848
849static void free_device(struct rcu_head *head)
850{
851 struct btrfs_device *device;
852
853 device = container_of(head, struct btrfs_device, rcu);
854
855 INIT_WORK(&device->rcu_work, __free_device);
856 schedule_work(&device->rcu_work);
857}
858
Anand Jain14238812016-07-22 06:04:53 +0800859static void btrfs_close_bdev(struct btrfs_device *device)
860{
861 if (device->bdev && device->writeable) {
862 sync_blockdev(device->bdev);
863 invalidate_bdev(device->bdev);
864 }
865
866 if (device->bdev)
867 blkdev_put(device->bdev, device->mode);
868}
869
Anand Jain0ccd0522016-09-22 12:56:13 +0800870static void btrfs_prepare_close_one_device(struct btrfs_device *device)
Anand Jainf4483412016-06-14 18:55:25 +0800871{
872 struct btrfs_fs_devices *fs_devices = device->fs_devices;
873 struct btrfs_device *new_device;
874 struct rcu_string *name;
875
876 if (device->bdev)
877 fs_devices->open_devices--;
878
879 if (device->writeable &&
880 device->devid != BTRFS_DEV_REPLACE_DEVID) {
881 list_del_init(&device->dev_alloc_list);
882 fs_devices->rw_devices--;
883 }
884
885 if (device->missing)
886 fs_devices->missing_devices--;
887
888 new_device = btrfs_alloc_device(NULL, &device->devid,
889 device->uuid);
890 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
891
892 /* Safe because we are under uuid_mutex */
893 if (device->name) {
894 name = rcu_string_strdup(device->name->str, GFP_NOFS);
895 BUG_ON(!name); /* -ENOMEM */
896 rcu_assign_pointer(new_device->name, name);
897 }
898
899 list_replace_rcu(&device->dev_list, &new_device->dev_list);
900 new_device->fs_devices = device->fs_devices;
Anand Jainf4483412016-06-14 18:55:25 +0800901}
902
Yan Zheng2b820322008-11-17 21:11:30 -0500903static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400904{
Sasha Levin2037a092015-05-12 19:31:37 -0400905 struct btrfs_device *device, *tmp;
Anand Jain0ccd0522016-09-22 12:56:13 +0800906 struct list_head pending_put;
907
908 INIT_LIST_HEAD(&pending_put);
Yan Zhenge4404d62008-12-12 10:03:26 -0500909
Yan Zheng2b820322008-11-17 21:11:30 -0500910 if (--fs_devices->opened > 0)
911 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400912
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000913 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -0400914 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Anand Jain0ccd0522016-09-22 12:56:13 +0800915 btrfs_prepare_close_one_device(device);
916 list_add(&device->dev_list, &pending_put);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400917 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000918 mutex_unlock(&fs_devices->device_list_mutex);
919
Anand Jain0ccd0522016-09-22 12:56:13 +0800920 /*
921 * btrfs_show_devname() is using the device_list_mutex,
922 * sometimes call to blkdev_put() leads vfs calling
923 * into this func. So do put outside of device_list_mutex,
924 * as of now.
925 */
926 while (!list_empty(&pending_put)) {
927 device = list_first_entry(&pending_put,
928 struct btrfs_device, dev_list);
929 list_del(&device->dev_list);
930 btrfs_close_bdev(device);
931 call_rcu(&device->rcu, free_device);
932 }
933
Yan Zhenge4404d62008-12-12 10:03:26 -0500934 WARN_ON(fs_devices->open_devices);
935 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500936 fs_devices->opened = 0;
937 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500938
Chris Mason8a4b83c2008-03-24 15:02:07 -0400939 return 0;
940}
941
Yan Zheng2b820322008-11-17 21:11:30 -0500942int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
943{
Yan Zhenge4404d62008-12-12 10:03:26 -0500944 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500945 int ret;
946
947 mutex_lock(&uuid_mutex);
948 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500949 if (!fs_devices->opened) {
950 seed_devices = fs_devices->seed;
951 fs_devices->seed = NULL;
952 }
Yan Zheng2b820322008-11-17 21:11:30 -0500953 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500954
955 while (seed_devices) {
956 fs_devices = seed_devices;
957 seed_devices = fs_devices->seed;
958 __btrfs_close_devices(fs_devices);
959 free_fs_devices(fs_devices);
960 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000961 /*
962 * Wait for rcu kworkers under __btrfs_close_devices
963 * to finish all blkdev_puts so device is really
964 * free when umount is done.
965 */
966 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500967 return ret;
968}
969
Yan Zhenge4404d62008-12-12 10:03:26 -0500970static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
971 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400972{
Josef Bacikd5e20032011-08-04 14:52:27 +0000973 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400974 struct block_device *bdev;
975 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400976 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +0800977 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -0400978 struct buffer_head *bh;
979 struct btrfs_super_block *disk_super;
Chris Masona0af4692008-05-13 16:03:06 -0400980 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500981 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400982 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400983
Tejun Heod4d77622010-11-13 11:55:18 +0100984 flags |= FMODE_EXCL;
985
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500986 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400987 if (device->bdev)
988 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400989 if (!device->name)
990 continue;
991
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000992 /* Just open everything we can; ignore failures here */
993 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
994 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100995 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400996
997 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000998 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400999 if (devid != device->devid)
1000 goto error_brelse;
1001
Yan Zheng2b820322008-11-17 21:11:30 -05001002 if (memcmp(device->uuid, disk_super->dev_item.uuid,
1003 BTRFS_UUID_SIZE))
1004 goto error_brelse;
1005
1006 device->generation = btrfs_super_generation(disk_super);
Miao Xie443f24f2014-07-24 11:37:15 +08001007 if (!latest_dev ||
1008 device->generation > latest_dev->generation)
1009 latest_dev = device;
Chris Masona0af4692008-05-13 16:03:06 -04001010
Yan Zheng2b820322008-11-17 21:11:30 -05001011 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
1012 device->writeable = 0;
1013 } else {
1014 device->writeable = !bdev_read_only(bdev);
1015 seeding = 0;
1016 }
1017
Josef Bacikd5e20032011-08-04 14:52:27 +00001018 q = bdev_get_queue(bdev);
Miao Xie90180da2014-09-03 21:35:30 +08001019 if (blk_queue_discard(q))
Josef Bacikd5e20032011-08-04 14:52:27 +00001020 device->can_discard = 1;
Anand Jaine884f4f2017-04-04 18:40:19 +08001021 if (!blk_queue_nonrot(q))
1022 fs_devices->rotating = 1;
Josef Bacikd5e20032011-08-04 14:52:27 +00001023
Chris Mason8a4b83c2008-03-24 15:02:07 -04001024 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001025 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -05001026 device->mode = flags;
1027
Chris Masona0af4692008-05-13 16:03:06 -04001028 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +03001029 if (device->writeable &&
1030 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -05001031 fs_devices->rw_devices++;
1032 list_add(&device->dev_alloc_list,
1033 &fs_devices->alloc_list);
1034 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +00001035 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -04001036 continue;
Chris Masona061fc82008-05-07 11:43:44 -04001037
Chris Masona0af4692008-05-13 16:03:06 -04001038error_brelse:
1039 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +01001040 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -04001041 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001042 }
Chris Masona0af4692008-05-13 16:03:06 -04001043 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +03001044 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -04001045 goto out;
1046 }
Yan Zheng2b820322008-11-17 21:11:30 -05001047 fs_devices->seeding = seeding;
1048 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +08001049 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -05001050 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -04001051out:
Yan Zheng2b820322008-11-17 21:11:30 -05001052 return ret;
1053}
1054
1055int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001056 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001057{
1058 int ret;
1059
1060 mutex_lock(&uuid_mutex);
1061 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001062 fs_devices->opened++;
1063 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001064 } else {
Chris Mason15916de2008-11-19 21:17:22 -05001065 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001066 }
Chris Mason8a4b83c2008-03-24 15:02:07 -04001067 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001068 return ret;
1069}
1070
Anand Jain6cf86a002016-02-13 10:01:29 +08001071void btrfs_release_disk_super(struct page *page)
1072{
1073 kunmap(page);
1074 put_page(page);
1075}
1076
1077int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1078 struct page **page, struct btrfs_super_block **disk_super)
1079{
1080 void *p;
1081 pgoff_t index;
1082
1083 /* make sure our super fits in the device */
1084 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1085 return 1;
1086
1087 /* make sure our super fits in the page */
1088 if (sizeof(**disk_super) > PAGE_SIZE)
1089 return 1;
1090
1091 /* make sure our super doesn't straddle pages on disk */
1092 index = bytenr >> PAGE_SHIFT;
1093 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1094 return 1;
1095
1096 /* pull in the page with our super */
1097 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1098 index, GFP_KERNEL);
1099
1100 if (IS_ERR_OR_NULL(*page))
1101 return 1;
1102
1103 p = kmap(*page);
1104
1105 /* align our pointer to the offset of the super block */
1106 *disk_super = p + (bytenr & ~PAGE_MASK);
1107
1108 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1109 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1110 btrfs_release_disk_super(*page);
1111 return 1;
1112 }
1113
1114 if ((*disk_super)->label[0] &&
1115 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1116 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1117
1118 return 0;
1119}
1120
David Sterba6f60cbd2013-02-15 11:31:02 -07001121/*
1122 * Look for a btrfs signature on a device. This may be called out of the mount path
1123 * and we are not allowed to call set_blocksize during the scan. The superblock
1124 * is read via pagecache
1125 */
Christoph Hellwig97288f22008-12-02 06:36:09 -05001126int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -04001127 struct btrfs_fs_devices **fs_devices_ret)
1128{
1129 struct btrfs_super_block *disk_super;
1130 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001131 struct page *page;
David Sterba6f60cbd2013-02-15 11:31:02 -07001132 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001133 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -04001134 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -04001135 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001136 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001137
David Sterba6f60cbd2013-02-15 11:31:02 -07001138 /*
1139 * we would like to check all the supers, but that would make
1140 * a btrfs mount succeed after a mkfs from a different FS.
1141 * So, we need to add a special mount option to scan for
1142 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1143 */
1144 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001145 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -05001146 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -07001147
1148 bdev = blkdev_get_by_path(path, flags, holder);
David Sterba6f60cbd2013-02-15 11:31:02 -07001149 if (IS_ERR(bdev)) {
1150 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001151 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -07001152 }
1153
Anand Jain6cf86a002016-02-13 10:01:29 +08001154 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super))
David Sterba6f60cbd2013-02-15 11:31:02 -07001155 goto error_bdev_put;
1156
Xiao Guangronga3438322010-01-06 11:48:18 +00001157 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -04001158 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -04001159 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -07001160
Chris Mason8a4b83c2008-03-24 15:02:07 -04001161 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +01001162 if (ret > 0) {
1163 if (disk_super->label[0]) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001164 pr_info("BTRFS: device label %s ", disk_super->label);
David Sterba60999ca2014-03-26 18:26:36 +01001165 } else {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001166 pr_info("BTRFS: device fsid %pU ", disk_super->fsid);
David Sterba60999ca2014-03-26 18:26:36 +01001167 }
1168
Jeff Mahoney62e85572016-09-20 10:05:01 -04001169 pr_cont("devid %llu transid %llu %s\n", devid, transid, path);
David Sterba60999ca2014-03-26 18:26:36 +01001170 ret = 0;
1171 }
Josef Bacik02db0842012-06-21 16:03:58 -04001172 if (!ret && fs_devices_ret)
1173 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001174
Anand Jain6cf86a002016-02-13 10:01:29 +08001175 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001176
1177error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001178 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001179error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001180 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001181 return ret;
1182}
Chris Mason0b86a832008-03-24 15:01:56 -04001183
Miao Xie6d07bce2011-01-05 10:07:31 +00001184/* helper to account the used device space in the range */
1185int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1186 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -04001187{
1188 struct btrfs_key key;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001189 struct btrfs_root *root = device->fs_info->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +00001190 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001191 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +00001192 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001193 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +00001194 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001195 struct extent_buffer *l;
1196
Miao Xie6d07bce2011-01-05 10:07:31 +00001197 *length = 0;
1198
Stefan Behrens63a212a2012-11-05 18:29:28 +01001199 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +00001200 return 0;
1201
Yan Zheng2b820322008-11-17 21:11:30 -05001202 path = btrfs_alloc_path();
1203 if (!path)
1204 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01001205 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04001206
Chris Mason0b86a832008-03-24 15:01:56 -04001207 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001208 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001209 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001210
1211 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001212 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001213 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001214 if (ret > 0) {
1215 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1216 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001217 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001218 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001219
Chris Mason0b86a832008-03-24 15:01:56 -04001220 while (1) {
1221 l = path->nodes[0];
1222 slot = path->slots[0];
1223 if (slot >= btrfs_header_nritems(l)) {
1224 ret = btrfs_next_leaf(root, path);
1225 if (ret == 0)
1226 continue;
1227 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001228 goto out;
1229
1230 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001231 }
1232 btrfs_item_key_to_cpu(l, &key, slot);
1233
1234 if (key.objectid < device->devid)
1235 goto next;
1236
1237 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001238 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001239
David Sterba962a2982014-06-04 18:41:45 +02001240 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001241 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001242
Chris Mason0b86a832008-03-24 15:01:56 -04001243 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001244 extent_end = key.offset + btrfs_dev_extent_length(l,
1245 dev_extent);
1246 if (key.offset <= start && extent_end > end) {
1247 *length = end - start + 1;
1248 break;
1249 } else if (key.offset <= start && extent_end > start)
1250 *length += extent_end - start;
1251 else if (key.offset > start && extent_end <= end)
1252 *length += extent_end - key.offset;
1253 else if (key.offset > start && key.offset <= end) {
1254 *length += end - key.offset + 1;
1255 break;
1256 } else if (key.offset > end)
1257 break;
1258
1259next:
1260 path->slots[0]++;
1261 }
1262 ret = 0;
1263out:
1264 btrfs_free_path(path);
1265 return ret;
1266}
1267
Jeff Mahoney499f3772015-06-15 09:41:17 -04001268static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001269 struct btrfs_device *device,
1270 u64 *start, u64 len)
1271{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001272 struct btrfs_fs_info *fs_info = device->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001273 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001274 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001275 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001276 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001277
Jeff Mahoney499f3772015-06-15 09:41:17 -04001278 if (transaction)
1279 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001280again:
1281 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001282 struct map_lookup *map;
1283 int i;
1284
Jeff Mahoney95617d62015-06-03 10:55:48 -04001285 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04001286 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001287 u64 end;
1288
Josef Bacik6df9a952013-06-27 13:22:46 -04001289 if (map->stripes[i].dev != device)
1290 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001291 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001292 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001293 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001294 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001295 /*
1296 * Make sure that while processing the pinned list we do
1297 * not override our *start with a lower value, because
1298 * we can have pinned chunks that fall within this
1299 * device hole and that have lower physical addresses
1300 * than the pending chunks we processed before. If we
1301 * do not take this special care we can end up getting
1302 * 2 pending chunks that start at the same physical
1303 * device offsets because the end offset of a pinned
1304 * chunk can be equal to the start offset of some
1305 * pending chunk.
1306 */
1307 end = map->stripes[i].physical + em->orig_block_len;
1308 if (end > *start) {
1309 *start = end;
1310 ret = 1;
1311 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001312 }
1313 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001314 if (search_list != &fs_info->pinned_chunks) {
1315 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001316 goto again;
1317 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001318
1319 return ret;
1320}
1321
1322
Chris Mason0b86a832008-03-24 15:01:56 -04001323/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001324 * find_free_dev_extent_start - find free space in the specified device
1325 * @device: the device which we search the free space in
1326 * @num_bytes: the size of the free space that we need
1327 * @search_start: the position from which to begin the search
1328 * @start: store the start of the free space.
1329 * @len: the size of the free space. that we find, or the size
1330 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001331 *
Chris Mason0b86a832008-03-24 15:01:56 -04001332 * this uses a pretty simple search, the expectation is that it is
1333 * called very infrequently and that a given device has a small number
1334 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001335 *
1336 * @start is used to store the start of the free space if we find. But if we
1337 * don't find suitable free space, it will be used to store the start position
1338 * of the max free space.
1339 *
1340 * @len is used to store the size of the free space that we find.
1341 * But if we don't find suitable free space, it is used to store the size of
1342 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001343 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001344int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1345 struct btrfs_device *device, u64 num_bytes,
1346 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001347{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001348 struct btrfs_fs_info *fs_info = device->fs_info;
1349 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001350 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001351 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001352 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001353 u64 hole_size;
1354 u64 max_hole_start;
1355 u64 max_hole_size;
1356 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001357 u64 search_end = device->total_bytes;
1358 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001359 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001360 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001361
1362 /*
1363 * We don't want to overwrite the superblock on the drive nor any area
1364 * used by the boot loader (grub for example), so we make sure to start
1365 * at an offset of at least 1MB.
1366 */
David Sterba0d0c71b2017-06-15 01:30:06 +02001367 search_start = max_t(u64, search_start, SZ_1M);
Chris Mason0b86a832008-03-24 15:01:56 -04001368
Josef Bacik6df9a952013-06-27 13:22:46 -04001369 path = btrfs_alloc_path();
1370 if (!path)
1371 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001372
Miao Xie7bfc8372011-01-05 10:07:26 +00001373 max_hole_start = search_start;
1374 max_hole_size = 0;
1375
Zhao Leif2ab7612015-02-16 18:52:17 +08001376again:
Stefan Behrens63a212a2012-11-05 18:29:28 +01001377 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001378 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001379 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001380 }
1381
David Sterbae4058b52015-11-27 16:31:35 +01001382 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001383 path->search_commit_root = 1;
1384 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001385
Chris Mason0b86a832008-03-24 15:01:56 -04001386 key.objectid = device->devid;
1387 key.offset = search_start;
1388 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001389
Li Zefan125ccb02011-12-08 15:07:24 +08001390 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001391 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001392 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001393 if (ret > 0) {
1394 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1395 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001396 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001397 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001398
Chris Mason0b86a832008-03-24 15:01:56 -04001399 while (1) {
1400 l = path->nodes[0];
1401 slot = path->slots[0];
1402 if (slot >= btrfs_header_nritems(l)) {
1403 ret = btrfs_next_leaf(root, path);
1404 if (ret == 0)
1405 continue;
1406 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001407 goto out;
1408
1409 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001410 }
1411 btrfs_item_key_to_cpu(l, &key, slot);
1412
1413 if (key.objectid < device->devid)
1414 goto next;
1415
1416 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001417 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001418
David Sterba962a2982014-06-04 18:41:45 +02001419 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001420 goto next;
1421
Miao Xie7bfc8372011-01-05 10:07:26 +00001422 if (key.offset > search_start) {
1423 hole_size = key.offset - search_start;
1424
Josef Bacik6df9a952013-06-27 13:22:46 -04001425 /*
1426 * Have to check before we set max_hole_start, otherwise
1427 * we could end up sending back this offset anyway.
1428 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001429 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001430 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001431 hole_size)) {
1432 if (key.offset >= search_start) {
1433 hole_size = key.offset - search_start;
1434 } else {
1435 WARN_ON_ONCE(1);
1436 hole_size = 0;
1437 }
1438 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001439
Miao Xie7bfc8372011-01-05 10:07:26 +00001440 if (hole_size > max_hole_size) {
1441 max_hole_start = search_start;
1442 max_hole_size = hole_size;
1443 }
1444
1445 /*
1446 * If this free space is greater than which we need,
1447 * it must be the max free space that we have found
1448 * until now, so max_hole_start must point to the start
1449 * of this free space and the length of this free space
1450 * is stored in max_hole_size. Thus, we return
1451 * max_hole_start and max_hole_size and go back to the
1452 * caller.
1453 */
1454 if (hole_size >= num_bytes) {
1455 ret = 0;
1456 goto out;
1457 }
1458 }
1459
Chris Mason0b86a832008-03-24 15:01:56 -04001460 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001461 extent_end = key.offset + btrfs_dev_extent_length(l,
1462 dev_extent);
1463 if (extent_end > search_start)
1464 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001465next:
1466 path->slots[0]++;
1467 cond_resched();
1468 }
Chris Mason0b86a832008-03-24 15:01:56 -04001469
liubo38c01b92011-08-02 02:39:03 +00001470 /*
1471 * At this point, search_start should be the end of
1472 * allocated dev extents, and when shrinking the device,
1473 * search_end may be smaller than search_start.
1474 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001475 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001476 hole_size = search_end - search_start;
1477
Jeff Mahoney499f3772015-06-15 09:41:17 -04001478 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001479 hole_size)) {
1480 btrfs_release_path(path);
1481 goto again;
1482 }
Chris Mason0b86a832008-03-24 15:01:56 -04001483
Zhao Leif2ab7612015-02-16 18:52:17 +08001484 if (hole_size > max_hole_size) {
1485 max_hole_start = search_start;
1486 max_hole_size = hole_size;
1487 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001488 }
1489
Miao Xie7bfc8372011-01-05 10:07:26 +00001490 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001491 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001492 ret = -ENOSPC;
1493 else
1494 ret = 0;
1495
1496out:
Yan Zheng2b820322008-11-17 21:11:30 -05001497 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001498 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001499 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001500 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001501 return ret;
1502}
1503
Jeff Mahoney499f3772015-06-15 09:41:17 -04001504int find_free_dev_extent(struct btrfs_trans_handle *trans,
1505 struct btrfs_device *device, u64 num_bytes,
1506 u64 *start, u64 *len)
1507{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001508 /* FIXME use last free of some kind */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001509 return find_free_dev_extent_start(trans->transaction, device,
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001510 num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001511}
1512
Christoph Hellwigb2950862008-12-02 09:54:17 -05001513static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001514 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001515 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001516{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001517 struct btrfs_fs_info *fs_info = device->fs_info;
1518 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001519 int ret;
1520 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001521 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001522 struct btrfs_key found_key;
1523 struct extent_buffer *leaf = NULL;
1524 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001525
1526 path = btrfs_alloc_path();
1527 if (!path)
1528 return -ENOMEM;
1529
1530 key.objectid = device->devid;
1531 key.offset = start;
1532 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001533again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001534 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001535 if (ret > 0) {
1536 ret = btrfs_previous_item(root, path, key.objectid,
1537 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001538 if (ret)
1539 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001540 leaf = path->nodes[0];
1541 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1542 extent = btrfs_item_ptr(leaf, path->slots[0],
1543 struct btrfs_dev_extent);
1544 BUG_ON(found_key.offset > start || found_key.offset +
1545 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001546 key = found_key;
1547 btrfs_release_path(path);
1548 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001549 } else if (ret == 0) {
1550 leaf = path->nodes[0];
1551 extent = btrfs_item_ptr(leaf, path->slots[0],
1552 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001553 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001554 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001555 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001556 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001557
Miao Xie2196d6e2014-09-03 21:35:41 +08001558 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1559
Chris Mason8f18cf12008-04-25 16:53:30 -04001560 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001561 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001562 btrfs_handle_fs_error(fs_info, ret,
1563 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001564 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001565 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001566 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001567out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001568 btrfs_free_path(path);
1569 return ret;
1570}
1571
Eric Sandeen48a3b632013-04-25 20:41:01 +00001572static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1573 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001574 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001575{
1576 int ret;
1577 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001578 struct btrfs_fs_info *fs_info = device->fs_info;
1579 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001580 struct btrfs_dev_extent *extent;
1581 struct extent_buffer *leaf;
1582 struct btrfs_key key;
1583
Chris Masondfe25022008-05-13 13:46:40 -04001584 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001585 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001586 path = btrfs_alloc_path();
1587 if (!path)
1588 return -ENOMEM;
1589
Chris Mason0b86a832008-03-24 15:01:56 -04001590 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001591 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001592 key.type = BTRFS_DEV_EXTENT_KEY;
1593 ret = btrfs_insert_empty_item(trans, root, path, &key,
1594 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001595 if (ret)
1596 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001597
1598 leaf = path->nodes[0];
1599 extent = btrfs_item_ptr(leaf, path->slots[0],
1600 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001601 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1602 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001603 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1604 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001605 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1606
Chris Mason0b86a832008-03-24 15:01:56 -04001607 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1608 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001609out:
Chris Mason0b86a832008-03-24 15:01:56 -04001610 btrfs_free_path(path);
1611 return ret;
1612}
1613
Josef Bacik6df9a952013-06-27 13:22:46 -04001614static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001615{
Josef Bacik6df9a952013-06-27 13:22:46 -04001616 struct extent_map_tree *em_tree;
1617 struct extent_map *em;
1618 struct rb_node *n;
1619 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001620
Josef Bacik6df9a952013-06-27 13:22:46 -04001621 em_tree = &fs_info->mapping_tree.map_tree;
1622 read_lock(&em_tree->lock);
1623 n = rb_last(&em_tree->map);
1624 if (n) {
1625 em = rb_entry(n, struct extent_map, rb_node);
1626 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001627 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001628 read_unlock(&em_tree->lock);
1629
Chris Mason0b86a832008-03-24 15:01:56 -04001630 return ret;
1631}
1632
Ilya Dryomov53f10652013-08-12 14:33:01 +03001633static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1634 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001635{
1636 int ret;
1637 struct btrfs_key key;
1638 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001639 struct btrfs_path *path;
1640
Yan Zheng2b820322008-11-17 21:11:30 -05001641 path = btrfs_alloc_path();
1642 if (!path)
1643 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001644
1645 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1646 key.type = BTRFS_DEV_ITEM_KEY;
1647 key.offset = (u64)-1;
1648
Ilya Dryomov53f10652013-08-12 14:33:01 +03001649 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001650 if (ret < 0)
1651 goto error;
1652
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001653 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001654
Ilya Dryomov53f10652013-08-12 14:33:01 +03001655 ret = btrfs_previous_item(fs_info->chunk_root, path,
1656 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001657 BTRFS_DEV_ITEM_KEY);
1658 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001659 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001660 } else {
1661 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1662 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001663 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001664 }
1665 ret = 0;
1666error:
Yan Zheng2b820322008-11-17 21:11:30 -05001667 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001668 return ret;
1669}
1670
1671/*
1672 * the device information is stored in the chunk root
1673 * the btrfs_device struct should be fully filled in
1674 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001675static int btrfs_add_device(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001676 struct btrfs_fs_info *fs_info,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001677 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001678{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001679 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001680 int ret;
1681 struct btrfs_path *path;
1682 struct btrfs_dev_item *dev_item;
1683 struct extent_buffer *leaf;
1684 struct btrfs_key key;
1685 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001686
Chris Mason0b86a832008-03-24 15:01:56 -04001687 path = btrfs_alloc_path();
1688 if (!path)
1689 return -ENOMEM;
1690
Chris Mason0b86a832008-03-24 15:01:56 -04001691 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1692 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001693 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001694
1695 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001696 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001697 if (ret)
1698 goto out;
1699
1700 leaf = path->nodes[0];
1701 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1702
1703 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001704 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001705 btrfs_set_device_type(leaf, dev_item, device->type);
1706 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1707 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1708 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001709 btrfs_set_device_total_bytes(leaf, dev_item,
1710 btrfs_device_get_disk_total_bytes(device));
1711 btrfs_set_device_bytes_used(leaf, dev_item,
1712 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001713 btrfs_set_device_group(leaf, dev_item, 0);
1714 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1715 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001716 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001717
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001718 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001719 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001720 ptr = btrfs_device_fsid(dev_item);
Anand Jain44880fd2017-07-29 17:50:09 +08001721 write_extent_buffer(leaf, fs_info->fsid, ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001722 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001723
Yan Zheng2b820322008-11-17 21:11:30 -05001724 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001725out:
1726 btrfs_free_path(path);
1727 return ret;
1728}
Chris Mason8f18cf12008-04-25 16:53:30 -04001729
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001730/*
1731 * Function to update ctime/mtime for a given device path.
1732 * Mainly used for ctime/mtime based probe like libblkid.
1733 */
David Sterbada353f62017-02-14 17:55:53 +01001734static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001735{
1736 struct file *filp;
1737
1738 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001739 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001740 return;
1741 file_update_time(filp);
1742 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001743}
1744
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001745static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
Chris Masona061fc82008-05-07 11:43:44 -04001746 struct btrfs_device *device)
1747{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001748 struct btrfs_root *root = fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001749 int ret;
1750 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001751 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001752 struct btrfs_trans_handle *trans;
1753
Chris Masona061fc82008-05-07 11:43:44 -04001754 path = btrfs_alloc_path();
1755 if (!path)
1756 return -ENOMEM;
1757
Yan, Zhenga22285a2010-05-16 10:48:46 -04001758 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001759 if (IS_ERR(trans)) {
1760 btrfs_free_path(path);
1761 return PTR_ERR(trans);
1762 }
Chris Masona061fc82008-05-07 11:43:44 -04001763 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1764 key.type = BTRFS_DEV_ITEM_KEY;
1765 key.offset = device->devid;
1766
1767 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1768 if (ret < 0)
1769 goto out;
1770
1771 if (ret > 0) {
1772 ret = -ENOENT;
1773 goto out;
1774 }
1775
1776 ret = btrfs_del_item(trans, root, path);
1777 if (ret)
1778 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001779out:
1780 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001781 btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001782 return ret;
1783}
1784
David Sterba3cc31a02016-02-15 16:00:26 +01001785/*
1786 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1787 * filesystem. It's up to the caller to adjust that number regarding eg. device
1788 * replace.
1789 */
1790static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1791 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001792{
Chris Masona061fc82008-05-07 11:43:44 -04001793 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00001794 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01001795 int i;
Chris Masona061fc82008-05-07 11:43:44 -04001796
Miao Xiede98ced2013-01-29 10:13:12 +00001797 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001798 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00001799
Anand Jainbd45ffb2016-02-13 10:01:34 +08001800 all_avail = fs_info->avail_data_alloc_bits |
1801 fs_info->avail_system_alloc_bits |
1802 fs_info->avail_metadata_alloc_bits;
1803 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08001804
David Sterba418775a2016-02-15 16:28:14 +01001805 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1806 if (!(all_avail & btrfs_raid_group[i]))
1807 continue;
Chris Masona061fc82008-05-07 11:43:44 -04001808
David Sterba418775a2016-02-15 16:28:14 +01001809 if (num_devices < btrfs_raid_array[i].devs_min) {
1810 int ret = btrfs_raid_mindev_error[i];
Chris Masona061fc82008-05-07 11:43:44 -04001811
David Sterba418775a2016-02-15 16:28:14 +01001812 if (ret)
1813 return ret;
1814 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08001815 }
1816
1817 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001818}
1819
Anand Jain88acff62016-05-03 17:44:43 +08001820struct btrfs_device *btrfs_find_next_active_device(struct btrfs_fs_devices *fs_devs,
1821 struct btrfs_device *device)
1822{
1823 struct btrfs_device *next_device;
1824
1825 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1826 if (next_device != device &&
1827 !next_device->missing && next_device->bdev)
1828 return next_device;
1829 }
1830
1831 return NULL;
1832}
1833
1834/*
1835 * Helper function to check if the given device is part of s_bdev / latest_bdev
1836 * and replace it with the provided or the next active device, in the context
1837 * where this function called, there should be always be another device (or
1838 * this_dev) which is active.
1839 */
1840void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info,
1841 struct btrfs_device *device, struct btrfs_device *this_dev)
1842{
1843 struct btrfs_device *next_device;
1844
1845 if (this_dev)
1846 next_device = this_dev;
1847 else
1848 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1849 device);
1850 ASSERT(next_device);
1851
1852 if (fs_info->sb->s_bdev &&
1853 (fs_info->sb->s_bdev == device->bdev))
1854 fs_info->sb->s_bdev = next_device->bdev;
1855
1856 if (fs_info->fs_devices->latest_bdev == device->bdev)
1857 fs_info->fs_devices->latest_bdev = next_device->bdev;
1858}
1859
David Sterbada353f62017-02-14 17:55:53 +01001860int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
1861 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08001862{
1863 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001864 struct btrfs_fs_devices *cur_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001865 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001866 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001867
1868 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001869
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001870 num_devices = fs_info->fs_devices->num_devices;
1871 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
1872 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
Chris Masona061fc82008-05-07 11:43:44 -04001873 WARN_ON(num_devices < 1);
1874 num_devices--;
1875 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001876 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Chris Masona061fc82008-05-07 11:43:44 -04001877
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001878 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08001879 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001880 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001881
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001882 ret = btrfs_find_device_by_devspec(fs_info, devid, device_path,
1883 &device);
Anand Jain24fc5722016-02-13 10:01:36 +08001884 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001885 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001886
Stefan Behrens63a212a2012-11-05 18:29:28 +01001887 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001888 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08001889 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001890 }
1891
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001892 if (device->writeable && fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001893 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08001894 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001895 }
1896
1897 if (device->writeable) {
David Sterba34441362016-10-04 19:34:27 +02001898 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001899 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001900 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02001901 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001902 }
Chris Masona061fc82008-05-07 11:43:44 -04001903
Carey Underwoodd7901552013-03-04 16:37:06 -06001904 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001905 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001906 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001907 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001908 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001909
Stefan Behrens63a212a2012-11-05 18:29:28 +01001910 /*
1911 * TODO: the superblock still includes this device in its num_devices
1912 * counter although write_all_supers() is not locked out. This
1913 * could give a filesystem state which requires a degraded mount.
1914 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001915 ret = btrfs_rm_dev_item(fs_info, device);
Chris Masona061fc82008-05-07 11:43:44 -04001916 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001917 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001918
Yan Zheng2b820322008-11-17 21:11:30 -05001919 device->in_fs_metadata = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001920 btrfs_scrub_cancel_dev(fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001921
1922 /*
1923 * the device list mutex makes sure that we don't change
1924 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001925 * the device supers. Whoever is writing all supers, should
1926 * lock the device list mutex before getting the number of
1927 * devices in the super block (super_copy). Conversely,
1928 * whoever updates the number of devices in the super block
1929 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001930 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001931
1932 cur_devices = device->fs_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001933 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001934 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001935
Yan Zhenge4404d62008-12-12 10:03:26 -05001936 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001937 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001938
Chris Masoncd02dca2010-12-13 14:56:23 -05001939 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001940 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001941
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001942 btrfs_assign_next_active_device(fs_info, device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05001943
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001944 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001945 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001946 /* remove sysfs entry */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001947 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001948 }
Anand Jain99994cd2014-06-03 11:36:00 +08001949
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001950 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
1951 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
1952 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001953
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04001954 /*
1955 * at this point, the device is zero sized and detached from
1956 * the devices list. All that's left is to zero out the old
1957 * supers and free the device.
1958 */
1959 if (device->writeable)
1960 btrfs_scratch_superblocks(device->bdev, device->name->str);
1961
1962 btrfs_close_bdev(device);
1963 call_rcu(&device->rcu, free_device);
1964
Xiao Guangrong1f781602011-04-20 10:09:16 +00001965 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001966 struct btrfs_fs_devices *fs_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001967 fs_devices = fs_info->fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001968 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001969 if (fs_devices->seed == cur_devices) {
1970 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001971 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001972 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001973 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001974 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001975 cur_devices->seed = NULL;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001976 __btrfs_close_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001977 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001978 }
1979
Chris Masona061fc82008-05-07 11:43:44 -04001980out:
1981 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001982 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08001983
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001984error_undo:
1985 if (device->writeable) {
David Sterba34441362016-10-04 19:34:27 +02001986 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001987 list_add(&device->dev_alloc_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001988 &fs_info->fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001989 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02001990 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001991 }
Anand Jain24fc5722016-02-13 10:01:36 +08001992 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001993}
1994
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001995void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
1996 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01001997{
Anand Jaind51908c2014-08-13 14:24:19 +08001998 struct btrfs_fs_devices *fs_devices;
1999
Stefan Behrense93c89c2012-11-05 17:33:06 +01002000 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03002001
Anand Jain25e8e912014-08-20 10:56:56 +08002002 /*
2003 * in case of fs with no seed, srcdev->fs_devices will point
2004 * to fs_devices of fs_info. However when the dev being replaced is
2005 * a seed dev it will point to the seed's local fs_devices. In short
2006 * srcdev will have its correct fs_devices in both the cases.
2007 */
2008 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002009
Stefan Behrense93c89c2012-11-05 17:33:06 +01002010 list_del_rcu(&srcdev->dev_list);
2011 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002012 fs_devices->num_devices--;
Miao Xie82372bc2014-09-03 21:35:44 +08002013 if (srcdev->missing)
Anand Jaind51908c2014-08-13 14:24:19 +08002014 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002015
Anand Jain48b3b9d2016-04-12 21:36:16 +08002016 if (srcdev->writeable)
Miao Xie82372bc2014-09-03 21:35:44 +08002017 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002018
Miao Xie82372bc2014-09-03 21:35:44 +08002019 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002020 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002021}
2022
2023void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2024 struct btrfs_device *srcdev)
2025{
2026 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002027
Anand Jain48b3b9d2016-04-12 21:36:16 +08002028 if (srcdev->writeable) {
2029 /* zero out the old super if it is writable */
2030 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2031 }
Anand Jain14238812016-07-22 06:04:53 +08002032
2033 btrfs_close_bdev(srcdev);
2034
Stefan Behrense93c89c2012-11-05 17:33:06 +01002035 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002036
2037 /*
2038 * unless fs_devices is seed fs, num_devices shouldn't go
2039 * zero
2040 */
2041 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
2042
2043 /* if this is no devs we rather delete the fs_devices */
2044 if (!fs_devices->num_devices) {
2045 struct btrfs_fs_devices *tmp_fs_devices;
2046
2047 tmp_fs_devices = fs_info->fs_devices;
2048 while (tmp_fs_devices) {
2049 if (tmp_fs_devices->seed == fs_devices) {
2050 tmp_fs_devices->seed = fs_devices->seed;
2051 break;
2052 }
2053 tmp_fs_devices = tmp_fs_devices->seed;
2054 }
2055 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08002056 __btrfs_close_devices(fs_devices);
2057 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002058 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002059}
2060
2061void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2062 struct btrfs_device *tgtdev)
2063{
Miao Xie67a2c452014-09-03 21:35:43 +08002064 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002065 WARN_ON(!tgtdev);
2066 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002067
Anand Jain32576042015-08-14 18:32:49 +08002068 btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002069
Anand Jain779bf3f2016-04-18 16:51:23 +08002070 if (tgtdev->bdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002071 fs_info->fs_devices->open_devices--;
Anand Jain779bf3f2016-04-18 16:51:23 +08002072
Stefan Behrense93c89c2012-11-05 17:33:06 +01002073 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002074
Anand Jain88acff62016-05-03 17:44:43 +08002075 btrfs_assign_next_active_device(fs_info, tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002076
Stefan Behrense93c89c2012-11-05 17:33:06 +01002077 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002078
2079 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08002080 mutex_unlock(&uuid_mutex);
Anand Jain779bf3f2016-04-18 16:51:23 +08002081
2082 /*
2083 * The update_dev_time() with in btrfs_scratch_superblocks()
2084 * may lead to a call to btrfs_show_devname() which will try
2085 * to hold device_list_mutex. And here this device
2086 * is already out of device list, so we don't have to hold
2087 * the device_list_mutex lock.
2088 */
2089 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002090
2091 btrfs_close_bdev(tgtdev);
Anand Jain779bf3f2016-04-18 16:51:23 +08002092 call_rcu(&tgtdev->rcu, free_device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002093}
2094
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002095static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002096 const char *device_path,
Eric Sandeen48a3b632013-04-25 20:41:01 +00002097 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002098{
2099 int ret = 0;
2100 struct btrfs_super_block *disk_super;
2101 u64 devid;
2102 u8 *dev_uuid;
2103 struct block_device *bdev;
2104 struct buffer_head *bh;
2105
2106 *device = NULL;
2107 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002108 fs_info->bdev_holder, 0, &bdev, &bh);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002109 if (ret)
2110 return ret;
2111 disk_super = (struct btrfs_super_block *)bh->b_data;
2112 devid = btrfs_stack_device_id(&disk_super->dev_item);
2113 dev_uuid = disk_super->dev_item.uuid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002114 *device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002115 brelse(bh);
2116 if (!*device)
2117 ret = -ENOENT;
2118 blkdev_put(bdev, FMODE_READ);
2119 return ret;
2120}
2121
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002122int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002123 const char *device_path,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002124 struct btrfs_device **device)
2125{
2126 *device = NULL;
2127 if (strcmp(device_path, "missing") == 0) {
2128 struct list_head *devices;
2129 struct btrfs_device *tmp;
2130
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002131 devices = &fs_info->fs_devices->devices;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002132 /*
2133 * It is safe to read the devices since the volume_mutex
2134 * is held by the caller.
2135 */
2136 list_for_each_entry(tmp, devices, dev_list) {
2137 if (tmp->in_fs_metadata && !tmp->bdev) {
2138 *device = tmp;
2139 break;
2140 }
2141 }
2142
Anand Jaind74a6252015-08-14 18:32:56 +08002143 if (!*device)
2144 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002145
2146 return 0;
2147 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002148 return btrfs_find_device_by_path(fs_info, device_path, device);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002149 }
2150}
2151
Yan Zheng2b820322008-11-17 21:11:30 -05002152/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002153 * Lookup a device given by device id, or the path if the id is 0.
2154 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002155int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid,
David Sterbada353f62017-02-14 17:55:53 +01002156 const char *devpath,
2157 struct btrfs_device **device)
Anand Jain24e04742016-02-13 10:01:35 +08002158{
2159 int ret;
2160
David Sterba5c5c0df2016-02-15 16:39:55 +01002161 if (devid) {
Anand Jain24e04742016-02-13 10:01:35 +08002162 ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002163 *device = btrfs_find_device(fs_info, devid, NULL, NULL);
Anand Jain24e04742016-02-13 10:01:35 +08002164 if (!*device)
2165 ret = -ENOENT;
2166 } else {
David Sterba5c5c0df2016-02-15 16:39:55 +01002167 if (!devpath || !devpath[0])
Anand Jainb3d1b152016-02-13 10:01:37 +08002168 return -EINVAL;
2169
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002170 ret = btrfs_find_device_missing_or_by_path(fs_info, devpath,
Anand Jain24e04742016-02-13 10:01:35 +08002171 device);
2172 }
2173 return ret;
2174}
2175
Yan Zheng2b820322008-11-17 21:11:30 -05002176/*
2177 * does all the dirty work required for changing file system's UUID.
2178 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002179static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002180{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002181 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002182 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002183 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002184 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002185 struct btrfs_device *device;
2186 u64 super_flags;
2187
2188 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05002189 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002190 return -EINVAL;
2191
David Sterba2dfeca92017-06-14 02:48:07 +02002192 seed_devices = alloc_fs_devices(NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002193 if (IS_ERR(seed_devices))
2194 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002195
Yan Zhenge4404d62008-12-12 10:03:26 -05002196 old_devices = clone_fs_devices(fs_devices);
2197 if (IS_ERR(old_devices)) {
2198 kfree(seed_devices);
2199 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002200 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002201
Yan Zheng2b820322008-11-17 21:11:30 -05002202 list_add(&old_devices->list, &fs_uuids);
2203
Yan Zhenge4404d62008-12-12 10:03:26 -05002204 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2205 seed_devices->opened = 1;
2206 INIT_LIST_HEAD(&seed_devices->devices);
2207 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002208 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002209
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002210 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002211 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2212 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002213 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002214 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002215
David Sterba34441362016-10-04 19:34:27 +02002216 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002217 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002218 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002219
Yan Zheng2b820322008-11-17 21:11:30 -05002220 fs_devices->seeding = 0;
2221 fs_devices->num_devices = 0;
2222 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002223 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002224 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002225 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002226
2227 generate_random_uuid(fs_devices->fsid);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002228 memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002229 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002230 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002231
Yan Zheng2b820322008-11-17 21:11:30 -05002232 super_flags = btrfs_super_flags(disk_super) &
2233 ~BTRFS_SUPER_FLAG_SEEDING;
2234 btrfs_set_super_flags(disk_super, super_flags);
2235
2236 return 0;
2237}
2238
2239/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002240 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002241 */
2242static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002243 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002244{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002245 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002246 struct btrfs_path *path;
2247 struct extent_buffer *leaf;
2248 struct btrfs_dev_item *dev_item;
2249 struct btrfs_device *device;
2250 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002251 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002252 u8 dev_uuid[BTRFS_UUID_SIZE];
2253 u64 devid;
2254 int ret;
2255
2256 path = btrfs_alloc_path();
2257 if (!path)
2258 return -ENOMEM;
2259
Yan Zheng2b820322008-11-17 21:11:30 -05002260 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2261 key.offset = 0;
2262 key.type = BTRFS_DEV_ITEM_KEY;
2263
2264 while (1) {
2265 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2266 if (ret < 0)
2267 goto error;
2268
2269 leaf = path->nodes[0];
2270next_slot:
2271 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2272 ret = btrfs_next_leaf(root, path);
2273 if (ret > 0)
2274 break;
2275 if (ret < 0)
2276 goto error;
2277 leaf = path->nodes[0];
2278 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002279 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002280 continue;
2281 }
2282
2283 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2284 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2285 key.type != BTRFS_DEV_ITEM_KEY)
2286 break;
2287
2288 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2289 struct btrfs_dev_item);
2290 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002291 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002292 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002293 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002294 BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002295 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002296 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002297
2298 if (device->fs_devices->seeding) {
2299 btrfs_set_device_generation(leaf, dev_item,
2300 device->generation);
2301 btrfs_mark_buffer_dirty(leaf);
2302 }
2303
2304 path->slots[0]++;
2305 goto next_slot;
2306 }
2307 ret = 0;
2308error:
2309 btrfs_free_path(path);
2310 return ret;
2311}
2312
David Sterbada353f62017-02-14 17:55:53 +01002313int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002314{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002315 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002316 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002317 struct btrfs_trans_handle *trans;
2318 struct btrfs_device *device;
2319 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002320 struct list_head *devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002321 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002322 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002323 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002324 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002325 int ret = 0;
2326
David Howellsbc98a422017-07-17 08:45:34 +01002327 if (sb_rdonly(sb) && !fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002328 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002329
Li Zefana5d16332011-12-07 20:08:40 -05002330 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002331 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002332 if (IS_ERR(bdev))
2333 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002334
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002335 if (fs_info->fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002336 seeding_dev = 1;
2337 down_write(&sb->s_umount);
2338 mutex_lock(&uuid_mutex);
2339 }
2340
Chris Mason8c8bee12008-09-29 11:19:10 -04002341 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002342
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002343 devices = &fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002344
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002345 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002346 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002347 if (device->bdev == bdev) {
2348 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002349 mutex_unlock(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002350 &fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002351 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002352 }
2353 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002354 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002355
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002356 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002357 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002358 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002359 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002360 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002361 }
2362
David Sterba78f2c9e2016-02-11 14:25:38 +01002363 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002364 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002365 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002366 ret = -ENOMEM;
2367 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002368 }
Josef Bacik606686e2012-06-04 14:03:51 -04002369 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002370
Yan, Zhenga22285a2010-05-16 10:48:46 -04002371 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002372 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002373 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002374 kfree(device);
2375 ret = PTR_ERR(trans);
2376 goto error;
2377 }
2378
Josef Bacikd5e20032011-08-04 14:52:27 +00002379 q = bdev_get_queue(bdev);
2380 if (blk_queue_discard(q))
2381 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002382 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002383 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002384 device->io_width = fs_info->sectorsize;
2385 device->io_align = fs_info->sectorsize;
2386 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002387 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2388 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002389 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002390 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002391 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002392 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002393 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002394 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002395 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002396 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002397 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002398
Yan Zheng2b820322008-11-17 21:11:30 -05002399 if (seeding_dev) {
2400 sb->s_flags &= ~MS_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002401 ret = btrfs_prepare_sprout(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002402 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002403 }
2404
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002405 device->fs_devices = fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002406
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002407 mutex_lock(&fs_info->fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002408 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002409 list_add_rcu(&device->dev_list, &fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002410 list_add(&device->dev_alloc_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002411 &fs_info->fs_devices->alloc_list);
2412 fs_info->fs_devices->num_devices++;
2413 fs_info->fs_devices->open_devices++;
2414 fs_info->fs_devices->rw_devices++;
2415 fs_info->fs_devices->total_devices++;
2416 fs_info->fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002417
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002418 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002419
Anand Jaine884f4f2017-04-04 18:40:19 +08002420 if (!blk_queue_nonrot(q))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002421 fs_info->fs_devices->rotating = 1;
Chris Masonc2898112009-06-10 09:51:32 -04002422
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002423 tmp = btrfs_super_total_bytes(fs_info->super_copy);
2424 btrfs_set_super_total_bytes(fs_info->super_copy,
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002425 round_down(tmp + device->total_bytes, fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002426
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002427 tmp = btrfs_super_num_devices(fs_info->super_copy);
2428 btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002429
2430 /* add sysfs device entry */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002431 btrfs_sysfs_add_device_link(fs_info->fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002432
Miao Xie2196d6e2014-09-03 21:35:41 +08002433 /*
2434 * we've got more storage, clear any full flags on the space
2435 * infos
2436 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002437 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002438
David Sterba34441362016-10-04 19:34:27 +02002439 mutex_unlock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002440 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002441
Yan Zheng2b820322008-11-17 21:11:30 -05002442 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002443 mutex_lock(&fs_info->chunk_mutex);
David Sterbae4a4dce2017-02-10 19:49:01 +01002444 ret = init_first_rw_device(trans, fs_info);
David Sterba34441362016-10-04 19:34:27 +02002445 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002446 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002447 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002448 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002449 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002450 }
2451
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002452 ret = btrfs_add_device(trans, fs_info, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002453 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002454 btrfs_abort_transaction(trans, ret);
Miao Xie2196d6e2014-09-03 21:35:41 +08002455 goto error_trans;
2456 }
2457
2458 if (seeding_dev) {
2459 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2460
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002461 ret = btrfs_finish_sprout(trans, fs_info);
David Sterba005d6422012-09-18 07:52:32 -06002462 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002463 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002464 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002465 }
Anand Jainb2373f22014-06-03 11:36:03 +08002466
2467 /* Sprouting would change fsid of the mounted root,
2468 * so rename the fsid on the sysfs
2469 */
2470 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002471 fs_info->fsid);
2472 if (kobject_rename(&fs_info->fs_devices->fsid_kobj, fsid_buf))
2473 btrfs_warn(fs_info,
2474 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002475 }
2476
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002477 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002478
2479 if (seeding_dev) {
2480 mutex_unlock(&uuid_mutex);
2481 up_write(&sb->s_umount);
2482
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002483 if (ret) /* transaction commit */
2484 return ret;
2485
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002486 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002487 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002488 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002489 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002490 trans = btrfs_attach_transaction(root);
2491 if (IS_ERR(trans)) {
2492 if (PTR_ERR(trans) == -ENOENT)
2493 return 0;
2494 return PTR_ERR(trans);
2495 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002496 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002497 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002498
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002499 /* Update ctime/mtime for libblkid */
2500 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002501 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002502
2503error_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002504 btrfs_end_transaction(trans);
Josef Bacik606686e2012-06-04 14:03:51 -04002505 rcu_string_free(device->name);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002506 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002507 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002508error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002509 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002510 if (seeding_dev) {
2511 mutex_unlock(&uuid_mutex);
2512 up_write(&sb->s_umount);
2513 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002514 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002515}
2516
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002517int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002518 const char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002519 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002520 struct btrfs_device **device_out)
2521{
2522 struct request_queue *q;
2523 struct btrfs_device *device;
2524 struct block_device *bdev;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002525 struct list_head *devices;
2526 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002527 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002528 int ret = 0;
2529
2530 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002531 if (fs_info->fs_devices->seeding) {
2532 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002533 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002534 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002535
2536 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2537 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002538 if (IS_ERR(bdev)) {
2539 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002540 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002541 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002542
2543 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2544
2545 devices = &fs_info->fs_devices->devices;
2546 list_for_each_entry(device, devices, dev_list) {
2547 if (device->bdev == bdev) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002548 btrfs_err(fs_info,
2549 "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002550 ret = -EEXIST;
2551 goto error;
2552 }
2553 }
2554
Miao Xie1c433662014-09-03 21:35:32 +08002555
Miao Xie7cc8e582014-09-03 21:35:38 +08002556 if (i_size_read(bdev->bd_inode) <
2557 btrfs_device_get_total_bytes(srcdev)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002558 btrfs_err(fs_info,
2559 "target device is smaller than source device!");
Miao Xie1c433662014-09-03 21:35:32 +08002560 ret = -EINVAL;
2561 goto error;
2562 }
2563
2564
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002565 device = btrfs_alloc_device(NULL, &devid, NULL);
2566 if (IS_ERR(device)) {
2567 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002568 goto error;
2569 }
2570
David Sterba61655722017-06-15 17:16:43 +02002571 name = rcu_string_strdup(device_path, GFP_KERNEL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002572 if (!name) {
2573 kfree(device);
2574 ret = -ENOMEM;
2575 goto error;
2576 }
2577 rcu_assign_pointer(device->name, name);
2578
2579 q = bdev_get_queue(bdev);
2580 if (blk_queue_discard(q))
2581 device->can_discard = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002582 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002583 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002584 device->generation = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002585 device->io_width = fs_info->sectorsize;
2586 device->io_align = fs_info->sectorsize;
2587 device->sector_size = fs_info->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002588 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2589 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2590 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002591 ASSERT(list_empty(&srcdev->resized_list));
2592 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002593 device->commit_bytes_used = device->bytes_used;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002594 device->fs_info = fs_info;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002595 device->bdev = bdev;
2596 device->in_fs_metadata = 1;
2597 device->is_tgtdev_for_dev_replace = 1;
2598 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002599 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002600 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002601 device->fs_devices = fs_info->fs_devices;
2602 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2603 fs_info->fs_devices->num_devices++;
2604 fs_info->fs_devices->open_devices++;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002605 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002606
2607 *device_out = device;
2608 return ret;
2609
2610error:
2611 blkdev_put(bdev, FMODE_EXCL);
2612 return ret;
2613}
2614
2615void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2616 struct btrfs_device *tgtdev)
2617{
Jeff Mahoneyda170662016-06-15 09:22:56 -04002618 u32 sectorsize = fs_info->sectorsize;
2619
Stefan Behrense93c89c2012-11-05 17:33:06 +01002620 WARN_ON(fs_info->fs_devices->rw_devices == 0);
Jeff Mahoneyda170662016-06-15 09:22:56 -04002621 tgtdev->io_width = sectorsize;
2622 tgtdev->io_align = sectorsize;
2623 tgtdev->sector_size = sectorsize;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002624 tgtdev->fs_info = fs_info;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002625 tgtdev->in_fs_metadata = 1;
2626}
2627
Chris Masond3977122009-01-05 21:25:51 -05002628static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2629 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002630{
2631 int ret;
2632 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002633 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002634 struct btrfs_dev_item *dev_item;
2635 struct extent_buffer *leaf;
2636 struct btrfs_key key;
2637
Chris Mason0b86a832008-03-24 15:01:56 -04002638 path = btrfs_alloc_path();
2639 if (!path)
2640 return -ENOMEM;
2641
2642 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2643 key.type = BTRFS_DEV_ITEM_KEY;
2644 key.offset = device->devid;
2645
2646 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2647 if (ret < 0)
2648 goto out;
2649
2650 if (ret > 0) {
2651 ret = -ENOENT;
2652 goto out;
2653 }
2654
2655 leaf = path->nodes[0];
2656 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2657
2658 btrfs_set_device_id(leaf, dev_item, device->devid);
2659 btrfs_set_device_type(leaf, dev_item, device->type);
2660 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2661 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2662 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002663 btrfs_set_device_total_bytes(leaf, dev_item,
2664 btrfs_device_get_disk_total_bytes(device));
2665 btrfs_set_device_bytes_used(leaf, dev_item,
2666 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002667 btrfs_mark_buffer_dirty(leaf);
2668
2669out:
2670 btrfs_free_path(path);
2671 return ret;
2672}
2673
Miao Xie2196d6e2014-09-03 21:35:41 +08002674int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002675 struct btrfs_device *device, u64 new_size)
2676{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002677 struct btrfs_fs_info *fs_info = device->fs_info;
2678 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002679 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002680 u64 old_total;
2681 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002682
Yan Zheng2b820322008-11-17 21:11:30 -05002683 if (!device->writeable)
2684 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002685
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002686 new_size = round_down(new_size, fs_info->sectorsize);
2687
David Sterba34441362016-10-04 19:34:27 +02002688 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002689 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002690 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002691
Stefan Behrens63a212a2012-11-05 18:29:28 +01002692 if (new_size <= device->total_bytes ||
Miao Xie2196d6e2014-09-03 21:35:41 +08002693 device->is_tgtdev_for_dev_replace) {
David Sterba34441362016-10-04 19:34:27 +02002694 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002695 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002696 }
Yan Zheng2b820322008-11-17 21:11:30 -05002697
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002698 fs_devices = fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002699
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002700 btrfs_set_super_total_bytes(super_copy,
2701 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002702 device->fs_devices->total_rw_bytes += diff;
2703
Miao Xie7cc8e582014-09-03 21:35:38 +08002704 btrfs_device_set_total_bytes(device, new_size);
2705 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002706 btrfs_clear_space_info_full(device->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002707 if (list_empty(&device->resized_list))
2708 list_add_tail(&device->resized_list,
2709 &fs_devices->resized_devices);
David Sterba34441362016-10-04 19:34:27 +02002710 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002711
Chris Mason8f18cf12008-04-25 16:53:30 -04002712 return btrfs_update_device(trans, device);
2713}
2714
2715static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002716 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002717{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002718 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002719 int ret;
2720 struct btrfs_path *path;
2721 struct btrfs_key key;
2722
Chris Mason8f18cf12008-04-25 16:53:30 -04002723 path = btrfs_alloc_path();
2724 if (!path)
2725 return -ENOMEM;
2726
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002727 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002728 key.offset = chunk_offset;
2729 key.type = BTRFS_CHUNK_ITEM_KEY;
2730
2731 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002732 if (ret < 0)
2733 goto out;
2734 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002735 btrfs_handle_fs_error(fs_info, -ENOENT,
2736 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002737 ret = -ENOENT;
2738 goto out;
2739 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002740
2741 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002742 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002743 btrfs_handle_fs_error(fs_info, ret,
2744 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002745out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002746 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002747 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002748}
2749
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002750static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002751{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002752 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002753 struct btrfs_disk_key *disk_key;
2754 struct btrfs_chunk *chunk;
2755 u8 *ptr;
2756 int ret = 0;
2757 u32 num_stripes;
2758 u32 array_size;
2759 u32 len = 0;
2760 u32 cur;
2761 struct btrfs_key key;
2762
David Sterba34441362016-10-04 19:34:27 +02002763 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002764 array_size = btrfs_super_sys_array_size(super_copy);
2765
2766 ptr = super_copy->sys_chunk_array;
2767 cur = 0;
2768
2769 while (cur < array_size) {
2770 disk_key = (struct btrfs_disk_key *)ptr;
2771 btrfs_disk_key_to_cpu(&key, disk_key);
2772
2773 len = sizeof(*disk_key);
2774
2775 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2776 chunk = (struct btrfs_chunk *)(ptr + len);
2777 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2778 len += btrfs_chunk_item_size(num_stripes);
2779 } else {
2780 ret = -EIO;
2781 break;
2782 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002783 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002784 key.offset == chunk_offset) {
2785 memmove(ptr, ptr + len, array_size - (cur + len));
2786 array_size -= len;
2787 btrfs_set_super_sys_array_size(super_copy, array_size);
2788 } else {
2789 ptr += len;
2790 cur += len;
2791 }
2792 }
David Sterba34441362016-10-04 19:34:27 +02002793 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002794 return ret;
2795}
2796
Liu Bo592d92e2017-03-14 13:33:55 -07002797static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info,
2798 u64 logical, u64 length)
2799{
2800 struct extent_map_tree *em_tree;
2801 struct extent_map *em;
2802
2803 em_tree = &fs_info->mapping_tree.map_tree;
2804 read_lock(&em_tree->lock);
2805 em = lookup_extent_mapping(em_tree, logical, length);
2806 read_unlock(&em_tree->lock);
2807
2808 if (!em) {
2809 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2810 logical, length);
2811 return ERR_PTR(-EINVAL);
2812 }
2813
2814 if (em->start > logical || em->start + em->len < logical) {
2815 btrfs_crit(fs_info,
2816 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2817 logical, length, em->start, em->start + em->len);
2818 free_extent_map(em);
2819 return ERR_PTR(-EINVAL);
2820 }
2821
2822 /* callers are responsible for dropping em's ref. */
2823 return em;
2824}
2825
Josef Bacik47ab2a62014-09-18 11:20:02 -04002826int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002827 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04002828{
Josef Bacik47ab2a62014-09-18 11:20:02 -04002829 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002830 struct map_lookup *map;
2831 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002832 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002833 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002834
Liu Bo592d92e2017-03-14 13:33:55 -07002835 em = get_chunk_map(fs_info, chunk_offset, 1);
2836 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04002837 /*
2838 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002839 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002840 * do anything we still error out.
2841 */
2842 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07002843 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002844 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002845 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02002846 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002847 check_system_chunk(trans, fs_info, map->type);
David Sterba34441362016-10-04 19:34:27 +02002848 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002849
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002850 /*
2851 * Take the device list mutex to prevent races with the final phase of
2852 * a device replace operation that replaces the device object associated
2853 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2854 */
2855 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002856 for (i = 0; i < map->num_stripes; i++) {
2857 struct btrfs_device *device = map->stripes[i].dev;
2858 ret = btrfs_free_dev_extent(trans, device,
2859 map->stripes[i].physical,
2860 &dev_extent_len);
2861 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002862 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002863 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002864 goto out;
2865 }
2866
2867 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02002868 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002869 btrfs_device_set_bytes_used(device,
2870 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002871 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002872 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02002873 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002874 }
2875
2876 if (map->stripes[i].dev) {
2877 ret = btrfs_update_device(trans, map->stripes[i].dev);
2878 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002879 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002880 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002881 goto out;
2882 }
2883 }
2884 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002885 mutex_unlock(&fs_devices->device_list_mutex);
2886
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002887 ret = btrfs_free_chunk(trans, fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002888 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002889 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002890 goto out;
2891 }
2892
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002893 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002894
2895 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002896 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002897 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002898 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002899 goto out;
2900 }
2901 }
2902
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002903 ret = btrfs_remove_block_group(trans, fs_info, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002904 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002905 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002906 goto out;
2907 }
2908
Josef Bacik47ab2a62014-09-18 11:20:02 -04002909out:
2910 /* once for us */
2911 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002912 return ret;
2913}
2914
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002915static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002916{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002917 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07002918 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002919 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002920
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002921 /*
2922 * Prevent races with automatic removal of unused block groups.
2923 * After we relocate and before we remove the chunk with offset
2924 * chunk_offset, automatic removal of the block group can kick in,
2925 * resulting in a failure when calling btrfs_remove_chunk() below.
2926 *
2927 * Make sure to acquire this mutex before doing a tree search (dev
2928 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2929 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2930 * we release the path used to search the chunk/dev tree and before
2931 * the current task acquires this mutex and calls us.
2932 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002933 ASSERT(mutex_is_locked(&fs_info->delete_unused_bgs_mutex));
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002934
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002935 ret = btrfs_can_relocate(fs_info, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002936 if (ret)
2937 return -ENOSPC;
2938
Chris Mason8f18cf12008-04-25 16:53:30 -04002939 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002940 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002941 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002942 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002943 if (ret)
2944 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002945
Chris Mason19c4d2f2016-10-10 13:43:31 -07002946 trans = btrfs_start_trans_remove_block_group(root->fs_info,
2947 chunk_offset);
2948 if (IS_ERR(trans)) {
2949 ret = PTR_ERR(trans);
2950 btrfs_handle_fs_error(root->fs_info, ret, NULL);
2951 return ret;
2952 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09002953
Chris Mason19c4d2f2016-10-10 13:43:31 -07002954 /*
2955 * step two, delete the device extents and the
2956 * chunk tree entries
2957 */
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002958 ret = btrfs_remove_chunk(trans, fs_info, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002959 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07002960 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002961}
2962
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002963static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002964{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002965 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002966 struct btrfs_path *path;
2967 struct extent_buffer *leaf;
2968 struct btrfs_chunk *chunk;
2969 struct btrfs_key key;
2970 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05002971 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002972 bool retried = false;
2973 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002974 int ret;
2975
2976 path = btrfs_alloc_path();
2977 if (!path)
2978 return -ENOMEM;
2979
Josef Bacikba1bf482009-09-11 16:11:19 -04002980again:
Yan Zheng2b820322008-11-17 21:11:30 -05002981 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2982 key.offset = (u64)-1;
2983 key.type = BTRFS_CHUNK_ITEM_KEY;
2984
2985 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002986 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002987 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002988 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002989 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002990 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002991 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002992 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002993
2994 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2995 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002996 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002997 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002998 if (ret < 0)
2999 goto error;
3000 if (ret > 0)
3001 break;
3002
3003 leaf = path->nodes[0];
3004 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3005
3006 chunk = btrfs_item_ptr(leaf, path->slots[0],
3007 struct btrfs_chunk);
3008 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003009 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003010
3011 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003012 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003013 if (ret == -ENOSPC)
3014 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303015 else
3016 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003017 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003018 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003019
3020 if (found_key.offset == 0)
3021 break;
3022 key.offset = found_key.offset - 1;
3023 }
3024 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003025 if (failed && !retried) {
3026 failed = 0;
3027 retried = true;
3028 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303029 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003030 ret = -ENOSPC;
3031 }
Yan Zheng2b820322008-11-17 21:11:30 -05003032error:
3033 btrfs_free_path(path);
3034 return ret;
3035}
3036
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003037static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003038 struct btrfs_balance_control *bctl)
3039{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003040 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003041 struct btrfs_trans_handle *trans;
3042 struct btrfs_balance_item *item;
3043 struct btrfs_disk_balance_args disk_bargs;
3044 struct btrfs_path *path;
3045 struct extent_buffer *leaf;
3046 struct btrfs_key key;
3047 int ret, err;
3048
3049 path = btrfs_alloc_path();
3050 if (!path)
3051 return -ENOMEM;
3052
3053 trans = btrfs_start_transaction(root, 0);
3054 if (IS_ERR(trans)) {
3055 btrfs_free_path(path);
3056 return PTR_ERR(trans);
3057 }
3058
3059 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003060 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003061 key.offset = 0;
3062
3063 ret = btrfs_insert_empty_item(trans, root, path, &key,
3064 sizeof(*item));
3065 if (ret)
3066 goto out;
3067
3068 leaf = path->nodes[0];
3069 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3070
David Sterbab159fa22016-11-08 18:09:03 +01003071 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003072
3073 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3074 btrfs_set_balance_data(leaf, item, &disk_bargs);
3075 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3076 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3077 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3078 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3079
3080 btrfs_set_balance_flags(leaf, item, bctl->flags);
3081
3082 btrfs_mark_buffer_dirty(leaf);
3083out:
3084 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003085 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003086 if (err && !ret)
3087 ret = err;
3088 return ret;
3089}
3090
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003091static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003092{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003093 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003094 struct btrfs_trans_handle *trans;
3095 struct btrfs_path *path;
3096 struct btrfs_key key;
3097 int ret, err;
3098
3099 path = btrfs_alloc_path();
3100 if (!path)
3101 return -ENOMEM;
3102
3103 trans = btrfs_start_transaction(root, 0);
3104 if (IS_ERR(trans)) {
3105 btrfs_free_path(path);
3106 return PTR_ERR(trans);
3107 }
3108
3109 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003110 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003111 key.offset = 0;
3112
3113 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3114 if (ret < 0)
3115 goto out;
3116 if (ret > 0) {
3117 ret = -ENOENT;
3118 goto out;
3119 }
3120
3121 ret = btrfs_del_item(trans, root, path);
3122out:
3123 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003124 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003125 if (err && !ret)
3126 ret = err;
3127 return ret;
3128}
3129
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003130/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003131 * This is a heuristic used to reduce the number of chunks balanced on
3132 * resume after balance was interrupted.
3133 */
3134static void update_balance_args(struct btrfs_balance_control *bctl)
3135{
3136 /*
3137 * Turn on soft mode for chunk types that were being converted.
3138 */
3139 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3140 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3141 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3142 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3143 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3144 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3145
3146 /*
3147 * Turn on usage filter if is not already used. The idea is
3148 * that chunks that we have already balanced should be
3149 * reasonably full. Don't do it for chunks that are being
3150 * converted - that will keep us from relocating unconverted
3151 * (albeit full) chunks.
3152 */
3153 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003154 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003155 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3156 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3157 bctl->data.usage = 90;
3158 }
3159 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003160 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003161 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3162 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3163 bctl->sys.usage = 90;
3164 }
3165 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003166 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003167 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3168 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3169 bctl->meta.usage = 90;
3170 }
3171}
3172
3173/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003174 * Should be called with both balance and volume mutexes held to
3175 * serialize other volume operations (add_dev/rm_dev/resize) with
3176 * restriper. Same goes for unset_balance_control.
3177 */
3178static void set_balance_control(struct btrfs_balance_control *bctl)
3179{
3180 struct btrfs_fs_info *fs_info = bctl->fs_info;
3181
3182 BUG_ON(fs_info->balance_ctl);
3183
3184 spin_lock(&fs_info->balance_lock);
3185 fs_info->balance_ctl = bctl;
3186 spin_unlock(&fs_info->balance_lock);
3187}
3188
3189static void unset_balance_control(struct btrfs_fs_info *fs_info)
3190{
3191 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3192
3193 BUG_ON(!fs_info->balance_ctl);
3194
3195 spin_lock(&fs_info->balance_lock);
3196 fs_info->balance_ctl = NULL;
3197 spin_unlock(&fs_info->balance_lock);
3198
3199 kfree(bctl);
3200}
3201
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003202/*
3203 * Balance filters. Return 1 if chunk should be filtered out
3204 * (should not be balanced).
3205 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003206static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003207 struct btrfs_balance_args *bargs)
3208{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003209 chunk_type = chunk_to_extended(chunk_type) &
3210 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003211
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003212 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003213 return 0;
3214
3215 return 1;
3216}
3217
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003218static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003219 struct btrfs_balance_args *bargs)
3220{
3221 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003222 u64 chunk_used;
3223 u64 user_thresh_min;
3224 u64 user_thresh_max;
3225 int ret = 1;
3226
3227 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3228 chunk_used = btrfs_block_group_used(&cache->item);
3229
3230 if (bargs->usage_min == 0)
3231 user_thresh_min = 0;
3232 else
3233 user_thresh_min = div_factor_fine(cache->key.offset,
3234 bargs->usage_min);
3235
3236 if (bargs->usage_max == 0)
3237 user_thresh_max = 1;
3238 else if (bargs->usage_max > 100)
3239 user_thresh_max = cache->key.offset;
3240 else
3241 user_thresh_max = div_factor_fine(cache->key.offset,
3242 bargs->usage_max);
3243
3244 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3245 ret = 0;
3246
3247 btrfs_put_block_group(cache);
3248 return ret;
3249}
3250
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003251static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003252 u64 chunk_offset, struct btrfs_balance_args *bargs)
3253{
3254 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003255 u64 chunk_used, user_thresh;
3256 int ret = 1;
3257
3258 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3259 chunk_used = btrfs_block_group_used(&cache->item);
3260
David Sterbabc309462015-10-20 18:22:13 +02003261 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003262 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003263 else if (bargs->usage > 100)
3264 user_thresh = cache->key.offset;
3265 else
3266 user_thresh = div_factor_fine(cache->key.offset,
3267 bargs->usage);
3268
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003269 if (chunk_used < user_thresh)
3270 ret = 0;
3271
3272 btrfs_put_block_group(cache);
3273 return ret;
3274}
3275
Ilya Dryomov409d4042012-01-16 22:04:47 +02003276static int chunk_devid_filter(struct extent_buffer *leaf,
3277 struct btrfs_chunk *chunk,
3278 struct btrfs_balance_args *bargs)
3279{
3280 struct btrfs_stripe *stripe;
3281 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3282 int i;
3283
3284 for (i = 0; i < num_stripes; i++) {
3285 stripe = btrfs_stripe_nr(chunk, i);
3286 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3287 return 0;
3288 }
3289
3290 return 1;
3291}
3292
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003293/* [pstart, pend) */
3294static int chunk_drange_filter(struct extent_buffer *leaf,
3295 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003296 struct btrfs_balance_args *bargs)
3297{
3298 struct btrfs_stripe *stripe;
3299 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3300 u64 stripe_offset;
3301 u64 stripe_length;
3302 int factor;
3303 int i;
3304
3305 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3306 return 0;
3307
3308 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003309 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3310 factor = num_stripes / 2;
3311 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3312 factor = num_stripes - 1;
3313 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3314 factor = num_stripes - 2;
3315 } else {
3316 factor = num_stripes;
3317 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003318
3319 for (i = 0; i < num_stripes; i++) {
3320 stripe = btrfs_stripe_nr(chunk, i);
3321 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3322 continue;
3323
3324 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3325 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003326 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003327
3328 if (stripe_offset < bargs->pend &&
3329 stripe_offset + stripe_length > bargs->pstart)
3330 return 0;
3331 }
3332
3333 return 1;
3334}
3335
Ilya Dryomovea671762012-01-16 22:04:48 +02003336/* [vstart, vend) */
3337static int chunk_vrange_filter(struct extent_buffer *leaf,
3338 struct btrfs_chunk *chunk,
3339 u64 chunk_offset,
3340 struct btrfs_balance_args *bargs)
3341{
3342 if (chunk_offset < bargs->vend &&
3343 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3344 /* at least part of the chunk is inside this vrange */
3345 return 0;
3346
3347 return 1;
3348}
3349
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003350static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3351 struct btrfs_chunk *chunk,
3352 struct btrfs_balance_args *bargs)
3353{
3354 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3355
3356 if (bargs->stripes_min <= num_stripes
3357 && num_stripes <= bargs->stripes_max)
3358 return 0;
3359
3360 return 1;
3361}
3362
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003363static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003364 struct btrfs_balance_args *bargs)
3365{
3366 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3367 return 0;
3368
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003369 chunk_type = chunk_to_extended(chunk_type) &
3370 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003371
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003372 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003373 return 1;
3374
3375 return 0;
3376}
3377
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003378static int should_balance_chunk(struct btrfs_fs_info *fs_info,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003379 struct extent_buffer *leaf,
3380 struct btrfs_chunk *chunk, u64 chunk_offset)
3381{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003382 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003383 struct btrfs_balance_args *bargs = NULL;
3384 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3385
3386 /* type filter */
3387 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3388 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3389 return 0;
3390 }
3391
3392 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3393 bargs = &bctl->data;
3394 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3395 bargs = &bctl->sys;
3396 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3397 bargs = &bctl->meta;
3398
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003399 /* profiles filter */
3400 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3401 chunk_profiles_filter(chunk_type, bargs)) {
3402 return 0;
3403 }
3404
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003405 /* usage filter */
3406 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003407 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003408 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003409 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003410 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003411 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003412 }
3413
Ilya Dryomov409d4042012-01-16 22:04:47 +02003414 /* devid filter */
3415 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3416 chunk_devid_filter(leaf, chunk, bargs)) {
3417 return 0;
3418 }
3419
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003420 /* drange filter, makes sense only with devid filter */
3421 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003422 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003423 return 0;
3424 }
3425
Ilya Dryomovea671762012-01-16 22:04:48 +02003426 /* vrange filter */
3427 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3428 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3429 return 0;
3430 }
3431
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003432 /* stripes filter */
3433 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3434 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3435 return 0;
3436 }
3437
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003438 /* soft profile changing mode */
3439 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3440 chunk_soft_convert_filter(chunk_type, bargs)) {
3441 return 0;
3442 }
3443
David Sterba7d824b62014-05-07 17:37:51 +02003444 /*
3445 * limited by count, must be the last filter
3446 */
3447 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3448 if (bargs->limit == 0)
3449 return 0;
3450 else
3451 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003452 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3453 /*
3454 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003455 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003456 * about the count of all chunks that satisfy the filters.
3457 */
3458 if (bargs->limit_max == 0)
3459 return 0;
3460 else
3461 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003462 }
3463
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003464 return 1;
3465}
3466
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003467static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003468{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003469 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003470 struct btrfs_root *chunk_root = fs_info->chunk_root;
3471 struct btrfs_root *dev_root = fs_info->dev_root;
3472 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003473 struct btrfs_device *device;
3474 u64 old_size;
3475 u64 size_to_free;
David Sterba12907fc2015-10-10 17:16:50 +02003476 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003477 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003478 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003479 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003480 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003481 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003482 struct extent_buffer *leaf;
3483 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003484 int ret;
3485 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003486 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003487 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003488 u64 limit_data = bctl->data.limit;
3489 u64 limit_meta = bctl->meta.limit;
3490 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003491 u32 count_data = 0;
3492 u32 count_meta = 0;
3493 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003494 int chunk_reserved = 0;
Liu Bocf25ce52015-12-14 18:29:32 -08003495 u64 bytes_used = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003496
Chris Masonec44a352008-04-28 15:29:52 -04003497 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003498 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003499 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003500 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003501 size_to_free = div_factor(old_size, 1);
Byongho Leeee221842015-12-15 01:42:10 +09003502 size_to_free = min_t(u64, size_to_free, SZ_1M);
Yan Zheng2b820322008-11-17 21:11:30 -05003503 if (!device->writeable ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003504 btrfs_device_get_total_bytes(device) -
3505 btrfs_device_get_bytes_used(device) > size_to_free ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003506 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003507 continue;
3508
3509 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003510 if (ret == -ENOSPC)
3511 break;
Liu Bo5a488b92016-07-12 11:24:21 -07003512 if (ret) {
3513 /* btrfs_shrink_device never returns ret > 0 */
3514 WARN_ON(ret > 0);
3515 goto error;
3516 }
Chris Masonec44a352008-04-28 15:29:52 -04003517
Yan, Zhenga22285a2010-05-16 10:48:46 -04003518 trans = btrfs_start_transaction(dev_root, 0);
Liu Bo5a488b92016-07-12 11:24:21 -07003519 if (IS_ERR(trans)) {
3520 ret = PTR_ERR(trans);
3521 btrfs_info_in_rcu(fs_info,
3522 "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu",
3523 rcu_str_deref(device->name), ret,
3524 old_size, old_size - size_to_free);
3525 goto error;
3526 }
Chris Masonec44a352008-04-28 15:29:52 -04003527
3528 ret = btrfs_grow_device(trans, device, old_size);
Liu Bo5a488b92016-07-12 11:24:21 -07003529 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003530 btrfs_end_transaction(trans);
Liu Bo5a488b92016-07-12 11:24:21 -07003531 /* btrfs_grow_device never returns ret > 0 */
3532 WARN_ON(ret > 0);
3533 btrfs_info_in_rcu(fs_info,
3534 "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu",
3535 rcu_str_deref(device->name), ret,
3536 old_size, old_size - size_to_free);
3537 goto error;
3538 }
Chris Masonec44a352008-04-28 15:29:52 -04003539
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003540 btrfs_end_transaction(trans);
Chris Masonec44a352008-04-28 15:29:52 -04003541 }
3542
3543 /* step two, relocate all the chunks */
3544 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003545 if (!path) {
3546 ret = -ENOMEM;
3547 goto error;
3548 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003549
3550 /* zero out stat counters */
3551 spin_lock(&fs_info->balance_lock);
3552 memset(&bctl->stat, 0, sizeof(bctl->stat));
3553 spin_unlock(&fs_info->balance_lock);
3554again:
David Sterba7d824b62014-05-07 17:37:51 +02003555 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003556 /*
3557 * The single value limit and min/max limits use the same bytes
3558 * in the
3559 */
David Sterba7d824b62014-05-07 17:37:51 +02003560 bctl->data.limit = limit_data;
3561 bctl->meta.limit = limit_meta;
3562 bctl->sys.limit = limit_sys;
3563 }
Chris Masonec44a352008-04-28 15:29:52 -04003564 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3565 key.offset = (u64)-1;
3566 key.type = BTRFS_CHUNK_ITEM_KEY;
3567
Chris Masond3977122009-01-05 21:25:51 -05003568 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003569 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003570 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003571 ret = -ECANCELED;
3572 goto error;
3573 }
3574
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003575 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003576 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003577 if (ret < 0) {
3578 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003579 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003580 }
Chris Masonec44a352008-04-28 15:29:52 -04003581
3582 /*
3583 * this shouldn't happen, it means the last relocate
3584 * failed
3585 */
3586 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003587 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003588
3589 ret = btrfs_previous_item(chunk_root, path, 0,
3590 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003591 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003592 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003593 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003594 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003595 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003596
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003597 leaf = path->nodes[0];
3598 slot = path->slots[0];
3599 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3600
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003601 if (found_key.objectid != key.objectid) {
3602 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003603 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003604 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003605
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003606 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003607 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003608
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003609 if (!counting) {
3610 spin_lock(&fs_info->balance_lock);
3611 bctl->stat.considered++;
3612 spin_unlock(&fs_info->balance_lock);
3613 }
3614
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003615 ret = should_balance_chunk(fs_info, leaf, chunk,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003616 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003617
David Sterbab3b4aa72011-04-21 01:20:15 +02003618 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003619 if (!ret) {
3620 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003621 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003622 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003623
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003624 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003625 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003626 spin_lock(&fs_info->balance_lock);
3627 bctl->stat.expected++;
3628 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003629
3630 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3631 count_data++;
3632 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3633 count_sys++;
3634 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3635 count_meta++;
3636
3637 goto loop;
3638 }
3639
3640 /*
3641 * Apply limit_min filter, no need to check if the LIMITS
3642 * filter is used, limit_min is 0 by default
3643 */
3644 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3645 count_data < bctl->data.limit_min)
3646 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3647 count_meta < bctl->meta.limit_min)
3648 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3649 count_sys < bctl->sys.limit_min)) {
3650 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003651 goto loop;
3652 }
3653
Liu Bocf25ce52015-12-14 18:29:32 -08003654 ASSERT(fs_info->data_sinfo);
3655 spin_lock(&fs_info->data_sinfo->lock);
3656 bytes_used = fs_info->data_sinfo->bytes_used;
3657 spin_unlock(&fs_info->data_sinfo->lock);
3658
3659 if ((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3660 !chunk_reserved && !bytes_used) {
Zhao Lei2c9fe832015-11-09 11:51:32 +08003661 trans = btrfs_start_transaction(chunk_root, 0);
3662 if (IS_ERR(trans)) {
3663 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3664 ret = PTR_ERR(trans);
3665 goto error;
3666 }
3667
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003668 ret = btrfs_force_chunk_alloc(trans, fs_info,
Zhao Lei2c9fe832015-11-09 11:51:32 +08003669 BTRFS_BLOCK_GROUP_DATA);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003670 btrfs_end_transaction(trans);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003671 if (ret < 0) {
3672 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3673 goto error;
3674 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003675 chunk_reserved = 1;
3676 }
3677
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003678 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003679 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik508794e2011-07-02 21:24:41 +00003680 if (ret && ret != -ENOSPC)
3681 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003682 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003683 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003684 } else {
3685 spin_lock(&fs_info->balance_lock);
3686 bctl->stat.completed++;
3687 spin_unlock(&fs_info->balance_lock);
3688 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003689loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003690 if (found_key.offset == 0)
3691 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003692 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003693 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003694
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003695 if (counting) {
3696 btrfs_release_path(path);
3697 counting = false;
3698 goto again;
3699 }
Chris Masonec44a352008-04-28 15:29:52 -04003700error:
3701 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003702 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003703 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003704 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003705 if (!ret)
3706 ret = -ENOSPC;
3707 }
3708
Chris Masonec44a352008-04-28 15:29:52 -04003709 return ret;
3710}
3711
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003712/**
3713 * alloc_profile_is_valid - see if a given profile is valid and reduced
3714 * @flags: profile to validate
3715 * @extended: if true @flags is treated as an extended profile
3716 */
3717static int alloc_profile_is_valid(u64 flags, int extended)
3718{
3719 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3720 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3721
3722 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3723
3724 /* 1) check that all other bits are zeroed */
3725 if (flags & ~mask)
3726 return 0;
3727
3728 /* 2) see if profile is reduced */
3729 if (flags == 0)
3730 return !extended; /* "0" is valid for usual profiles */
3731
3732 /* true if exactly one bit set */
3733 return (flags & (flags - 1)) == 0;
3734}
3735
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003736static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3737{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003738 /* cancel requested || normal exit path */
3739 return atomic_read(&fs_info->balance_cancel_req) ||
3740 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3741 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003742}
3743
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003744static void __cancel_balance(struct btrfs_fs_info *fs_info)
3745{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003746 int ret;
3747
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003748 unset_balance_control(fs_info);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003749 ret = del_balance_item(fs_info);
Liu Bo0f788c52013-03-04 16:25:40 +00003750 if (ret)
Anand Jain34d97002016-03-16 16:43:06 +08003751 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003752
David Sterba171938e2017-03-28 14:44:21 +02003753 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003754}
3755
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003756/* Non-zero return value signifies invalidity */
3757static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3758 u64 allowed)
3759{
3760 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3761 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3762 (bctl_arg->target & ~allowed)));
3763}
3764
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003765/*
3766 * Should be called with both balance and volume mutexes held
3767 */
3768int btrfs_balance(struct btrfs_balance_control *bctl,
3769 struct btrfs_ioctl_balance_args *bargs)
3770{
3771 struct btrfs_fs_info *fs_info = bctl->fs_info;
Adam Borowski14506122017-03-07 23:34:44 +01003772 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003773 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003774 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003775 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003776 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003777 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003778
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003779 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003780 atomic_read(&fs_info->balance_pause_req) ||
3781 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003782 ret = -EINVAL;
3783 goto out;
3784 }
3785
Ilya Dryomove4837f82012-03-27 17:09:17 +03003786 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3787 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3788 mixed = 1;
3789
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003790 /*
3791 * In case of mixed groups both data and meta should be picked,
3792 * and identical options should be given for both of them.
3793 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003794 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3795 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003796 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3797 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3798 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003799 btrfs_err(fs_info,
3800 "with mixed groups data and metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003801 ret = -EINVAL;
3802 goto out;
3803 }
3804 }
3805
Stefan Behrens8dabb742012-11-06 13:15:27 +01003806 num_devices = fs_info->fs_devices->num_devices;
Liu Bo73beece2015-07-17 16:49:19 +08003807 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Stefan Behrens8dabb742012-11-06 13:15:27 +01003808 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3809 BUG_ON(num_devices < 1);
3810 num_devices--;
3811 }
Liu Bo73beece2015-07-17 16:49:19 +08003812 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Austin S. Hemmelgarn88be1592016-03-23 14:22:59 -04003813 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
3814 if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003815 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003816 if (num_devices > 2)
3817 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3818 if (num_devices > 3)
3819 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3820 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003821 if (validate_convert_profile(&bctl->data, allowed)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003822 btrfs_err(fs_info,
3823 "unable to start balance with target data profile %llu",
3824 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003825 ret = -EINVAL;
3826 goto out;
3827 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003828 if (validate_convert_profile(&bctl->meta, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003829 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003830 "unable to start balance with target metadata profile %llu",
3831 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003832 ret = -EINVAL;
3833 goto out;
3834 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003835 if (validate_convert_profile(&bctl->sys, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003836 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003837 "unable to start balance with target system profile %llu",
3838 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003839 ret = -EINVAL;
3840 goto out;
3841 }
3842
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003843 /* allow to reduce meta or sys integrity only if force set */
3844 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003845 BTRFS_BLOCK_GROUP_RAID10 |
3846 BTRFS_BLOCK_GROUP_RAID5 |
3847 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003848 do {
3849 seq = read_seqbegin(&fs_info->profiles_lock);
3850
3851 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3852 (fs_info->avail_system_alloc_bits & allowed) &&
3853 !(bctl->sys.target & allowed)) ||
3854 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3855 (fs_info->avail_metadata_alloc_bits & allowed) &&
3856 !(bctl->meta.target & allowed))) {
3857 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003858 btrfs_info(fs_info,
3859 "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003860 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003861 btrfs_err(fs_info,
3862 "balance will reduce metadata integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003863 ret = -EINVAL;
3864 goto out;
3865 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003866 }
Miao Xiede98ced2013-01-29 10:13:12 +00003867 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003868
Adam Borowski14506122017-03-07 23:34:44 +01003869 /* if we're not converting, the target field is uninitialized */
3870 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3871 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
3872 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3873 bctl->data.target : fs_info->avail_data_alloc_bits;
3874 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
3875 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Sam Tygieree592d02016-01-06 08:46:12 +00003876 btrfs_warn(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003877 "metadata profile 0x%llx has lower redundancy than data profile 0x%llx",
Adam Borowski14506122017-03-07 23:34:44 +01003878 meta_target, data_target);
Sam Tygieree592d02016-01-06 08:46:12 +00003879 }
3880
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003881 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003882 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003883 goto out;
3884
Ilya Dryomov59641012012-01-16 22:04:48 +02003885 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3886 BUG_ON(ret == -EEXIST);
3887 set_balance_control(bctl);
3888 } else {
3889 BUG_ON(ret != -EEXIST);
3890 spin_lock(&fs_info->balance_lock);
3891 update_balance_args(bctl);
3892 spin_unlock(&fs_info->balance_lock);
3893 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003894
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003895 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003896 mutex_unlock(&fs_info->balance_mutex);
3897
3898 ret = __btrfs_balance(fs_info);
3899
3900 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003901 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003902
3903 if (bargs) {
3904 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003905 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003906 }
3907
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003908 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3909 balance_need_close(fs_info)) {
3910 __cancel_balance(fs_info);
3911 }
3912
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003913 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003914
3915 return ret;
3916out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003917 if (bctl->flags & BTRFS_BALANCE_RESUME)
3918 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003919 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003920 kfree(bctl);
David Sterba171938e2017-03-28 14:44:21 +02003921 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003922 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003923 return ret;
3924}
3925
3926static int balance_kthread(void *data)
3927{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003928 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003929 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003930
3931 mutex_lock(&fs_info->volume_mutex);
3932 mutex_lock(&fs_info->balance_mutex);
3933
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003934 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003935 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003936 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003937 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003938
3939 mutex_unlock(&fs_info->balance_mutex);
3940 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003941
Ilya Dryomov59641012012-01-16 22:04:48 +02003942 return ret;
3943}
3944
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003945int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3946{
3947 struct task_struct *tsk;
3948
3949 spin_lock(&fs_info->balance_lock);
3950 if (!fs_info->balance_ctl) {
3951 spin_unlock(&fs_info->balance_lock);
3952 return 0;
3953 }
3954 spin_unlock(&fs_info->balance_lock);
3955
Jeff Mahoney3cdde222016-06-09 21:38:35 -04003956 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003957 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003958 return 0;
3959 }
3960
3961 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303962 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003963}
3964
Ilya Dryomov68310a52012-06-22 12:24:12 -06003965int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003966{
Ilya Dryomov59641012012-01-16 22:04:48 +02003967 struct btrfs_balance_control *bctl;
3968 struct btrfs_balance_item *item;
3969 struct btrfs_disk_balance_args disk_bargs;
3970 struct btrfs_path *path;
3971 struct extent_buffer *leaf;
3972 struct btrfs_key key;
3973 int ret;
3974
3975 path = btrfs_alloc_path();
3976 if (!path)
3977 return -ENOMEM;
3978
Ilya Dryomov68310a52012-06-22 12:24:12 -06003979 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003980 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06003981 key.offset = 0;
3982
3983 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3984 if (ret < 0)
3985 goto out;
3986 if (ret > 0) { /* ret = -ENOENT; */
3987 ret = 0;
3988 goto out;
3989 }
3990
Ilya Dryomov59641012012-01-16 22:04:48 +02003991 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3992 if (!bctl) {
3993 ret = -ENOMEM;
3994 goto out;
3995 }
3996
Ilya Dryomov59641012012-01-16 22:04:48 +02003997 leaf = path->nodes[0];
3998 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3999
Ilya Dryomov68310a52012-06-22 12:24:12 -06004000 bctl->fs_info = fs_info;
4001 bctl->flags = btrfs_balance_flags(leaf, item);
4002 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004003
4004 btrfs_balance_data(leaf, item, &disk_bargs);
4005 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4006 btrfs_balance_meta(leaf, item, &disk_bargs);
4007 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4008 btrfs_balance_sys(leaf, item, &disk_bargs);
4009 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4010
David Sterba171938e2017-03-28 14:44:21 +02004011 WARN_ON(test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004012
Ilya Dryomov68310a52012-06-22 12:24:12 -06004013 mutex_lock(&fs_info->volume_mutex);
4014 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004015
Ilya Dryomov68310a52012-06-22 12:24:12 -06004016 set_balance_control(bctl);
4017
4018 mutex_unlock(&fs_info->balance_mutex);
4019 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004020out:
4021 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004022 return ret;
4023}
4024
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004025int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4026{
4027 int ret = 0;
4028
4029 mutex_lock(&fs_info->balance_mutex);
4030 if (!fs_info->balance_ctl) {
4031 mutex_unlock(&fs_info->balance_mutex);
4032 return -ENOTCONN;
4033 }
4034
4035 if (atomic_read(&fs_info->balance_running)) {
4036 atomic_inc(&fs_info->balance_pause_req);
4037 mutex_unlock(&fs_info->balance_mutex);
4038
4039 wait_event(fs_info->balance_wait_q,
4040 atomic_read(&fs_info->balance_running) == 0);
4041
4042 mutex_lock(&fs_info->balance_mutex);
4043 /* we are good with balance_ctl ripped off from under us */
4044 BUG_ON(atomic_read(&fs_info->balance_running));
4045 atomic_dec(&fs_info->balance_pause_req);
4046 } else {
4047 ret = -ENOTCONN;
4048 }
4049
4050 mutex_unlock(&fs_info->balance_mutex);
4051 return ret;
4052}
4053
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004054int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4055{
David Howellsbc98a422017-07-17 08:45:34 +01004056 if (sb_rdonly(fs_info->sb))
Ilya Dryomove649e582013-10-10 20:40:21 +03004057 return -EROFS;
4058
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004059 mutex_lock(&fs_info->balance_mutex);
4060 if (!fs_info->balance_ctl) {
4061 mutex_unlock(&fs_info->balance_mutex);
4062 return -ENOTCONN;
4063 }
4064
4065 atomic_inc(&fs_info->balance_cancel_req);
4066 /*
4067 * if we are running just wait and return, balance item is
4068 * deleted in btrfs_balance in this case
4069 */
4070 if (atomic_read(&fs_info->balance_running)) {
4071 mutex_unlock(&fs_info->balance_mutex);
4072 wait_event(fs_info->balance_wait_q,
4073 atomic_read(&fs_info->balance_running) == 0);
4074 mutex_lock(&fs_info->balance_mutex);
4075 } else {
4076 /* __cancel_balance needs volume_mutex */
4077 mutex_unlock(&fs_info->balance_mutex);
4078 mutex_lock(&fs_info->volume_mutex);
4079 mutex_lock(&fs_info->balance_mutex);
4080
4081 if (fs_info->balance_ctl)
4082 __cancel_balance(fs_info);
4083
4084 mutex_unlock(&fs_info->volume_mutex);
4085 }
4086
4087 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
4088 atomic_dec(&fs_info->balance_cancel_req);
4089 mutex_unlock(&fs_info->balance_mutex);
4090 return 0;
4091}
4092
Stefan Behrens803b2f52013-08-15 17:11:21 +02004093static int btrfs_uuid_scan_kthread(void *data)
4094{
4095 struct btrfs_fs_info *fs_info = data;
4096 struct btrfs_root *root = fs_info->tree_root;
4097 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004098 struct btrfs_path *path = NULL;
4099 int ret = 0;
4100 struct extent_buffer *eb;
4101 int slot;
4102 struct btrfs_root_item root_item;
4103 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004104 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004105
4106 path = btrfs_alloc_path();
4107 if (!path) {
4108 ret = -ENOMEM;
4109 goto out;
4110 }
4111
4112 key.objectid = 0;
4113 key.type = BTRFS_ROOT_ITEM_KEY;
4114 key.offset = 0;
4115
Stefan Behrens803b2f52013-08-15 17:11:21 +02004116 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004117 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004118 if (ret) {
4119 if (ret > 0)
4120 ret = 0;
4121 break;
4122 }
4123
4124 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4125 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4126 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4127 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4128 goto skip;
4129
4130 eb = path->nodes[0];
4131 slot = path->slots[0];
4132 item_size = btrfs_item_size_nr(eb, slot);
4133 if (item_size < sizeof(root_item))
4134 goto skip;
4135
Stefan Behrens803b2f52013-08-15 17:11:21 +02004136 read_extent_buffer(eb, &root_item,
4137 btrfs_item_ptr_offset(eb, slot),
4138 (int)sizeof(root_item));
4139 if (btrfs_root_refs(&root_item) == 0)
4140 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004141
4142 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4143 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4144 if (trans)
4145 goto update_tree;
4146
4147 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004148 /*
4149 * 1 - subvol uuid item
4150 * 1 - received_subvol uuid item
4151 */
4152 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4153 if (IS_ERR(trans)) {
4154 ret = PTR_ERR(trans);
4155 break;
4156 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004157 continue;
4158 } else {
4159 goto skip;
4160 }
4161update_tree:
4162 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004163 ret = btrfs_uuid_tree_add(trans, fs_info,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004164 root_item.uuid,
4165 BTRFS_UUID_KEY_SUBVOL,
4166 key.objectid);
4167 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004168 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004169 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004170 break;
4171 }
4172 }
4173
4174 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004175 ret = btrfs_uuid_tree_add(trans, fs_info,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004176 root_item.received_uuid,
4177 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4178 key.objectid);
4179 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004180 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004181 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004182 break;
4183 }
4184 }
4185
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004186skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004187 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004188 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004189 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004190 if (ret)
4191 break;
4192 }
4193
Stefan Behrens803b2f52013-08-15 17:11:21 +02004194 btrfs_release_path(path);
4195 if (key.offset < (u64)-1) {
4196 key.offset++;
4197 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4198 key.offset = 0;
4199 key.type = BTRFS_ROOT_ITEM_KEY;
4200 } else if (key.objectid < (u64)-1) {
4201 key.offset = 0;
4202 key.type = BTRFS_ROOT_ITEM_KEY;
4203 key.objectid++;
4204 } else {
4205 break;
4206 }
4207 cond_resched();
4208 }
4209
4210out:
4211 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004212 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004213 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004214 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004215 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004216 else
Josef Bacikafcdd122016-09-02 15:40:02 -04004217 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004218 up(&fs_info->uuid_tree_rescan_sem);
4219 return 0;
4220}
4221
Stefan Behrens70f80172013-08-15 17:11:23 +02004222/*
4223 * Callback for btrfs_uuid_tree_iterate().
4224 * returns:
4225 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004226 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004227 * > 0 if the check failed, which means the caller shall remove the entry.
4228 */
4229static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4230 u8 *uuid, u8 type, u64 subid)
4231{
4232 struct btrfs_key key;
4233 int ret = 0;
4234 struct btrfs_root *subvol_root;
4235
4236 if (type != BTRFS_UUID_KEY_SUBVOL &&
4237 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4238 goto out;
4239
4240 key.objectid = subid;
4241 key.type = BTRFS_ROOT_ITEM_KEY;
4242 key.offset = (u64)-1;
4243 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4244 if (IS_ERR(subvol_root)) {
4245 ret = PTR_ERR(subvol_root);
4246 if (ret == -ENOENT)
4247 ret = 1;
4248 goto out;
4249 }
4250
4251 switch (type) {
4252 case BTRFS_UUID_KEY_SUBVOL:
4253 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4254 ret = 1;
4255 break;
4256 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4257 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4258 BTRFS_UUID_SIZE))
4259 ret = 1;
4260 break;
4261 }
4262
4263out:
4264 return ret;
4265}
4266
4267static int btrfs_uuid_rescan_kthread(void *data)
4268{
4269 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4270 int ret;
4271
4272 /*
4273 * 1st step is to iterate through the existing UUID tree and
4274 * to delete all entries that contain outdated data.
4275 * 2nd step is to add all missing entries to the UUID tree.
4276 */
4277 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4278 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004279 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004280 up(&fs_info->uuid_tree_rescan_sem);
4281 return ret;
4282 }
4283 return btrfs_uuid_scan_kthread(data);
4284}
4285
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004286int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4287{
4288 struct btrfs_trans_handle *trans;
4289 struct btrfs_root *tree_root = fs_info->tree_root;
4290 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004291 struct task_struct *task;
4292 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004293
4294 /*
4295 * 1 - root node
4296 * 1 - root item
4297 */
4298 trans = btrfs_start_transaction(tree_root, 2);
4299 if (IS_ERR(trans))
4300 return PTR_ERR(trans);
4301
4302 uuid_root = btrfs_create_tree(trans, fs_info,
4303 BTRFS_UUID_TREE_OBJECTID);
4304 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004305 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004306 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004307 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004308 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004309 }
4310
4311 fs_info->uuid_root = uuid_root;
4312
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004313 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004314 if (ret)
4315 return ret;
4316
4317 down(&fs_info->uuid_tree_rescan_sem);
4318 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4319 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004320 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004321 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004322 up(&fs_info->uuid_tree_rescan_sem);
4323 return PTR_ERR(task);
4324 }
4325
4326 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004327}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004328
Stefan Behrens70f80172013-08-15 17:11:23 +02004329int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4330{
4331 struct task_struct *task;
4332
4333 down(&fs_info->uuid_tree_rescan_sem);
4334 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4335 if (IS_ERR(task)) {
4336 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004337 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004338 up(&fs_info->uuid_tree_rescan_sem);
4339 return PTR_ERR(task);
4340 }
4341
4342 return 0;
4343}
4344
Chris Mason8f18cf12008-04-25 16:53:30 -04004345/*
4346 * shrinking a device means finding all of the device extents past
4347 * the new size, and then following the back refs to the chunks.
4348 * The chunk relocation code actually frees the device extent
4349 */
4350int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4351{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004352 struct btrfs_fs_info *fs_info = device->fs_info;
4353 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004354 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004355 struct btrfs_dev_extent *dev_extent = NULL;
4356 struct btrfs_path *path;
4357 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004358 u64 chunk_offset;
4359 int ret;
4360 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004361 int failed = 0;
4362 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004363 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004364 struct extent_buffer *l;
4365 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004366 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004367 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004368 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004369 u64 diff;
4370
4371 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004372 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004373
Stefan Behrens63a212a2012-11-05 18:29:28 +01004374 if (device->is_tgtdev_for_dev_replace)
4375 return -EINVAL;
4376
Chris Mason8f18cf12008-04-25 16:53:30 -04004377 path = btrfs_alloc_path();
4378 if (!path)
4379 return -ENOMEM;
4380
David Sterbae4058b52015-11-27 16:31:35 +01004381 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04004382
David Sterba34441362016-10-04 19:34:27 +02004383 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004384
Miao Xie7cc8e582014-09-03 21:35:38 +08004385 btrfs_device_set_total_bytes(device, new_size);
Josef Bacik2bf64752011-09-26 17:12:22 -04004386 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004387 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004388 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004389 }
David Sterba34441362016-10-04 19:34:27 +02004390 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004391
Josef Bacikba1bf482009-09-11 16:11:19 -04004392again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004393 key.objectid = device->devid;
4394 key.offset = (u64)-1;
4395 key.type = BTRFS_DEV_EXTENT_KEY;
4396
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004397 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004398 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004399 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004400 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004401 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004402 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004403 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004404
4405 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004406 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004407 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004408 if (ret < 0)
4409 goto done;
4410 if (ret) {
4411 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004412 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004413 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004414 }
4415
4416 l = path->nodes[0];
4417 slot = path->slots[0];
4418 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4419
Josef Bacikba1bf482009-09-11 16:11:19 -04004420 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004421 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004422 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004423 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004424 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004425
4426 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4427 length = btrfs_dev_extent_length(l, dev_extent);
4428
Josef Bacikba1bf482009-09-11 16:11:19 -04004429 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004430 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004431 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004432 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004433 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004434
Chris Mason8f18cf12008-04-25 16:53:30 -04004435 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004436 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004437
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004438 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4439 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04004440 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004441 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004442 if (ret == -ENOSPC)
4443 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004444 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004445
4446 if (failed && !retried) {
4447 failed = 0;
4448 retried = true;
4449 goto again;
4450 } else if (failed && retried) {
4451 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004452 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004453 }
4454
Chris Balld6397ba2009-04-27 07:29:03 -04004455 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004456 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004457 if (IS_ERR(trans)) {
4458 ret = PTR_ERR(trans);
4459 goto done;
4460 }
4461
David Sterba34441362016-10-04 19:34:27 +02004462 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004463
4464 /*
4465 * We checked in the above loop all device extents that were already in
4466 * the device tree. However before we have updated the device's
4467 * total_bytes to the new size, we might have had chunk allocations that
4468 * have not complete yet (new block groups attached to transaction
4469 * handles), and therefore their device extents were not yet in the
4470 * device tree and we missed them in the loop above. So if we have any
4471 * pending chunk using a device extent that overlaps the device range
4472 * that we can not use anymore, commit the current transaction and
4473 * repeat the search on the device tree - this way we guarantee we will
4474 * not have chunks using device extents that end beyond 'new_size'.
4475 */
4476 if (!checked_pending_chunks) {
4477 u64 start = new_size;
4478 u64 len = old_size - new_size;
4479
Jeff Mahoney499f3772015-06-15 09:41:17 -04004480 if (contains_pending_extent(trans->transaction, device,
4481 &start, len)) {
David Sterba34441362016-10-04 19:34:27 +02004482 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004483 checked_pending_chunks = true;
4484 failed = 0;
4485 retried = false;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004486 ret = btrfs_commit_transaction(trans);
Filipe Manana53e489b2015-06-02 14:43:21 +01004487 if (ret)
4488 goto done;
4489 goto again;
4490 }
4491 }
4492
Miao Xie7cc8e582014-09-03 21:35:38 +08004493 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004494 if (list_empty(&device->resized_list))
4495 list_add_tail(&device->resized_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004496 &fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004497
Chris Balld6397ba2009-04-27 07:29:03 -04004498 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004499 btrfs_set_super_total_bytes(super_copy,
4500 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004501 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004502
4503 /* Now btrfs_update_device() will change the on-disk size. */
4504 ret = btrfs_update_device(trans, device);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004505 btrfs_end_transaction(trans);
Chris Mason8f18cf12008-04-25 16:53:30 -04004506done:
4507 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004508 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004509 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004510 btrfs_device_set_total_bytes(device, old_size);
4511 if (device->writeable)
4512 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004513 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004514 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004515 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004516 return ret;
4517}
4518
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004519static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004520 struct btrfs_key *key,
4521 struct btrfs_chunk *chunk, int item_size)
4522{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004523 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004524 struct btrfs_disk_key disk_key;
4525 u32 array_size;
4526 u8 *ptr;
4527
David Sterba34441362016-10-04 19:34:27 +02004528 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004529 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004530 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004531 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004532 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004533 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004534 }
Chris Mason0b86a832008-03-24 15:01:56 -04004535
4536 ptr = super_copy->sys_chunk_array + array_size;
4537 btrfs_cpu_key_to_disk(&disk_key, key);
4538 memcpy(ptr, &disk_key, sizeof(disk_key));
4539 ptr += sizeof(disk_key);
4540 memcpy(ptr, chunk, item_size);
4541 item_size += sizeof(disk_key);
4542 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004543 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004544
Chris Mason0b86a832008-03-24 15:01:56 -04004545 return 0;
4546}
4547
Miao Xieb2117a32011-01-05 10:07:28 +00004548/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004549 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004550 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004551static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004552{
Arne Jansen73c5de02011-04-12 12:07:57 +02004553 const struct btrfs_device_info *di_a = a;
4554 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004555
Arne Jansen73c5de02011-04-12 12:07:57 +02004556 if (di_a->max_avail > di_b->max_avail)
4557 return -1;
4558 if (di_a->max_avail < di_b->max_avail)
4559 return 1;
4560 if (di_a->total_avail > di_b->total_avail)
4561 return -1;
4562 if (di_a->total_avail < di_b->total_avail)
4563 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004564 return 0;
4565}
4566
David Woodhouse53b381b2013-01-29 18:40:14 -05004567static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4568{
Zhao Leiffe2d202015-01-20 15:11:44 +08004569 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004570 return;
4571
Miao Xieceda0862013-04-11 10:30:16 +00004572 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004573}
4574
Jeff Mahoneyda170662016-06-15 09:22:56 -04004575#define BTRFS_MAX_DEVS(r) ((BTRFS_MAX_ITEM_SIZE(r->fs_info) \
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004576 - sizeof(struct btrfs_chunk)) \
4577 / sizeof(struct btrfs_stripe) + 1)
4578
4579#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4580 - 2 * sizeof(struct btrfs_disk_key) \
4581 - 2 * sizeof(struct btrfs_chunk)) \
4582 / sizeof(struct btrfs_stripe) + 1)
4583
Miao Xieb2117a32011-01-05 10:07:28 +00004584static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
David Sterba72b468c2017-02-10 19:46:27 +01004585 u64 start, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004586{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004587 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004588 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004589 struct btrfs_device *device;
Arne Jansen73c5de02011-04-12 12:07:57 +02004590 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004591 struct extent_map_tree *em_tree;
4592 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004593 struct btrfs_device_info *devices_info = NULL;
4594 u64 total_avail;
4595 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004596 int data_stripes; /* number of stripes that count for
4597 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004598 int sub_stripes; /* sub_stripes info for map */
4599 int dev_stripes; /* stripes per dev */
4600 int devs_max; /* max devs to use */
4601 int devs_min; /* min devs needed */
4602 int devs_increment; /* ndevs has to be a multiple of this */
4603 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004604 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004605 u64 max_stripe_size;
4606 u64 max_chunk_size;
4607 u64 stripe_size;
4608 u64 num_bytes;
4609 int ndevs;
4610 int i;
4611 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004612 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004613
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004614 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004615
Miao Xieb2117a32011-01-05 10:07:28 +00004616 if (list_empty(&fs_devices->alloc_list))
4617 return -ENOSPC;
4618
Liu Bo31e50222012-11-21 14:18:10 +00004619 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004620
Liu Bo31e50222012-11-21 14:18:10 +00004621 sub_stripes = btrfs_raid_array[index].sub_stripes;
4622 dev_stripes = btrfs_raid_array[index].dev_stripes;
4623 devs_max = btrfs_raid_array[index].devs_max;
4624 devs_min = btrfs_raid_array[index].devs_min;
4625 devs_increment = btrfs_raid_array[index].devs_increment;
4626 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004627
4628 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09004629 max_stripe_size = SZ_1G;
Arne Jansen73c5de02011-04-12 12:07:57 +02004630 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004631 if (!devs_max)
4632 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004633 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004634 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09004635 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4636 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05004637 else
Byongho Leeee221842015-12-15 01:42:10 +09004638 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004639 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004640 if (!devs_max)
4641 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004642 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09004643 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004644 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004645 if (!devs_max)
4646 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004647 } else {
David Sterba351fd352014-05-15 16:48:20 +02004648 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004649 type);
4650 BUG_ON(1);
4651 }
4652
4653 /* we don't want a chunk larger than 10% of writeable space */
4654 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4655 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004656
David Sterba31e818f2015-02-20 18:00:26 +01004657 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004658 GFP_NOFS);
4659 if (!devices_info)
4660 return -ENOMEM;
4661
Arne Jansen73c5de02011-04-12 12:07:57 +02004662 /*
4663 * in the first pass through the devices list, we gather information
4664 * about the available holes on each device.
4665 */
4666 ndevs = 0;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004667 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004668 u64 max_avail;
4669 u64 dev_offset;
4670
Arne Jansen73c5de02011-04-12 12:07:57 +02004671 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004672 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004673 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004674 continue;
4675 }
4676
Stefan Behrens63a212a2012-11-05 18:29:28 +01004677 if (!device->in_fs_metadata ||
4678 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004679 continue;
4680
4681 if (device->total_bytes > device->bytes_used)
4682 total_avail = device->total_bytes - device->bytes_used;
4683 else
4684 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004685
4686 /* If there is no space on this device, skip it. */
4687 if (total_avail == 0)
4688 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004689
Josef Bacik6df9a952013-06-27 13:22:46 -04004690 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004691 max_stripe_size * dev_stripes,
4692 &dev_offset, &max_avail);
4693 if (ret && ret != -ENOSPC)
4694 goto error;
4695
4696 if (ret == 0)
4697 max_avail = max_stripe_size * dev_stripes;
4698
4699 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4700 continue;
4701
Eric Sandeen063d0062013-01-31 00:55:01 +00004702 if (ndevs == fs_devices->rw_devices) {
4703 WARN(1, "%s: found more than %llu devices\n",
4704 __func__, fs_devices->rw_devices);
4705 break;
4706 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004707 devices_info[ndevs].dev_offset = dev_offset;
4708 devices_info[ndevs].max_avail = max_avail;
4709 devices_info[ndevs].total_avail = total_avail;
4710 devices_info[ndevs].dev = device;
4711 ++ndevs;
4712 }
4713
4714 /*
4715 * now sort the devices by hole size / available space
4716 */
4717 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4718 btrfs_cmp_device_info, NULL);
4719
4720 /* round down to number of usable stripes */
Nikolay Borisove5600fd2017-06-27 10:02:25 +03004721 ndevs = round_down(ndevs, devs_increment);
Arne Jansen73c5de02011-04-12 12:07:57 +02004722
4723 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4724 ret = -ENOSPC;
4725 goto error;
4726 }
4727
Nikolay Borisovf148ef42017-06-27 10:02:26 +03004728 ndevs = min(ndevs, devs_max);
4729
Arne Jansen73c5de02011-04-12 12:07:57 +02004730 /*
4731 * the primary goal is to maximize the number of stripes, so use as many
4732 * devices as possible, even if the stripes are not maximum sized.
4733 */
4734 stripe_size = devices_info[ndevs-1].max_avail;
4735 num_stripes = ndevs * dev_stripes;
4736
David Woodhouse53b381b2013-01-29 18:40:14 -05004737 /*
4738 * this will have to be fixed for RAID1 and RAID10 over
4739 * more drives
4740 */
4741 data_stripes = num_stripes / ncopies;
4742
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004743 if (type & BTRFS_BLOCK_GROUP_RAID5)
David Woodhouse53b381b2013-01-29 18:40:14 -05004744 data_stripes = num_stripes - 1;
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004745
4746 if (type & BTRFS_BLOCK_GROUP_RAID6)
David Woodhouse53b381b2013-01-29 18:40:14 -05004747 data_stripes = num_stripes - 2;
Chris Mason86db2572013-02-20 16:23:40 -05004748
4749 /*
4750 * Use the number of data stripes to figure out how big this chunk
4751 * is really going to be in terms of logical address space,
4752 * and compare that answer with the max chunk size
4753 */
4754 if (stripe_size * data_stripes > max_chunk_size) {
4755 u64 mask = (1ULL << 24) - 1;
David Sterbab8b93ad2015-01-16 17:26:13 +01004756
4757 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004758
4759 /* bump the answer up to a 16MB boundary */
4760 stripe_size = (stripe_size + mask) & ~mask;
4761
4762 /* but don't go higher than the limits we found
4763 * while searching for free extents
4764 */
4765 if (stripe_size > devices_info[ndevs-1].max_avail)
4766 stripe_size = devices_info[ndevs-1].max_avail;
4767 }
4768
David Sterbab8b93ad2015-01-16 17:26:13 +01004769 stripe_size = div_u64(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004770
4771 /* align to BTRFS_STRIPE_LEN */
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004772 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
Arne Jansen73c5de02011-04-12 12:07:57 +02004773
Miao Xieb2117a32011-01-05 10:07:28 +00004774 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4775 if (!map) {
4776 ret = -ENOMEM;
4777 goto error;
4778 }
4779 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004780
Arne Jansen73c5de02011-04-12 12:07:57 +02004781 for (i = 0; i < ndevs; ++i) {
4782 for (j = 0; j < dev_stripes; ++j) {
4783 int s = i * dev_stripes + j;
4784 map->stripes[s].dev = devices_info[i].dev;
4785 map->stripes[s].physical = devices_info[i].dev_offset +
4786 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004787 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004788 }
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004789 map->stripe_len = BTRFS_STRIPE_LEN;
4790 map->io_align = BTRFS_STRIPE_LEN;
4791 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04004792 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004793 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004794
David Woodhouse53b381b2013-01-29 18:40:14 -05004795 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004796
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004797 trace_btrfs_chunk_alloc(info, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004798
David Sterba172ddd62011-04-21 00:48:27 +02004799 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004800 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004801 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004802 ret = -ENOMEM;
4803 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004804 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004805 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04004806 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05004807 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004808 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004809 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004810 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004811 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004812
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004813 em_tree = &info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004814 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004815 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004816 if (!ret) {
4817 list_add_tail(&em->list, &trans->transaction->pending_chunks);
Elena Reshetova490b54d2017-03-03 10:55:12 +02004818 refcount_inc(&em->refs);
Josef Bacik6df9a952013-06-27 13:22:46 -04004819 }
Chris Mason890871b2009-09-02 16:24:52 -04004820 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004821 if (ret) {
4822 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004823 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004824 }
Yan Zheng2b820322008-11-17 21:11:30 -05004825
Nikolay Borisov01744842017-07-27 14:22:11 +03004826 ret = btrfs_make_block_group(trans, info, 0, type, start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004827 if (ret)
4828 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004829
Miao Xie7cc8e582014-09-03 21:35:38 +08004830 for (i = 0; i < map->num_stripes; i++) {
4831 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4832 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4833 }
Miao Xie43530c42014-09-03 21:35:36 +08004834
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004835 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
Miao Xie1c116182014-09-03 21:35:37 +08004836
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004837 free_extent_map(em);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004838 check_raid56_incompat_flag(info, type);
David Woodhouse53b381b2013-01-29 18:40:14 -05004839
Miao Xieb2117a32011-01-05 10:07:28 +00004840 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004841 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004842
Josef Bacik6df9a952013-06-27 13:22:46 -04004843error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004844 write_lock(&em_tree->lock);
4845 remove_extent_mapping(em_tree, em);
4846 write_unlock(&em_tree->lock);
4847
4848 /* One for our allocation */
4849 free_extent_map(em);
4850 /* One for the tree reference */
4851 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004852 /* One for the pending_chunks list reference */
4853 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004854error:
Miao Xieb2117a32011-01-05 10:07:28 +00004855 kfree(devices_info);
4856 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004857}
4858
Josef Bacik6df9a952013-06-27 13:22:46 -04004859int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004860 struct btrfs_fs_info *fs_info,
Josef Bacik6df9a952013-06-27 13:22:46 -04004861 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004862{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004863 struct btrfs_root *extent_root = fs_info->extent_root;
4864 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05004865 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05004866 struct btrfs_device *device;
4867 struct btrfs_chunk *chunk;
4868 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004869 struct extent_map *em;
4870 struct map_lookup *map;
4871 size_t item_size;
4872 u64 dev_offset;
4873 u64 stripe_size;
4874 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08004875 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004876
Liu Bo592d92e2017-03-14 13:33:55 -07004877 em = get_chunk_map(fs_info, chunk_offset, chunk_size);
4878 if (IS_ERR(em))
4879 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04004880
Jeff Mahoney95617d62015-06-03 10:55:48 -04004881 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04004882 item_size = btrfs_chunk_item_size(map->num_stripes);
4883 stripe_size = em->orig_block_len;
4884
4885 chunk = kzalloc(item_size, GFP_NOFS);
4886 if (!chunk) {
4887 ret = -ENOMEM;
4888 goto out;
4889 }
4890
Filipe Manana50460e32015-11-20 10:42:47 +00004891 /*
4892 * Take the device list mutex to prevent races with the final phase of
4893 * a device replace operation that replaces the device object associated
4894 * with the map's stripes, because the device object's id can change
4895 * at any time during that final phase of the device replace operation
4896 * (dev-replace.c:btrfs_dev_replace_finishing()).
4897 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004898 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04004899 for (i = 0; i < map->num_stripes; i++) {
4900 device = map->stripes[i].dev;
4901 dev_offset = map->stripes[i].physical;
4902
Yan Zheng2b820322008-11-17 21:11:30 -05004903 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004904 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004905 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03004906 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
4907 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04004908 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004909 break;
4910 }
4911 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004912 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00004913 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004914 }
4915
Yan Zheng2b820322008-11-17 21:11:30 -05004916 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004917 for (i = 0; i < map->num_stripes; i++) {
4918 device = map->stripes[i].dev;
4919 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004920
4921 btrfs_set_stack_stripe_devid(stripe, device->devid);
4922 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4923 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4924 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004925 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004926 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05004927
4928 btrfs_set_stack_chunk_length(chunk, chunk_size);
4929 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4930 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4931 btrfs_set_stack_chunk_type(chunk, map->type);
4932 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4933 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4934 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004935 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05004936 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4937
4938 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4939 key.type = BTRFS_CHUNK_ITEM_KEY;
4940 key.offset = chunk_offset;
4941
4942 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004943 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4944 /*
4945 * TODO: Cleanup of inserted chunk root in case of
4946 * failure.
4947 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004948 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004949 }
liubo1abe9b82011-03-24 11:18:59 +00004950
Josef Bacik6df9a952013-06-27 13:22:46 -04004951out:
Yan Zheng2b820322008-11-17 21:11:30 -05004952 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004953 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004954 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004955}
4956
4957/*
4958 * Chunk allocation falls into two parts. The first part does works
4959 * that make the new allocated chunk useable, but not do any operation
4960 * that modifies the chunk tree. The second part does the works that
4961 * require modifying the chunk tree. This division is important for the
4962 * bootstrap process of adding storage to a seed btrfs.
4963 */
4964int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004965 struct btrfs_fs_info *fs_info, u64 type)
Yan Zheng2b820322008-11-17 21:11:30 -05004966{
4967 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004968
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004969 ASSERT(mutex_is_locked(&fs_info->chunk_mutex));
4970 chunk_offset = find_next_chunk(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01004971 return __btrfs_alloc_chunk(trans, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004972}
4973
Chris Masond3977122009-01-05 21:25:51 -05004974static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +01004975 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05004976{
4977 u64 chunk_offset;
4978 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004979 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004980 int ret;
4981
Josef Bacik6df9a952013-06-27 13:22:46 -04004982 chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04004983 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01004984 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004985 if (ret)
4986 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004987
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004988 sys_chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04004989 alloc_profile = btrfs_system_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01004990 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004991 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004992}
4993
Miao Xied20983b2014-07-03 18:22:13 +08004994static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4995{
4996 int max_errors;
4997
4998 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4999 BTRFS_BLOCK_GROUP_RAID10 |
5000 BTRFS_BLOCK_GROUP_RAID5 |
5001 BTRFS_BLOCK_GROUP_DUP)) {
5002 max_errors = 1;
5003 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5004 max_errors = 2;
5005 } else {
5006 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005007 }
5008
Miao Xied20983b2014-07-03 18:22:13 +08005009 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05005010}
5011
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005012int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005013{
5014 struct extent_map *em;
5015 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005016 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005017 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005018 int i;
5019
Liu Bo592d92e2017-03-14 13:33:55 -07005020 em = get_chunk_map(fs_info, chunk_offset, 1);
5021 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005022 return 1;
5023
Jeff Mahoney95617d62015-06-03 10:55:48 -04005024 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005025 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08005026 if (map->stripes[i].dev->missing) {
5027 miss_ndevs++;
5028 continue;
5029 }
5030
Yan Zheng2b820322008-11-17 21:11:30 -05005031 if (!map->stripes[i].dev->writeable) {
5032 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005033 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005034 }
5035 }
Miao Xied20983b2014-07-03 18:22:13 +08005036
5037 /*
5038 * If the number of missing devices is larger than max errors,
5039 * we can not write the data into that chunk successfully, so
5040 * set it readonly.
5041 */
5042 if (miss_ndevs > btrfs_chunk_max_errors(map))
5043 readonly = 1;
5044end:
Yan Zheng2b820322008-11-17 21:11:30 -05005045 free_extent_map(em);
5046 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005047}
5048
5049void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
5050{
David Sterbaa8067e02011-04-21 00:34:43 +02005051 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04005052}
5053
5054void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
5055{
5056 struct extent_map *em;
5057
Chris Masond3977122009-01-05 21:25:51 -05005058 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04005059 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005060 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
5061 if (em)
5062 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005063 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005064 if (!em)
5065 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005066 /* once for us */
5067 free_extent_map(em);
5068 /* once for the tree */
5069 free_extent_map(em);
5070 }
5071}
5072
Stefan Behrens5d964052012-11-05 14:59:07 +01005073int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005074{
5075 struct extent_map *em;
5076 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005077 int ret;
5078
Liu Bo592d92e2017-03-14 13:33:55 -07005079 em = get_chunk_map(fs_info, logical, len);
5080 if (IS_ERR(em))
5081 /*
5082 * We could return errors for these cases, but that could get
5083 * ugly and we'd probably do the same thing which is just not do
5084 * anything else and exit, so return 1 so the callers don't try
5085 * to use other copies.
5086 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005087 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005088
Jeff Mahoney95617d62015-06-03 10:55:48 -04005089 map = em->map_lookup;
Chris Masonf1885912008-04-09 16:28:12 -04005090 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5091 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005092 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5093 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005094 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5095 ret = 2;
5096 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5097 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04005098 else
5099 ret = 1;
5100 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005101
Liu Bo73beece2015-07-17 16:49:19 +08005102 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Liu Bo6fad8232017-03-14 13:33:59 -07005103 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5104 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005105 ret++;
Liu Bo73beece2015-07-17 16:49:19 +08005106 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005107
Chris Masonf1885912008-04-09 16:28:12 -04005108 return ret;
5109}
5110
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005111unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005112 u64 logical)
5113{
5114 struct extent_map *em;
5115 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005116 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005117
Liu Bo592d92e2017-03-14 13:33:55 -07005118 em = get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005119
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005120 if (!WARN_ON(IS_ERR(em))) {
5121 map = em->map_lookup;
5122 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5123 len = map->stripe_len * nr_data_stripes(map);
5124 free_extent_map(em);
5125 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005126 return len;
5127}
5128
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005129int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005130{
5131 struct extent_map *em;
5132 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005133 int ret = 0;
5134
Liu Bo592d92e2017-03-14 13:33:55 -07005135 em = get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005136
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005137 if(!WARN_ON(IS_ERR(em))) {
5138 map = em->map_lookup;
5139 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5140 ret = 1;
5141 free_extent_map(em);
5142 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005143 return ret;
5144}
5145
Stefan Behrens30d98612012-11-06 14:52:18 +01005146static int find_live_mirror(struct btrfs_fs_info *fs_info,
5147 struct map_lookup *map, int first, int num,
5148 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005149{
5150 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01005151 int tolerance;
5152 struct btrfs_device *srcdev;
5153
5154 if (dev_replace_is_ongoing &&
5155 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5156 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5157 srcdev = fs_info->dev_replace.srcdev;
5158 else
5159 srcdev = NULL;
5160
5161 /*
5162 * try to avoid the drive that is the source drive for a
5163 * dev-replace procedure, only choose it if no other non-missing
5164 * mirror is available
5165 */
5166 for (tolerance = 0; tolerance < 2; tolerance++) {
5167 if (map->stripes[optimal].dev->bdev &&
5168 (tolerance || map->stripes[optimal].dev != srcdev))
5169 return optimal;
5170 for (i = first; i < first + num; i++) {
5171 if (map->stripes[i].dev->bdev &&
5172 (tolerance || map->stripes[i].dev != srcdev))
5173 return i;
5174 }
Chris Masondfe25022008-05-13 13:46:40 -04005175 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005176
Chris Masondfe25022008-05-13 13:46:40 -04005177 /* we couldn't find one that doesn't fail. Just return something
5178 * and the io error handling code will clean up eventually
5179 */
5180 return optimal;
5181}
5182
David Woodhouse53b381b2013-01-29 18:40:14 -05005183static inline int parity_smaller(u64 a, u64 b)
5184{
5185 return a > b;
5186}
5187
5188/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005189static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005190{
5191 struct btrfs_bio_stripe s;
5192 int i;
5193 u64 l;
5194 int again = 1;
5195
5196 while (again) {
5197 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005198 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005199 if (parity_smaller(bbio->raid_map[i],
5200 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005201 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005202 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005203 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005204 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005205 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005206 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005207
David Woodhouse53b381b2013-01-29 18:40:14 -05005208 again = 1;
5209 }
5210 }
5211 }
5212}
5213
Zhao Lei6e9606d2015-01-20 15:11:34 +08005214static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5215{
5216 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005217 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005218 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005219 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005220 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005221 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005222 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005223 /*
5224 * plus the raid_map, which includes both the tgt dev
5225 * and the stripes
5226 */
5227 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005228 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005229
5230 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005231 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005232
5233 return bbio;
5234}
5235
5236void btrfs_get_bbio(struct btrfs_bio *bbio)
5237{
Elena Reshetova140475a2017-03-03 10:55:10 +02005238 WARN_ON(!refcount_read(&bbio->refs));
5239 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005240}
5241
5242void btrfs_put_bbio(struct btrfs_bio *bbio)
5243{
5244 if (!bbio)
5245 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005246 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005247 kfree(bbio);
5248}
5249
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005250/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5251/*
5252 * Please note that, discard won't be sent to target device of device
5253 * replace.
5254 */
5255static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5256 u64 logical, u64 length,
5257 struct btrfs_bio **bbio_ret)
5258{
5259 struct extent_map *em;
5260 struct map_lookup *map;
5261 struct btrfs_bio *bbio;
5262 u64 offset;
5263 u64 stripe_nr;
5264 u64 stripe_nr_end;
5265 u64 stripe_end_offset;
5266 u64 stripe_cnt;
5267 u64 stripe_len;
5268 u64 stripe_offset;
5269 u64 num_stripes;
5270 u32 stripe_index;
5271 u32 factor = 0;
5272 u32 sub_stripes = 0;
5273 u64 stripes_per_dev = 0;
5274 u32 remaining_stripes = 0;
5275 u32 last_stripe = 0;
5276 int ret = 0;
5277 int i;
5278
5279 /* discard always return a bbio */
5280 ASSERT(bbio_ret);
5281
5282 em = get_chunk_map(fs_info, logical, length);
5283 if (IS_ERR(em))
5284 return PTR_ERR(em);
5285
5286 map = em->map_lookup;
5287 /* we don't discard raid56 yet */
5288 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5289 ret = -EOPNOTSUPP;
5290 goto out;
5291 }
5292
5293 offset = logical - em->start;
5294 length = min_t(u64, em->len - offset, length);
5295
5296 stripe_len = map->stripe_len;
5297 /*
5298 * stripe_nr counts the total number of stripes we have to stride
5299 * to get to this block
5300 */
5301 stripe_nr = div64_u64(offset, stripe_len);
5302
5303 /* stripe_offset is the offset of this block in its stripe */
5304 stripe_offset = offset - stripe_nr * stripe_len;
5305
5306 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005307 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005308 stripe_cnt = stripe_nr_end - stripe_nr;
5309 stripe_end_offset = stripe_nr_end * map->stripe_len -
5310 (offset + length);
5311 /*
5312 * after this, stripe_nr is the number of stripes on this
5313 * device we have to walk to find the data, and stripe_index is
5314 * the number of our device in the stripe array
5315 */
5316 num_stripes = 1;
5317 stripe_index = 0;
5318 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5319 BTRFS_BLOCK_GROUP_RAID10)) {
5320 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5321 sub_stripes = 1;
5322 else
5323 sub_stripes = map->sub_stripes;
5324
5325 factor = map->num_stripes / sub_stripes;
5326 num_stripes = min_t(u64, map->num_stripes,
5327 sub_stripes * stripe_cnt);
5328 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5329 stripe_index *= sub_stripes;
5330 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5331 &remaining_stripes);
5332 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5333 last_stripe *= sub_stripes;
5334 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5335 BTRFS_BLOCK_GROUP_DUP)) {
5336 num_stripes = map->num_stripes;
5337 } else {
5338 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5339 &stripe_index);
5340 }
5341
5342 bbio = alloc_btrfs_bio(num_stripes, 0);
5343 if (!bbio) {
5344 ret = -ENOMEM;
5345 goto out;
5346 }
5347
5348 for (i = 0; i < num_stripes; i++) {
5349 bbio->stripes[i].physical =
5350 map->stripes[stripe_index].physical +
5351 stripe_offset + stripe_nr * map->stripe_len;
5352 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5353
5354 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5355 BTRFS_BLOCK_GROUP_RAID10)) {
5356 bbio->stripes[i].length = stripes_per_dev *
5357 map->stripe_len;
5358
5359 if (i / sub_stripes < remaining_stripes)
5360 bbio->stripes[i].length +=
5361 map->stripe_len;
5362
5363 /*
5364 * Special for the first stripe and
5365 * the last stripe:
5366 *
5367 * |-------|...|-------|
5368 * |----------|
5369 * off end_off
5370 */
5371 if (i < sub_stripes)
5372 bbio->stripes[i].length -=
5373 stripe_offset;
5374
5375 if (stripe_index >= last_stripe &&
5376 stripe_index <= (last_stripe +
5377 sub_stripes - 1))
5378 bbio->stripes[i].length -=
5379 stripe_end_offset;
5380
5381 if (i == sub_stripes - 1)
5382 stripe_offset = 0;
5383 } else {
5384 bbio->stripes[i].length = length;
5385 }
5386
5387 stripe_index++;
5388 if (stripe_index == map->num_stripes) {
5389 stripe_index = 0;
5390 stripe_nr++;
5391 }
5392 }
5393
5394 *bbio_ret = bbio;
5395 bbio->map_type = map->type;
5396 bbio->num_stripes = num_stripes;
5397out:
5398 free_extent_map(em);
5399 return ret;
5400}
5401
Liu Bo5ab56092017-03-14 13:33:57 -07005402/*
5403 * In dev-replace case, for repair case (that's the only case where the mirror
5404 * is selected explicitly when calling btrfs_map_block), blocks left of the
5405 * left cursor can also be read from the target drive.
5406 *
5407 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5408 * array of stripes.
5409 * For READ, it also needs to be supported using the same mirror number.
5410 *
5411 * If the requested block is not left of the left cursor, EIO is returned. This
5412 * can happen because btrfs_num_copies() returns one more in the dev-replace
5413 * case.
5414 */
5415static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5416 u64 logical, u64 length,
5417 u64 srcdev_devid, int *mirror_num,
5418 u64 *physical)
5419{
5420 struct btrfs_bio *bbio = NULL;
5421 int num_stripes;
5422 int index_srcdev = 0;
5423 int found = 0;
5424 u64 physical_of_found = 0;
5425 int i;
5426 int ret = 0;
5427
5428 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5429 logical, &length, &bbio, 0, 0);
5430 if (ret) {
5431 ASSERT(bbio == NULL);
5432 return ret;
5433 }
5434
5435 num_stripes = bbio->num_stripes;
5436 if (*mirror_num > num_stripes) {
5437 /*
5438 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5439 * that means that the requested area is not left of the left
5440 * cursor
5441 */
5442 btrfs_put_bbio(bbio);
5443 return -EIO;
5444 }
5445
5446 /*
5447 * process the rest of the function using the mirror_num of the source
5448 * drive. Therefore look it up first. At the end, patch the device
5449 * pointer to the one of the target drive.
5450 */
5451 for (i = 0; i < num_stripes; i++) {
5452 if (bbio->stripes[i].dev->devid != srcdev_devid)
5453 continue;
5454
5455 /*
5456 * In case of DUP, in order to keep it simple, only add the
5457 * mirror with the lowest physical address
5458 */
5459 if (found &&
5460 physical_of_found <= bbio->stripes[i].physical)
5461 continue;
5462
5463 index_srcdev = i;
5464 found = 1;
5465 physical_of_found = bbio->stripes[i].physical;
5466 }
5467
5468 btrfs_put_bbio(bbio);
5469
5470 ASSERT(found);
5471 if (!found)
5472 return -EIO;
5473
5474 *mirror_num = index_srcdev + 1;
5475 *physical = physical_of_found;
5476 return ret;
5477}
5478
Liu Bo73c0f222017-03-14 13:33:58 -07005479static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5480 struct btrfs_bio **bbio_ret,
5481 struct btrfs_dev_replace *dev_replace,
5482 int *num_stripes_ret, int *max_errors_ret)
5483{
5484 struct btrfs_bio *bbio = *bbio_ret;
5485 u64 srcdev_devid = dev_replace->srcdev->devid;
5486 int tgtdev_indexes = 0;
5487 int num_stripes = *num_stripes_ret;
5488 int max_errors = *max_errors_ret;
5489 int i;
5490
5491 if (op == BTRFS_MAP_WRITE) {
5492 int index_where_to_add;
5493
5494 /*
5495 * duplicate the write operations while the dev replace
5496 * procedure is running. Since the copying of the old disk to
5497 * the new disk takes place at run time while the filesystem is
5498 * mounted writable, the regular write operations to the old
5499 * disk have to be duplicated to go to the new disk as well.
5500 *
5501 * Note that device->missing is handled by the caller, and that
5502 * the write to the old disk is already set up in the stripes
5503 * array.
5504 */
5505 index_where_to_add = num_stripes;
5506 for (i = 0; i < num_stripes; i++) {
5507 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5508 /* write to new disk, too */
5509 struct btrfs_bio_stripe *new =
5510 bbio->stripes + index_where_to_add;
5511 struct btrfs_bio_stripe *old =
5512 bbio->stripes + i;
5513
5514 new->physical = old->physical;
5515 new->length = old->length;
5516 new->dev = dev_replace->tgtdev;
5517 bbio->tgtdev_map[i] = index_where_to_add;
5518 index_where_to_add++;
5519 max_errors++;
5520 tgtdev_indexes++;
5521 }
5522 }
5523 num_stripes = index_where_to_add;
5524 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5525 int index_srcdev = 0;
5526 int found = 0;
5527 u64 physical_of_found = 0;
5528
5529 /*
5530 * During the dev-replace procedure, the target drive can also
5531 * be used to read data in case it is needed to repair a corrupt
5532 * block elsewhere. This is possible if the requested area is
5533 * left of the left cursor. In this area, the target drive is a
5534 * full copy of the source drive.
5535 */
5536 for (i = 0; i < num_stripes; i++) {
5537 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5538 /*
5539 * In case of DUP, in order to keep it simple,
5540 * only add the mirror with the lowest physical
5541 * address
5542 */
5543 if (found &&
5544 physical_of_found <=
5545 bbio->stripes[i].physical)
5546 continue;
5547 index_srcdev = i;
5548 found = 1;
5549 physical_of_found = bbio->stripes[i].physical;
5550 }
5551 }
5552 if (found) {
5553 struct btrfs_bio_stripe *tgtdev_stripe =
5554 bbio->stripes + num_stripes;
5555
5556 tgtdev_stripe->physical = physical_of_found;
5557 tgtdev_stripe->length =
5558 bbio->stripes[index_srcdev].length;
5559 tgtdev_stripe->dev = dev_replace->tgtdev;
5560 bbio->tgtdev_map[index_srcdev] = num_stripes;
5561
5562 tgtdev_indexes++;
5563 num_stripes++;
5564 }
5565 }
5566
5567 *num_stripes_ret = num_stripes;
5568 *max_errors_ret = max_errors;
5569 bbio->num_tgtdevs = tgtdev_indexes;
5570 *bbio_ret = bbio;
5571}
5572
Liu Bo2b19a1f2017-03-14 13:34:00 -07005573static bool need_full_stripe(enum btrfs_map_op op)
5574{
5575 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5576}
5577
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005578static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5579 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005580 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005581 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005582 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005583{
5584 struct extent_map *em;
5585 struct map_lookup *map;
Chris Mason0b86a832008-03-24 15:01:56 -04005586 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005587 u64 stripe_offset;
5588 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005589 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005590 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005591 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005592 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005593 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005594 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005595 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005596 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005597 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5598 int dev_replace_is_ongoing = 0;
5599 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005600 int patch_the_first_stripe_for_dev_replace = 0;
5601 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005602 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005603
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005604 if (op == BTRFS_MAP_DISCARD)
5605 return __btrfs_map_block_for_discard(fs_info, logical,
5606 *length, bbio_ret);
5607
Liu Bo592d92e2017-03-14 13:33:55 -07005608 em = get_chunk_map(fs_info, logical, *length);
5609 if (IS_ERR(em))
5610 return PTR_ERR(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005611
Jeff Mahoney95617d62015-06-03 10:55:48 -04005612 map = em->map_lookup;
Chris Mason0b86a832008-03-24 15:01:56 -04005613 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005614
David Woodhouse53b381b2013-01-29 18:40:14 -05005615 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005616 stripe_nr = offset;
5617 /*
5618 * stripe_nr counts the total number of stripes we have to stride
5619 * to get to this block
5620 */
David Sterba47c57132015-02-20 18:43:47 +01005621 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005622
David Woodhouse53b381b2013-01-29 18:40:14 -05005623 stripe_offset = stripe_nr * stripe_len;
Josef Bacike042d1e2016-04-12 12:54:40 -04005624 if (offset < stripe_offset) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005625 btrfs_crit(fs_info,
5626 "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
Josef Bacike042d1e2016-04-12 12:54:40 -04005627 stripe_offset, offset, em->start, logical,
5628 stripe_len);
5629 free_extent_map(em);
5630 return -EINVAL;
5631 }
Chris Mason593060d2008-03-25 16:50:33 -04005632
5633 /* stripe_offset is the offset of this block in its stripe*/
5634 stripe_offset = offset - stripe_offset;
5635
David Woodhouse53b381b2013-01-29 18:40:14 -05005636 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005637 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005638 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5639 raid56_full_stripe_start = offset;
5640
5641 /* allow a write of a full stripe, but make sure we don't
5642 * allow straddling of stripes
5643 */
David Sterba47c57132015-02-20 18:43:47 +01005644 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5645 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005646 raid56_full_stripe_start *= full_stripe_len;
5647 }
5648
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005649 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005650 u64 max_len;
5651 /* For writes to RAID[56], allow a full stripeset across all disks.
5652 For other RAID types and for RAID[56] reads, just allow a single
5653 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005654 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005655 (op == BTRFS_MAP_WRITE)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005656 max_len = stripe_len * nr_data_stripes(map) -
5657 (offset - raid56_full_stripe_start);
5658 } else {
5659 /* we limit the length of each bio to what fits in a stripe */
5660 max_len = stripe_len - stripe_offset;
5661 }
5662 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005663 } else {
5664 *length = em->len - offset;
5665 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005666
David Woodhouse53b381b2013-01-29 18:40:14 -05005667 /* This is for when we're called from btrfs_merge_bio_hook() and all
5668 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005669 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005670 goto out;
5671
Liu Bo73beece2015-07-17 16:49:19 +08005672 btrfs_dev_replace_lock(dev_replace, 0);
Stefan Behrens472262f2012-11-06 14:43:46 +01005673 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5674 if (!dev_replace_is_ongoing)
Liu Bo73beece2015-07-17 16:49:19 +08005675 btrfs_dev_replace_unlock(dev_replace, 0);
5676 else
5677 btrfs_dev_replace_set_lock_blocking(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005678
Stefan Behrensad6d6202012-11-06 15:06:47 +01005679 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005680 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07005681 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
5682 dev_replace->srcdev->devid,
5683 &mirror_num,
5684 &physical_to_patch_in_first_stripe);
5685 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005686 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07005687 else
5688 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005689 } else if (mirror_num > map->num_stripes) {
5690 mirror_num = 0;
5691 }
5692
Chris Masonf2d8d742008-04-21 10:03:05 -04005693 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005694 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005695 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01005696 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5697 &stripe_index);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005698 if (op != BTRFS_MAP_WRITE && op != BTRFS_MAP_GET_READ_MIRRORS)
Miao Xie28e1cc72014-09-12 18:44:02 +08005699 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005700 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005701 if (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS)
Chris Masonf2d8d742008-04-21 10:03:05 -04005702 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005703 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005704 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005705 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005706 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005707 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005708 current->pid % map->num_stripes,
5709 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005710 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005711 }
Chris Mason2fff7342008-04-29 14:12:09 -04005712
Chris Mason611f0e02008-04-03 16:29:03 -04005713 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005714 if (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005715 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005716 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005717 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005718 } else {
5719 mirror_num = 1;
5720 }
Chris Mason2fff7342008-04-29 14:12:09 -04005721
Chris Mason321aecc2008-04-16 10:49:51 -04005722 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005723 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005724
David Sterba47c57132015-02-20 18:43:47 +01005725 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005726 stripe_index *= map->sub_stripes;
5727
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005728 if (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS)
Chris Masonf2d8d742008-04-21 10:03:05 -04005729 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005730 else if (mirror_num)
5731 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005732 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005733 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005734 stripe_index = find_live_mirror(fs_info, map,
5735 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005736 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005737 current->pid % map->sub_stripes,
5738 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005739 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005740 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005741
Zhao Leiffe2d202015-01-20 15:11:44 +08005742 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005743 if (need_raid_map &&
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005744 (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS ||
Miao Xieaf8e2d12014-10-23 14:42:50 +08005745 mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005746 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07005747 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbab8b93ad2015-01-16 17:26:13 +01005748 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005749
5750 /* RAID[56] write or recovery. Return all stripes */
5751 num_stripes = map->num_stripes;
5752 max_errors = nr_parity_stripes(map);
5753
David Woodhouse53b381b2013-01-29 18:40:14 -05005754 *length = map->stripe_len;
5755 stripe_index = 0;
5756 stripe_offset = 0;
5757 } else {
5758 /*
5759 * Mirror #0 or #1 means the original data block.
5760 * Mirror #2 is RAID5 parity block.
5761 * Mirror #3 is RAID6 Q block.
5762 */
David Sterba47c57132015-02-20 18:43:47 +01005763 stripe_nr = div_u64_rem(stripe_nr,
5764 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005765 if (mirror_num > 1)
5766 stripe_index = nr_data_stripes(map) +
5767 mirror_num - 2;
5768
5769 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005770 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5771 &stripe_index);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005772 if ((op != BTRFS_MAP_WRITE &&
5773 op != BTRFS_MAP_GET_READ_MIRRORS) &&
5774 mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08005775 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005776 }
Chris Mason8790d502008-04-03 16:29:03 -04005777 } else {
5778 /*
David Sterba47c57132015-02-20 18:43:47 +01005779 * after this, stripe_nr is the number of stripes on this
5780 * device we have to walk to find the data, and stripe_index is
5781 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005782 */
David Sterba47c57132015-02-20 18:43:47 +01005783 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5784 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005785 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005786 }
Josef Bacike042d1e2016-04-12 12:54:40 -04005787 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005788 btrfs_crit(fs_info,
5789 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04005790 stripe_index, map->num_stripes);
5791 ret = -EINVAL;
5792 goto out;
5793 }
Chris Mason593060d2008-03-25 16:50:33 -04005794
Stefan Behrens472262f2012-11-06 14:43:46 +01005795 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07005796 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005797 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005798 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005799 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005800 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005801 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005802 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005803
Zhao Lei6e9606d2015-01-20 15:11:34 +08005804 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005805 if (!bbio) {
5806 ret = -ENOMEM;
5807 goto out;
5808 }
Liu Bo6fad8232017-03-14 13:33:59 -07005809 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
Miao Xie2c8cdd62014-11-14 16:06:25 +08005810 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005811
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005812 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07005813 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
5814 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005815 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005816 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005817
5818 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5819 sizeof(struct btrfs_bio_stripe) *
5820 num_alloc_stripes +
5821 sizeof(int) * tgtdev_indexes);
5822
5823 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005824 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005825
5826 /* Fill in the logical address of each stripe */
5827 tmp = stripe_nr * nr_data_stripes(map);
5828 for (i = 0; i < nr_data_stripes(map); i++)
5829 bbio->raid_map[(i+rot) % num_stripes] =
5830 em->start + (tmp + i) * map->stripe_len;
5831
5832 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5833 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5834 bbio->raid_map[(i+rot+1) % num_stripes] =
5835 RAID6_Q_STRIPE;
5836 }
5837
Li Zefanec9ef7a2011-12-01 14:06:42 +08005838
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005839 for (i = 0; i < num_stripes; i++) {
5840 bbio->stripes[i].physical =
5841 map->stripes[stripe_index].physical +
5842 stripe_offset +
5843 stripe_nr * map->stripe_len;
5844 bbio->stripes[i].dev =
5845 map->stripes[stripe_index].dev;
5846 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04005847 }
Li Zefande11cc12011-12-01 12:55:47 +08005848
Liu Bo2b19a1f2017-03-14 13:34:00 -07005849 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08005850 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005851
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005852 if (bbio->raid_map)
5853 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005854
Liu Bo73c0f222017-03-14 13:33:58 -07005855 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005856 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07005857 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
5858 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01005859 }
5860
Li Zefande11cc12011-12-01 12:55:47 +08005861 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005862 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005863 bbio->num_stripes = num_stripes;
5864 bbio->max_errors = max_errors;
5865 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005866
5867 /*
5868 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5869 * mirror_num == num_stripes + 1 && dev_replace target drive is
5870 * available as a mirror
5871 */
5872 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5873 WARN_ON(num_stripes > 1);
5874 bbio->stripes[0].dev = dev_replace->tgtdev;
5875 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5876 bbio->mirror_num = map->num_stripes + 1;
5877 }
Chris Masoncea9e442008-04-09 16:28:12 -04005878out:
Liu Bo73beece2015-07-17 16:49:19 +08005879 if (dev_replace_is_ongoing) {
5880 btrfs_dev_replace_clear_lock_blocking(dev_replace);
5881 btrfs_dev_replace_unlock(dev_replace, 0);
5882 }
Chris Mason0b86a832008-03-24 15:01:56 -04005883 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005884 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005885}
5886
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005887int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005888 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005889 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005890{
Mike Christieb3d3fa52016-06-05 14:31:53 -05005891 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005892 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005893}
5894
Miao Xieaf8e2d12014-10-23 14:42:50 +08005895/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005896int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08005897 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02005898 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005899{
David Sterba825ad4c2017-03-28 14:45:22 +02005900 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005901}
5902
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005903int btrfs_rmap_block(struct btrfs_fs_info *fs_info,
Yan Zhenga512bbf2008-12-08 16:46:26 -05005904 u64 chunk_start, u64 physical, u64 devid,
5905 u64 **logical, int *naddrs, int *stripe_len)
5906{
Yan Zhenga512bbf2008-12-08 16:46:26 -05005907 struct extent_map *em;
5908 struct map_lookup *map;
5909 u64 *buf;
5910 u64 bytenr;
5911 u64 length;
5912 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005913 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005914 int i, j, nr = 0;
5915
Liu Bo592d92e2017-03-14 13:33:55 -07005916 em = get_chunk_map(fs_info, chunk_start, 1);
5917 if (IS_ERR(em))
Josef Bacik835d9742013-03-19 12:13:25 -04005918 return -EIO;
Josef Bacik835d9742013-03-19 12:13:25 -04005919
Jeff Mahoney95617d62015-06-03 10:55:48 -04005920 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005921 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005922 rmap_len = map->stripe_len;
5923
Yan Zhenga512bbf2008-12-08 16:46:26 -05005924 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01005925 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005926 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01005927 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08005928 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01005929 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005930 rmap_len = map->stripe_len * nr_data_stripes(map);
5931 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005932
David Sterba31e818f2015-02-20 18:00:26 +01005933 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005934 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005935
5936 for (i = 0; i < map->num_stripes; i++) {
5937 if (devid && map->stripes[i].dev->devid != devid)
5938 continue;
5939 if (map->stripes[i].physical > physical ||
5940 map->stripes[i].physical + length <= physical)
5941 continue;
5942
5943 stripe_nr = physical - map->stripes[i].physical;
Liu Bo42c61ab2017-04-03 13:45:24 -07005944 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005945
5946 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5947 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01005948 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005949 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5950 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005951 } /* else if RAID[56], multiply by nr_data_stripes().
5952 * Alternatively, just use rmap_len below instead of
5953 * map->stripe_len */
5954
5955 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005956 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005957 for (j = 0; j < nr; j++) {
5958 if (buf[j] == bytenr)
5959 break;
5960 }
Chris Mason934d3752008-12-08 16:43:10 -05005961 if (j == nr) {
5962 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005963 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005964 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005965 }
5966
Yan Zhenga512bbf2008-12-08 16:46:26 -05005967 *logical = buf;
5968 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005969 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005970
5971 free_extent_map(em);
5972 return 0;
5973}
5974
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005975static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08005976{
Mike Snitzer326e1db2015-05-22 09:14:03 -04005977 bio->bi_private = bbio->private;
5978 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005979 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04005980
Zhao Lei6e9606d2015-01-20 15:11:34 +08005981 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08005982}
5983
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005984static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04005985{
Chris Mason9be33952013-05-17 18:30:14 -04005986 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005987 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005988
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02005989 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005990 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02005991 if (bio->bi_status == BLK_STS_IOERR ||
5992 bio->bi_status == BLK_STS_TARGET) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02005993 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005994 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08005995 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005996
5997 BUG_ON(stripe_index >= bbio->num_stripes);
5998 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005999 if (dev->bdev) {
Mike Christie37226b22016-06-05 14:31:52 -05006000 if (bio_op(bio) == REQ_OP_WRITE)
Stefan Behrens597a60f2012-06-14 16:42:31 +02006001 btrfs_dev_stat_inc(dev,
6002 BTRFS_DEV_STAT_WRITE_ERRS);
6003 else
6004 btrfs_dev_stat_inc(dev,
6005 BTRFS_DEV_STAT_READ_ERRS);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06006006 if (bio->bi_opf & REQ_PREFLUSH)
Stefan Behrens597a60f2012-06-14 16:42:31 +02006007 btrfs_dev_stat_inc(dev,
6008 BTRFS_DEV_STAT_FLUSH_ERRS);
6009 btrfs_dev_stat_print_on_error(dev);
6010 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02006011 }
6012 }
Chris Mason8790d502008-04-03 16:29:03 -04006013
Jan Schmidta1d3c472011-08-04 17:15:33 +02006014 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006015 is_orig_bio = 1;
6016
Miao Xiec404e0d2014-01-30 16:46:55 +08006017 btrfs_bio_counter_dec(bbio->fs_info);
6018
Jan Schmidta1d3c472011-08-04 17:15:33 +02006019 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006020 if (!is_orig_bio) {
6021 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006022 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006023 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006024
Chris Mason9be33952013-05-17 18:30:14 -04006025 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006026 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006027 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006028 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006029 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006030 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006031 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006032 /*
6033 * this bio is actually up to date, we didn't
6034 * go over the max number of errors
6035 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006036 bio->bi_status = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04006037 }
Miao Xiec55f1392014-06-19 10:42:54 +08006038
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006039 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006040 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006041 bio_put(bio);
6042 }
Chris Mason8790d502008-04-03 16:29:03 -04006043}
6044
Chris Mason8b712842008-06-11 16:50:36 -04006045/*
6046 * see run_scheduled_bios for a description of why bios are collected for
6047 * async submit.
6048 *
6049 * This will add one bio to the pending list for a device and make sure
6050 * the work struct is scheduled.
6051 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006052static noinline void btrfs_schedule_bio(struct btrfs_device *device,
Mike Christie4e49ea42016-06-05 14:31:41 -05006053 struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04006054{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006055 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -04006056 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04006057 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006058
David Woodhouse53b381b2013-01-29 18:40:14 -05006059 if (device->missing || !device->bdev) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006060 bio_io_error(bio);
David Woodhouse53b381b2013-01-29 18:40:14 -05006061 return;
6062 }
6063
Chris Mason8b712842008-06-11 16:50:36 -04006064 /* don't bother with additional async steps for reads, right now */
Mike Christie37226b22016-06-05 14:31:52 -05006065 if (bio_op(bio) == REQ_OP_READ) {
Chris Mason492bb6de2008-07-31 16:29:02 -04006066 bio_get(bio);
Mike Christie4e49ea42016-06-05 14:31:41 -05006067 btrfsic_submit_bio(bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04006068 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01006069 return;
Chris Mason8b712842008-06-11 16:50:36 -04006070 }
6071
6072 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04006073 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04006074 * higher layers. Otherwise, the async bio makes it appear we have
6075 * made progress against dirty pages when we've really just put it
6076 * on a queue for later
6077 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006078 atomic_inc(&fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04006079 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04006080 bio->bi_next = NULL;
Chris Mason8b712842008-06-11 16:50:36 -04006081
6082 spin_lock(&device->io_lock);
Christoph Hellwig67f055c2016-11-01 07:40:06 -06006083 if (op_is_sync(bio->bi_opf))
Chris Masonffbd5172009-04-20 15:50:09 -04006084 pending_bios = &device->pending_sync_bios;
6085 else
6086 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006087
Chris Masonffbd5172009-04-20 15:50:09 -04006088 if (pending_bios->tail)
6089 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006090
Chris Masonffbd5172009-04-20 15:50:09 -04006091 pending_bios->tail = bio;
6092 if (!pending_bios->head)
6093 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006094 if (device->running_pending)
6095 should_queue = 0;
6096
6097 spin_unlock(&device->io_lock);
6098
6099 if (should_queue)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006100 btrfs_queue_work(fs_info->submit_workers, &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04006101}
6102
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006103static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6104 u64 physical, int dev_nr, int async)
Josef Bacikde1ee922012-10-19 16:50:56 -04006105{
6106 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006107 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006108
6109 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006110 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006111 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006112 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04006113#ifdef DEBUG
6114 {
6115 struct rcu_string *name;
6116
6117 rcu_read_lock();
6118 name = rcu_dereference(dev->name);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006119 btrfs_debug(fs_info,
6120 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6121 bio_op(bio), bio->bi_opf,
6122 (u64)bio->bi_iter.bi_sector,
6123 (u_long)dev->bdev->bd_dev, name->str, dev->devid,
6124 bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04006125 rcu_read_unlock();
6126 }
6127#endif
Christoph Hellwig74d46992017-08-23 19:10:32 +02006128 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006129
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006130 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006131
Josef Bacikde1ee922012-10-19 16:50:56 -04006132 if (async)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006133 btrfs_schedule_bio(dev, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006134 else
Mike Christie4e49ea42016-06-05 14:31:41 -05006135 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006136}
6137
Josef Bacikde1ee922012-10-19 16:50:56 -04006138static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6139{
6140 atomic_inc(&bbio->error);
6141 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006142 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006143 WARN_ON(bio != bbio->orig_bio);
6144
Chris Mason9be33952013-05-17 18:30:14 -04006145 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006146 bio->bi_iter.bi_sector = logical >> 9;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006147 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006148 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006149 }
6150}
6151
Omar Sandoval58efbc92017-08-22 23:45:59 -07006152blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6153 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006154{
Chris Mason0b86a832008-03-24 15:01:56 -04006155 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006156 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006157 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006158 u64 length = 0;
6159 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006160 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006161 int dev_nr;
6162 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006163 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006164
Kent Overstreet4f024f32013-10-11 15:44:27 -07006165 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006166 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006167
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006168 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006169 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006170 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006171 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006172 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006173 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006174 }
Chris Masoncea9e442008-04-09 16:28:12 -04006175
Jan Schmidta1d3c472011-08-04 17:15:33 +02006176 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006177 bbio->orig_bio = first_bio;
6178 bbio->private = first_bio->bi_private;
6179 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006180 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006181 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6182
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006183 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006184 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006185 /* In this case, map_length has been set to the length of
6186 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006187 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006188 ret = raid56_parity_write(fs_info, bio, bbio,
6189 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006190 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006191 ret = raid56_parity_recover(fs_info, bio, bbio,
6192 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006193 }
Miao Xie42452152014-11-25 16:39:28 +08006194
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006195 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006196 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006197 }
6198
Chris Masoncea9e442008-04-09 16:28:12 -04006199 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006200 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006201 "mapping failed logical %llu bio len %llu len %llu",
6202 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006203 BUG();
6204 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006205
Zhao Lei08da7572015-02-12 15:42:16 +08006206 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006207 dev = bbio->stripes[dev_nr].dev;
Mike Christie37226b22016-06-05 14:31:52 -05006208 if (!dev || !dev->bdev ||
Liu Boa967efb2017-04-10 12:36:26 -07006209 (bio_op(first_bio) == REQ_OP_WRITE && !dev->writeable)) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006210 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006211 continue;
6212 }
6213
David Sterba3aa8e072017-06-02 17:38:30 +02006214 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006215 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006216 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006217 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006218
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006219 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6220 dev_nr, async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006221 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006222 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006223 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006224}
6225
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006226struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006227 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006228{
Yan Zheng2b820322008-11-17 21:11:30 -05006229 struct btrfs_device *device;
6230 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006231
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006232 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006233 while (cur_devices) {
6234 if (!fsid ||
Anand Jain44880fd2017-07-29 17:50:09 +08006235 !memcmp(cur_devices->fsid, fsid, BTRFS_FSID_SIZE)) {
David Sterba35c70102017-06-15 19:51:51 +02006236 device = find_device(cur_devices, devid, uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006237 if (device)
6238 return device;
6239 }
6240 cur_devices = cur_devices->seed;
6241 }
6242 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006243}
6244
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006245static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006246 u64 devid, u8 *dev_uuid)
6247{
6248 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006249
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006250 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6251 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05006252 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006253
6254 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006255 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006256 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006257
6258 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05006259 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006260
Chris Masondfe25022008-05-13 13:46:40 -04006261 return device;
6262}
6263
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006264/**
6265 * btrfs_alloc_device - allocate struct btrfs_device
6266 * @fs_info: used only for generating a new devid, can be NULL if
6267 * devid is provided (i.e. @devid != NULL).
6268 * @devid: a pointer to devid for this device. If NULL a new devid
6269 * is generated.
6270 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6271 * is generated.
6272 *
6273 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6274 * on error. Returned struct is not linked onto any lists and can be
6275 * destroyed with kfree() right away.
6276 */
6277struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6278 const u64 *devid,
6279 const u8 *uuid)
6280{
6281 struct btrfs_device *dev;
6282 u64 tmp;
6283
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306284 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006285 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006286
6287 dev = __alloc_device();
6288 if (IS_ERR(dev))
6289 return dev;
6290
6291 if (devid)
6292 tmp = *devid;
6293 else {
6294 int ret;
6295
6296 ret = find_next_devid(fs_info, &tmp);
6297 if (ret) {
6298 kfree(dev);
6299 return ERR_PTR(ret);
6300 }
6301 }
6302 dev->devid = tmp;
6303
6304 if (uuid)
6305 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6306 else
6307 generate_random_uuid(dev->uuid);
6308
Liu Bo9e0af232014-08-15 23:36:53 +08006309 btrfs_init_work(&dev->work, btrfs_submit_helper,
6310 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006311
6312 return dev;
6313}
6314
Liu Boe06cd3d2016-06-03 12:05:15 -07006315/* Return -EIO if any error, otherwise return 0. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006316static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006317 struct extent_buffer *leaf,
6318 struct btrfs_chunk *chunk, u64 logical)
6319{
6320 u64 length;
6321 u64 stripe_len;
6322 u16 num_stripes;
6323 u16 sub_stripes;
6324 u64 type;
6325
6326 length = btrfs_chunk_length(leaf, chunk);
6327 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6328 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6329 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6330 type = btrfs_chunk_type(leaf, chunk);
6331
6332 if (!num_stripes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006333 btrfs_err(fs_info, "invalid chunk num_stripes: %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006334 num_stripes);
6335 return -EIO;
6336 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006337 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
6338 btrfs_err(fs_info, "invalid chunk logical %llu", logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006339 return -EIO;
6340 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006341 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
6342 btrfs_err(fs_info, "invalid chunk sectorsize %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006343 btrfs_chunk_sector_size(leaf, chunk));
6344 return -EIO;
6345 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006346 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
6347 btrfs_err(fs_info, "invalid chunk length %llu", length);
Liu Boe06cd3d2016-06-03 12:05:15 -07006348 return -EIO;
6349 }
6350 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006351 btrfs_err(fs_info, "invalid chunk stripe length: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006352 stripe_len);
6353 return -EIO;
6354 }
6355 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6356 type) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006357 btrfs_err(fs_info, "unrecognized chunk type: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006358 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6359 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6360 btrfs_chunk_type(leaf, chunk));
6361 return -EIO;
6362 }
6363 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
6364 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
6365 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
6366 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
6367 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
6368 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
6369 num_stripes != 1)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006370 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006371 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
6372 num_stripes, sub_stripes,
6373 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
6374 return -EIO;
6375 }
6376
6377 return 0;
6378}
6379
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006380static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
Chris Mason0b86a832008-03-24 15:01:56 -04006381 struct extent_buffer *leaf,
6382 struct btrfs_chunk *chunk)
6383{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006384 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006385 struct map_lookup *map;
6386 struct extent_map *em;
6387 u64 logical;
6388 u64 length;
6389 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006390 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006391 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006392 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006393 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006394
Chris Masone17cade2008-04-15 15:41:47 -04006395 logical = key->offset;
6396 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006397 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006398
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006399 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006400 if (ret)
6401 return ret;
Chris Masona061fc82008-05-07 11:43:44 -04006402
Chris Mason890871b2009-09-02 16:24:52 -04006403 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006404 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006405 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006406
6407 /* already mapped? */
6408 if (em && em->start <= logical && em->start + em->len > logical) {
6409 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006410 return 0;
6411 } else if (em) {
6412 free_extent_map(em);
6413 }
Chris Mason0b86a832008-03-24 15:01:56 -04006414
David Sterba172ddd62011-04-21 00:48:27 +02006415 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006416 if (!em)
6417 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006418 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006419 if (!map) {
6420 free_extent_map(em);
6421 return -ENOMEM;
6422 }
6423
Wang Shilong298a8f92014-06-19 10:42:52 +08006424 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006425 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006426 em->start = logical;
6427 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006428 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006429 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006430 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006431
Chris Mason593060d2008-03-25 16:50:33 -04006432 map->num_stripes = num_stripes;
6433 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6434 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006435 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6436 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006437 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006438 for (i = 0; i < num_stripes; i++) {
6439 map->stripes[i].physical =
6440 btrfs_stripe_offset_nr(leaf, chunk, i);
6441 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006442 read_extent_buffer(leaf, uuid, (unsigned long)
6443 btrfs_stripe_dev_uuid_nr(chunk, i),
6444 BTRFS_UUID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006445 map->stripes[i].dev = btrfs_find_device(fs_info, devid,
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006446 uuid, NULL);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006447 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006448 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006449 free_extent_map(em);
Qu Wenruoc5502452017-03-09 09:34:42 +08006450 btrfs_report_missing_device(fs_info, devid, uuid);
Chris Mason593060d2008-03-25 16:50:33 -04006451 return -EIO;
6452 }
Chris Masondfe25022008-05-13 13:46:40 -04006453 if (!map->stripes[i].dev) {
6454 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006455 add_missing_dev(fs_info->fs_devices, devid,
6456 uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006457 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04006458 free_extent_map(em);
6459 return -EIO;
6460 }
Qu Wenruoc5502452017-03-09 09:34:42 +08006461 btrfs_report_missing_device(fs_info, devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006462 }
6463 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04006464 }
6465
Chris Mason890871b2009-09-02 16:24:52 -04006466 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006467 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006468 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006469 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006470 free_extent_map(em);
6471
6472 return 0;
6473}
6474
Jeff Mahoney143bede2012-03-01 14:56:26 +01006475static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006476 struct btrfs_dev_item *dev_item,
6477 struct btrfs_device *device)
6478{
6479 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006480
6481 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006482 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6483 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006484 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006485 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006486 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006487 device->type = btrfs_device_type(leaf, dev_item);
6488 device->io_align = btrfs_device_io_align(leaf, dev_item);
6489 device->io_width = btrfs_device_io_width(leaf, dev_item);
6490 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006491 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006492 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006493
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006494 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006495 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006496}
6497
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006498static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006499 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006500{
6501 struct btrfs_fs_devices *fs_devices;
6502 int ret;
6503
Li Zefanb367e472011-12-07 11:38:24 +08006504 BUG_ON(!mutex_is_locked(&uuid_mutex));
David Sterba2dfeca92017-06-14 02:48:07 +02006505 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006506
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006507 fs_devices = fs_info->fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006508 while (fs_devices) {
Anand Jain44880fd2017-07-29 17:50:09 +08006509 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006510 return fs_devices;
6511
Yan Zheng2b820322008-11-17 21:11:30 -05006512 fs_devices = fs_devices->seed;
6513 }
6514
6515 fs_devices = find_fsid(fsid);
6516 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006517 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006518 return ERR_PTR(-ENOENT);
6519
6520 fs_devices = alloc_fs_devices(fsid);
6521 if (IS_ERR(fs_devices))
6522 return fs_devices;
6523
6524 fs_devices->seeding = 1;
6525 fs_devices->opened = 1;
6526 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006527 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006528
6529 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006530 if (IS_ERR(fs_devices))
6531 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006532
Christoph Hellwig97288f22008-12-02 06:36:09 -05006533 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006534 fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006535 if (ret) {
6536 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006537 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006538 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006539 }
Yan Zheng2b820322008-11-17 21:11:30 -05006540
6541 if (!fs_devices->seeding) {
6542 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006543 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006544 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006545 goto out;
6546 }
6547
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006548 fs_devices->seed = fs_info->fs_devices->seed;
6549 fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006550out:
Miao Xie5f375832014-09-03 21:35:46 +08006551 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006552}
6553
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006554static int read_one_dev(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04006555 struct extent_buffer *leaf,
6556 struct btrfs_dev_item *dev_item)
6557{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006558 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006559 struct btrfs_device *device;
6560 u64 devid;
6561 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08006562 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006563 u8 dev_uuid[BTRFS_UUID_SIZE];
6564
Chris Mason0b86a832008-03-24 15:01:56 -04006565 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006566 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006567 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006568 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08006569 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05006570
Anand Jain44880fd2017-07-29 17:50:09 +08006571 if (memcmp(fs_uuid, fs_info->fsid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006572 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006573 if (IS_ERR(fs_devices))
6574 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006575 }
6576
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006577 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006578 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08006579 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6580 btrfs_report_missing_device(fs_info, devid, dev_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006581 return -EIO;
Qu Wenruoc5502452017-03-09 09:34:42 +08006582 }
Yan Zheng2b820322008-11-17 21:11:30 -05006583
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006584 device = add_missing_dev(fs_devices, devid, dev_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006585 if (!device)
6586 return -ENOMEM;
Qu Wenruoc5502452017-03-09 09:34:42 +08006587 btrfs_report_missing_device(fs_info, devid, dev_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006588 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08006589 if (!device->bdev) {
6590 btrfs_report_missing_device(fs_info, devid, dev_uuid);
6591 if (!btrfs_test_opt(fs_info, DEGRADED))
6592 return -EIO;
6593 }
Miao Xie5f375832014-09-03 21:35:46 +08006594
6595 if(!device->bdev && !device->missing) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006596 /*
6597 * this happens when a device that was properly setup
6598 * in the device info lists suddenly goes bad.
6599 * device->bdev is NULL, and so we have to set
6600 * device->missing to one here
6601 */
Miao Xie5f375832014-09-03 21:35:46 +08006602 device->fs_devices->missing_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05006603 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006604 }
Miao Xie5f375832014-09-03 21:35:46 +08006605
6606 /* Move the device to its own fs_devices */
6607 if (device->fs_devices != fs_devices) {
6608 ASSERT(device->missing);
6609
6610 list_move(&device->dev_list, &fs_devices->devices);
6611 device->fs_devices->num_devices--;
6612 fs_devices->num_devices++;
6613
6614 device->fs_devices->missing_devices--;
6615 fs_devices->missing_devices++;
6616
6617 device->fs_devices = fs_devices;
6618 }
Yan Zheng2b820322008-11-17 21:11:30 -05006619 }
6620
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006621 if (device->fs_devices != fs_info->fs_devices) {
Yan Zheng2b820322008-11-17 21:11:30 -05006622 BUG_ON(device->writeable);
6623 if (device->generation !=
6624 btrfs_device_generation(leaf, dev_item))
6625 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006626 }
Chris Mason0b86a832008-03-24 15:01:56 -04006627
6628 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006629 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006630 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006631 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03006632 atomic64_add(device->total_bytes - device->bytes_used,
6633 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04006634 }
Chris Mason0b86a832008-03-24 15:01:56 -04006635 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006636 return ret;
6637}
6638
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006639int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006640{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006641 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006642 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006643 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006644 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006645 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006646 u8 *array_ptr;
6647 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006648 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006649 u32 num_stripes;
6650 u32 array_size;
6651 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006652 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07006653 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04006654 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006655
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006656 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02006657 /*
6658 * This will create extent buffer of nodesize, superblock size is
6659 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6660 * overallocate but we can keep it as-is, only the first page is used.
6661 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006662 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07006663 if (IS_ERR(sb))
6664 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01006665 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006666 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006667 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006668 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01006669 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02006670 * pages up-to-date when the page is larger: extent does not cover the
6671 * whole page and consequently check_page_uptodate does not find all
6672 * the page's extents up-to-date (the hole beyond sb),
6673 * write_extent_buffer then triggers a WARN_ON.
6674 *
6675 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6676 * but sb spans only this function. Add an explicit SetPageUptodate call
6677 * to silence the warning eg. on PowerPC 64.
6678 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006679 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006680 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006681
Chris Masona061fc82008-05-07 11:43:44 -04006682 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006683 array_size = btrfs_super_sys_array_size(super_copy);
6684
David Sterba1ffb22c2014-10-31 19:02:42 +01006685 array_ptr = super_copy->sys_chunk_array;
6686 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6687 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006688
David Sterba1ffb22c2014-10-31 19:02:42 +01006689 while (cur_offset < array_size) {
6690 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006691 len = sizeof(*disk_key);
6692 if (cur_offset + len > array_size)
6693 goto out_short_read;
6694
Chris Mason0b86a832008-03-24 15:01:56 -04006695 btrfs_disk_key_to_cpu(&key, disk_key);
6696
David Sterba1ffb22c2014-10-31 19:02:42 +01006697 array_ptr += len;
6698 sb_array_offset += len;
6699 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006700
Chris Mason0d81ba52008-03-24 15:02:07 -04006701 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006702 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006703 /*
6704 * At least one btrfs_chunk with one stripe must be
6705 * present, exact stripe count check comes afterwards
6706 */
6707 len = btrfs_chunk_item_size(1);
6708 if (cur_offset + len > array_size)
6709 goto out_short_read;
6710
6711 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01006712 if (!num_stripes) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006713 btrfs_err(fs_info,
6714 "invalid number of stripes %u in sys_array at offset %u",
David Sterbaf5cdedd2015-11-30 17:27:06 +01006715 num_stripes, cur_offset);
6716 ret = -EIO;
6717 break;
6718 }
6719
Liu Boe06cd3d2016-06-03 12:05:15 -07006720 type = btrfs_chunk_type(sb, chunk);
6721 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006722 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006723 "invalid chunk type %llu in sys_array at offset %u",
6724 type, cur_offset);
6725 ret = -EIO;
6726 break;
6727 }
6728
David Sterbae3540ea2014-11-05 15:24:51 +01006729 len = btrfs_chunk_item_size(num_stripes);
6730 if (cur_offset + len > array_size)
6731 goto out_short_read;
6732
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006733 ret = read_one_chunk(fs_info, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006734 if (ret)
6735 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006736 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006737 btrfs_err(fs_info,
6738 "unexpected item type %u in sys_array at offset %u",
6739 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04006740 ret = -EIO;
6741 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006742 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006743 array_ptr += len;
6744 sb_array_offset += len;
6745 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006746 }
Liu Bod8651772016-06-03 17:41:42 -07006747 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006748 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006749 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006750
6751out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006752 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01006753 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07006754 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006755 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01006756 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006757}
6758
Qu Wenruoc5502452017-03-09 09:34:42 +08006759void btrfs_report_missing_device(struct btrfs_fs_info *fs_info, u64 devid,
6760 u8 *uuid)
6761{
6762 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing", devid, uuid);
6763}
6764
Qu Wenruo21634a12017-03-09 09:34:36 +08006765/*
6766 * Check if all chunks in the fs are OK for read-write degraded mount
6767 *
6768 * Return true if all chunks meet the minimal RW mount requirements.
6769 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6770 */
6771bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info)
6772{
6773 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
6774 struct extent_map *em;
6775 u64 next_start = 0;
6776 bool ret = true;
6777
6778 read_lock(&map_tree->map_tree.lock);
6779 em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1);
6780 read_unlock(&map_tree->map_tree.lock);
6781 /* No chunk at all? Return false anyway */
6782 if (!em) {
6783 ret = false;
6784 goto out;
6785 }
6786 while (em) {
6787 struct map_lookup *map;
6788 int missing = 0;
6789 int max_tolerated;
6790 int i;
6791
6792 map = em->map_lookup;
6793 max_tolerated =
6794 btrfs_get_num_tolerated_disk_barrier_failures(
6795 map->type);
6796 for (i = 0; i < map->num_stripes; i++) {
6797 struct btrfs_device *dev = map->stripes[i].dev;
6798
6799 if (!dev || !dev->bdev || dev->missing ||
6800 dev->last_flush_error)
6801 missing++;
6802 }
6803 if (missing > max_tolerated) {
6804 btrfs_warn(fs_info,
6805 "chunk %llu missing %d devices, max tolerance is %d for writeable mount",
6806 em->start, missing, max_tolerated);
6807 free_extent_map(em);
6808 ret = false;
6809 goto out;
6810 }
6811 next_start = extent_map_end(em);
6812 free_extent_map(em);
6813
6814 read_lock(&map_tree->map_tree.lock);
6815 em = lookup_extent_mapping(&map_tree->map_tree, next_start,
6816 (u64)(-1) - next_start);
6817 read_unlock(&map_tree->map_tree.lock);
6818 }
6819out:
6820 return ret;
6821}
6822
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006823int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006824{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006825 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04006826 struct btrfs_path *path;
6827 struct extent_buffer *leaf;
6828 struct btrfs_key key;
6829 struct btrfs_key found_key;
6830 int ret;
6831 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006832 u64 total_dev = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006833
Chris Mason0b86a832008-03-24 15:01:56 -04006834 path = btrfs_alloc_path();
6835 if (!path)
6836 return -ENOMEM;
6837
Li Zefanb367e472011-12-07 11:38:24 +08006838 mutex_lock(&uuid_mutex);
David Sterba34441362016-10-04 19:34:27 +02006839 mutex_lock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08006840
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006841 /*
6842 * Read all device items, and then all the chunk items. All
6843 * device items are found before any chunk item (their object id
6844 * is smaller than the lowest possible object id for a chunk
6845 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006846 */
6847 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6848 key.offset = 0;
6849 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006850 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006851 if (ret < 0)
6852 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006853 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006854 leaf = path->nodes[0];
6855 slot = path->slots[0];
6856 if (slot >= btrfs_header_nritems(leaf)) {
6857 ret = btrfs_next_leaf(root, path);
6858 if (ret == 0)
6859 continue;
6860 if (ret < 0)
6861 goto error;
6862 break;
6863 }
6864 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006865 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6866 struct btrfs_dev_item *dev_item;
6867 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006868 struct btrfs_dev_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006869 ret = read_one_dev(fs_info, leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006870 if (ret)
6871 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006872 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04006873 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6874 struct btrfs_chunk *chunk;
6875 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006876 ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006877 if (ret)
6878 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006879 }
6880 path->slots[0]++;
6881 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07006882
6883 /*
6884 * After loading chunk tree, we've got all device information,
6885 * do another round of validation checks.
6886 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006887 if (total_dev != fs_info->fs_devices->total_devices) {
6888 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07006889 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006890 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07006891 total_dev);
6892 ret = -EINVAL;
6893 goto error;
6894 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006895 if (btrfs_super_total_bytes(fs_info->super_copy) <
6896 fs_info->fs_devices->total_rw_bytes) {
6897 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07006898 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006899 btrfs_super_total_bytes(fs_info->super_copy),
6900 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07006901 ret = -EINVAL;
6902 goto error;
6903 }
Chris Mason0b86a832008-03-24 15:01:56 -04006904 ret = 0;
6905error:
David Sterba34441362016-10-04 19:34:27 +02006906 mutex_unlock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08006907 mutex_unlock(&uuid_mutex);
6908
Yan Zheng2b820322008-11-17 21:11:30 -05006909 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006910 return ret;
6911}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006912
Miao Xiecb517ea2013-05-15 07:48:19 +00006913void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6914{
6915 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6916 struct btrfs_device *device;
6917
Liu Bo29cc83f2014-05-11 23:14:59 +08006918 while (fs_devices) {
6919 mutex_lock(&fs_devices->device_list_mutex);
6920 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04006921 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08006922 mutex_unlock(&fs_devices->device_list_mutex);
6923
6924 fs_devices = fs_devices->seed;
6925 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006926}
6927
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006928static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6929{
6930 int i;
6931
6932 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6933 btrfs_dev_stat_reset(dev, i);
6934}
6935
6936int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6937{
6938 struct btrfs_key key;
6939 struct btrfs_key found_key;
6940 struct btrfs_root *dev_root = fs_info->dev_root;
6941 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6942 struct extent_buffer *eb;
6943 int slot;
6944 int ret = 0;
6945 struct btrfs_device *device;
6946 struct btrfs_path *path = NULL;
6947 int i;
6948
6949 path = btrfs_alloc_path();
6950 if (!path) {
6951 ret = -ENOMEM;
6952 goto out;
6953 }
6954
6955 mutex_lock(&fs_devices->device_list_mutex);
6956 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6957 int item_size;
6958 struct btrfs_dev_stats_item *ptr;
6959
David Sterba242e2952016-01-25 17:51:31 +01006960 key.objectid = BTRFS_DEV_STATS_OBJECTID;
6961 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006962 key.offset = device->devid;
6963 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6964 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006965 __btrfs_reset_dev_stats(device);
6966 device->dev_stats_valid = 1;
6967 btrfs_release_path(path);
6968 continue;
6969 }
6970 slot = path->slots[0];
6971 eb = path->nodes[0];
6972 btrfs_item_key_to_cpu(eb, &found_key, slot);
6973 item_size = btrfs_item_size_nr(eb, slot);
6974
6975 ptr = btrfs_item_ptr(eb, slot,
6976 struct btrfs_dev_stats_item);
6977
6978 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6979 if (item_size >= (1 + i) * sizeof(__le64))
6980 btrfs_dev_stat_set(device, i,
6981 btrfs_dev_stats_value(eb, ptr, i));
6982 else
6983 btrfs_dev_stat_reset(device, i);
6984 }
6985
6986 device->dev_stats_valid = 1;
6987 btrfs_dev_stat_print_on_load(device);
6988 btrfs_release_path(path);
6989 }
6990 mutex_unlock(&fs_devices->device_list_mutex);
6991
6992out:
6993 btrfs_free_path(path);
6994 return ret < 0 ? ret : 0;
6995}
6996
6997static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006998 struct btrfs_fs_info *fs_info,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006999 struct btrfs_device *device)
7000{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007001 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007002 struct btrfs_path *path;
7003 struct btrfs_key key;
7004 struct extent_buffer *eb;
7005 struct btrfs_dev_stats_item *ptr;
7006 int ret;
7007 int i;
7008
David Sterba242e2952016-01-25 17:51:31 +01007009 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7010 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007011 key.offset = device->devid;
7012
7013 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007014 if (!path)
7015 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007016 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7017 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007018 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007019 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007020 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007021 goto out;
7022 }
7023
7024 if (ret == 0 &&
7025 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7026 /* need to delete old one and insert a new one */
7027 ret = btrfs_del_item(trans, dev_root, path);
7028 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007029 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007030 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007031 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007032 goto out;
7033 }
7034 ret = 1;
7035 }
7036
7037 if (ret == 1) {
7038 /* need to insert a new item */
7039 btrfs_release_path(path);
7040 ret = btrfs_insert_empty_item(trans, dev_root, path,
7041 &key, sizeof(*ptr));
7042 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007043 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007044 "insert dev_stats item for device %s failed %d",
7045 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007046 goto out;
7047 }
7048 }
7049
7050 eb = path->nodes[0];
7051 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7052 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7053 btrfs_set_dev_stats_value(eb, ptr, i,
7054 btrfs_dev_stat_read(device, i));
7055 btrfs_mark_buffer_dirty(eb);
7056
7057out:
7058 btrfs_free_path(path);
7059 return ret;
7060}
7061
7062/*
7063 * called from commit_transaction. Writes all changed device stats to disk.
7064 */
7065int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
7066 struct btrfs_fs_info *fs_info)
7067{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007068 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7069 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007070 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007071 int ret = 0;
7072
7073 mutex_lock(&fs_devices->device_list_mutex);
7074 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08007075 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007076 continue;
7077
Miao Xieaddc3fa2014-07-24 11:37:11 +08007078 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007079 ret = update_dev_stat_item(trans, fs_info, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007080 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007081 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007082 }
7083 mutex_unlock(&fs_devices->device_list_mutex);
7084
7085 return ret;
7086}
7087
Stefan Behrens442a4f62012-05-25 16:06:08 +02007088void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7089{
7090 btrfs_dev_stat_inc(dev, index);
7091 btrfs_dev_stat_print_on_error(dev);
7092}
7093
Eric Sandeen48a3b632013-04-25 20:41:01 +00007094static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007095{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007096 if (!dev->dev_stats_valid)
7097 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007098 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007099 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007100 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007101 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7102 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7103 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007104 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7105 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007106}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007107
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007108static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7109{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007110 int i;
7111
7112 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7113 if (btrfs_dev_stat_read(dev, i) != 0)
7114 break;
7115 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7116 return; /* all values == 0, suppress message */
7117
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007118 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +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 Behrens733f4fb2012-05-25 16:06:10 +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),
7124 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7125 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7126}
7127
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007128int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007129 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007130{
7131 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007132 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007133 int i;
7134
7135 mutex_lock(&fs_devices->device_list_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007136 dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007137 mutex_unlock(&fs_devices->device_list_mutex);
7138
7139 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007140 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007141 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007142 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007143 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007144 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007145 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007146 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7147 if (stats->nr_items > i)
7148 stats->values[i] =
7149 btrfs_dev_stat_read_and_reset(dev, i);
7150 else
7151 btrfs_dev_stat_reset(dev, i);
7152 }
7153 } else {
7154 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7155 if (stats->nr_items > i)
7156 stats->values[i] = btrfs_dev_stat_read(dev, i);
7157 }
7158 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7159 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7160 return 0;
7161}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007162
David Sterbada353f62017-02-14 17:55:53 +01007163void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007164{
7165 struct buffer_head *bh;
7166 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08007167 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007168
Anand Jain12b1c262015-08-14 18:32:59 +08007169 if (!bdev)
7170 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007171
Anand Jain12b1c262015-08-14 18:32:59 +08007172 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7173 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007174
Anand Jain12b1c262015-08-14 18:32:59 +08007175 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7176 continue;
7177
7178 disk_super = (struct btrfs_super_block *)bh->b_data;
7179
7180 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7181 set_buffer_dirty(bh);
7182 sync_dirty_buffer(bh);
7183 brelse(bh);
7184 }
7185
7186 /* Notify udev that device has changed */
7187 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7188
7189 /* Update ctime/mtime for device path for libblkid */
7190 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007191}
Miao Xie935e5cc2014-09-03 21:35:33 +08007192
7193/*
7194 * Update the size of all devices, which is used for writing out the
7195 * super blocks.
7196 */
7197void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
7198{
7199 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7200 struct btrfs_device *curr, *next;
7201
7202 if (list_empty(&fs_devices->resized_devices))
7203 return;
7204
7205 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02007206 mutex_lock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007207 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
7208 resized_list) {
7209 list_del_init(&curr->resized_list);
7210 curr->commit_total_bytes = curr->disk_total_bytes;
7211 }
David Sterba34441362016-10-04 19:34:27 +02007212 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007213 mutex_unlock(&fs_devices->device_list_mutex);
7214}
Miao Xiece7213c2014-09-03 21:35:34 +08007215
7216/* Must be invoked during the transaction commit */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007217void btrfs_update_commit_device_bytes_used(struct btrfs_fs_info *fs_info,
Miao Xiece7213c2014-09-03 21:35:34 +08007218 struct btrfs_transaction *transaction)
7219{
7220 struct extent_map *em;
7221 struct map_lookup *map;
7222 struct btrfs_device *dev;
7223 int i;
7224
7225 if (list_empty(&transaction->pending_chunks))
7226 return;
7227
7228 /* In order to kick the device replace finish process */
David Sterba34441362016-10-04 19:34:27 +02007229 mutex_lock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007230 list_for_each_entry(em, &transaction->pending_chunks, list) {
Jeff Mahoney95617d62015-06-03 10:55:48 -04007231 map = em->map_lookup;
Miao Xiece7213c2014-09-03 21:35:34 +08007232
7233 for (i = 0; i < map->num_stripes; i++) {
7234 dev = map->stripes[i].dev;
7235 dev->commit_bytes_used = dev->bytes_used;
7236 }
7237 }
David Sterba34441362016-10-04 19:34:27 +02007238 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007239}
Anand Jain5a13f432015-03-10 06:38:31 +08007240
7241void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7242{
7243 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7244 while (fs_devices) {
7245 fs_devices->fs_info = fs_info;
7246 fs_devices = fs_devices->seed;
7247 }
7248}
7249
7250void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7251{
7252 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7253 while (fs_devices) {
7254 fs_devices->fs_info = NULL;
7255 fs_devices = fs_devices->seed;
7256 }
7257}