blob: 12bd04a4104fa4afefad2e88ffddfcb69d2f0190 [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 Wenruoa8c93d4e2014-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 Jainf448341a2016-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 Jainf448341a2016-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,
1574 u64 chunk_tree, u64 chunk_objectid,
1575 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001576{
1577 int ret;
1578 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001579 struct btrfs_fs_info *fs_info = device->fs_info;
1580 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001581 struct btrfs_dev_extent *extent;
1582 struct extent_buffer *leaf;
1583 struct btrfs_key key;
1584
Chris Masondfe25022008-05-13 13:46:40 -04001585 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001586 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001587 path = btrfs_alloc_path();
1588 if (!path)
1589 return -ENOMEM;
1590
Chris Mason0b86a832008-03-24 15:01:56 -04001591 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001592 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001593 key.type = BTRFS_DEV_EXTENT_KEY;
1594 ret = btrfs_insert_empty_item(trans, root, path, &key,
1595 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001596 if (ret)
1597 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001598
1599 leaf = path->nodes[0];
1600 extent = btrfs_item_ptr(leaf, path->slots[0],
1601 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001602 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1603 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1604 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1605
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001606 write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid);
Chris Masone17cade2008-04-15 15:41:47 -04001607
Chris Mason0b86a832008-03-24 15:01:56 -04001608 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1609 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001610out:
Chris Mason0b86a832008-03-24 15:01:56 -04001611 btrfs_free_path(path);
1612 return ret;
1613}
1614
Josef Bacik6df9a952013-06-27 13:22:46 -04001615static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001616{
Josef Bacik6df9a952013-06-27 13:22:46 -04001617 struct extent_map_tree *em_tree;
1618 struct extent_map *em;
1619 struct rb_node *n;
1620 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001621
Josef Bacik6df9a952013-06-27 13:22:46 -04001622 em_tree = &fs_info->mapping_tree.map_tree;
1623 read_lock(&em_tree->lock);
1624 n = rb_last(&em_tree->map);
1625 if (n) {
1626 em = rb_entry(n, struct extent_map, rb_node);
1627 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001628 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001629 read_unlock(&em_tree->lock);
1630
Chris Mason0b86a832008-03-24 15:01:56 -04001631 return ret;
1632}
1633
Ilya Dryomov53f10652013-08-12 14:33:01 +03001634static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1635 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001636{
1637 int ret;
1638 struct btrfs_key key;
1639 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001640 struct btrfs_path *path;
1641
Yan Zheng2b820322008-11-17 21:11:30 -05001642 path = btrfs_alloc_path();
1643 if (!path)
1644 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001645
1646 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1647 key.type = BTRFS_DEV_ITEM_KEY;
1648 key.offset = (u64)-1;
1649
Ilya Dryomov53f10652013-08-12 14:33:01 +03001650 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001651 if (ret < 0)
1652 goto error;
1653
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001654 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001655
Ilya Dryomov53f10652013-08-12 14:33:01 +03001656 ret = btrfs_previous_item(fs_info->chunk_root, path,
1657 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001658 BTRFS_DEV_ITEM_KEY);
1659 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001660 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001661 } else {
1662 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1663 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001664 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001665 }
1666 ret = 0;
1667error:
Yan Zheng2b820322008-11-17 21:11:30 -05001668 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001669 return ret;
1670}
1671
1672/*
1673 * the device information is stored in the chunk root
1674 * the btrfs_device struct should be fully filled in
1675 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001676static int btrfs_add_device(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001677 struct btrfs_fs_info *fs_info,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001678 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001679{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001680 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001681 int ret;
1682 struct btrfs_path *path;
1683 struct btrfs_dev_item *dev_item;
1684 struct extent_buffer *leaf;
1685 struct btrfs_key key;
1686 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001687
Chris Mason0b86a832008-03-24 15:01:56 -04001688 path = btrfs_alloc_path();
1689 if (!path)
1690 return -ENOMEM;
1691
Chris Mason0b86a832008-03-24 15:01:56 -04001692 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1693 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001694 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001695
1696 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001697 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001698 if (ret)
1699 goto out;
1700
1701 leaf = path->nodes[0];
1702 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1703
1704 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001705 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001706 btrfs_set_device_type(leaf, dev_item, device->type);
1707 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1708 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1709 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001710 btrfs_set_device_total_bytes(leaf, dev_item,
1711 btrfs_device_get_disk_total_bytes(device));
1712 btrfs_set_device_bytes_used(leaf, dev_item,
1713 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001714 btrfs_set_device_group(leaf, dev_item, 0);
1715 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1716 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001717 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001718
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001719 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001720 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001721 ptr = btrfs_device_fsid(dev_item);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001722 write_extent_buffer(leaf, fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001723 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001724
Yan Zheng2b820322008-11-17 21:11:30 -05001725 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001726out:
1727 btrfs_free_path(path);
1728 return ret;
1729}
Chris Mason8f18cf12008-04-25 16:53:30 -04001730
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001731/*
1732 * Function to update ctime/mtime for a given device path.
1733 * Mainly used for ctime/mtime based probe like libblkid.
1734 */
David Sterbada353f62017-02-14 17:55:53 +01001735static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001736{
1737 struct file *filp;
1738
1739 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001740 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001741 return;
1742 file_update_time(filp);
1743 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001744}
1745
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001746static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
Chris Masona061fc82008-05-07 11:43:44 -04001747 struct btrfs_device *device)
1748{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001749 struct btrfs_root *root = fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001750 int ret;
1751 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001752 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001753 struct btrfs_trans_handle *trans;
1754
Chris Masona061fc82008-05-07 11:43:44 -04001755 path = btrfs_alloc_path();
1756 if (!path)
1757 return -ENOMEM;
1758
Yan, Zhenga22285a2010-05-16 10:48:46 -04001759 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001760 if (IS_ERR(trans)) {
1761 btrfs_free_path(path);
1762 return PTR_ERR(trans);
1763 }
Chris Masona061fc82008-05-07 11:43:44 -04001764 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1765 key.type = BTRFS_DEV_ITEM_KEY;
1766 key.offset = device->devid;
1767
1768 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1769 if (ret < 0)
1770 goto out;
1771
1772 if (ret > 0) {
1773 ret = -ENOENT;
1774 goto out;
1775 }
1776
1777 ret = btrfs_del_item(trans, root, path);
1778 if (ret)
1779 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001780out:
1781 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001782 btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001783 return ret;
1784}
1785
David Sterba3cc31a02016-02-15 16:00:26 +01001786/*
1787 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1788 * filesystem. It's up to the caller to adjust that number regarding eg. device
1789 * replace.
1790 */
1791static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1792 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001793{
Chris Masona061fc82008-05-07 11:43:44 -04001794 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00001795 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01001796 int i;
Chris Masona061fc82008-05-07 11:43:44 -04001797
Miao Xiede98ced2013-01-29 10:13:12 +00001798 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001799 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00001800
Anand Jainbd45ffb2016-02-13 10:01:34 +08001801 all_avail = fs_info->avail_data_alloc_bits |
1802 fs_info->avail_system_alloc_bits |
1803 fs_info->avail_metadata_alloc_bits;
1804 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08001805
David Sterba418775a2016-02-15 16:28:14 +01001806 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1807 if (!(all_avail & btrfs_raid_group[i]))
1808 continue;
Chris Masona061fc82008-05-07 11:43:44 -04001809
David Sterba418775a2016-02-15 16:28:14 +01001810 if (num_devices < btrfs_raid_array[i].devs_min) {
1811 int ret = btrfs_raid_mindev_error[i];
Chris Masona061fc82008-05-07 11:43:44 -04001812
David Sterba418775a2016-02-15 16:28:14 +01001813 if (ret)
1814 return ret;
1815 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08001816 }
1817
1818 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001819}
1820
Anand Jain88acff62016-05-03 17:44:43 +08001821struct btrfs_device *btrfs_find_next_active_device(struct btrfs_fs_devices *fs_devs,
1822 struct btrfs_device *device)
1823{
1824 struct btrfs_device *next_device;
1825
1826 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1827 if (next_device != device &&
1828 !next_device->missing && next_device->bdev)
1829 return next_device;
1830 }
1831
1832 return NULL;
1833}
1834
1835/*
1836 * Helper function to check if the given device is part of s_bdev / latest_bdev
1837 * and replace it with the provided or the next active device, in the context
1838 * where this function called, there should be always be another device (or
1839 * this_dev) which is active.
1840 */
1841void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info,
1842 struct btrfs_device *device, struct btrfs_device *this_dev)
1843{
1844 struct btrfs_device *next_device;
1845
1846 if (this_dev)
1847 next_device = this_dev;
1848 else
1849 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1850 device);
1851 ASSERT(next_device);
1852
1853 if (fs_info->sb->s_bdev &&
1854 (fs_info->sb->s_bdev == device->bdev))
1855 fs_info->sb->s_bdev = next_device->bdev;
1856
1857 if (fs_info->fs_devices->latest_bdev == device->bdev)
1858 fs_info->fs_devices->latest_bdev = next_device->bdev;
1859}
1860
David Sterbada353f62017-02-14 17:55:53 +01001861int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
1862 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08001863{
1864 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001865 struct btrfs_fs_devices *cur_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001866 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001867 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001868
1869 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001870
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001871 num_devices = fs_info->fs_devices->num_devices;
1872 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
1873 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
Chris Masona061fc82008-05-07 11:43:44 -04001874 WARN_ON(num_devices < 1);
1875 num_devices--;
1876 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001877 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Chris Masona061fc82008-05-07 11:43:44 -04001878
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001879 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08001880 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001881 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001882
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001883 ret = btrfs_find_device_by_devspec(fs_info, devid, device_path,
1884 &device);
Anand Jain24fc5722016-02-13 10:01:36 +08001885 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001886 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001887
Stefan Behrens63a212a2012-11-05 18:29:28 +01001888 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001889 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08001890 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001891 }
1892
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001893 if (device->writeable && fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001894 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08001895 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001896 }
1897
1898 if (device->writeable) {
David Sterba34441362016-10-04 19:34:27 +02001899 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001900 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001901 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02001902 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001903 }
Chris Masona061fc82008-05-07 11:43:44 -04001904
Carey Underwoodd7901552013-03-04 16:37:06 -06001905 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001906 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001907 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001908 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001909 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001910
Stefan Behrens63a212a2012-11-05 18:29:28 +01001911 /*
1912 * TODO: the superblock still includes this device in its num_devices
1913 * counter although write_all_supers() is not locked out. This
1914 * could give a filesystem state which requires a degraded mount.
1915 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001916 ret = btrfs_rm_dev_item(fs_info, device);
Chris Masona061fc82008-05-07 11:43:44 -04001917 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001918 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001919
Yan Zheng2b820322008-11-17 21:11:30 -05001920 device->in_fs_metadata = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001921 btrfs_scrub_cancel_dev(fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001922
1923 /*
1924 * the device list mutex makes sure that we don't change
1925 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001926 * the device supers. Whoever is writing all supers, should
1927 * lock the device list mutex before getting the number of
1928 * devices in the super block (super_copy). Conversely,
1929 * whoever updates the number of devices in the super block
1930 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001931 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001932
1933 cur_devices = device->fs_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001934 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001935 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001936
Yan Zhenge4404d62008-12-12 10:03:26 -05001937 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001938 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001939
Chris Masoncd02dca2010-12-13 14:56:23 -05001940 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001941 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001942
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001943 btrfs_assign_next_active_device(fs_info, device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05001944
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001945 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001946 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001947 /* remove sysfs entry */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001948 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001949 }
Anand Jain99994cd2014-06-03 11:36:00 +08001950
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001951 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
1952 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
1953 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001954
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04001955 /*
1956 * at this point, the device is zero sized and detached from
1957 * the devices list. All that's left is to zero out the old
1958 * supers and free the device.
1959 */
1960 if (device->writeable)
1961 btrfs_scratch_superblocks(device->bdev, device->name->str);
1962
1963 btrfs_close_bdev(device);
1964 call_rcu(&device->rcu, free_device);
1965
Xiao Guangrong1f781602011-04-20 10:09:16 +00001966 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001967 struct btrfs_fs_devices *fs_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001968 fs_devices = fs_info->fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001969 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001970 if (fs_devices->seed == cur_devices) {
1971 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001972 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001973 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001974 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001975 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001976 cur_devices->seed = NULL;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001977 __btrfs_close_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001978 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001979 }
1980
Chris Masona061fc82008-05-07 11:43:44 -04001981out:
1982 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001983 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08001984
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001985error_undo:
1986 if (device->writeable) {
David Sterba34441362016-10-04 19:34:27 +02001987 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001988 list_add(&device->dev_alloc_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001989 &fs_info->fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001990 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02001991 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001992 }
Anand Jain24fc5722016-02-13 10:01:36 +08001993 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001994}
1995
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001996void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
1997 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01001998{
Anand Jaind51908c2014-08-13 14:24:19 +08001999 struct btrfs_fs_devices *fs_devices;
2000
Stefan Behrense93c89c2012-11-05 17:33:06 +01002001 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03002002
Anand Jain25e8e912014-08-20 10:56:56 +08002003 /*
2004 * in case of fs with no seed, srcdev->fs_devices will point
2005 * to fs_devices of fs_info. However when the dev being replaced is
2006 * a seed dev it will point to the seed's local fs_devices. In short
2007 * srcdev will have its correct fs_devices in both the cases.
2008 */
2009 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002010
Stefan Behrense93c89c2012-11-05 17:33:06 +01002011 list_del_rcu(&srcdev->dev_list);
2012 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002013 fs_devices->num_devices--;
Miao Xie82372bc2014-09-03 21:35:44 +08002014 if (srcdev->missing)
Anand Jaind51908c2014-08-13 14:24:19 +08002015 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002016
Anand Jain48b3b9d2016-04-12 21:36:16 +08002017 if (srcdev->writeable)
Miao Xie82372bc2014-09-03 21:35:44 +08002018 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002019
Miao Xie82372bc2014-09-03 21:35:44 +08002020 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002021 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002022}
2023
2024void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2025 struct btrfs_device *srcdev)
2026{
2027 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002028
Anand Jain48b3b9d2016-04-12 21:36:16 +08002029 if (srcdev->writeable) {
2030 /* zero out the old super if it is writable */
2031 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2032 }
Anand Jain14238812016-07-22 06:04:53 +08002033
2034 btrfs_close_bdev(srcdev);
2035
Stefan Behrense93c89c2012-11-05 17:33:06 +01002036 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002037
2038 /*
2039 * unless fs_devices is seed fs, num_devices shouldn't go
2040 * zero
2041 */
2042 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
2043
2044 /* if this is no devs we rather delete the fs_devices */
2045 if (!fs_devices->num_devices) {
2046 struct btrfs_fs_devices *tmp_fs_devices;
2047
2048 tmp_fs_devices = fs_info->fs_devices;
2049 while (tmp_fs_devices) {
2050 if (tmp_fs_devices->seed == fs_devices) {
2051 tmp_fs_devices->seed = fs_devices->seed;
2052 break;
2053 }
2054 tmp_fs_devices = tmp_fs_devices->seed;
2055 }
2056 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08002057 __btrfs_close_devices(fs_devices);
2058 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002059 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002060}
2061
2062void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2063 struct btrfs_device *tgtdev)
2064{
Miao Xie67a2c452014-09-03 21:35:43 +08002065 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002066 WARN_ON(!tgtdev);
2067 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002068
Anand Jain32576042015-08-14 18:32:49 +08002069 btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002070
Anand Jain779bf3fe2016-04-18 16:51:23 +08002071 if (tgtdev->bdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002072 fs_info->fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002073
Stefan Behrense93c89c2012-11-05 17:33:06 +01002074 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002075
Anand Jain88acff62016-05-03 17:44:43 +08002076 btrfs_assign_next_active_device(fs_info, tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002077
Stefan Behrense93c89c2012-11-05 17:33:06 +01002078 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002079
2080 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08002081 mutex_unlock(&uuid_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002082
2083 /*
2084 * The update_dev_time() with in btrfs_scratch_superblocks()
2085 * may lead to a call to btrfs_show_devname() which will try
2086 * to hold device_list_mutex. And here this device
2087 * is already out of device list, so we don't have to hold
2088 * the device_list_mutex lock.
2089 */
2090 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002091
2092 btrfs_close_bdev(tgtdev);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002093 call_rcu(&tgtdev->rcu, free_device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002094}
2095
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002096static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002097 const char *device_path,
Eric Sandeen48a3b632013-04-25 20:41:01 +00002098 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002099{
2100 int ret = 0;
2101 struct btrfs_super_block *disk_super;
2102 u64 devid;
2103 u8 *dev_uuid;
2104 struct block_device *bdev;
2105 struct buffer_head *bh;
2106
2107 *device = NULL;
2108 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002109 fs_info->bdev_holder, 0, &bdev, &bh);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002110 if (ret)
2111 return ret;
2112 disk_super = (struct btrfs_super_block *)bh->b_data;
2113 devid = btrfs_stack_device_id(&disk_super->dev_item);
2114 dev_uuid = disk_super->dev_item.uuid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002115 *device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002116 brelse(bh);
2117 if (!*device)
2118 ret = -ENOENT;
2119 blkdev_put(bdev, FMODE_READ);
2120 return ret;
2121}
2122
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002123int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002124 const char *device_path,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002125 struct btrfs_device **device)
2126{
2127 *device = NULL;
2128 if (strcmp(device_path, "missing") == 0) {
2129 struct list_head *devices;
2130 struct btrfs_device *tmp;
2131
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002132 devices = &fs_info->fs_devices->devices;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002133 /*
2134 * It is safe to read the devices since the volume_mutex
2135 * is held by the caller.
2136 */
2137 list_for_each_entry(tmp, devices, dev_list) {
2138 if (tmp->in_fs_metadata && !tmp->bdev) {
2139 *device = tmp;
2140 break;
2141 }
2142 }
2143
Anand Jaind74a6252015-08-14 18:32:56 +08002144 if (!*device)
2145 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002146
2147 return 0;
2148 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002149 return btrfs_find_device_by_path(fs_info, device_path, device);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002150 }
2151}
2152
Yan Zheng2b820322008-11-17 21:11:30 -05002153/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002154 * Lookup a device given by device id, or the path if the id is 0.
2155 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002156int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid,
David Sterbada353f62017-02-14 17:55:53 +01002157 const char *devpath,
2158 struct btrfs_device **device)
Anand Jain24e04742016-02-13 10:01:35 +08002159{
2160 int ret;
2161
David Sterba5c5c0df2016-02-15 16:39:55 +01002162 if (devid) {
Anand Jain24e04742016-02-13 10:01:35 +08002163 ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002164 *device = btrfs_find_device(fs_info, devid, NULL, NULL);
Anand Jain24e04742016-02-13 10:01:35 +08002165 if (!*device)
2166 ret = -ENOENT;
2167 } else {
David Sterba5c5c0df2016-02-15 16:39:55 +01002168 if (!devpath || !devpath[0])
Anand Jainb3d1b152016-02-13 10:01:37 +08002169 return -EINVAL;
2170
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002171 ret = btrfs_find_device_missing_or_by_path(fs_info, devpath,
Anand Jain24e04742016-02-13 10:01:35 +08002172 device);
2173 }
2174 return ret;
2175}
2176
Yan Zheng2b820322008-11-17 21:11:30 -05002177/*
2178 * does all the dirty work required for changing file system's UUID.
2179 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002180static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002181{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002182 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002183 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002184 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002185 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002186 struct btrfs_device *device;
2187 u64 super_flags;
2188
2189 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05002190 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002191 return -EINVAL;
2192
David Sterba2dfeca92017-06-14 02:48:07 +02002193 seed_devices = alloc_fs_devices(NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002194 if (IS_ERR(seed_devices))
2195 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002196
Yan Zhenge4404d62008-12-12 10:03:26 -05002197 old_devices = clone_fs_devices(fs_devices);
2198 if (IS_ERR(old_devices)) {
2199 kfree(seed_devices);
2200 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002201 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002202
Yan Zheng2b820322008-11-17 21:11:30 -05002203 list_add(&old_devices->list, &fs_uuids);
2204
Yan Zhenge4404d62008-12-12 10:03:26 -05002205 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2206 seed_devices->opened = 1;
2207 INIT_LIST_HEAD(&seed_devices->devices);
2208 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002209 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002210
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002211 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002212 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2213 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002214 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002215 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002216
David Sterba34441362016-10-04 19:34:27 +02002217 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002218 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002219 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002220
Yan Zheng2b820322008-11-17 21:11:30 -05002221 fs_devices->seeding = 0;
2222 fs_devices->num_devices = 0;
2223 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002224 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002225 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002226 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002227
2228 generate_random_uuid(fs_devices->fsid);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002229 memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002230 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002231 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002232
Yan Zheng2b820322008-11-17 21:11:30 -05002233 super_flags = btrfs_super_flags(disk_super) &
2234 ~BTRFS_SUPER_FLAG_SEEDING;
2235 btrfs_set_super_flags(disk_super, super_flags);
2236
2237 return 0;
2238}
2239
2240/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002241 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002242 */
2243static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002244 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002245{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002246 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002247 struct btrfs_path *path;
2248 struct extent_buffer *leaf;
2249 struct btrfs_dev_item *dev_item;
2250 struct btrfs_device *device;
2251 struct btrfs_key key;
2252 u8 fs_uuid[BTRFS_UUID_SIZE];
2253 u8 dev_uuid[BTRFS_UUID_SIZE];
2254 u64 devid;
2255 int ret;
2256
2257 path = btrfs_alloc_path();
2258 if (!path)
2259 return -ENOMEM;
2260
Yan Zheng2b820322008-11-17 21:11:30 -05002261 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2262 key.offset = 0;
2263 key.type = BTRFS_DEV_ITEM_KEY;
2264
2265 while (1) {
2266 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2267 if (ret < 0)
2268 goto error;
2269
2270 leaf = path->nodes[0];
2271next_slot:
2272 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2273 ret = btrfs_next_leaf(root, path);
2274 if (ret > 0)
2275 break;
2276 if (ret < 0)
2277 goto error;
2278 leaf = path->nodes[0];
2279 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002280 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002281 continue;
2282 }
2283
2284 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2285 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2286 key.type != BTRFS_DEV_ITEM_KEY)
2287 break;
2288
2289 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2290 struct btrfs_dev_item);
2291 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002292 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002293 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002294 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002295 BTRFS_UUID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002296 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002297 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002298
2299 if (device->fs_devices->seeding) {
2300 btrfs_set_device_generation(leaf, dev_item,
2301 device->generation);
2302 btrfs_mark_buffer_dirty(leaf);
2303 }
2304
2305 path->slots[0]++;
2306 goto next_slot;
2307 }
2308 ret = 0;
2309error:
2310 btrfs_free_path(path);
2311 return ret;
2312}
2313
David Sterbada353f62017-02-14 17:55:53 +01002314int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002315{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002316 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002317 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002318 struct btrfs_trans_handle *trans;
2319 struct btrfs_device *device;
2320 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002321 struct list_head *devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002322 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002323 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002324 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002325 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002326 int ret = 0;
2327
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002328 if ((sb->s_flags & MS_RDONLY) && !fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002329 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002330
Li Zefana5d163332011-12-07 20:08:40 -05002331 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002332 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002333 if (IS_ERR(bdev))
2334 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002335
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002336 if (fs_info->fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002337 seeding_dev = 1;
2338 down_write(&sb->s_umount);
2339 mutex_lock(&uuid_mutex);
2340 }
2341
Chris Mason8c8bee12008-09-29 11:19:10 -04002342 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002343
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002344 devices = &fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002345
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002346 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002347 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002348 if (device->bdev == bdev) {
2349 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002350 mutex_unlock(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002351 &fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002352 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002353 }
2354 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002355 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002356
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002357 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002358 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002359 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002360 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002361 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002362 }
2363
David Sterba78f2c9e2016-02-11 14:25:38 +01002364 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002365 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002366 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002367 ret = -ENOMEM;
2368 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002369 }
Josef Bacik606686e2012-06-04 14:03:51 -04002370 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002371
Yan, Zhenga22285a2010-05-16 10:48:46 -04002372 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002373 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002374 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002375 kfree(device);
2376 ret = PTR_ERR(trans);
2377 goto error;
2378 }
2379
Josef Bacikd5e20032011-08-04 14:52:27 +00002380 q = bdev_get_queue(bdev);
2381 if (blk_queue_discard(q))
2382 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002383 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002384 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002385 device->io_width = fs_info->sectorsize;
2386 device->io_align = fs_info->sectorsize;
2387 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002388 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2389 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002390 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002391 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002392 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002393 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002394 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002395 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002396 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002397 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002398 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002399
Yan Zheng2b820322008-11-17 21:11:30 -05002400 if (seeding_dev) {
2401 sb->s_flags &= ~MS_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002402 ret = btrfs_prepare_sprout(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002403 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002404 }
2405
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002406 device->fs_devices = fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002407
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002408 mutex_lock(&fs_info->fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002409 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002410 list_add_rcu(&device->dev_list, &fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002411 list_add(&device->dev_alloc_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002412 &fs_info->fs_devices->alloc_list);
2413 fs_info->fs_devices->num_devices++;
2414 fs_info->fs_devices->open_devices++;
2415 fs_info->fs_devices->rw_devices++;
2416 fs_info->fs_devices->total_devices++;
2417 fs_info->fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002418
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002419 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002420
Anand Jaine884f4f2017-04-04 18:40:19 +08002421 if (!blk_queue_nonrot(q))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002422 fs_info->fs_devices->rotating = 1;
Chris Masonc2898112009-06-10 09:51:32 -04002423
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002424 tmp = btrfs_super_total_bytes(fs_info->super_copy);
2425 btrfs_set_super_total_bytes(fs_info->super_copy,
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002426 round_down(tmp + device->total_bytes, fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002427
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002428 tmp = btrfs_super_num_devices(fs_info->super_copy);
2429 btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002430
2431 /* add sysfs device entry */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002432 btrfs_sysfs_add_device_link(fs_info->fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002433
Miao Xie2196d6e2014-09-03 21:35:41 +08002434 /*
2435 * we've got more storage, clear any full flags on the space
2436 * infos
2437 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002438 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002439
David Sterba34441362016-10-04 19:34:27 +02002440 mutex_unlock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002441 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002442
Yan Zheng2b820322008-11-17 21:11:30 -05002443 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002444 mutex_lock(&fs_info->chunk_mutex);
David Sterbae4a4dce2017-02-10 19:49:01 +01002445 ret = init_first_rw_device(trans, fs_info);
David Sterba34441362016-10-04 19:34:27 +02002446 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002447 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002448 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002449 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002450 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002451 }
2452
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002453 ret = btrfs_add_device(trans, fs_info, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002454 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002455 btrfs_abort_transaction(trans, ret);
Miao Xie2196d6e2014-09-03 21:35:41 +08002456 goto error_trans;
2457 }
2458
2459 if (seeding_dev) {
2460 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2461
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002462 ret = btrfs_finish_sprout(trans, fs_info);
David Sterba005d6422012-09-18 07:52:32 -06002463 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002464 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002465 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002466 }
Anand Jainb2373f22014-06-03 11:36:03 +08002467
2468 /* Sprouting would change fsid of the mounted root,
2469 * so rename the fsid on the sysfs
2470 */
2471 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002472 fs_info->fsid);
2473 if (kobject_rename(&fs_info->fs_devices->fsid_kobj, fsid_buf))
2474 btrfs_warn(fs_info,
2475 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002476 }
2477
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002478 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002479
2480 if (seeding_dev) {
2481 mutex_unlock(&uuid_mutex);
2482 up_write(&sb->s_umount);
2483
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002484 if (ret) /* transaction commit */
2485 return ret;
2486
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002487 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002488 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002489 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002490 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002491 trans = btrfs_attach_transaction(root);
2492 if (IS_ERR(trans)) {
2493 if (PTR_ERR(trans) == -ENOENT)
2494 return 0;
2495 return PTR_ERR(trans);
2496 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002497 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002498 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002499
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002500 /* Update ctime/mtime for libblkid */
2501 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002502 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002503
2504error_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002505 btrfs_end_transaction(trans);
Josef Bacik606686e2012-06-04 14:03:51 -04002506 rcu_string_free(device->name);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002507 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002508 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002509error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002510 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002511 if (seeding_dev) {
2512 mutex_unlock(&uuid_mutex);
2513 up_write(&sb->s_umount);
2514 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002515 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002516}
2517
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002518int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002519 const char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002520 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002521 struct btrfs_device **device_out)
2522{
2523 struct request_queue *q;
2524 struct btrfs_device *device;
2525 struct block_device *bdev;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002526 struct list_head *devices;
2527 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002528 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002529 int ret = 0;
2530
2531 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002532 if (fs_info->fs_devices->seeding) {
2533 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002534 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002535 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002536
2537 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2538 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002539 if (IS_ERR(bdev)) {
2540 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002541 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002542 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002543
2544 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2545
2546 devices = &fs_info->fs_devices->devices;
2547 list_for_each_entry(device, devices, dev_list) {
2548 if (device->bdev == bdev) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002549 btrfs_err(fs_info,
2550 "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002551 ret = -EEXIST;
2552 goto error;
2553 }
2554 }
2555
Miao Xie1c433662014-09-03 21:35:32 +08002556
Miao Xie7cc8e582014-09-03 21:35:38 +08002557 if (i_size_read(bdev->bd_inode) <
2558 btrfs_device_get_total_bytes(srcdev)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002559 btrfs_err(fs_info,
2560 "target device is smaller than source device!");
Miao Xie1c433662014-09-03 21:35:32 +08002561 ret = -EINVAL;
2562 goto error;
2563 }
2564
2565
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002566 device = btrfs_alloc_device(NULL, &devid, NULL);
2567 if (IS_ERR(device)) {
2568 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002569 goto error;
2570 }
2571
David Sterba61655722017-06-15 17:16:43 +02002572 name = rcu_string_strdup(device_path, GFP_KERNEL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002573 if (!name) {
2574 kfree(device);
2575 ret = -ENOMEM;
2576 goto error;
2577 }
2578 rcu_assign_pointer(device->name, name);
2579
2580 q = bdev_get_queue(bdev);
2581 if (blk_queue_discard(q))
2582 device->can_discard = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002583 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002584 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002585 device->generation = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002586 device->io_width = fs_info->sectorsize;
2587 device->io_align = fs_info->sectorsize;
2588 device->sector_size = fs_info->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002589 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2590 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2591 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002592 ASSERT(list_empty(&srcdev->resized_list));
2593 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002594 device->commit_bytes_used = device->bytes_used;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002595 device->fs_info = fs_info;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002596 device->bdev = bdev;
2597 device->in_fs_metadata = 1;
2598 device->is_tgtdev_for_dev_replace = 1;
2599 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002600 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002601 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002602 device->fs_devices = fs_info->fs_devices;
2603 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2604 fs_info->fs_devices->num_devices++;
2605 fs_info->fs_devices->open_devices++;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002606 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002607
2608 *device_out = device;
2609 return ret;
2610
2611error:
2612 blkdev_put(bdev, FMODE_EXCL);
2613 return ret;
2614}
2615
2616void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2617 struct btrfs_device *tgtdev)
2618{
Jeff Mahoneyda170662016-06-15 09:22:56 -04002619 u32 sectorsize = fs_info->sectorsize;
2620
Stefan Behrense93c89c2012-11-05 17:33:06 +01002621 WARN_ON(fs_info->fs_devices->rw_devices == 0);
Jeff Mahoneyda170662016-06-15 09:22:56 -04002622 tgtdev->io_width = sectorsize;
2623 tgtdev->io_align = sectorsize;
2624 tgtdev->sector_size = sectorsize;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002625 tgtdev->fs_info = fs_info;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002626 tgtdev->in_fs_metadata = 1;
2627}
2628
Chris Masond3977122009-01-05 21:25:51 -05002629static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2630 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002631{
2632 int ret;
2633 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002634 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002635 struct btrfs_dev_item *dev_item;
2636 struct extent_buffer *leaf;
2637 struct btrfs_key key;
2638
Chris Mason0b86a832008-03-24 15:01:56 -04002639 path = btrfs_alloc_path();
2640 if (!path)
2641 return -ENOMEM;
2642
2643 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2644 key.type = BTRFS_DEV_ITEM_KEY;
2645 key.offset = device->devid;
2646
2647 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2648 if (ret < 0)
2649 goto out;
2650
2651 if (ret > 0) {
2652 ret = -ENOENT;
2653 goto out;
2654 }
2655
2656 leaf = path->nodes[0];
2657 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2658
2659 btrfs_set_device_id(leaf, dev_item, device->devid);
2660 btrfs_set_device_type(leaf, dev_item, device->type);
2661 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2662 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2663 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002664 btrfs_set_device_total_bytes(leaf, dev_item,
2665 btrfs_device_get_disk_total_bytes(device));
2666 btrfs_set_device_bytes_used(leaf, dev_item,
2667 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002668 btrfs_mark_buffer_dirty(leaf);
2669
2670out:
2671 btrfs_free_path(path);
2672 return ret;
2673}
2674
Miao Xie2196d6e2014-09-03 21:35:41 +08002675int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002676 struct btrfs_device *device, u64 new_size)
2677{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002678 struct btrfs_fs_info *fs_info = device->fs_info;
2679 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002680 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002681 u64 old_total;
2682 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002683
Yan Zheng2b820322008-11-17 21:11:30 -05002684 if (!device->writeable)
2685 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002686
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002687 new_size = round_down(new_size, fs_info->sectorsize);
2688
David Sterba34441362016-10-04 19:34:27 +02002689 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002690 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002691 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002692
Stefan Behrens63a212a2012-11-05 18:29:28 +01002693 if (new_size <= device->total_bytes ||
Miao Xie2196d6e2014-09-03 21:35:41 +08002694 device->is_tgtdev_for_dev_replace) {
David Sterba34441362016-10-04 19:34:27 +02002695 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002696 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002697 }
Yan Zheng2b820322008-11-17 21:11:30 -05002698
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002699 fs_devices = fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002700
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002701 btrfs_set_super_total_bytes(super_copy,
2702 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002703 device->fs_devices->total_rw_bytes += diff;
2704
Miao Xie7cc8e582014-09-03 21:35:38 +08002705 btrfs_device_set_total_bytes(device, new_size);
2706 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002707 btrfs_clear_space_info_full(device->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002708 if (list_empty(&device->resized_list))
2709 list_add_tail(&device->resized_list,
2710 &fs_devices->resized_devices);
David Sterba34441362016-10-04 19:34:27 +02002711 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002712
Chris Mason8f18cf12008-04-25 16:53:30 -04002713 return btrfs_update_device(trans, device);
2714}
2715
2716static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002717 struct btrfs_fs_info *fs_info, u64 chunk_objectid,
Chris Mason8f18cf12008-04-25 16:53:30 -04002718 u64 chunk_offset)
2719{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002720 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002721 int ret;
2722 struct btrfs_path *path;
2723 struct btrfs_key key;
2724
Chris Mason8f18cf12008-04-25 16:53:30 -04002725 path = btrfs_alloc_path();
2726 if (!path)
2727 return -ENOMEM;
2728
2729 key.objectid = chunk_objectid;
2730 key.offset = chunk_offset;
2731 key.type = BTRFS_CHUNK_ITEM_KEY;
2732
2733 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002734 if (ret < 0)
2735 goto out;
2736 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002737 btrfs_handle_fs_error(fs_info, -ENOENT,
2738 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002739 ret = -ENOENT;
2740 goto out;
2741 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002742
2743 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002744 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002745 btrfs_handle_fs_error(fs_info, ret,
2746 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002747out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002748 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002749 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002750}
2751
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002752static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info,
2753 u64 chunk_objectid, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002754{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002755 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002756 struct btrfs_disk_key *disk_key;
2757 struct btrfs_chunk *chunk;
2758 u8 *ptr;
2759 int ret = 0;
2760 u32 num_stripes;
2761 u32 array_size;
2762 u32 len = 0;
2763 u32 cur;
2764 struct btrfs_key key;
2765
David Sterba34441362016-10-04 19:34:27 +02002766 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002767 array_size = btrfs_super_sys_array_size(super_copy);
2768
2769 ptr = super_copy->sys_chunk_array;
2770 cur = 0;
2771
2772 while (cur < array_size) {
2773 disk_key = (struct btrfs_disk_key *)ptr;
2774 btrfs_disk_key_to_cpu(&key, disk_key);
2775
2776 len = sizeof(*disk_key);
2777
2778 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2779 chunk = (struct btrfs_chunk *)(ptr + len);
2780 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2781 len += btrfs_chunk_item_size(num_stripes);
2782 } else {
2783 ret = -EIO;
2784 break;
2785 }
2786 if (key.objectid == chunk_objectid &&
2787 key.offset == chunk_offset) {
2788 memmove(ptr, ptr + len, array_size - (cur + len));
2789 array_size -= len;
2790 btrfs_set_super_sys_array_size(super_copy, array_size);
2791 } else {
2792 ptr += len;
2793 cur += len;
2794 }
2795 }
David Sterba34441362016-10-04 19:34:27 +02002796 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002797 return ret;
2798}
2799
Liu Bo592d92e2017-03-14 13:33:55 -07002800static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info,
2801 u64 logical, u64 length)
2802{
2803 struct extent_map_tree *em_tree;
2804 struct extent_map *em;
2805
2806 em_tree = &fs_info->mapping_tree.map_tree;
2807 read_lock(&em_tree->lock);
2808 em = lookup_extent_mapping(em_tree, logical, length);
2809 read_unlock(&em_tree->lock);
2810
2811 if (!em) {
2812 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2813 logical, length);
2814 return ERR_PTR(-EINVAL);
2815 }
2816
2817 if (em->start > logical || em->start + em->len < logical) {
2818 btrfs_crit(fs_info,
2819 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2820 logical, length, em->start, em->start + em->len);
2821 free_extent_map(em);
2822 return ERR_PTR(-EINVAL);
2823 }
2824
2825 /* callers are responsible for dropping em's ref. */
2826 return em;
2827}
2828
Josef Bacik47ab2a62014-09-18 11:20:02 -04002829int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002830 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04002831{
Josef Bacik47ab2a62014-09-18 11:20:02 -04002832 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002833 struct map_lookup *map;
2834 u64 dev_extent_len = 0;
2835 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002836 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002837 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002838
Liu Bo592d92e2017-03-14 13:33:55 -07002839 em = get_chunk_map(fs_info, chunk_offset, 1);
2840 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04002841 /*
2842 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002843 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002844 * do anything we still error out.
2845 */
2846 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07002847 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002848 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002849 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02002850 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002851 check_system_chunk(trans, fs_info, map->type);
David Sterba34441362016-10-04 19:34:27 +02002852 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002853
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002854 /*
2855 * Take the device list mutex to prevent races with the final phase of
2856 * a device replace operation that replaces the device object associated
2857 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2858 */
2859 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002860 for (i = 0; i < map->num_stripes; i++) {
2861 struct btrfs_device *device = map->stripes[i].dev;
2862 ret = btrfs_free_dev_extent(trans, device,
2863 map->stripes[i].physical,
2864 &dev_extent_len);
2865 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002866 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002867 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002868 goto out;
2869 }
2870
2871 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02002872 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002873 btrfs_device_set_bytes_used(device,
2874 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002875 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002876 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02002877 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002878 }
2879
2880 if (map->stripes[i].dev) {
2881 ret = btrfs_update_device(trans, map->stripes[i].dev);
2882 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002883 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002884 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002885 goto out;
2886 }
2887 }
2888 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002889 mutex_unlock(&fs_devices->device_list_mutex);
2890
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002891 ret = btrfs_free_chunk(trans, fs_info, chunk_objectid, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002892 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002893 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002894 goto out;
2895 }
2896
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002897 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002898
2899 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002900 ret = btrfs_del_sys_chunk(fs_info, chunk_objectid,
2901 chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002902 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002903 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002904 goto out;
2905 }
2906 }
2907
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002908 ret = btrfs_remove_block_group(trans, fs_info, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002909 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002910 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002911 goto out;
2912 }
2913
Josef Bacik47ab2a62014-09-18 11:20:02 -04002914out:
2915 /* once for us */
2916 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002917 return ret;
2918}
2919
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002920static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002921{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002922 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07002923 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002924 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002925
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002926 /*
2927 * Prevent races with automatic removal of unused block groups.
2928 * After we relocate and before we remove the chunk with offset
2929 * chunk_offset, automatic removal of the block group can kick in,
2930 * resulting in a failure when calling btrfs_remove_chunk() below.
2931 *
2932 * Make sure to acquire this mutex before doing a tree search (dev
2933 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2934 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2935 * we release the path used to search the chunk/dev tree and before
2936 * the current task acquires this mutex and calls us.
2937 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002938 ASSERT(mutex_is_locked(&fs_info->delete_unused_bgs_mutex));
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002939
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002940 ret = btrfs_can_relocate(fs_info, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002941 if (ret)
2942 return -ENOSPC;
2943
Chris Mason8f18cf12008-04-25 16:53:30 -04002944 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002945 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002946 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002947 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002948 if (ret)
2949 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002950
Chris Mason19c4d2f2016-10-10 13:43:31 -07002951 trans = btrfs_start_trans_remove_block_group(root->fs_info,
2952 chunk_offset);
2953 if (IS_ERR(trans)) {
2954 ret = PTR_ERR(trans);
2955 btrfs_handle_fs_error(root->fs_info, ret, NULL);
2956 return ret;
2957 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09002958
Chris Mason19c4d2f2016-10-10 13:43:31 -07002959 /*
2960 * step two, delete the device extents and the
2961 * chunk tree entries
2962 */
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002963 ret = btrfs_remove_chunk(trans, fs_info, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002964 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07002965 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002966}
2967
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002968static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002969{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002970 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002971 struct btrfs_path *path;
2972 struct extent_buffer *leaf;
2973 struct btrfs_chunk *chunk;
2974 struct btrfs_key key;
2975 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05002976 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002977 bool retried = false;
2978 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002979 int ret;
2980
2981 path = btrfs_alloc_path();
2982 if (!path)
2983 return -ENOMEM;
2984
Josef Bacikba1bf482009-09-11 16:11:19 -04002985again:
Yan Zheng2b820322008-11-17 21:11:30 -05002986 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2987 key.offset = (u64)-1;
2988 key.type = BTRFS_CHUNK_ITEM_KEY;
2989
2990 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002991 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002992 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002993 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002994 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002995 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002996 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002997 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002998
2999 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3000 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003001 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003002 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003003 if (ret < 0)
3004 goto error;
3005 if (ret > 0)
3006 break;
3007
3008 leaf = path->nodes[0];
3009 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3010
3011 chunk = btrfs_item_ptr(leaf, path->slots[0],
3012 struct btrfs_chunk);
3013 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003014 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003015
3016 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003017 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003018 if (ret == -ENOSPC)
3019 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303020 else
3021 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003022 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003023 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003024
3025 if (found_key.offset == 0)
3026 break;
3027 key.offset = found_key.offset - 1;
3028 }
3029 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003030 if (failed && !retried) {
3031 failed = 0;
3032 retried = true;
3033 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303034 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003035 ret = -ENOSPC;
3036 }
Yan Zheng2b820322008-11-17 21:11:30 -05003037error:
3038 btrfs_free_path(path);
3039 return ret;
3040}
3041
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003042static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003043 struct btrfs_balance_control *bctl)
3044{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003045 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003046 struct btrfs_trans_handle *trans;
3047 struct btrfs_balance_item *item;
3048 struct btrfs_disk_balance_args disk_bargs;
3049 struct btrfs_path *path;
3050 struct extent_buffer *leaf;
3051 struct btrfs_key key;
3052 int ret, err;
3053
3054 path = btrfs_alloc_path();
3055 if (!path)
3056 return -ENOMEM;
3057
3058 trans = btrfs_start_transaction(root, 0);
3059 if (IS_ERR(trans)) {
3060 btrfs_free_path(path);
3061 return PTR_ERR(trans);
3062 }
3063
3064 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003065 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003066 key.offset = 0;
3067
3068 ret = btrfs_insert_empty_item(trans, root, path, &key,
3069 sizeof(*item));
3070 if (ret)
3071 goto out;
3072
3073 leaf = path->nodes[0];
3074 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3075
David Sterbab159fa22016-11-08 18:09:03 +01003076 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003077
3078 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3079 btrfs_set_balance_data(leaf, item, &disk_bargs);
3080 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3081 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3082 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3083 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3084
3085 btrfs_set_balance_flags(leaf, item, bctl->flags);
3086
3087 btrfs_mark_buffer_dirty(leaf);
3088out:
3089 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003090 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003091 if (err && !ret)
3092 ret = err;
3093 return ret;
3094}
3095
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003096static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003097{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003098 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003099 struct btrfs_trans_handle *trans;
3100 struct btrfs_path *path;
3101 struct btrfs_key key;
3102 int ret, err;
3103
3104 path = btrfs_alloc_path();
3105 if (!path)
3106 return -ENOMEM;
3107
3108 trans = btrfs_start_transaction(root, 0);
3109 if (IS_ERR(trans)) {
3110 btrfs_free_path(path);
3111 return PTR_ERR(trans);
3112 }
3113
3114 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003115 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003116 key.offset = 0;
3117
3118 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3119 if (ret < 0)
3120 goto out;
3121 if (ret > 0) {
3122 ret = -ENOENT;
3123 goto out;
3124 }
3125
3126 ret = btrfs_del_item(trans, root, path);
3127out:
3128 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003129 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003130 if (err && !ret)
3131 ret = err;
3132 return ret;
3133}
3134
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003135/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003136 * This is a heuristic used to reduce the number of chunks balanced on
3137 * resume after balance was interrupted.
3138 */
3139static void update_balance_args(struct btrfs_balance_control *bctl)
3140{
3141 /*
3142 * Turn on soft mode for chunk types that were being converted.
3143 */
3144 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3145 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3146 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3147 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3148 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3149 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3150
3151 /*
3152 * Turn on usage filter if is not already used. The idea is
3153 * that chunks that we have already balanced should be
3154 * reasonably full. Don't do it for chunks that are being
3155 * converted - that will keep us from relocating unconverted
3156 * (albeit full) chunks.
3157 */
3158 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003159 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003160 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3161 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3162 bctl->data.usage = 90;
3163 }
3164 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003165 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003166 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3167 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3168 bctl->sys.usage = 90;
3169 }
3170 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003171 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003172 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3173 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3174 bctl->meta.usage = 90;
3175 }
3176}
3177
3178/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003179 * Should be called with both balance and volume mutexes held to
3180 * serialize other volume operations (add_dev/rm_dev/resize) with
3181 * restriper. Same goes for unset_balance_control.
3182 */
3183static void set_balance_control(struct btrfs_balance_control *bctl)
3184{
3185 struct btrfs_fs_info *fs_info = bctl->fs_info;
3186
3187 BUG_ON(fs_info->balance_ctl);
3188
3189 spin_lock(&fs_info->balance_lock);
3190 fs_info->balance_ctl = bctl;
3191 spin_unlock(&fs_info->balance_lock);
3192}
3193
3194static void unset_balance_control(struct btrfs_fs_info *fs_info)
3195{
3196 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3197
3198 BUG_ON(!fs_info->balance_ctl);
3199
3200 spin_lock(&fs_info->balance_lock);
3201 fs_info->balance_ctl = NULL;
3202 spin_unlock(&fs_info->balance_lock);
3203
3204 kfree(bctl);
3205}
3206
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003207/*
3208 * Balance filters. Return 1 if chunk should be filtered out
3209 * (should not be balanced).
3210 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003211static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003212 struct btrfs_balance_args *bargs)
3213{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003214 chunk_type = chunk_to_extended(chunk_type) &
3215 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003216
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003217 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003218 return 0;
3219
3220 return 1;
3221}
3222
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003223static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003224 struct btrfs_balance_args *bargs)
3225{
3226 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003227 u64 chunk_used;
3228 u64 user_thresh_min;
3229 u64 user_thresh_max;
3230 int ret = 1;
3231
3232 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3233 chunk_used = btrfs_block_group_used(&cache->item);
3234
3235 if (bargs->usage_min == 0)
3236 user_thresh_min = 0;
3237 else
3238 user_thresh_min = div_factor_fine(cache->key.offset,
3239 bargs->usage_min);
3240
3241 if (bargs->usage_max == 0)
3242 user_thresh_max = 1;
3243 else if (bargs->usage_max > 100)
3244 user_thresh_max = cache->key.offset;
3245 else
3246 user_thresh_max = div_factor_fine(cache->key.offset,
3247 bargs->usage_max);
3248
3249 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3250 ret = 0;
3251
3252 btrfs_put_block_group(cache);
3253 return ret;
3254}
3255
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003256static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003257 u64 chunk_offset, struct btrfs_balance_args *bargs)
3258{
3259 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003260 u64 chunk_used, user_thresh;
3261 int ret = 1;
3262
3263 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3264 chunk_used = btrfs_block_group_used(&cache->item);
3265
David Sterbabc309462015-10-20 18:22:13 +02003266 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003267 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003268 else if (bargs->usage > 100)
3269 user_thresh = cache->key.offset;
3270 else
3271 user_thresh = div_factor_fine(cache->key.offset,
3272 bargs->usage);
3273
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003274 if (chunk_used < user_thresh)
3275 ret = 0;
3276
3277 btrfs_put_block_group(cache);
3278 return ret;
3279}
3280
Ilya Dryomov409d4042012-01-16 22:04:47 +02003281static int chunk_devid_filter(struct extent_buffer *leaf,
3282 struct btrfs_chunk *chunk,
3283 struct btrfs_balance_args *bargs)
3284{
3285 struct btrfs_stripe *stripe;
3286 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3287 int i;
3288
3289 for (i = 0; i < num_stripes; i++) {
3290 stripe = btrfs_stripe_nr(chunk, i);
3291 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3292 return 0;
3293 }
3294
3295 return 1;
3296}
3297
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003298/* [pstart, pend) */
3299static int chunk_drange_filter(struct extent_buffer *leaf,
3300 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003301 struct btrfs_balance_args *bargs)
3302{
3303 struct btrfs_stripe *stripe;
3304 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3305 u64 stripe_offset;
3306 u64 stripe_length;
3307 int factor;
3308 int i;
3309
3310 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3311 return 0;
3312
3313 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003314 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3315 factor = num_stripes / 2;
3316 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3317 factor = num_stripes - 1;
3318 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3319 factor = num_stripes - 2;
3320 } else {
3321 factor = num_stripes;
3322 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003323
3324 for (i = 0; i < num_stripes; i++) {
3325 stripe = btrfs_stripe_nr(chunk, i);
3326 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3327 continue;
3328
3329 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3330 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003331 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003332
3333 if (stripe_offset < bargs->pend &&
3334 stripe_offset + stripe_length > bargs->pstart)
3335 return 0;
3336 }
3337
3338 return 1;
3339}
3340
Ilya Dryomovea671762012-01-16 22:04:48 +02003341/* [vstart, vend) */
3342static int chunk_vrange_filter(struct extent_buffer *leaf,
3343 struct btrfs_chunk *chunk,
3344 u64 chunk_offset,
3345 struct btrfs_balance_args *bargs)
3346{
3347 if (chunk_offset < bargs->vend &&
3348 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3349 /* at least part of the chunk is inside this vrange */
3350 return 0;
3351
3352 return 1;
3353}
3354
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003355static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3356 struct btrfs_chunk *chunk,
3357 struct btrfs_balance_args *bargs)
3358{
3359 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3360
3361 if (bargs->stripes_min <= num_stripes
3362 && num_stripes <= bargs->stripes_max)
3363 return 0;
3364
3365 return 1;
3366}
3367
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003368static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003369 struct btrfs_balance_args *bargs)
3370{
3371 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3372 return 0;
3373
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003374 chunk_type = chunk_to_extended(chunk_type) &
3375 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003376
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003377 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003378 return 1;
3379
3380 return 0;
3381}
3382
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003383static int should_balance_chunk(struct btrfs_fs_info *fs_info,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003384 struct extent_buffer *leaf,
3385 struct btrfs_chunk *chunk, u64 chunk_offset)
3386{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003387 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003388 struct btrfs_balance_args *bargs = NULL;
3389 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3390
3391 /* type filter */
3392 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3393 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3394 return 0;
3395 }
3396
3397 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3398 bargs = &bctl->data;
3399 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3400 bargs = &bctl->sys;
3401 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3402 bargs = &bctl->meta;
3403
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003404 /* profiles filter */
3405 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3406 chunk_profiles_filter(chunk_type, bargs)) {
3407 return 0;
3408 }
3409
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003410 /* usage filter */
3411 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003412 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003413 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003414 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003415 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003416 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003417 }
3418
Ilya Dryomov409d4042012-01-16 22:04:47 +02003419 /* devid filter */
3420 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3421 chunk_devid_filter(leaf, chunk, bargs)) {
3422 return 0;
3423 }
3424
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003425 /* drange filter, makes sense only with devid filter */
3426 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003427 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003428 return 0;
3429 }
3430
Ilya Dryomovea671762012-01-16 22:04:48 +02003431 /* vrange filter */
3432 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3433 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3434 return 0;
3435 }
3436
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003437 /* stripes filter */
3438 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3439 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3440 return 0;
3441 }
3442
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003443 /* soft profile changing mode */
3444 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3445 chunk_soft_convert_filter(chunk_type, bargs)) {
3446 return 0;
3447 }
3448
David Sterba7d824b62014-05-07 17:37:51 +02003449 /*
3450 * limited by count, must be the last filter
3451 */
3452 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3453 if (bargs->limit == 0)
3454 return 0;
3455 else
3456 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003457 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3458 /*
3459 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003460 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003461 * about the count of all chunks that satisfy the filters.
3462 */
3463 if (bargs->limit_max == 0)
3464 return 0;
3465 else
3466 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003467 }
3468
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003469 return 1;
3470}
3471
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003472static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003473{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003474 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003475 struct btrfs_root *chunk_root = fs_info->chunk_root;
3476 struct btrfs_root *dev_root = fs_info->dev_root;
3477 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003478 struct btrfs_device *device;
3479 u64 old_size;
3480 u64 size_to_free;
David Sterba12907fc2015-10-10 17:16:50 +02003481 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003482 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003483 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003484 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003485 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003486 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003487 struct extent_buffer *leaf;
3488 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003489 int ret;
3490 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003491 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003492 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003493 u64 limit_data = bctl->data.limit;
3494 u64 limit_meta = bctl->meta.limit;
3495 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003496 u32 count_data = 0;
3497 u32 count_meta = 0;
3498 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003499 int chunk_reserved = 0;
Liu Bocf25ce52015-12-14 18:29:32 -08003500 u64 bytes_used = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003501
Chris Masonec44a352008-04-28 15:29:52 -04003502 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003503 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003504 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003505 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003506 size_to_free = div_factor(old_size, 1);
Byongho Leeee221842015-12-15 01:42:10 +09003507 size_to_free = min_t(u64, size_to_free, SZ_1M);
Yan Zheng2b820322008-11-17 21:11:30 -05003508 if (!device->writeable ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003509 btrfs_device_get_total_bytes(device) -
3510 btrfs_device_get_bytes_used(device) > size_to_free ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003511 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003512 continue;
3513
3514 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003515 if (ret == -ENOSPC)
3516 break;
Liu Bo5a488b92016-07-12 11:24:21 -07003517 if (ret) {
3518 /* btrfs_shrink_device never returns ret > 0 */
3519 WARN_ON(ret > 0);
3520 goto error;
3521 }
Chris Masonec44a352008-04-28 15:29:52 -04003522
Yan, Zhenga22285a2010-05-16 10:48:46 -04003523 trans = btrfs_start_transaction(dev_root, 0);
Liu Bo5a488b92016-07-12 11:24:21 -07003524 if (IS_ERR(trans)) {
3525 ret = PTR_ERR(trans);
3526 btrfs_info_in_rcu(fs_info,
3527 "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu",
3528 rcu_str_deref(device->name), ret,
3529 old_size, old_size - size_to_free);
3530 goto error;
3531 }
Chris Masonec44a352008-04-28 15:29:52 -04003532
3533 ret = btrfs_grow_device(trans, device, old_size);
Liu Bo5a488b92016-07-12 11:24:21 -07003534 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003535 btrfs_end_transaction(trans);
Liu Bo5a488b92016-07-12 11:24:21 -07003536 /* btrfs_grow_device never returns ret > 0 */
3537 WARN_ON(ret > 0);
3538 btrfs_info_in_rcu(fs_info,
3539 "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu",
3540 rcu_str_deref(device->name), ret,
3541 old_size, old_size - size_to_free);
3542 goto error;
3543 }
Chris Masonec44a352008-04-28 15:29:52 -04003544
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003545 btrfs_end_transaction(trans);
Chris Masonec44a352008-04-28 15:29:52 -04003546 }
3547
3548 /* step two, relocate all the chunks */
3549 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003550 if (!path) {
3551 ret = -ENOMEM;
3552 goto error;
3553 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003554
3555 /* zero out stat counters */
3556 spin_lock(&fs_info->balance_lock);
3557 memset(&bctl->stat, 0, sizeof(bctl->stat));
3558 spin_unlock(&fs_info->balance_lock);
3559again:
David Sterba7d824b62014-05-07 17:37:51 +02003560 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003561 /*
3562 * The single value limit and min/max limits use the same bytes
3563 * in the
3564 */
David Sterba7d824b62014-05-07 17:37:51 +02003565 bctl->data.limit = limit_data;
3566 bctl->meta.limit = limit_meta;
3567 bctl->sys.limit = limit_sys;
3568 }
Chris Masonec44a352008-04-28 15:29:52 -04003569 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3570 key.offset = (u64)-1;
3571 key.type = BTRFS_CHUNK_ITEM_KEY;
3572
Chris Masond3977122009-01-05 21:25:51 -05003573 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003574 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003575 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003576 ret = -ECANCELED;
3577 goto error;
3578 }
3579
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003580 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003581 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003582 if (ret < 0) {
3583 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003584 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003585 }
Chris Masonec44a352008-04-28 15:29:52 -04003586
3587 /*
3588 * this shouldn't happen, it means the last relocate
3589 * failed
3590 */
3591 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003592 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003593
3594 ret = btrfs_previous_item(chunk_root, path, 0,
3595 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003596 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003597 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003598 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003599 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003600 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003601
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003602 leaf = path->nodes[0];
3603 slot = path->slots[0];
3604 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3605
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003606 if (found_key.objectid != key.objectid) {
3607 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003608 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003609 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003610
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003611 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003612 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003613
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003614 if (!counting) {
3615 spin_lock(&fs_info->balance_lock);
3616 bctl->stat.considered++;
3617 spin_unlock(&fs_info->balance_lock);
3618 }
3619
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003620 ret = should_balance_chunk(fs_info, leaf, chunk,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003621 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003622
David Sterbab3b4aa72011-04-21 01:20:15 +02003623 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003624 if (!ret) {
3625 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003626 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003627 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003628
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003629 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003630 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003631 spin_lock(&fs_info->balance_lock);
3632 bctl->stat.expected++;
3633 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003634
3635 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3636 count_data++;
3637 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3638 count_sys++;
3639 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3640 count_meta++;
3641
3642 goto loop;
3643 }
3644
3645 /*
3646 * Apply limit_min filter, no need to check if the LIMITS
3647 * filter is used, limit_min is 0 by default
3648 */
3649 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3650 count_data < bctl->data.limit_min)
3651 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3652 count_meta < bctl->meta.limit_min)
3653 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3654 count_sys < bctl->sys.limit_min)) {
3655 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003656 goto loop;
3657 }
3658
Liu Bocf25ce52015-12-14 18:29:32 -08003659 ASSERT(fs_info->data_sinfo);
3660 spin_lock(&fs_info->data_sinfo->lock);
3661 bytes_used = fs_info->data_sinfo->bytes_used;
3662 spin_unlock(&fs_info->data_sinfo->lock);
3663
3664 if ((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3665 !chunk_reserved && !bytes_used) {
Zhao Lei2c9fe832015-11-09 11:51:32 +08003666 trans = btrfs_start_transaction(chunk_root, 0);
3667 if (IS_ERR(trans)) {
3668 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3669 ret = PTR_ERR(trans);
3670 goto error;
3671 }
3672
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003673 ret = btrfs_force_chunk_alloc(trans, fs_info,
Zhao Lei2c9fe832015-11-09 11:51:32 +08003674 BTRFS_BLOCK_GROUP_DATA);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003675 btrfs_end_transaction(trans);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003676 if (ret < 0) {
3677 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3678 goto error;
3679 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003680 chunk_reserved = 1;
3681 }
3682
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003683 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003684 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik508794e2011-07-02 21:24:41 +00003685 if (ret && ret != -ENOSPC)
3686 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003687 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003688 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003689 } else {
3690 spin_lock(&fs_info->balance_lock);
3691 bctl->stat.completed++;
3692 spin_unlock(&fs_info->balance_lock);
3693 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003694loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003695 if (found_key.offset == 0)
3696 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003697 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003698 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003699
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003700 if (counting) {
3701 btrfs_release_path(path);
3702 counting = false;
3703 goto again;
3704 }
Chris Masonec44a352008-04-28 15:29:52 -04003705error:
3706 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003707 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003708 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003709 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003710 if (!ret)
3711 ret = -ENOSPC;
3712 }
3713
Chris Masonec44a352008-04-28 15:29:52 -04003714 return ret;
3715}
3716
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003717/**
3718 * alloc_profile_is_valid - see if a given profile is valid and reduced
3719 * @flags: profile to validate
3720 * @extended: if true @flags is treated as an extended profile
3721 */
3722static int alloc_profile_is_valid(u64 flags, int extended)
3723{
3724 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3725 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3726
3727 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3728
3729 /* 1) check that all other bits are zeroed */
3730 if (flags & ~mask)
3731 return 0;
3732
3733 /* 2) see if profile is reduced */
3734 if (flags == 0)
3735 return !extended; /* "0" is valid for usual profiles */
3736
3737 /* true if exactly one bit set */
3738 return (flags & (flags - 1)) == 0;
3739}
3740
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003741static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3742{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003743 /* cancel requested || normal exit path */
3744 return atomic_read(&fs_info->balance_cancel_req) ||
3745 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3746 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003747}
3748
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003749static void __cancel_balance(struct btrfs_fs_info *fs_info)
3750{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003751 int ret;
3752
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003753 unset_balance_control(fs_info);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003754 ret = del_balance_item(fs_info);
Liu Bo0f788c52013-03-04 16:25:40 +00003755 if (ret)
Anand Jain34d97002016-03-16 16:43:06 +08003756 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003757
David Sterba171938e2017-03-28 14:44:21 +02003758 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003759}
3760
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003761/* Non-zero return value signifies invalidity */
3762static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3763 u64 allowed)
3764{
3765 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3766 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3767 (bctl_arg->target & ~allowed)));
3768}
3769
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003770/*
3771 * Should be called with both balance and volume mutexes held
3772 */
3773int btrfs_balance(struct btrfs_balance_control *bctl,
3774 struct btrfs_ioctl_balance_args *bargs)
3775{
3776 struct btrfs_fs_info *fs_info = bctl->fs_info;
Adam Borowski14506122017-03-07 23:34:44 +01003777 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003778 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003779 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003780 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003781 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003782 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003783
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003784 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003785 atomic_read(&fs_info->balance_pause_req) ||
3786 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003787 ret = -EINVAL;
3788 goto out;
3789 }
3790
Ilya Dryomove4837f82012-03-27 17:09:17 +03003791 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3792 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3793 mixed = 1;
3794
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003795 /*
3796 * In case of mixed groups both data and meta should be picked,
3797 * and identical options should be given for both of them.
3798 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003799 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3800 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003801 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3802 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3803 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003804 btrfs_err(fs_info,
3805 "with mixed groups data and metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003806 ret = -EINVAL;
3807 goto out;
3808 }
3809 }
3810
Stefan Behrens8dabb742012-11-06 13:15:27 +01003811 num_devices = fs_info->fs_devices->num_devices;
Liu Bo73beece2015-07-17 16:49:19 +08003812 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Stefan Behrens8dabb742012-11-06 13:15:27 +01003813 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3814 BUG_ON(num_devices < 1);
3815 num_devices--;
3816 }
Liu Bo73beece2015-07-17 16:49:19 +08003817 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Austin S. Hemmelgarn88be1592016-03-23 14:22:59 -04003818 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
3819 if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003820 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003821 if (num_devices > 2)
3822 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3823 if (num_devices > 3)
3824 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3825 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003826 if (validate_convert_profile(&bctl->data, allowed)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003827 btrfs_err(fs_info,
3828 "unable to start balance with target data profile %llu",
3829 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003830 ret = -EINVAL;
3831 goto out;
3832 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003833 if (validate_convert_profile(&bctl->meta, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003834 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003835 "unable to start balance with target metadata profile %llu",
3836 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003837 ret = -EINVAL;
3838 goto out;
3839 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003840 if (validate_convert_profile(&bctl->sys, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003841 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003842 "unable to start balance with target system profile %llu",
3843 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003844 ret = -EINVAL;
3845 goto out;
3846 }
3847
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003848 /* allow to reduce meta or sys integrity only if force set */
3849 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003850 BTRFS_BLOCK_GROUP_RAID10 |
3851 BTRFS_BLOCK_GROUP_RAID5 |
3852 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003853 do {
3854 seq = read_seqbegin(&fs_info->profiles_lock);
3855
3856 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3857 (fs_info->avail_system_alloc_bits & allowed) &&
3858 !(bctl->sys.target & allowed)) ||
3859 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3860 (fs_info->avail_metadata_alloc_bits & allowed) &&
3861 !(bctl->meta.target & allowed))) {
3862 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003863 btrfs_info(fs_info,
3864 "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003865 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003866 btrfs_err(fs_info,
3867 "balance will reduce metadata integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003868 ret = -EINVAL;
3869 goto out;
3870 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003871 }
Miao Xiede98ced2013-01-29 10:13:12 +00003872 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003873
Adam Borowski14506122017-03-07 23:34:44 +01003874 /* if we're not converting, the target field is uninitialized */
3875 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3876 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
3877 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3878 bctl->data.target : fs_info->avail_data_alloc_bits;
3879 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
3880 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Sam Tygieree592d02016-01-06 08:46:12 +00003881 btrfs_warn(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003882 "metadata profile 0x%llx has lower redundancy than data profile 0x%llx",
Adam Borowski14506122017-03-07 23:34:44 +01003883 meta_target, data_target);
Sam Tygieree592d02016-01-06 08:46:12 +00003884 }
3885
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003886 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003887 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003888 goto out;
3889
Ilya Dryomov59641012012-01-16 22:04:48 +02003890 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3891 BUG_ON(ret == -EEXIST);
3892 set_balance_control(bctl);
3893 } else {
3894 BUG_ON(ret != -EEXIST);
3895 spin_lock(&fs_info->balance_lock);
3896 update_balance_args(bctl);
3897 spin_unlock(&fs_info->balance_lock);
3898 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003899
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003900 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003901 mutex_unlock(&fs_info->balance_mutex);
3902
3903 ret = __btrfs_balance(fs_info);
3904
3905 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003906 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003907
3908 if (bargs) {
3909 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003910 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003911 }
3912
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003913 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3914 balance_need_close(fs_info)) {
3915 __cancel_balance(fs_info);
3916 }
3917
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003918 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003919
3920 return ret;
3921out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003922 if (bctl->flags & BTRFS_BALANCE_RESUME)
3923 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003924 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003925 kfree(bctl);
David Sterba171938e2017-03-28 14:44:21 +02003926 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003927 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003928 return ret;
3929}
3930
3931static int balance_kthread(void *data)
3932{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003933 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003934 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003935
3936 mutex_lock(&fs_info->volume_mutex);
3937 mutex_lock(&fs_info->balance_mutex);
3938
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003939 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003940 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003941 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003942 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003943
3944 mutex_unlock(&fs_info->balance_mutex);
3945 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003946
Ilya Dryomov59641012012-01-16 22:04:48 +02003947 return ret;
3948}
3949
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003950int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3951{
3952 struct task_struct *tsk;
3953
3954 spin_lock(&fs_info->balance_lock);
3955 if (!fs_info->balance_ctl) {
3956 spin_unlock(&fs_info->balance_lock);
3957 return 0;
3958 }
3959 spin_unlock(&fs_info->balance_lock);
3960
Jeff Mahoney3cdde222016-06-09 21:38:35 -04003961 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003962 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003963 return 0;
3964 }
3965
3966 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303967 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003968}
3969
Ilya Dryomov68310a52012-06-22 12:24:12 -06003970int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003971{
Ilya Dryomov59641012012-01-16 22:04:48 +02003972 struct btrfs_balance_control *bctl;
3973 struct btrfs_balance_item *item;
3974 struct btrfs_disk_balance_args disk_bargs;
3975 struct btrfs_path *path;
3976 struct extent_buffer *leaf;
3977 struct btrfs_key key;
3978 int ret;
3979
3980 path = btrfs_alloc_path();
3981 if (!path)
3982 return -ENOMEM;
3983
Ilya Dryomov68310a52012-06-22 12:24:12 -06003984 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003985 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06003986 key.offset = 0;
3987
3988 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3989 if (ret < 0)
3990 goto out;
3991 if (ret > 0) { /* ret = -ENOENT; */
3992 ret = 0;
3993 goto out;
3994 }
3995
Ilya Dryomov59641012012-01-16 22:04:48 +02003996 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3997 if (!bctl) {
3998 ret = -ENOMEM;
3999 goto out;
4000 }
4001
Ilya Dryomov59641012012-01-16 22:04:48 +02004002 leaf = path->nodes[0];
4003 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4004
Ilya Dryomov68310a52012-06-22 12:24:12 -06004005 bctl->fs_info = fs_info;
4006 bctl->flags = btrfs_balance_flags(leaf, item);
4007 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004008
4009 btrfs_balance_data(leaf, item, &disk_bargs);
4010 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4011 btrfs_balance_meta(leaf, item, &disk_bargs);
4012 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4013 btrfs_balance_sys(leaf, item, &disk_bargs);
4014 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4015
David Sterba171938e2017-03-28 14:44:21 +02004016 WARN_ON(test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004017
Ilya Dryomov68310a52012-06-22 12:24:12 -06004018 mutex_lock(&fs_info->volume_mutex);
4019 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004020
Ilya Dryomov68310a52012-06-22 12:24:12 -06004021 set_balance_control(bctl);
4022
4023 mutex_unlock(&fs_info->balance_mutex);
4024 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004025out:
4026 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004027 return ret;
4028}
4029
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004030int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4031{
4032 int ret = 0;
4033
4034 mutex_lock(&fs_info->balance_mutex);
4035 if (!fs_info->balance_ctl) {
4036 mutex_unlock(&fs_info->balance_mutex);
4037 return -ENOTCONN;
4038 }
4039
4040 if (atomic_read(&fs_info->balance_running)) {
4041 atomic_inc(&fs_info->balance_pause_req);
4042 mutex_unlock(&fs_info->balance_mutex);
4043
4044 wait_event(fs_info->balance_wait_q,
4045 atomic_read(&fs_info->balance_running) == 0);
4046
4047 mutex_lock(&fs_info->balance_mutex);
4048 /* we are good with balance_ctl ripped off from under us */
4049 BUG_ON(atomic_read(&fs_info->balance_running));
4050 atomic_dec(&fs_info->balance_pause_req);
4051 } else {
4052 ret = -ENOTCONN;
4053 }
4054
4055 mutex_unlock(&fs_info->balance_mutex);
4056 return ret;
4057}
4058
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004059int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4060{
Ilya Dryomove649e582013-10-10 20:40:21 +03004061 if (fs_info->sb->s_flags & MS_RDONLY)
4062 return -EROFS;
4063
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004064 mutex_lock(&fs_info->balance_mutex);
4065 if (!fs_info->balance_ctl) {
4066 mutex_unlock(&fs_info->balance_mutex);
4067 return -ENOTCONN;
4068 }
4069
4070 atomic_inc(&fs_info->balance_cancel_req);
4071 /*
4072 * if we are running just wait and return, balance item is
4073 * deleted in btrfs_balance in this case
4074 */
4075 if (atomic_read(&fs_info->balance_running)) {
4076 mutex_unlock(&fs_info->balance_mutex);
4077 wait_event(fs_info->balance_wait_q,
4078 atomic_read(&fs_info->balance_running) == 0);
4079 mutex_lock(&fs_info->balance_mutex);
4080 } else {
4081 /* __cancel_balance needs volume_mutex */
4082 mutex_unlock(&fs_info->balance_mutex);
4083 mutex_lock(&fs_info->volume_mutex);
4084 mutex_lock(&fs_info->balance_mutex);
4085
4086 if (fs_info->balance_ctl)
4087 __cancel_balance(fs_info);
4088
4089 mutex_unlock(&fs_info->volume_mutex);
4090 }
4091
4092 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
4093 atomic_dec(&fs_info->balance_cancel_req);
4094 mutex_unlock(&fs_info->balance_mutex);
4095 return 0;
4096}
4097
Stefan Behrens803b2f52013-08-15 17:11:21 +02004098static int btrfs_uuid_scan_kthread(void *data)
4099{
4100 struct btrfs_fs_info *fs_info = data;
4101 struct btrfs_root *root = fs_info->tree_root;
4102 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004103 struct btrfs_path *path = NULL;
4104 int ret = 0;
4105 struct extent_buffer *eb;
4106 int slot;
4107 struct btrfs_root_item root_item;
4108 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004109 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004110
4111 path = btrfs_alloc_path();
4112 if (!path) {
4113 ret = -ENOMEM;
4114 goto out;
4115 }
4116
4117 key.objectid = 0;
4118 key.type = BTRFS_ROOT_ITEM_KEY;
4119 key.offset = 0;
4120
Stefan Behrens803b2f52013-08-15 17:11:21 +02004121 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004122 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004123 if (ret) {
4124 if (ret > 0)
4125 ret = 0;
4126 break;
4127 }
4128
4129 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4130 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4131 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4132 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4133 goto skip;
4134
4135 eb = path->nodes[0];
4136 slot = path->slots[0];
4137 item_size = btrfs_item_size_nr(eb, slot);
4138 if (item_size < sizeof(root_item))
4139 goto skip;
4140
Stefan Behrens803b2f52013-08-15 17:11:21 +02004141 read_extent_buffer(eb, &root_item,
4142 btrfs_item_ptr_offset(eb, slot),
4143 (int)sizeof(root_item));
4144 if (btrfs_root_refs(&root_item) == 0)
4145 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004146
4147 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4148 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4149 if (trans)
4150 goto update_tree;
4151
4152 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004153 /*
4154 * 1 - subvol uuid item
4155 * 1 - received_subvol uuid item
4156 */
4157 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4158 if (IS_ERR(trans)) {
4159 ret = PTR_ERR(trans);
4160 break;
4161 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004162 continue;
4163 } else {
4164 goto skip;
4165 }
4166update_tree:
4167 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004168 ret = btrfs_uuid_tree_add(trans, fs_info,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004169 root_item.uuid,
4170 BTRFS_UUID_KEY_SUBVOL,
4171 key.objectid);
4172 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004173 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004174 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004175 break;
4176 }
4177 }
4178
4179 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004180 ret = btrfs_uuid_tree_add(trans, fs_info,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004181 root_item.received_uuid,
4182 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4183 key.objectid);
4184 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004185 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004186 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004187 break;
4188 }
4189 }
4190
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004191skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004192 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004193 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004194 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004195 if (ret)
4196 break;
4197 }
4198
Stefan Behrens803b2f52013-08-15 17:11:21 +02004199 btrfs_release_path(path);
4200 if (key.offset < (u64)-1) {
4201 key.offset++;
4202 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4203 key.offset = 0;
4204 key.type = BTRFS_ROOT_ITEM_KEY;
4205 } else if (key.objectid < (u64)-1) {
4206 key.offset = 0;
4207 key.type = BTRFS_ROOT_ITEM_KEY;
4208 key.objectid++;
4209 } else {
4210 break;
4211 }
4212 cond_resched();
4213 }
4214
4215out:
4216 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004217 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004218 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004219 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004220 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004221 else
Josef Bacikafcdd122016-09-02 15:40:02 -04004222 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004223 up(&fs_info->uuid_tree_rescan_sem);
4224 return 0;
4225}
4226
Stefan Behrens70f80172013-08-15 17:11:23 +02004227/*
4228 * Callback for btrfs_uuid_tree_iterate().
4229 * returns:
4230 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004231 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004232 * > 0 if the check failed, which means the caller shall remove the entry.
4233 */
4234static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4235 u8 *uuid, u8 type, u64 subid)
4236{
4237 struct btrfs_key key;
4238 int ret = 0;
4239 struct btrfs_root *subvol_root;
4240
4241 if (type != BTRFS_UUID_KEY_SUBVOL &&
4242 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4243 goto out;
4244
4245 key.objectid = subid;
4246 key.type = BTRFS_ROOT_ITEM_KEY;
4247 key.offset = (u64)-1;
4248 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4249 if (IS_ERR(subvol_root)) {
4250 ret = PTR_ERR(subvol_root);
4251 if (ret == -ENOENT)
4252 ret = 1;
4253 goto out;
4254 }
4255
4256 switch (type) {
4257 case BTRFS_UUID_KEY_SUBVOL:
4258 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4259 ret = 1;
4260 break;
4261 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4262 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4263 BTRFS_UUID_SIZE))
4264 ret = 1;
4265 break;
4266 }
4267
4268out:
4269 return ret;
4270}
4271
4272static int btrfs_uuid_rescan_kthread(void *data)
4273{
4274 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4275 int ret;
4276
4277 /*
4278 * 1st step is to iterate through the existing UUID tree and
4279 * to delete all entries that contain outdated data.
4280 * 2nd step is to add all missing entries to the UUID tree.
4281 */
4282 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4283 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004284 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004285 up(&fs_info->uuid_tree_rescan_sem);
4286 return ret;
4287 }
4288 return btrfs_uuid_scan_kthread(data);
4289}
4290
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004291int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4292{
4293 struct btrfs_trans_handle *trans;
4294 struct btrfs_root *tree_root = fs_info->tree_root;
4295 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004296 struct task_struct *task;
4297 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004298
4299 /*
4300 * 1 - root node
4301 * 1 - root item
4302 */
4303 trans = btrfs_start_transaction(tree_root, 2);
4304 if (IS_ERR(trans))
4305 return PTR_ERR(trans);
4306
4307 uuid_root = btrfs_create_tree(trans, fs_info,
4308 BTRFS_UUID_TREE_OBJECTID);
4309 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004310 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004311 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004312 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004313 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004314 }
4315
4316 fs_info->uuid_root = uuid_root;
4317
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004318 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004319 if (ret)
4320 return ret;
4321
4322 down(&fs_info->uuid_tree_rescan_sem);
4323 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4324 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004325 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004326 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004327 up(&fs_info->uuid_tree_rescan_sem);
4328 return PTR_ERR(task);
4329 }
4330
4331 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004332}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004333
Stefan Behrens70f80172013-08-15 17:11:23 +02004334int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4335{
4336 struct task_struct *task;
4337
4338 down(&fs_info->uuid_tree_rescan_sem);
4339 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4340 if (IS_ERR(task)) {
4341 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004342 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004343 up(&fs_info->uuid_tree_rescan_sem);
4344 return PTR_ERR(task);
4345 }
4346
4347 return 0;
4348}
4349
Chris Mason8f18cf12008-04-25 16:53:30 -04004350/*
4351 * shrinking a device means finding all of the device extents past
4352 * the new size, and then following the back refs to the chunks.
4353 * The chunk relocation code actually frees the device extent
4354 */
4355int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4356{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004357 struct btrfs_fs_info *fs_info = device->fs_info;
4358 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004359 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004360 struct btrfs_dev_extent *dev_extent = NULL;
4361 struct btrfs_path *path;
4362 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004363 u64 chunk_offset;
4364 int ret;
4365 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004366 int failed = 0;
4367 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004368 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004369 struct extent_buffer *l;
4370 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004371 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004372 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004373 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004374 u64 diff;
4375
4376 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004377 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004378
Stefan Behrens63a212a2012-11-05 18:29:28 +01004379 if (device->is_tgtdev_for_dev_replace)
4380 return -EINVAL;
4381
Chris Mason8f18cf12008-04-25 16:53:30 -04004382 path = btrfs_alloc_path();
4383 if (!path)
4384 return -ENOMEM;
4385
David Sterbae4058b52015-11-27 16:31:35 +01004386 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04004387
David Sterba34441362016-10-04 19:34:27 +02004388 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004389
Miao Xie7cc8e582014-09-03 21:35:38 +08004390 btrfs_device_set_total_bytes(device, new_size);
Josef Bacik2bf64752011-09-26 17:12:22 -04004391 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004392 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004393 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004394 }
David Sterba34441362016-10-04 19:34:27 +02004395 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004396
Josef Bacikba1bf482009-09-11 16:11:19 -04004397again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004398 key.objectid = device->devid;
4399 key.offset = (u64)-1;
4400 key.type = BTRFS_DEV_EXTENT_KEY;
4401
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004402 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004403 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004404 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004405 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004406 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004407 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004408 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004409
4410 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004411 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004412 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004413 if (ret < 0)
4414 goto done;
4415 if (ret) {
4416 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004417 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004418 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004419 }
4420
4421 l = path->nodes[0];
4422 slot = path->slots[0];
4423 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4424
Josef Bacikba1bf482009-09-11 16:11:19 -04004425 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004426 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004427 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004428 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004429 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004430
4431 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4432 length = btrfs_dev_extent_length(l, dev_extent);
4433
Josef Bacikba1bf482009-09-11 16:11:19 -04004434 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004435 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004436 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004437 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004438 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004439
Chris Mason8f18cf12008-04-25 16:53:30 -04004440 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004441 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004442
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004443 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4444 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04004445 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004446 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004447 if (ret == -ENOSPC)
4448 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004449 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004450
4451 if (failed && !retried) {
4452 failed = 0;
4453 retried = true;
4454 goto again;
4455 } else if (failed && retried) {
4456 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004457 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004458 }
4459
Chris Balld6397ba2009-04-27 07:29:03 -04004460 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004461 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004462 if (IS_ERR(trans)) {
4463 ret = PTR_ERR(trans);
4464 goto done;
4465 }
4466
David Sterba34441362016-10-04 19:34:27 +02004467 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004468
4469 /*
4470 * We checked in the above loop all device extents that were already in
4471 * the device tree. However before we have updated the device's
4472 * total_bytes to the new size, we might have had chunk allocations that
4473 * have not complete yet (new block groups attached to transaction
4474 * handles), and therefore their device extents were not yet in the
4475 * device tree and we missed them in the loop above. So if we have any
4476 * pending chunk using a device extent that overlaps the device range
4477 * that we can not use anymore, commit the current transaction and
4478 * repeat the search on the device tree - this way we guarantee we will
4479 * not have chunks using device extents that end beyond 'new_size'.
4480 */
4481 if (!checked_pending_chunks) {
4482 u64 start = new_size;
4483 u64 len = old_size - new_size;
4484
Jeff Mahoney499f3772015-06-15 09:41:17 -04004485 if (contains_pending_extent(trans->transaction, device,
4486 &start, len)) {
David Sterba34441362016-10-04 19:34:27 +02004487 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004488 checked_pending_chunks = true;
4489 failed = 0;
4490 retried = false;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004491 ret = btrfs_commit_transaction(trans);
Filipe Manana53e489b2015-06-02 14:43:21 +01004492 if (ret)
4493 goto done;
4494 goto again;
4495 }
4496 }
4497
Miao Xie7cc8e582014-09-03 21:35:38 +08004498 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004499 if (list_empty(&device->resized_list))
4500 list_add_tail(&device->resized_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004501 &fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004502
Chris Balld6397ba2009-04-27 07:29:03 -04004503 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004504 btrfs_set_super_total_bytes(super_copy,
4505 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004506 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004507
4508 /* Now btrfs_update_device() will change the on-disk size. */
4509 ret = btrfs_update_device(trans, device);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004510 btrfs_end_transaction(trans);
Chris Mason8f18cf12008-04-25 16:53:30 -04004511done:
4512 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004513 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004514 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004515 btrfs_device_set_total_bytes(device, old_size);
4516 if (device->writeable)
4517 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004518 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004519 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004520 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004521 return ret;
4522}
4523
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004524static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004525 struct btrfs_key *key,
4526 struct btrfs_chunk *chunk, int item_size)
4527{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004528 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004529 struct btrfs_disk_key disk_key;
4530 u32 array_size;
4531 u8 *ptr;
4532
David Sterba34441362016-10-04 19:34:27 +02004533 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004534 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004535 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004536 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004537 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004538 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004539 }
Chris Mason0b86a832008-03-24 15:01:56 -04004540
4541 ptr = super_copy->sys_chunk_array + array_size;
4542 btrfs_cpu_key_to_disk(&disk_key, key);
4543 memcpy(ptr, &disk_key, sizeof(disk_key));
4544 ptr += sizeof(disk_key);
4545 memcpy(ptr, chunk, item_size);
4546 item_size += sizeof(disk_key);
4547 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004548 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004549
Chris Mason0b86a832008-03-24 15:01:56 -04004550 return 0;
4551}
4552
Miao Xieb2117a32011-01-05 10:07:28 +00004553/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004554 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004555 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004556static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004557{
Arne Jansen73c5de02011-04-12 12:07:57 +02004558 const struct btrfs_device_info *di_a = a;
4559 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004560
Arne Jansen73c5de02011-04-12 12:07:57 +02004561 if (di_a->max_avail > di_b->max_avail)
4562 return -1;
4563 if (di_a->max_avail < di_b->max_avail)
4564 return 1;
4565 if (di_a->total_avail > di_b->total_avail)
4566 return -1;
4567 if (di_a->total_avail < di_b->total_avail)
4568 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004569 return 0;
4570}
4571
David Woodhouse53b381b2013-01-29 18:40:14 -05004572static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4573{
Zhao Leiffe2d202015-01-20 15:11:44 +08004574 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004575 return;
4576
Miao Xieceda0862013-04-11 10:30:16 +00004577 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004578}
4579
Jeff Mahoneyda170662016-06-15 09:22:56 -04004580#define BTRFS_MAX_DEVS(r) ((BTRFS_MAX_ITEM_SIZE(r->fs_info) \
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004581 - sizeof(struct btrfs_chunk)) \
4582 / sizeof(struct btrfs_stripe) + 1)
4583
4584#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4585 - 2 * sizeof(struct btrfs_disk_key) \
4586 - 2 * sizeof(struct btrfs_chunk)) \
4587 / sizeof(struct btrfs_stripe) + 1)
4588
Miao Xieb2117a32011-01-05 10:07:28 +00004589static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
David Sterba72b468c2017-02-10 19:46:27 +01004590 u64 start, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004591{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004592 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004593 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004594 struct btrfs_device *device;
Arne Jansen73c5de02011-04-12 12:07:57 +02004595 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004596 struct extent_map_tree *em_tree;
4597 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004598 struct btrfs_device_info *devices_info = NULL;
4599 u64 total_avail;
4600 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004601 int data_stripes; /* number of stripes that count for
4602 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004603 int sub_stripes; /* sub_stripes info for map */
4604 int dev_stripes; /* stripes per dev */
4605 int devs_max; /* max devs to use */
4606 int devs_min; /* min devs needed */
4607 int devs_increment; /* ndevs has to be a multiple of this */
4608 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004609 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004610 u64 max_stripe_size;
4611 u64 max_chunk_size;
4612 u64 stripe_size;
4613 u64 num_bytes;
4614 int ndevs;
4615 int i;
4616 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004617 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004618
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004619 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004620
Miao Xieb2117a32011-01-05 10:07:28 +00004621 if (list_empty(&fs_devices->alloc_list))
4622 return -ENOSPC;
4623
Liu Bo31e50222012-11-21 14:18:10 +00004624 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004625
Liu Bo31e50222012-11-21 14:18:10 +00004626 sub_stripes = btrfs_raid_array[index].sub_stripes;
4627 dev_stripes = btrfs_raid_array[index].dev_stripes;
4628 devs_max = btrfs_raid_array[index].devs_max;
4629 devs_min = btrfs_raid_array[index].devs_min;
4630 devs_increment = btrfs_raid_array[index].devs_increment;
4631 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004632
4633 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09004634 max_stripe_size = SZ_1G;
Arne Jansen73c5de02011-04-12 12:07:57 +02004635 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004636 if (!devs_max)
4637 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004638 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004639 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09004640 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4641 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05004642 else
Byongho Leeee221842015-12-15 01:42:10 +09004643 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004644 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004645 if (!devs_max)
4646 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004647 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09004648 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004649 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004650 if (!devs_max)
4651 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004652 } else {
David Sterba351fd352014-05-15 16:48:20 +02004653 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004654 type);
4655 BUG_ON(1);
4656 }
4657
4658 /* we don't want a chunk larger than 10% of writeable space */
4659 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4660 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004661
David Sterba31e818f2015-02-20 18:00:26 +01004662 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004663 GFP_NOFS);
4664 if (!devices_info)
4665 return -ENOMEM;
4666
Arne Jansen73c5de02011-04-12 12:07:57 +02004667 /*
4668 * in the first pass through the devices list, we gather information
4669 * about the available holes on each device.
4670 */
4671 ndevs = 0;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004672 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004673 u64 max_avail;
4674 u64 dev_offset;
4675
Arne Jansen73c5de02011-04-12 12:07:57 +02004676 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004677 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004678 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004679 continue;
4680 }
4681
Stefan Behrens63a212a2012-11-05 18:29:28 +01004682 if (!device->in_fs_metadata ||
4683 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004684 continue;
4685
4686 if (device->total_bytes > device->bytes_used)
4687 total_avail = device->total_bytes - device->bytes_used;
4688 else
4689 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004690
4691 /* If there is no space on this device, skip it. */
4692 if (total_avail == 0)
4693 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004694
Josef Bacik6df9a952013-06-27 13:22:46 -04004695 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004696 max_stripe_size * dev_stripes,
4697 &dev_offset, &max_avail);
4698 if (ret && ret != -ENOSPC)
4699 goto error;
4700
4701 if (ret == 0)
4702 max_avail = max_stripe_size * dev_stripes;
4703
4704 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4705 continue;
4706
Eric Sandeen063d0062013-01-31 00:55:01 +00004707 if (ndevs == fs_devices->rw_devices) {
4708 WARN(1, "%s: found more than %llu devices\n",
4709 __func__, fs_devices->rw_devices);
4710 break;
4711 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004712 devices_info[ndevs].dev_offset = dev_offset;
4713 devices_info[ndevs].max_avail = max_avail;
4714 devices_info[ndevs].total_avail = total_avail;
4715 devices_info[ndevs].dev = device;
4716 ++ndevs;
4717 }
4718
4719 /*
4720 * now sort the devices by hole size / available space
4721 */
4722 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4723 btrfs_cmp_device_info, NULL);
4724
4725 /* round down to number of usable stripes */
Nikolay Borisove5600fd2017-06-27 10:02:25 +03004726 ndevs = round_down(ndevs, devs_increment);
Arne Jansen73c5de02011-04-12 12:07:57 +02004727
4728 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4729 ret = -ENOSPC;
4730 goto error;
4731 }
4732
Nikolay Borisovf148ef42017-06-27 10:02:26 +03004733 ndevs = min(ndevs, devs_max);
4734
Arne Jansen73c5de02011-04-12 12:07:57 +02004735 /*
4736 * the primary goal is to maximize the number of stripes, so use as many
4737 * devices as possible, even if the stripes are not maximum sized.
4738 */
4739 stripe_size = devices_info[ndevs-1].max_avail;
4740 num_stripes = ndevs * dev_stripes;
4741
David Woodhouse53b381b2013-01-29 18:40:14 -05004742 /*
4743 * this will have to be fixed for RAID1 and RAID10 over
4744 * more drives
4745 */
4746 data_stripes = num_stripes / ncopies;
4747
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004748 if (type & BTRFS_BLOCK_GROUP_RAID5)
David Woodhouse53b381b2013-01-29 18:40:14 -05004749 data_stripes = num_stripes - 1;
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004750
4751 if (type & BTRFS_BLOCK_GROUP_RAID6)
David Woodhouse53b381b2013-01-29 18:40:14 -05004752 data_stripes = num_stripes - 2;
Chris Mason86db2572013-02-20 16:23:40 -05004753
4754 /*
4755 * Use the number of data stripes to figure out how big this chunk
4756 * is really going to be in terms of logical address space,
4757 * and compare that answer with the max chunk size
4758 */
4759 if (stripe_size * data_stripes > max_chunk_size) {
4760 u64 mask = (1ULL << 24) - 1;
David Sterbab8b93ad2015-01-16 17:26:13 +01004761
4762 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004763
4764 /* bump the answer up to a 16MB boundary */
4765 stripe_size = (stripe_size + mask) & ~mask;
4766
4767 /* but don't go higher than the limits we found
4768 * while searching for free extents
4769 */
4770 if (stripe_size > devices_info[ndevs-1].max_avail)
4771 stripe_size = devices_info[ndevs-1].max_avail;
4772 }
4773
David Sterbab8b93ad2015-01-16 17:26:13 +01004774 stripe_size = div_u64(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004775
4776 /* align to BTRFS_STRIPE_LEN */
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004777 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
Arne Jansen73c5de02011-04-12 12:07:57 +02004778
Miao Xieb2117a32011-01-05 10:07:28 +00004779 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4780 if (!map) {
4781 ret = -ENOMEM;
4782 goto error;
4783 }
4784 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004785
Arne Jansen73c5de02011-04-12 12:07:57 +02004786 for (i = 0; i < ndevs; ++i) {
4787 for (j = 0; j < dev_stripes; ++j) {
4788 int s = i * dev_stripes + j;
4789 map->stripes[s].dev = devices_info[i].dev;
4790 map->stripes[s].physical = devices_info[i].dev_offset +
4791 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004792 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004793 }
Jeff Mahoneyda170662016-06-15 09:22:56 -04004794 map->sector_size = info->sectorsize;
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004795 map->stripe_len = BTRFS_STRIPE_LEN;
4796 map->io_align = BTRFS_STRIPE_LEN;
4797 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04004798 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004799 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004800
David Woodhouse53b381b2013-01-29 18:40:14 -05004801 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004802
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004803 trace_btrfs_chunk_alloc(info, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004804
David Sterba172ddd62011-04-21 00:48:27 +02004805 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004806 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004807 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004808 ret = -ENOMEM;
4809 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004810 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004811 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04004812 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05004813 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004814 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004815 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004816 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004817 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004818
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004819 em_tree = &info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004820 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004821 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004822 if (!ret) {
4823 list_add_tail(&em->list, &trans->transaction->pending_chunks);
Elena Reshetova490b54d2017-03-03 10:55:12 +02004824 refcount_inc(&em->refs);
Josef Bacik6df9a952013-06-27 13:22:46 -04004825 }
Chris Mason890871b2009-09-02 16:24:52 -04004826 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004827 if (ret) {
4828 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004829 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004830 }
Yan Zheng2b820322008-11-17 21:11:30 -05004831
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004832 ret = btrfs_make_block_group(trans, info, 0, type,
Josef Bacik04487482013-01-29 15:03:37 -05004833 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4834 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004835 if (ret)
4836 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004837
Miao Xie7cc8e582014-09-03 21:35:38 +08004838 for (i = 0; i < map->num_stripes; i++) {
4839 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4840 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4841 }
Miao Xie43530c42014-09-03 21:35:36 +08004842
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004843 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
Miao Xie1c116182014-09-03 21:35:37 +08004844
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004845 free_extent_map(em);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004846 check_raid56_incompat_flag(info, type);
David Woodhouse53b381b2013-01-29 18:40:14 -05004847
Miao Xieb2117a32011-01-05 10:07:28 +00004848 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004849 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004850
Josef Bacik6df9a952013-06-27 13:22:46 -04004851error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004852 write_lock(&em_tree->lock);
4853 remove_extent_mapping(em_tree, em);
4854 write_unlock(&em_tree->lock);
4855
4856 /* One for our allocation */
4857 free_extent_map(em);
4858 /* One for the tree reference */
4859 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004860 /* One for the pending_chunks list reference */
4861 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004862error:
Miao Xieb2117a32011-01-05 10:07:28 +00004863 kfree(devices_info);
4864 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004865}
4866
Josef Bacik6df9a952013-06-27 13:22:46 -04004867int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004868 struct btrfs_fs_info *fs_info,
Josef Bacik6df9a952013-06-27 13:22:46 -04004869 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004870{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004871 struct btrfs_root *extent_root = fs_info->extent_root;
4872 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05004873 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05004874 struct btrfs_device *device;
4875 struct btrfs_chunk *chunk;
4876 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004877 struct extent_map *em;
4878 struct map_lookup *map;
4879 size_t item_size;
4880 u64 dev_offset;
4881 u64 stripe_size;
4882 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08004883 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004884
Liu Bo592d92e2017-03-14 13:33:55 -07004885 em = get_chunk_map(fs_info, chunk_offset, chunk_size);
4886 if (IS_ERR(em))
4887 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04004888
Jeff Mahoney95617d62015-06-03 10:55:48 -04004889 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04004890 item_size = btrfs_chunk_item_size(map->num_stripes);
4891 stripe_size = em->orig_block_len;
4892
4893 chunk = kzalloc(item_size, GFP_NOFS);
4894 if (!chunk) {
4895 ret = -ENOMEM;
4896 goto out;
4897 }
4898
Filipe Manana50460e32015-11-20 10:42:47 +00004899 /*
4900 * Take the device list mutex to prevent races with the final phase of
4901 * a device replace operation that replaces the device object associated
4902 * with the map's stripes, because the device object's id can change
4903 * at any time during that final phase of the device replace operation
4904 * (dev-replace.c:btrfs_dev_replace_finishing()).
4905 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004906 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04004907 for (i = 0; i < map->num_stripes; i++) {
4908 device = map->stripes[i].dev;
4909 dev_offset = map->stripes[i].physical;
4910
Yan Zheng2b820322008-11-17 21:11:30 -05004911 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004912 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004913 break;
Josef Bacik6df9a952013-06-27 13:22:46 -04004914 ret = btrfs_alloc_dev_extent(trans, device,
4915 chunk_root->root_key.objectid,
4916 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4917 chunk_offset, dev_offset,
4918 stripe_size);
4919 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004920 break;
4921 }
4922 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004923 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00004924 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004925 }
4926
Yan Zheng2b820322008-11-17 21:11:30 -05004927 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004928 for (i = 0; i < map->num_stripes; i++) {
4929 device = map->stripes[i].dev;
4930 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004931
4932 btrfs_set_stack_stripe_devid(stripe, device->devid);
4933 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4934 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4935 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004936 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004937 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05004938
4939 btrfs_set_stack_chunk_length(chunk, chunk_size);
4940 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4941 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4942 btrfs_set_stack_chunk_type(chunk, map->type);
4943 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4944 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4945 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004946 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05004947 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4948
4949 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4950 key.type = BTRFS_CHUNK_ITEM_KEY;
4951 key.offset = chunk_offset;
4952
4953 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004954 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4955 /*
4956 * TODO: Cleanup of inserted chunk root in case of
4957 * failure.
4958 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004959 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004960 }
liubo1abe9b82011-03-24 11:18:59 +00004961
Josef Bacik6df9a952013-06-27 13:22:46 -04004962out:
Yan Zheng2b820322008-11-17 21:11:30 -05004963 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004964 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004965 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004966}
4967
4968/*
4969 * Chunk allocation falls into two parts. The first part does works
4970 * that make the new allocated chunk useable, but not do any operation
4971 * that modifies the chunk tree. The second part does the works that
4972 * require modifying the chunk tree. This division is important for the
4973 * bootstrap process of adding storage to a seed btrfs.
4974 */
4975int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004976 struct btrfs_fs_info *fs_info, u64 type)
Yan Zheng2b820322008-11-17 21:11:30 -05004977{
4978 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004979
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004980 ASSERT(mutex_is_locked(&fs_info->chunk_mutex));
4981 chunk_offset = find_next_chunk(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01004982 return __btrfs_alloc_chunk(trans, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004983}
4984
Chris Masond3977122009-01-05 21:25:51 -05004985static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +01004986 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05004987{
4988 u64 chunk_offset;
4989 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004990 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004991 int ret;
4992
Josef Bacik6df9a952013-06-27 13:22:46 -04004993 chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04004994 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01004995 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004996 if (ret)
4997 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004998
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004999 sys_chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005000 alloc_profile = btrfs_system_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005001 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005002 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005003}
5004
Miao Xied20983b2014-07-03 18:22:13 +08005005static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5006{
5007 int max_errors;
5008
5009 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5010 BTRFS_BLOCK_GROUP_RAID10 |
5011 BTRFS_BLOCK_GROUP_RAID5 |
5012 BTRFS_BLOCK_GROUP_DUP)) {
5013 max_errors = 1;
5014 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5015 max_errors = 2;
5016 } else {
5017 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005018 }
5019
Miao Xied20983b2014-07-03 18:22:13 +08005020 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05005021}
5022
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005023int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005024{
5025 struct extent_map *em;
5026 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005027 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005028 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005029 int i;
5030
Liu Bo592d92e2017-03-14 13:33:55 -07005031 em = get_chunk_map(fs_info, chunk_offset, 1);
5032 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005033 return 1;
5034
Jeff Mahoney95617d62015-06-03 10:55:48 -04005035 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005036 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08005037 if (map->stripes[i].dev->missing) {
5038 miss_ndevs++;
5039 continue;
5040 }
5041
Yan Zheng2b820322008-11-17 21:11:30 -05005042 if (!map->stripes[i].dev->writeable) {
5043 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005044 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005045 }
5046 }
Miao Xied20983b2014-07-03 18:22:13 +08005047
5048 /*
5049 * If the number of missing devices is larger than max errors,
5050 * we can not write the data into that chunk successfully, so
5051 * set it readonly.
5052 */
5053 if (miss_ndevs > btrfs_chunk_max_errors(map))
5054 readonly = 1;
5055end:
Yan Zheng2b820322008-11-17 21:11:30 -05005056 free_extent_map(em);
5057 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005058}
5059
5060void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
5061{
David Sterbaa8067e02011-04-21 00:34:43 +02005062 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04005063}
5064
5065void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
5066{
5067 struct extent_map *em;
5068
Chris Masond3977122009-01-05 21:25:51 -05005069 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04005070 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005071 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
5072 if (em)
5073 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005074 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005075 if (!em)
5076 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005077 /* once for us */
5078 free_extent_map(em);
5079 /* once for the tree */
5080 free_extent_map(em);
5081 }
5082}
5083
Stefan Behrens5d964052012-11-05 14:59:07 +01005084int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005085{
5086 struct extent_map *em;
5087 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005088 int ret;
5089
Liu Bo592d92e2017-03-14 13:33:55 -07005090 em = get_chunk_map(fs_info, logical, len);
5091 if (IS_ERR(em))
5092 /*
5093 * We could return errors for these cases, but that could get
5094 * ugly and we'd probably do the same thing which is just not do
5095 * anything else and exit, so return 1 so the callers don't try
5096 * to use other copies.
5097 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005098 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005099
Jeff Mahoney95617d62015-06-03 10:55:48 -04005100 map = em->map_lookup;
Chris Masonf1885912008-04-09 16:28:12 -04005101 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5102 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005103 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5104 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005105 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5106 ret = 2;
5107 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5108 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04005109 else
5110 ret = 1;
5111 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005112
Liu Bo73beece2015-07-17 16:49:19 +08005113 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Liu Bo6fad8232017-03-14 13:33:59 -07005114 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5115 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005116 ret++;
Liu Bo73beece2015-07-17 16:49:19 +08005117 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005118
Chris Masonf1885912008-04-09 16:28:12 -04005119 return ret;
5120}
5121
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005122unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005123 u64 logical)
5124{
5125 struct extent_map *em;
5126 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005127 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005128
Liu Bo592d92e2017-03-14 13:33:55 -07005129 em = get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005130
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005131 if (!WARN_ON(IS_ERR(em))) {
5132 map = em->map_lookup;
5133 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5134 len = map->stripe_len * nr_data_stripes(map);
5135 free_extent_map(em);
5136 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005137 return len;
5138}
5139
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005140int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005141{
5142 struct extent_map *em;
5143 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005144 int ret = 0;
5145
Liu Bo592d92e2017-03-14 13:33:55 -07005146 em = get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005147
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005148 if(!WARN_ON(IS_ERR(em))) {
5149 map = em->map_lookup;
5150 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5151 ret = 1;
5152 free_extent_map(em);
5153 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005154 return ret;
5155}
5156
Stefan Behrens30d98612012-11-06 14:52:18 +01005157static int find_live_mirror(struct btrfs_fs_info *fs_info,
5158 struct map_lookup *map, int first, int num,
5159 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005160{
5161 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01005162 int tolerance;
5163 struct btrfs_device *srcdev;
5164
5165 if (dev_replace_is_ongoing &&
5166 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5167 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5168 srcdev = fs_info->dev_replace.srcdev;
5169 else
5170 srcdev = NULL;
5171
5172 /*
5173 * try to avoid the drive that is the source drive for a
5174 * dev-replace procedure, only choose it if no other non-missing
5175 * mirror is available
5176 */
5177 for (tolerance = 0; tolerance < 2; tolerance++) {
5178 if (map->stripes[optimal].dev->bdev &&
5179 (tolerance || map->stripes[optimal].dev != srcdev))
5180 return optimal;
5181 for (i = first; i < first + num; i++) {
5182 if (map->stripes[i].dev->bdev &&
5183 (tolerance || map->stripes[i].dev != srcdev))
5184 return i;
5185 }
Chris Masondfe25022008-05-13 13:46:40 -04005186 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005187
Chris Masondfe25022008-05-13 13:46:40 -04005188 /* we couldn't find one that doesn't fail. Just return something
5189 * and the io error handling code will clean up eventually
5190 */
5191 return optimal;
5192}
5193
David Woodhouse53b381b2013-01-29 18:40:14 -05005194static inline int parity_smaller(u64 a, u64 b)
5195{
5196 return a > b;
5197}
5198
5199/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005200static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005201{
5202 struct btrfs_bio_stripe s;
5203 int i;
5204 u64 l;
5205 int again = 1;
5206
5207 while (again) {
5208 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005209 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005210 if (parity_smaller(bbio->raid_map[i],
5211 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005212 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005213 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005214 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005215 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005216 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005217 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005218
David Woodhouse53b381b2013-01-29 18:40:14 -05005219 again = 1;
5220 }
5221 }
5222 }
5223}
5224
Zhao Lei6e9606d2015-01-20 15:11:34 +08005225static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5226{
5227 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005228 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005229 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005230 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005231 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005232 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005233 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005234 /*
5235 * plus the raid_map, which includes both the tgt dev
5236 * and the stripes
5237 */
5238 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005239 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005240
5241 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005242 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005243
5244 return bbio;
5245}
5246
5247void btrfs_get_bbio(struct btrfs_bio *bbio)
5248{
Elena Reshetova140475a2017-03-03 10:55:10 +02005249 WARN_ON(!refcount_read(&bbio->refs));
5250 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005251}
5252
5253void btrfs_put_bbio(struct btrfs_bio *bbio)
5254{
5255 if (!bbio)
5256 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005257 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005258 kfree(bbio);
5259}
5260
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005261/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5262/*
5263 * Please note that, discard won't be sent to target device of device
5264 * replace.
5265 */
5266static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5267 u64 logical, u64 length,
5268 struct btrfs_bio **bbio_ret)
5269{
5270 struct extent_map *em;
5271 struct map_lookup *map;
5272 struct btrfs_bio *bbio;
5273 u64 offset;
5274 u64 stripe_nr;
5275 u64 stripe_nr_end;
5276 u64 stripe_end_offset;
5277 u64 stripe_cnt;
5278 u64 stripe_len;
5279 u64 stripe_offset;
5280 u64 num_stripes;
5281 u32 stripe_index;
5282 u32 factor = 0;
5283 u32 sub_stripes = 0;
5284 u64 stripes_per_dev = 0;
5285 u32 remaining_stripes = 0;
5286 u32 last_stripe = 0;
5287 int ret = 0;
5288 int i;
5289
5290 /* discard always return a bbio */
5291 ASSERT(bbio_ret);
5292
5293 em = get_chunk_map(fs_info, logical, length);
5294 if (IS_ERR(em))
5295 return PTR_ERR(em);
5296
5297 map = em->map_lookup;
5298 /* we don't discard raid56 yet */
5299 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5300 ret = -EOPNOTSUPP;
5301 goto out;
5302 }
5303
5304 offset = logical - em->start;
5305 length = min_t(u64, em->len - offset, length);
5306
5307 stripe_len = map->stripe_len;
5308 /*
5309 * stripe_nr counts the total number of stripes we have to stride
5310 * to get to this block
5311 */
5312 stripe_nr = div64_u64(offset, stripe_len);
5313
5314 /* stripe_offset is the offset of this block in its stripe */
5315 stripe_offset = offset - stripe_nr * stripe_len;
5316
5317 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005318 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005319 stripe_cnt = stripe_nr_end - stripe_nr;
5320 stripe_end_offset = stripe_nr_end * map->stripe_len -
5321 (offset + length);
5322 /*
5323 * after this, stripe_nr is the number of stripes on this
5324 * device we have to walk to find the data, and stripe_index is
5325 * the number of our device in the stripe array
5326 */
5327 num_stripes = 1;
5328 stripe_index = 0;
5329 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5330 BTRFS_BLOCK_GROUP_RAID10)) {
5331 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5332 sub_stripes = 1;
5333 else
5334 sub_stripes = map->sub_stripes;
5335
5336 factor = map->num_stripes / sub_stripes;
5337 num_stripes = min_t(u64, map->num_stripes,
5338 sub_stripes * stripe_cnt);
5339 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5340 stripe_index *= sub_stripes;
5341 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5342 &remaining_stripes);
5343 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5344 last_stripe *= sub_stripes;
5345 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5346 BTRFS_BLOCK_GROUP_DUP)) {
5347 num_stripes = map->num_stripes;
5348 } else {
5349 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5350 &stripe_index);
5351 }
5352
5353 bbio = alloc_btrfs_bio(num_stripes, 0);
5354 if (!bbio) {
5355 ret = -ENOMEM;
5356 goto out;
5357 }
5358
5359 for (i = 0; i < num_stripes; i++) {
5360 bbio->stripes[i].physical =
5361 map->stripes[stripe_index].physical +
5362 stripe_offset + stripe_nr * map->stripe_len;
5363 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5364
5365 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5366 BTRFS_BLOCK_GROUP_RAID10)) {
5367 bbio->stripes[i].length = stripes_per_dev *
5368 map->stripe_len;
5369
5370 if (i / sub_stripes < remaining_stripes)
5371 bbio->stripes[i].length +=
5372 map->stripe_len;
5373
5374 /*
5375 * Special for the first stripe and
5376 * the last stripe:
5377 *
5378 * |-------|...|-------|
5379 * |----------|
5380 * off end_off
5381 */
5382 if (i < sub_stripes)
5383 bbio->stripes[i].length -=
5384 stripe_offset;
5385
5386 if (stripe_index >= last_stripe &&
5387 stripe_index <= (last_stripe +
5388 sub_stripes - 1))
5389 bbio->stripes[i].length -=
5390 stripe_end_offset;
5391
5392 if (i == sub_stripes - 1)
5393 stripe_offset = 0;
5394 } else {
5395 bbio->stripes[i].length = length;
5396 }
5397
5398 stripe_index++;
5399 if (stripe_index == map->num_stripes) {
5400 stripe_index = 0;
5401 stripe_nr++;
5402 }
5403 }
5404
5405 *bbio_ret = bbio;
5406 bbio->map_type = map->type;
5407 bbio->num_stripes = num_stripes;
5408out:
5409 free_extent_map(em);
5410 return ret;
5411}
5412
Liu Bo5ab56092017-03-14 13:33:57 -07005413/*
5414 * In dev-replace case, for repair case (that's the only case where the mirror
5415 * is selected explicitly when calling btrfs_map_block), blocks left of the
5416 * left cursor can also be read from the target drive.
5417 *
5418 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5419 * array of stripes.
5420 * For READ, it also needs to be supported using the same mirror number.
5421 *
5422 * If the requested block is not left of the left cursor, EIO is returned. This
5423 * can happen because btrfs_num_copies() returns one more in the dev-replace
5424 * case.
5425 */
5426static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5427 u64 logical, u64 length,
5428 u64 srcdev_devid, int *mirror_num,
5429 u64 *physical)
5430{
5431 struct btrfs_bio *bbio = NULL;
5432 int num_stripes;
5433 int index_srcdev = 0;
5434 int found = 0;
5435 u64 physical_of_found = 0;
5436 int i;
5437 int ret = 0;
5438
5439 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5440 logical, &length, &bbio, 0, 0);
5441 if (ret) {
5442 ASSERT(bbio == NULL);
5443 return ret;
5444 }
5445
5446 num_stripes = bbio->num_stripes;
5447 if (*mirror_num > num_stripes) {
5448 /*
5449 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5450 * that means that the requested area is not left of the left
5451 * cursor
5452 */
5453 btrfs_put_bbio(bbio);
5454 return -EIO;
5455 }
5456
5457 /*
5458 * process the rest of the function using the mirror_num of the source
5459 * drive. Therefore look it up first. At the end, patch the device
5460 * pointer to the one of the target drive.
5461 */
5462 for (i = 0; i < num_stripes; i++) {
5463 if (bbio->stripes[i].dev->devid != srcdev_devid)
5464 continue;
5465
5466 /*
5467 * In case of DUP, in order to keep it simple, only add the
5468 * mirror with the lowest physical address
5469 */
5470 if (found &&
5471 physical_of_found <= bbio->stripes[i].physical)
5472 continue;
5473
5474 index_srcdev = i;
5475 found = 1;
5476 physical_of_found = bbio->stripes[i].physical;
5477 }
5478
5479 btrfs_put_bbio(bbio);
5480
5481 ASSERT(found);
5482 if (!found)
5483 return -EIO;
5484
5485 *mirror_num = index_srcdev + 1;
5486 *physical = physical_of_found;
5487 return ret;
5488}
5489
Liu Bo73c0f222017-03-14 13:33:58 -07005490static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5491 struct btrfs_bio **bbio_ret,
5492 struct btrfs_dev_replace *dev_replace,
5493 int *num_stripes_ret, int *max_errors_ret)
5494{
5495 struct btrfs_bio *bbio = *bbio_ret;
5496 u64 srcdev_devid = dev_replace->srcdev->devid;
5497 int tgtdev_indexes = 0;
5498 int num_stripes = *num_stripes_ret;
5499 int max_errors = *max_errors_ret;
5500 int i;
5501
5502 if (op == BTRFS_MAP_WRITE) {
5503 int index_where_to_add;
5504
5505 /*
5506 * duplicate the write operations while the dev replace
5507 * procedure is running. Since the copying of the old disk to
5508 * the new disk takes place at run time while the filesystem is
5509 * mounted writable, the regular write operations to the old
5510 * disk have to be duplicated to go to the new disk as well.
5511 *
5512 * Note that device->missing is handled by the caller, and that
5513 * the write to the old disk is already set up in the stripes
5514 * array.
5515 */
5516 index_where_to_add = num_stripes;
5517 for (i = 0; i < num_stripes; i++) {
5518 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5519 /* write to new disk, too */
5520 struct btrfs_bio_stripe *new =
5521 bbio->stripes + index_where_to_add;
5522 struct btrfs_bio_stripe *old =
5523 bbio->stripes + i;
5524
5525 new->physical = old->physical;
5526 new->length = old->length;
5527 new->dev = dev_replace->tgtdev;
5528 bbio->tgtdev_map[i] = index_where_to_add;
5529 index_where_to_add++;
5530 max_errors++;
5531 tgtdev_indexes++;
5532 }
5533 }
5534 num_stripes = index_where_to_add;
5535 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5536 int index_srcdev = 0;
5537 int found = 0;
5538 u64 physical_of_found = 0;
5539
5540 /*
5541 * During the dev-replace procedure, the target drive can also
5542 * be used to read data in case it is needed to repair a corrupt
5543 * block elsewhere. This is possible if the requested area is
5544 * left of the left cursor. In this area, the target drive is a
5545 * full copy of the source drive.
5546 */
5547 for (i = 0; i < num_stripes; i++) {
5548 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5549 /*
5550 * In case of DUP, in order to keep it simple,
5551 * only add the mirror with the lowest physical
5552 * address
5553 */
5554 if (found &&
5555 physical_of_found <=
5556 bbio->stripes[i].physical)
5557 continue;
5558 index_srcdev = i;
5559 found = 1;
5560 physical_of_found = bbio->stripes[i].physical;
5561 }
5562 }
5563 if (found) {
5564 struct btrfs_bio_stripe *tgtdev_stripe =
5565 bbio->stripes + num_stripes;
5566
5567 tgtdev_stripe->physical = physical_of_found;
5568 tgtdev_stripe->length =
5569 bbio->stripes[index_srcdev].length;
5570 tgtdev_stripe->dev = dev_replace->tgtdev;
5571 bbio->tgtdev_map[index_srcdev] = num_stripes;
5572
5573 tgtdev_indexes++;
5574 num_stripes++;
5575 }
5576 }
5577
5578 *num_stripes_ret = num_stripes;
5579 *max_errors_ret = max_errors;
5580 bbio->num_tgtdevs = tgtdev_indexes;
5581 *bbio_ret = bbio;
5582}
5583
Liu Bo2b19a1f2017-03-14 13:34:00 -07005584static bool need_full_stripe(enum btrfs_map_op op)
5585{
5586 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5587}
5588
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005589static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5590 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005591 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005592 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005593 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005594{
5595 struct extent_map *em;
5596 struct map_lookup *map;
Chris Mason0b86a832008-03-24 15:01:56 -04005597 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005598 u64 stripe_offset;
5599 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005600 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005601 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005602 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005603 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005604 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005605 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005606 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005607 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005608 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5609 int dev_replace_is_ongoing = 0;
5610 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005611 int patch_the_first_stripe_for_dev_replace = 0;
5612 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005613 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005614
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005615 if (op == BTRFS_MAP_DISCARD)
5616 return __btrfs_map_block_for_discard(fs_info, logical,
5617 *length, bbio_ret);
5618
Liu Bo592d92e2017-03-14 13:33:55 -07005619 em = get_chunk_map(fs_info, logical, *length);
5620 if (IS_ERR(em))
5621 return PTR_ERR(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005622
Jeff Mahoney95617d62015-06-03 10:55:48 -04005623 map = em->map_lookup;
Chris Mason0b86a832008-03-24 15:01:56 -04005624 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005625
David Woodhouse53b381b2013-01-29 18:40:14 -05005626 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005627 stripe_nr = offset;
5628 /*
5629 * stripe_nr counts the total number of stripes we have to stride
5630 * to get to this block
5631 */
David Sterba47c57132015-02-20 18:43:47 +01005632 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005633
David Woodhouse53b381b2013-01-29 18:40:14 -05005634 stripe_offset = stripe_nr * stripe_len;
Josef Bacike042d1e2016-04-12 12:54:40 -04005635 if (offset < stripe_offset) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005636 btrfs_crit(fs_info,
5637 "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
Josef Bacike042d1e2016-04-12 12:54:40 -04005638 stripe_offset, offset, em->start, logical,
5639 stripe_len);
5640 free_extent_map(em);
5641 return -EINVAL;
5642 }
Chris Mason593060d2008-03-25 16:50:33 -04005643
5644 /* stripe_offset is the offset of this block in its stripe*/
5645 stripe_offset = offset - stripe_offset;
5646
David Woodhouse53b381b2013-01-29 18:40:14 -05005647 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005648 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005649 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5650 raid56_full_stripe_start = offset;
5651
5652 /* allow a write of a full stripe, but make sure we don't
5653 * allow straddling of stripes
5654 */
David Sterba47c57132015-02-20 18:43:47 +01005655 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5656 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005657 raid56_full_stripe_start *= full_stripe_len;
5658 }
5659
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005660 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005661 u64 max_len;
5662 /* For writes to RAID[56], allow a full stripeset across all disks.
5663 For other RAID types and for RAID[56] reads, just allow a single
5664 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005665 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005666 (op == BTRFS_MAP_WRITE)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005667 max_len = stripe_len * nr_data_stripes(map) -
5668 (offset - raid56_full_stripe_start);
5669 } else {
5670 /* we limit the length of each bio to what fits in a stripe */
5671 max_len = stripe_len - stripe_offset;
5672 }
5673 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005674 } else {
5675 *length = em->len - offset;
5676 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005677
David Woodhouse53b381b2013-01-29 18:40:14 -05005678 /* This is for when we're called from btrfs_merge_bio_hook() and all
5679 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005680 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005681 goto out;
5682
Liu Bo73beece2015-07-17 16:49:19 +08005683 btrfs_dev_replace_lock(dev_replace, 0);
Stefan Behrens472262f2012-11-06 14:43:46 +01005684 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5685 if (!dev_replace_is_ongoing)
Liu Bo73beece2015-07-17 16:49:19 +08005686 btrfs_dev_replace_unlock(dev_replace, 0);
5687 else
5688 btrfs_dev_replace_set_lock_blocking(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005689
Stefan Behrensad6d6202012-11-06 15:06:47 +01005690 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005691 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07005692 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
5693 dev_replace->srcdev->devid,
5694 &mirror_num,
5695 &physical_to_patch_in_first_stripe);
5696 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005697 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07005698 else
5699 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005700 } else if (mirror_num > map->num_stripes) {
5701 mirror_num = 0;
5702 }
5703
Chris Masonf2d8d742008-04-21 10:03:05 -04005704 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005705 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005706 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01005707 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5708 &stripe_index);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005709 if (op != BTRFS_MAP_WRITE && op != BTRFS_MAP_GET_READ_MIRRORS)
Miao Xie28e1cc72014-09-12 18:44:02 +08005710 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005711 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005712 if (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS)
Chris Masonf2d8d742008-04-21 10:03:05 -04005713 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005714 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005715 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005716 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005717 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005718 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005719 current->pid % map->num_stripes,
5720 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005721 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005722 }
Chris Mason2fff7342008-04-29 14:12:09 -04005723
Chris Mason611f0e02008-04-03 16:29:03 -04005724 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005725 if (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005726 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005727 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005728 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005729 } else {
5730 mirror_num = 1;
5731 }
Chris Mason2fff7342008-04-29 14:12:09 -04005732
Chris Mason321aecc2008-04-16 10:49:51 -04005733 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005734 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005735
David Sterba47c57132015-02-20 18:43:47 +01005736 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005737 stripe_index *= map->sub_stripes;
5738
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005739 if (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS)
Chris Masonf2d8d742008-04-21 10:03:05 -04005740 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005741 else if (mirror_num)
5742 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005743 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005744 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005745 stripe_index = find_live_mirror(fs_info, map,
5746 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005747 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005748 current->pid % map->sub_stripes,
5749 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005750 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005751 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005752
Zhao Leiffe2d202015-01-20 15:11:44 +08005753 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005754 if (need_raid_map &&
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005755 (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS ||
Miao Xieaf8e2d12014-10-23 14:42:50 +08005756 mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005757 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07005758 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbab8b93ad2015-01-16 17:26:13 +01005759 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005760
5761 /* RAID[56] write or recovery. Return all stripes */
5762 num_stripes = map->num_stripes;
5763 max_errors = nr_parity_stripes(map);
5764
David Woodhouse53b381b2013-01-29 18:40:14 -05005765 *length = map->stripe_len;
5766 stripe_index = 0;
5767 stripe_offset = 0;
5768 } else {
5769 /*
5770 * Mirror #0 or #1 means the original data block.
5771 * Mirror #2 is RAID5 parity block.
5772 * Mirror #3 is RAID6 Q block.
5773 */
David Sterba47c57132015-02-20 18:43:47 +01005774 stripe_nr = div_u64_rem(stripe_nr,
5775 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005776 if (mirror_num > 1)
5777 stripe_index = nr_data_stripes(map) +
5778 mirror_num - 2;
5779
5780 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005781 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5782 &stripe_index);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005783 if ((op != BTRFS_MAP_WRITE &&
5784 op != BTRFS_MAP_GET_READ_MIRRORS) &&
5785 mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08005786 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005787 }
Chris Mason8790d502008-04-03 16:29:03 -04005788 } else {
5789 /*
David Sterba47c57132015-02-20 18:43:47 +01005790 * after this, stripe_nr is the number of stripes on this
5791 * device we have to walk to find the data, and stripe_index is
5792 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005793 */
David Sterba47c57132015-02-20 18:43:47 +01005794 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5795 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005796 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005797 }
Josef Bacike042d1e2016-04-12 12:54:40 -04005798 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005799 btrfs_crit(fs_info,
5800 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04005801 stripe_index, map->num_stripes);
5802 ret = -EINVAL;
5803 goto out;
5804 }
Chris Mason593060d2008-03-25 16:50:33 -04005805
Stefan Behrens472262f2012-11-06 14:43:46 +01005806 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07005807 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005808 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005809 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005810 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005811 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005812 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005813 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005814
Zhao Lei6e9606d2015-01-20 15:11:34 +08005815 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005816 if (!bbio) {
5817 ret = -ENOMEM;
5818 goto out;
5819 }
Liu Bo6fad8232017-03-14 13:33:59 -07005820 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
Miao Xie2c8cdd62014-11-14 16:06:25 +08005821 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005822
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005823 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07005824 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
5825 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005826 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005827 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005828
5829 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5830 sizeof(struct btrfs_bio_stripe) *
5831 num_alloc_stripes +
5832 sizeof(int) * tgtdev_indexes);
5833
5834 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005835 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005836
5837 /* Fill in the logical address of each stripe */
5838 tmp = stripe_nr * nr_data_stripes(map);
5839 for (i = 0; i < nr_data_stripes(map); i++)
5840 bbio->raid_map[(i+rot) % num_stripes] =
5841 em->start + (tmp + i) * map->stripe_len;
5842
5843 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5844 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5845 bbio->raid_map[(i+rot+1) % num_stripes] =
5846 RAID6_Q_STRIPE;
5847 }
5848
Li Zefanec9ef7a2011-12-01 14:06:42 +08005849
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005850 for (i = 0; i < num_stripes; i++) {
5851 bbio->stripes[i].physical =
5852 map->stripes[stripe_index].physical +
5853 stripe_offset +
5854 stripe_nr * map->stripe_len;
5855 bbio->stripes[i].dev =
5856 map->stripes[stripe_index].dev;
5857 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04005858 }
Li Zefande11cc12011-12-01 12:55:47 +08005859
Liu Bo2b19a1f2017-03-14 13:34:00 -07005860 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08005861 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005862
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005863 if (bbio->raid_map)
5864 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005865
Liu Bo73c0f222017-03-14 13:33:58 -07005866 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005867 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07005868 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
5869 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01005870 }
5871
Li Zefande11cc12011-12-01 12:55:47 +08005872 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005873 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005874 bbio->num_stripes = num_stripes;
5875 bbio->max_errors = max_errors;
5876 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005877
5878 /*
5879 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5880 * mirror_num == num_stripes + 1 && dev_replace target drive is
5881 * available as a mirror
5882 */
5883 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5884 WARN_ON(num_stripes > 1);
5885 bbio->stripes[0].dev = dev_replace->tgtdev;
5886 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5887 bbio->mirror_num = map->num_stripes + 1;
5888 }
Chris Masoncea9e442008-04-09 16:28:12 -04005889out:
Liu Bo73beece2015-07-17 16:49:19 +08005890 if (dev_replace_is_ongoing) {
5891 btrfs_dev_replace_clear_lock_blocking(dev_replace);
5892 btrfs_dev_replace_unlock(dev_replace, 0);
5893 }
Chris Mason0b86a832008-03-24 15:01:56 -04005894 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005895 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005896}
5897
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005898int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005899 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005900 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005901{
Mike Christieb3d3fa52016-06-05 14:31:53 -05005902 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005903 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005904}
5905
Miao Xieaf8e2d12014-10-23 14:42:50 +08005906/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005907int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08005908 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02005909 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005910{
David Sterba825ad4c2017-03-28 14:45:22 +02005911 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005912}
5913
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005914int btrfs_rmap_block(struct btrfs_fs_info *fs_info,
Yan Zhenga512bbf2008-12-08 16:46:26 -05005915 u64 chunk_start, u64 physical, u64 devid,
5916 u64 **logical, int *naddrs, int *stripe_len)
5917{
Yan Zhenga512bbf2008-12-08 16:46:26 -05005918 struct extent_map *em;
5919 struct map_lookup *map;
5920 u64 *buf;
5921 u64 bytenr;
5922 u64 length;
5923 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005924 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005925 int i, j, nr = 0;
5926
Liu Bo592d92e2017-03-14 13:33:55 -07005927 em = get_chunk_map(fs_info, chunk_start, 1);
5928 if (IS_ERR(em))
Josef Bacik835d9742013-03-19 12:13:25 -04005929 return -EIO;
Josef Bacik835d9742013-03-19 12:13:25 -04005930
Jeff Mahoney95617d62015-06-03 10:55:48 -04005931 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005932 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005933 rmap_len = map->stripe_len;
5934
Yan Zhenga512bbf2008-12-08 16:46:26 -05005935 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01005936 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005937 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01005938 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08005939 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01005940 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005941 rmap_len = map->stripe_len * nr_data_stripes(map);
5942 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005943
David Sterba31e818f2015-02-20 18:00:26 +01005944 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005945 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005946
5947 for (i = 0; i < map->num_stripes; i++) {
5948 if (devid && map->stripes[i].dev->devid != devid)
5949 continue;
5950 if (map->stripes[i].physical > physical ||
5951 map->stripes[i].physical + length <= physical)
5952 continue;
5953
5954 stripe_nr = physical - map->stripes[i].physical;
Liu Bo42c61ab2017-04-03 13:45:24 -07005955 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005956
5957 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5958 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01005959 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005960 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5961 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005962 } /* else if RAID[56], multiply by nr_data_stripes().
5963 * Alternatively, just use rmap_len below instead of
5964 * map->stripe_len */
5965
5966 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005967 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005968 for (j = 0; j < nr; j++) {
5969 if (buf[j] == bytenr)
5970 break;
5971 }
Chris Mason934d3752008-12-08 16:43:10 -05005972 if (j == nr) {
5973 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005974 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005975 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005976 }
5977
Yan Zhenga512bbf2008-12-08 16:46:26 -05005978 *logical = buf;
5979 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005980 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005981
5982 free_extent_map(em);
5983 return 0;
5984}
5985
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005986static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08005987{
Mike Snitzer326e1db2015-05-22 09:14:03 -04005988 bio->bi_private = bbio->private;
5989 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005990 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04005991
Zhao Lei6e9606d2015-01-20 15:11:34 +08005992 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08005993}
5994
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005995static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04005996{
Chris Mason9be33952013-05-17 18:30:14 -04005997 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005998 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005999
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006000 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02006001 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006002 if (bio->bi_status == BLK_STS_IOERR ||
6003 bio->bi_status == BLK_STS_TARGET) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02006004 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04006005 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08006006 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006007
6008 BUG_ON(stripe_index >= bbio->num_stripes);
6009 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02006010 if (dev->bdev) {
Mike Christie37226b22016-06-05 14:31:52 -05006011 if (bio_op(bio) == REQ_OP_WRITE)
Stefan Behrens597a60f2012-06-14 16:42:31 +02006012 btrfs_dev_stat_inc(dev,
6013 BTRFS_DEV_STAT_WRITE_ERRS);
6014 else
6015 btrfs_dev_stat_inc(dev,
6016 BTRFS_DEV_STAT_READ_ERRS);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06006017 if (bio->bi_opf & REQ_PREFLUSH)
Stefan Behrens597a60f2012-06-14 16:42:31 +02006018 btrfs_dev_stat_inc(dev,
6019 BTRFS_DEV_STAT_FLUSH_ERRS);
6020 btrfs_dev_stat_print_on_error(dev);
6021 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02006022 }
6023 }
Chris Mason8790d502008-04-03 16:29:03 -04006024
Jan Schmidta1d3c472011-08-04 17:15:33 +02006025 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006026 is_orig_bio = 1;
6027
Miao Xiec404e0d2014-01-30 16:46:55 +08006028 btrfs_bio_counter_dec(bbio->fs_info);
6029
Jan Schmidta1d3c472011-08-04 17:15:33 +02006030 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006031 if (!is_orig_bio) {
6032 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006033 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006034 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006035
Chris Mason9be33952013-05-17 18:30:14 -04006036 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006037 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006038 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006039 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006040 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006041 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006042 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006043 /*
6044 * this bio is actually up to date, we didn't
6045 * go over the max number of errors
6046 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006047 bio->bi_status = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04006048 }
Miao Xiec55f1392014-06-19 10:42:54 +08006049
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006050 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006051 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006052 bio_put(bio);
6053 }
Chris Mason8790d502008-04-03 16:29:03 -04006054}
6055
Chris Mason8b712842008-06-11 16:50:36 -04006056/*
6057 * see run_scheduled_bios for a description of why bios are collected for
6058 * async submit.
6059 *
6060 * This will add one bio to the pending list for a device and make sure
6061 * the work struct is scheduled.
6062 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006063static noinline void btrfs_schedule_bio(struct btrfs_device *device,
Mike Christie4e49ea42016-06-05 14:31:41 -05006064 struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04006065{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006066 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -04006067 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04006068 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006069
David Woodhouse53b381b2013-01-29 18:40:14 -05006070 if (device->missing || !device->bdev) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006071 bio_io_error(bio);
David Woodhouse53b381b2013-01-29 18:40:14 -05006072 return;
6073 }
6074
Chris Mason8b712842008-06-11 16:50:36 -04006075 /* don't bother with additional async steps for reads, right now */
Mike Christie37226b22016-06-05 14:31:52 -05006076 if (bio_op(bio) == REQ_OP_READ) {
Chris Mason492bb6de2008-07-31 16:29:02 -04006077 bio_get(bio);
Mike Christie4e49ea42016-06-05 14:31:41 -05006078 btrfsic_submit_bio(bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04006079 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01006080 return;
Chris Mason8b712842008-06-11 16:50:36 -04006081 }
6082
6083 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04006084 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04006085 * higher layers. Otherwise, the async bio makes it appear we have
6086 * made progress against dirty pages when we've really just put it
6087 * on a queue for later
6088 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006089 atomic_inc(&fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04006090 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04006091 bio->bi_next = NULL;
Chris Mason8b712842008-06-11 16:50:36 -04006092
6093 spin_lock(&device->io_lock);
Christoph Hellwig67f055c2016-11-01 07:40:06 -06006094 if (op_is_sync(bio->bi_opf))
Chris Masonffbd5172009-04-20 15:50:09 -04006095 pending_bios = &device->pending_sync_bios;
6096 else
6097 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006098
Chris Masonffbd5172009-04-20 15:50:09 -04006099 if (pending_bios->tail)
6100 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006101
Chris Masonffbd5172009-04-20 15:50:09 -04006102 pending_bios->tail = bio;
6103 if (!pending_bios->head)
6104 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006105 if (device->running_pending)
6106 should_queue = 0;
6107
6108 spin_unlock(&device->io_lock);
6109
6110 if (should_queue)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006111 btrfs_queue_work(fs_info->submit_workers, &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04006112}
6113
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006114static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6115 u64 physical, int dev_nr, int async)
Josef Bacikde1ee922012-10-19 16:50:56 -04006116{
6117 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006118 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006119
6120 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006121 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006122 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006123 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04006124#ifdef DEBUG
6125 {
6126 struct rcu_string *name;
6127
6128 rcu_read_lock();
6129 name = rcu_dereference(dev->name);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006130 btrfs_debug(fs_info,
6131 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6132 bio_op(bio), bio->bi_opf,
6133 (u64)bio->bi_iter.bi_sector,
6134 (u_long)dev->bdev->bd_dev, name->str, dev->devid,
6135 bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04006136 rcu_read_unlock();
6137 }
6138#endif
6139 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08006140
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006141 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006142
Josef Bacikde1ee922012-10-19 16:50:56 -04006143 if (async)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006144 btrfs_schedule_bio(dev, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006145 else
Mike Christie4e49ea42016-06-05 14:31:41 -05006146 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006147}
6148
Josef Bacikde1ee922012-10-19 16:50:56 -04006149static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6150{
6151 atomic_inc(&bbio->error);
6152 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006153 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006154 WARN_ON(bio != bbio->orig_bio);
6155
Chris Mason9be33952013-05-17 18:30:14 -04006156 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006157 bio->bi_iter.bi_sector = logical >> 9;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006158 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006159 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006160 }
6161}
6162
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006163int btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04006164 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006165{
Chris Mason0b86a832008-03-24 15:01:56 -04006166 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006167 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006168 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006169 u64 length = 0;
6170 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006171 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006172 int dev_nr;
6173 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006174 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006175
Kent Overstreet4f024f32013-10-11 15:44:27 -07006176 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006177 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006178
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006179 btrfs_bio_counter_inc_blocked(fs_info);
6180 ret = __btrfs_map_block(fs_info, bio_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006181 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006182 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006183 btrfs_bio_counter_dec(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006184 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08006185 }
Chris Masoncea9e442008-04-09 16:28:12 -04006186
Jan Schmidta1d3c472011-08-04 17:15:33 +02006187 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006188 bbio->orig_bio = first_bio;
6189 bbio->private = first_bio->bi_private;
6190 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006191 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006192 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6193
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006194 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006195 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006196 /* In this case, map_length has been set to the length of
6197 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006198 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006199 ret = raid56_parity_write(fs_info, bio, bbio,
6200 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006201 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006202 ret = raid56_parity_recover(fs_info, bio, bbio,
6203 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006204 }
Miao Xie42452152014-11-25 16:39:28 +08006205
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006206 btrfs_bio_counter_dec(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006207 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05006208 }
6209
Chris Masoncea9e442008-04-09 16:28:12 -04006210 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006211 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006212 "mapping failed logical %llu bio len %llu len %llu",
6213 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006214 BUG();
6215 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006216
Zhao Lei08da7572015-02-12 15:42:16 +08006217 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006218 dev = bbio->stripes[dev_nr].dev;
Mike Christie37226b22016-06-05 14:31:52 -05006219 if (!dev || !dev->bdev ||
Liu Boa967efb2017-04-10 12:36:26 -07006220 (bio_op(first_bio) == REQ_OP_WRITE && !dev->writeable)) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006221 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006222 continue;
6223 }
6224
David Sterba3aa8e072017-06-02 17:38:30 +02006225 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006226 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006227 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006228 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006229
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006230 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6231 dev_nr, async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006232 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006233 btrfs_bio_counter_dec(fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04006234 return 0;
6235}
6236
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006237struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006238 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006239{
Yan Zheng2b820322008-11-17 21:11:30 -05006240 struct btrfs_device *device;
6241 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006242
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006243 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006244 while (cur_devices) {
6245 if (!fsid ||
6246 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
David Sterba35c70102017-06-15 19:51:51 +02006247 device = find_device(cur_devices, devid, uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006248 if (device)
6249 return device;
6250 }
6251 cur_devices = cur_devices->seed;
6252 }
6253 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006254}
6255
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006256static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006257 u64 devid, u8 *dev_uuid)
6258{
6259 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006260
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006261 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6262 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05006263 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006264
6265 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006266 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006267 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006268
6269 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05006270 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006271
Chris Masondfe25022008-05-13 13:46:40 -04006272 return device;
6273}
6274
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006275/**
6276 * btrfs_alloc_device - allocate struct btrfs_device
6277 * @fs_info: used only for generating a new devid, can be NULL if
6278 * devid is provided (i.e. @devid != NULL).
6279 * @devid: a pointer to devid for this device. If NULL a new devid
6280 * is generated.
6281 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6282 * is generated.
6283 *
6284 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6285 * on error. Returned struct is not linked onto any lists and can be
6286 * destroyed with kfree() right away.
6287 */
6288struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6289 const u64 *devid,
6290 const u8 *uuid)
6291{
6292 struct btrfs_device *dev;
6293 u64 tmp;
6294
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306295 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006296 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006297
6298 dev = __alloc_device();
6299 if (IS_ERR(dev))
6300 return dev;
6301
6302 if (devid)
6303 tmp = *devid;
6304 else {
6305 int ret;
6306
6307 ret = find_next_devid(fs_info, &tmp);
6308 if (ret) {
6309 kfree(dev);
6310 return ERR_PTR(ret);
6311 }
6312 }
6313 dev->devid = tmp;
6314
6315 if (uuid)
6316 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6317 else
6318 generate_random_uuid(dev->uuid);
6319
Liu Bo9e0af232014-08-15 23:36:53 +08006320 btrfs_init_work(&dev->work, btrfs_submit_helper,
6321 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006322
6323 return dev;
6324}
6325
Liu Boe06cd3d2016-06-03 12:05:15 -07006326/* Return -EIO if any error, otherwise return 0. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006327static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006328 struct extent_buffer *leaf,
6329 struct btrfs_chunk *chunk, u64 logical)
6330{
6331 u64 length;
6332 u64 stripe_len;
6333 u16 num_stripes;
6334 u16 sub_stripes;
6335 u64 type;
6336
6337 length = btrfs_chunk_length(leaf, chunk);
6338 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6339 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6340 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6341 type = btrfs_chunk_type(leaf, chunk);
6342
6343 if (!num_stripes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006344 btrfs_err(fs_info, "invalid chunk num_stripes: %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006345 num_stripes);
6346 return -EIO;
6347 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006348 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
6349 btrfs_err(fs_info, "invalid chunk logical %llu", logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006350 return -EIO;
6351 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006352 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
6353 btrfs_err(fs_info, "invalid chunk sectorsize %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006354 btrfs_chunk_sector_size(leaf, chunk));
6355 return -EIO;
6356 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006357 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
6358 btrfs_err(fs_info, "invalid chunk length %llu", length);
Liu Boe06cd3d2016-06-03 12:05:15 -07006359 return -EIO;
6360 }
6361 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006362 btrfs_err(fs_info, "invalid chunk stripe length: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006363 stripe_len);
6364 return -EIO;
6365 }
6366 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6367 type) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006368 btrfs_err(fs_info, "unrecognized chunk type: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006369 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6370 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6371 btrfs_chunk_type(leaf, chunk));
6372 return -EIO;
6373 }
6374 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
6375 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
6376 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
6377 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
6378 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
6379 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
6380 num_stripes != 1)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006381 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006382 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
6383 num_stripes, sub_stripes,
6384 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
6385 return -EIO;
6386 }
6387
6388 return 0;
6389}
6390
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006391static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
Chris Mason0b86a832008-03-24 15:01:56 -04006392 struct extent_buffer *leaf,
6393 struct btrfs_chunk *chunk)
6394{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006395 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006396 struct map_lookup *map;
6397 struct extent_map *em;
6398 u64 logical;
6399 u64 length;
6400 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006401 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006402 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006403 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006404 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006405
Chris Masone17cade2008-04-15 15:41:47 -04006406 logical = key->offset;
6407 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006408 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006409
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006410 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006411 if (ret)
6412 return ret;
Chris Masona061fc82008-05-07 11:43:44 -04006413
Chris Mason890871b2009-09-02 16:24:52 -04006414 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006415 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006416 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006417
6418 /* already mapped? */
6419 if (em && em->start <= logical && em->start + em->len > logical) {
6420 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006421 return 0;
6422 } else if (em) {
6423 free_extent_map(em);
6424 }
Chris Mason0b86a832008-03-24 15:01:56 -04006425
David Sterba172ddd62011-04-21 00:48:27 +02006426 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006427 if (!em)
6428 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006429 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006430 if (!map) {
6431 free_extent_map(em);
6432 return -ENOMEM;
6433 }
6434
Wang Shilong298a8f92014-06-19 10:42:52 +08006435 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006436 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006437 em->start = logical;
6438 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006439 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006440 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006441 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006442
Chris Mason593060d2008-03-25 16:50:33 -04006443 map->num_stripes = num_stripes;
6444 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6445 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6446 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
6447 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6448 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006449 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006450 for (i = 0; i < num_stripes; i++) {
6451 map->stripes[i].physical =
6452 btrfs_stripe_offset_nr(leaf, chunk, i);
6453 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006454 read_extent_buffer(leaf, uuid, (unsigned long)
6455 btrfs_stripe_dev_uuid_nr(chunk, i),
6456 BTRFS_UUID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006457 map->stripes[i].dev = btrfs_find_device(fs_info, devid,
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006458 uuid, NULL);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006459 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006460 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006461 free_extent_map(em);
Qu Wenruoc5502452017-03-09 09:34:42 +08006462 btrfs_report_missing_device(fs_info, devid, uuid);
Chris Mason593060d2008-03-25 16:50:33 -04006463 return -EIO;
6464 }
Chris Masondfe25022008-05-13 13:46:40 -04006465 if (!map->stripes[i].dev) {
6466 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006467 add_missing_dev(fs_info->fs_devices, devid,
6468 uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006469 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04006470 free_extent_map(em);
6471 return -EIO;
6472 }
Qu Wenruoc5502452017-03-09 09:34:42 +08006473 btrfs_report_missing_device(fs_info, devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006474 }
6475 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04006476 }
6477
Chris Mason890871b2009-09-02 16:24:52 -04006478 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006479 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006480 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006481 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006482 free_extent_map(em);
6483
6484 return 0;
6485}
6486
Jeff Mahoney143bede2012-03-01 14:56:26 +01006487static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006488 struct btrfs_dev_item *dev_item,
6489 struct btrfs_device *device)
6490{
6491 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006492
6493 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006494 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6495 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006496 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006497 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006498 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006499 device->type = btrfs_device_type(leaf, dev_item);
6500 device->io_align = btrfs_device_io_align(leaf, dev_item);
6501 device->io_width = btrfs_device_io_width(leaf, dev_item);
6502 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006503 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006504 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006505
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006506 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006507 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006508}
6509
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006510static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006511 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006512{
6513 struct btrfs_fs_devices *fs_devices;
6514 int ret;
6515
Li Zefanb367e472011-12-07 11:38:24 +08006516 BUG_ON(!mutex_is_locked(&uuid_mutex));
David Sterba2dfeca92017-06-14 02:48:07 +02006517 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006518
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006519 fs_devices = fs_info->fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006520 while (fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006521 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE))
6522 return fs_devices;
6523
Yan Zheng2b820322008-11-17 21:11:30 -05006524 fs_devices = fs_devices->seed;
6525 }
6526
6527 fs_devices = find_fsid(fsid);
6528 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006529 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006530 return ERR_PTR(-ENOENT);
6531
6532 fs_devices = alloc_fs_devices(fsid);
6533 if (IS_ERR(fs_devices))
6534 return fs_devices;
6535
6536 fs_devices->seeding = 1;
6537 fs_devices->opened = 1;
6538 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006539 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006540
6541 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006542 if (IS_ERR(fs_devices))
6543 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006544
Christoph Hellwig97288f22008-12-02 06:36:09 -05006545 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006546 fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006547 if (ret) {
6548 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006549 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006550 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006551 }
Yan Zheng2b820322008-11-17 21:11:30 -05006552
6553 if (!fs_devices->seeding) {
6554 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006555 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006556 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006557 goto out;
6558 }
6559
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006560 fs_devices->seed = fs_info->fs_devices->seed;
6561 fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006562out:
Miao Xie5f375832014-09-03 21:35:46 +08006563 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006564}
6565
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006566static int read_one_dev(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04006567 struct extent_buffer *leaf,
6568 struct btrfs_dev_item *dev_item)
6569{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006570 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006571 struct btrfs_device *device;
6572 u64 devid;
6573 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006574 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006575 u8 dev_uuid[BTRFS_UUID_SIZE];
6576
Chris Mason0b86a832008-03-24 15:01:56 -04006577 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006578 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006579 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006580 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006581 BTRFS_UUID_SIZE);
6582
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006583 if (memcmp(fs_uuid, fs_info->fsid, BTRFS_UUID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006584 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006585 if (IS_ERR(fs_devices))
6586 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006587 }
6588
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006589 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006590 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08006591 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6592 btrfs_report_missing_device(fs_info, devid, dev_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006593 return -EIO;
Qu Wenruoc5502452017-03-09 09:34:42 +08006594 }
Yan Zheng2b820322008-11-17 21:11:30 -05006595
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006596 device = add_missing_dev(fs_devices, devid, dev_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006597 if (!device)
6598 return -ENOMEM;
Qu Wenruoc5502452017-03-09 09:34:42 +08006599 btrfs_report_missing_device(fs_info, devid, dev_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006600 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08006601 if (!device->bdev) {
6602 btrfs_report_missing_device(fs_info, devid, dev_uuid);
6603 if (!btrfs_test_opt(fs_info, DEGRADED))
6604 return -EIO;
6605 }
Miao Xie5f375832014-09-03 21:35:46 +08006606
6607 if(!device->bdev && !device->missing) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006608 /*
6609 * this happens when a device that was properly setup
6610 * in the device info lists suddenly goes bad.
6611 * device->bdev is NULL, and so we have to set
6612 * device->missing to one here
6613 */
Miao Xie5f375832014-09-03 21:35:46 +08006614 device->fs_devices->missing_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05006615 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006616 }
Miao Xie5f375832014-09-03 21:35:46 +08006617
6618 /* Move the device to its own fs_devices */
6619 if (device->fs_devices != fs_devices) {
6620 ASSERT(device->missing);
6621
6622 list_move(&device->dev_list, &fs_devices->devices);
6623 device->fs_devices->num_devices--;
6624 fs_devices->num_devices++;
6625
6626 device->fs_devices->missing_devices--;
6627 fs_devices->missing_devices++;
6628
6629 device->fs_devices = fs_devices;
6630 }
Yan Zheng2b820322008-11-17 21:11:30 -05006631 }
6632
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006633 if (device->fs_devices != fs_info->fs_devices) {
Yan Zheng2b820322008-11-17 21:11:30 -05006634 BUG_ON(device->writeable);
6635 if (device->generation !=
6636 btrfs_device_generation(leaf, dev_item))
6637 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006638 }
Chris Mason0b86a832008-03-24 15:01:56 -04006639
6640 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006641 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006642 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006643 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03006644 atomic64_add(device->total_bytes - device->bytes_used,
6645 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04006646 }
Chris Mason0b86a832008-03-24 15:01:56 -04006647 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006648 return ret;
6649}
6650
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006651int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006652{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006653 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006654 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006655 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006656 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006657 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006658 u8 *array_ptr;
6659 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006660 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006661 u32 num_stripes;
6662 u32 array_size;
6663 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006664 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07006665 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04006666 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006667
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006668 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02006669 /*
6670 * This will create extent buffer of nodesize, superblock size is
6671 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6672 * overallocate but we can keep it as-is, only the first page is used.
6673 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006674 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07006675 if (IS_ERR(sb))
6676 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01006677 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006678 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006679 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006680 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01006681 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02006682 * pages up-to-date when the page is larger: extent does not cover the
6683 * whole page and consequently check_page_uptodate does not find all
6684 * the page's extents up-to-date (the hole beyond sb),
6685 * write_extent_buffer then triggers a WARN_ON.
6686 *
6687 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6688 * but sb spans only this function. Add an explicit SetPageUptodate call
6689 * to silence the warning eg. on PowerPC 64.
6690 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006691 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006692 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006693
Chris Masona061fc82008-05-07 11:43:44 -04006694 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006695 array_size = btrfs_super_sys_array_size(super_copy);
6696
David Sterba1ffb22c2014-10-31 19:02:42 +01006697 array_ptr = super_copy->sys_chunk_array;
6698 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6699 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006700
David Sterba1ffb22c2014-10-31 19:02:42 +01006701 while (cur_offset < array_size) {
6702 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006703 len = sizeof(*disk_key);
6704 if (cur_offset + len > array_size)
6705 goto out_short_read;
6706
Chris Mason0b86a832008-03-24 15:01:56 -04006707 btrfs_disk_key_to_cpu(&key, disk_key);
6708
David Sterba1ffb22c2014-10-31 19:02:42 +01006709 array_ptr += len;
6710 sb_array_offset += len;
6711 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006712
Chris Mason0d81ba52008-03-24 15:02:07 -04006713 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006714 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006715 /*
6716 * At least one btrfs_chunk with one stripe must be
6717 * present, exact stripe count check comes afterwards
6718 */
6719 len = btrfs_chunk_item_size(1);
6720 if (cur_offset + len > array_size)
6721 goto out_short_read;
6722
6723 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01006724 if (!num_stripes) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006725 btrfs_err(fs_info,
6726 "invalid number of stripes %u in sys_array at offset %u",
David Sterbaf5cdedd2015-11-30 17:27:06 +01006727 num_stripes, cur_offset);
6728 ret = -EIO;
6729 break;
6730 }
6731
Liu Boe06cd3d2016-06-03 12:05:15 -07006732 type = btrfs_chunk_type(sb, chunk);
6733 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006734 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006735 "invalid chunk type %llu in sys_array at offset %u",
6736 type, cur_offset);
6737 ret = -EIO;
6738 break;
6739 }
6740
David Sterbae3540ea2014-11-05 15:24:51 +01006741 len = btrfs_chunk_item_size(num_stripes);
6742 if (cur_offset + len > array_size)
6743 goto out_short_read;
6744
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006745 ret = read_one_chunk(fs_info, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006746 if (ret)
6747 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006748 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006749 btrfs_err(fs_info,
6750 "unexpected item type %u in sys_array at offset %u",
6751 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04006752 ret = -EIO;
6753 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006754 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006755 array_ptr += len;
6756 sb_array_offset += len;
6757 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006758 }
Liu Bod8651772016-06-03 17:41:42 -07006759 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006760 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006761 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006762
6763out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006764 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01006765 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07006766 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006767 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01006768 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006769}
6770
Qu Wenruoc5502452017-03-09 09:34:42 +08006771void btrfs_report_missing_device(struct btrfs_fs_info *fs_info, u64 devid,
6772 u8 *uuid)
6773{
6774 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing", devid, uuid);
6775}
6776
Qu Wenruo21634a12017-03-09 09:34:36 +08006777/*
6778 * Check if all chunks in the fs are OK for read-write degraded mount
6779 *
6780 * Return true if all chunks meet the minimal RW mount requirements.
6781 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6782 */
6783bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info)
6784{
6785 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
6786 struct extent_map *em;
6787 u64 next_start = 0;
6788 bool ret = true;
6789
6790 read_lock(&map_tree->map_tree.lock);
6791 em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1);
6792 read_unlock(&map_tree->map_tree.lock);
6793 /* No chunk at all? Return false anyway */
6794 if (!em) {
6795 ret = false;
6796 goto out;
6797 }
6798 while (em) {
6799 struct map_lookup *map;
6800 int missing = 0;
6801 int max_tolerated;
6802 int i;
6803
6804 map = em->map_lookup;
6805 max_tolerated =
6806 btrfs_get_num_tolerated_disk_barrier_failures(
6807 map->type);
6808 for (i = 0; i < map->num_stripes; i++) {
6809 struct btrfs_device *dev = map->stripes[i].dev;
6810
6811 if (!dev || !dev->bdev || dev->missing ||
6812 dev->last_flush_error)
6813 missing++;
6814 }
6815 if (missing > max_tolerated) {
6816 btrfs_warn(fs_info,
6817 "chunk %llu missing %d devices, max tolerance is %d for writeable mount",
6818 em->start, missing, max_tolerated);
6819 free_extent_map(em);
6820 ret = false;
6821 goto out;
6822 }
6823 next_start = extent_map_end(em);
6824 free_extent_map(em);
6825
6826 read_lock(&map_tree->map_tree.lock);
6827 em = lookup_extent_mapping(&map_tree->map_tree, next_start,
6828 (u64)(-1) - next_start);
6829 read_unlock(&map_tree->map_tree.lock);
6830 }
6831out:
6832 return ret;
6833}
6834
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006835int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006836{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006837 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04006838 struct btrfs_path *path;
6839 struct extent_buffer *leaf;
6840 struct btrfs_key key;
6841 struct btrfs_key found_key;
6842 int ret;
6843 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006844 u64 total_dev = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006845
Chris Mason0b86a832008-03-24 15:01:56 -04006846 path = btrfs_alloc_path();
6847 if (!path)
6848 return -ENOMEM;
6849
Li Zefanb367e472011-12-07 11:38:24 +08006850 mutex_lock(&uuid_mutex);
David Sterba34441362016-10-04 19:34:27 +02006851 mutex_lock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08006852
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006853 /*
6854 * Read all device items, and then all the chunk items. All
6855 * device items are found before any chunk item (their object id
6856 * is smaller than the lowest possible object id for a chunk
6857 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006858 */
6859 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6860 key.offset = 0;
6861 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006862 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006863 if (ret < 0)
6864 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006865 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006866 leaf = path->nodes[0];
6867 slot = path->slots[0];
6868 if (slot >= btrfs_header_nritems(leaf)) {
6869 ret = btrfs_next_leaf(root, path);
6870 if (ret == 0)
6871 continue;
6872 if (ret < 0)
6873 goto error;
6874 break;
6875 }
6876 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006877 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6878 struct btrfs_dev_item *dev_item;
6879 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006880 struct btrfs_dev_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006881 ret = read_one_dev(fs_info, leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006882 if (ret)
6883 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006884 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04006885 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6886 struct btrfs_chunk *chunk;
6887 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006888 ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006889 if (ret)
6890 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006891 }
6892 path->slots[0]++;
6893 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07006894
6895 /*
6896 * After loading chunk tree, we've got all device information,
6897 * do another round of validation checks.
6898 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006899 if (total_dev != fs_info->fs_devices->total_devices) {
6900 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07006901 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006902 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07006903 total_dev);
6904 ret = -EINVAL;
6905 goto error;
6906 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006907 if (btrfs_super_total_bytes(fs_info->super_copy) <
6908 fs_info->fs_devices->total_rw_bytes) {
6909 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07006910 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006911 btrfs_super_total_bytes(fs_info->super_copy),
6912 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07006913 ret = -EINVAL;
6914 goto error;
6915 }
Chris Mason0b86a832008-03-24 15:01:56 -04006916 ret = 0;
6917error:
David Sterba34441362016-10-04 19:34:27 +02006918 mutex_unlock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08006919 mutex_unlock(&uuid_mutex);
6920
Yan Zheng2b820322008-11-17 21:11:30 -05006921 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006922 return ret;
6923}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006924
Miao Xiecb517ea2013-05-15 07:48:19 +00006925void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6926{
6927 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6928 struct btrfs_device *device;
6929
Liu Bo29cc83f2014-05-11 23:14:59 +08006930 while (fs_devices) {
6931 mutex_lock(&fs_devices->device_list_mutex);
6932 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04006933 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08006934 mutex_unlock(&fs_devices->device_list_mutex);
6935
6936 fs_devices = fs_devices->seed;
6937 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006938}
6939
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006940static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6941{
6942 int i;
6943
6944 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6945 btrfs_dev_stat_reset(dev, i);
6946}
6947
6948int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6949{
6950 struct btrfs_key key;
6951 struct btrfs_key found_key;
6952 struct btrfs_root *dev_root = fs_info->dev_root;
6953 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6954 struct extent_buffer *eb;
6955 int slot;
6956 int ret = 0;
6957 struct btrfs_device *device;
6958 struct btrfs_path *path = NULL;
6959 int i;
6960
6961 path = btrfs_alloc_path();
6962 if (!path) {
6963 ret = -ENOMEM;
6964 goto out;
6965 }
6966
6967 mutex_lock(&fs_devices->device_list_mutex);
6968 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6969 int item_size;
6970 struct btrfs_dev_stats_item *ptr;
6971
David Sterba242e2952016-01-25 17:51:31 +01006972 key.objectid = BTRFS_DEV_STATS_OBJECTID;
6973 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006974 key.offset = device->devid;
6975 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6976 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006977 __btrfs_reset_dev_stats(device);
6978 device->dev_stats_valid = 1;
6979 btrfs_release_path(path);
6980 continue;
6981 }
6982 slot = path->slots[0];
6983 eb = path->nodes[0];
6984 btrfs_item_key_to_cpu(eb, &found_key, slot);
6985 item_size = btrfs_item_size_nr(eb, slot);
6986
6987 ptr = btrfs_item_ptr(eb, slot,
6988 struct btrfs_dev_stats_item);
6989
6990 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6991 if (item_size >= (1 + i) * sizeof(__le64))
6992 btrfs_dev_stat_set(device, i,
6993 btrfs_dev_stats_value(eb, ptr, i));
6994 else
6995 btrfs_dev_stat_reset(device, i);
6996 }
6997
6998 device->dev_stats_valid = 1;
6999 btrfs_dev_stat_print_on_load(device);
7000 btrfs_release_path(path);
7001 }
7002 mutex_unlock(&fs_devices->device_list_mutex);
7003
7004out:
7005 btrfs_free_path(path);
7006 return ret < 0 ? ret : 0;
7007}
7008
7009static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007010 struct btrfs_fs_info *fs_info,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007011 struct btrfs_device *device)
7012{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007013 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007014 struct btrfs_path *path;
7015 struct btrfs_key key;
7016 struct extent_buffer *eb;
7017 struct btrfs_dev_stats_item *ptr;
7018 int ret;
7019 int i;
7020
David Sterba242e2952016-01-25 17:51:31 +01007021 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7022 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007023 key.offset = device->devid;
7024
7025 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007026 if (!path)
7027 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007028 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7029 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007030 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007031 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007032 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007033 goto out;
7034 }
7035
7036 if (ret == 0 &&
7037 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7038 /* need to delete old one and insert a new one */
7039 ret = btrfs_del_item(trans, dev_root, path);
7040 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007041 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007042 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007043 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007044 goto out;
7045 }
7046 ret = 1;
7047 }
7048
7049 if (ret == 1) {
7050 /* need to insert a new item */
7051 btrfs_release_path(path);
7052 ret = btrfs_insert_empty_item(trans, dev_root, path,
7053 &key, sizeof(*ptr));
7054 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007055 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007056 "insert dev_stats item for device %s failed %d",
7057 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007058 goto out;
7059 }
7060 }
7061
7062 eb = path->nodes[0];
7063 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7064 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7065 btrfs_set_dev_stats_value(eb, ptr, i,
7066 btrfs_dev_stat_read(device, i));
7067 btrfs_mark_buffer_dirty(eb);
7068
7069out:
7070 btrfs_free_path(path);
7071 return ret;
7072}
7073
7074/*
7075 * called from commit_transaction. Writes all changed device stats to disk.
7076 */
7077int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
7078 struct btrfs_fs_info *fs_info)
7079{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007080 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7081 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007082 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007083 int ret = 0;
7084
7085 mutex_lock(&fs_devices->device_list_mutex);
7086 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08007087 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007088 continue;
7089
Miao Xieaddc3fa2014-07-24 11:37:11 +08007090 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007091 ret = update_dev_stat_item(trans, fs_info, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007092 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007093 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007094 }
7095 mutex_unlock(&fs_devices->device_list_mutex);
7096
7097 return ret;
7098}
7099
Stefan Behrens442a4f62012-05-25 16:06:08 +02007100void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7101{
7102 btrfs_dev_stat_inc(dev, index);
7103 btrfs_dev_stat_print_on_error(dev);
7104}
7105
Eric Sandeen48a3b632013-04-25 20:41:01 +00007106static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007107{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007108 if (!dev->dev_stats_valid)
7109 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007110 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007111 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007112 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007113 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7114 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7115 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007116 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7117 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007118}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007119
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007120static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7121{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007122 int i;
7123
7124 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7125 if (btrfs_dev_stat_read(dev, i) != 0)
7126 break;
7127 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7128 return; /* all values == 0, suppress message */
7129
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007130 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007131 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007132 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007133 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7134 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7135 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7136 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7137 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7138}
7139
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007140int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007141 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007142{
7143 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007144 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007145 int i;
7146
7147 mutex_lock(&fs_devices->device_list_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007148 dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007149 mutex_unlock(&fs_devices->device_list_mutex);
7150
7151 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007152 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007153 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007154 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007155 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007156 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007157 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007158 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7159 if (stats->nr_items > i)
7160 stats->values[i] =
7161 btrfs_dev_stat_read_and_reset(dev, i);
7162 else
7163 btrfs_dev_stat_reset(dev, i);
7164 }
7165 } else {
7166 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7167 if (stats->nr_items > i)
7168 stats->values[i] = btrfs_dev_stat_read(dev, i);
7169 }
7170 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7171 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7172 return 0;
7173}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007174
David Sterbada353f62017-02-14 17:55:53 +01007175void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007176{
7177 struct buffer_head *bh;
7178 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08007179 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007180
Anand Jain12b1c262015-08-14 18:32:59 +08007181 if (!bdev)
7182 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007183
Anand Jain12b1c262015-08-14 18:32:59 +08007184 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7185 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007186
Anand Jain12b1c262015-08-14 18:32:59 +08007187 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7188 continue;
7189
7190 disk_super = (struct btrfs_super_block *)bh->b_data;
7191
7192 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7193 set_buffer_dirty(bh);
7194 sync_dirty_buffer(bh);
7195 brelse(bh);
7196 }
7197
7198 /* Notify udev that device has changed */
7199 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7200
7201 /* Update ctime/mtime for device path for libblkid */
7202 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007203}
Miao Xie935e5cc2014-09-03 21:35:33 +08007204
7205/*
7206 * Update the size of all devices, which is used for writing out the
7207 * super blocks.
7208 */
7209void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
7210{
7211 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7212 struct btrfs_device *curr, *next;
7213
7214 if (list_empty(&fs_devices->resized_devices))
7215 return;
7216
7217 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02007218 mutex_lock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007219 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
7220 resized_list) {
7221 list_del_init(&curr->resized_list);
7222 curr->commit_total_bytes = curr->disk_total_bytes;
7223 }
David Sterba34441362016-10-04 19:34:27 +02007224 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007225 mutex_unlock(&fs_devices->device_list_mutex);
7226}
Miao Xiece7213c2014-09-03 21:35:34 +08007227
7228/* Must be invoked during the transaction commit */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007229void btrfs_update_commit_device_bytes_used(struct btrfs_fs_info *fs_info,
Miao Xiece7213c2014-09-03 21:35:34 +08007230 struct btrfs_transaction *transaction)
7231{
7232 struct extent_map *em;
7233 struct map_lookup *map;
7234 struct btrfs_device *dev;
7235 int i;
7236
7237 if (list_empty(&transaction->pending_chunks))
7238 return;
7239
7240 /* In order to kick the device replace finish process */
David Sterba34441362016-10-04 19:34:27 +02007241 mutex_lock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007242 list_for_each_entry(em, &transaction->pending_chunks, list) {
Jeff Mahoney95617d62015-06-03 10:55:48 -04007243 map = em->map_lookup;
Miao Xiece7213c2014-09-03 21:35:34 +08007244
7245 for (i = 0; i < map->num_stripes; i++) {
7246 dev = map->stripes[i].dev;
7247 dev->commit_bytes_used = dev->bytes_used;
7248 }
7249 }
David Sterba34441362016-10-04 19:34:27 +02007250 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007251}
Anand Jain5a13f432015-03-10 06:38:31 +08007252
7253void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7254{
7255 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7256 while (fs_devices) {
7257 fs_devices->fs_info = fs_info;
7258 fs_devices = fs_devices->seed;
7259 }
7260}
7261
7262void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7263{
7264 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7265 while (fs_devices) {
7266 fs_devices->fs_info = NULL;
7267 fs_devices = fs_devices->seed;
7268 }
7269}