blob: 82c21f6eeb3ec518d3d1825cf16db119367373bb [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>
Anand Jainf8e10cd2018-01-22 14:49:36 -080030#include <linux/list_sort.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050031#include <asm/div64.h>
Chris Mason0b86a832008-03-24 15:01:56 -040032#include "ctree.h"
33#include "extent_map.h"
34#include "disk-io.h"
35#include "transaction.h"
36#include "print-tree.h"
37#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050038#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040039#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010040#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040041#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060042#include "math.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010043#include "dev-replace.h"
Anand Jain99994cd2014-06-03 11:36:00 +080044#include "sysfs.h"
Chris Mason0b86a832008-03-24 15:01:56 -040045
Zhao Leiaf902042015-09-15 21:08:06 +080046const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
47 [BTRFS_RAID_RAID10] = {
48 .sub_stripes = 2,
49 .dev_stripes = 1,
50 .devs_max = 0, /* 0 == as many as possible */
51 .devs_min = 4,
Zhao Lei8789f4f2015-09-15 21:08:07 +080052 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080053 .devs_increment = 2,
54 .ncopies = 2,
55 },
56 [BTRFS_RAID_RAID1] = {
57 .sub_stripes = 1,
58 .dev_stripes = 1,
59 .devs_max = 2,
60 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080061 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080062 .devs_increment = 2,
63 .ncopies = 2,
64 },
65 [BTRFS_RAID_DUP] = {
66 .sub_stripes = 1,
67 .dev_stripes = 2,
68 .devs_max = 1,
69 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080070 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080071 .devs_increment = 1,
72 .ncopies = 2,
73 },
74 [BTRFS_RAID_RAID0] = {
75 .sub_stripes = 1,
76 .dev_stripes = 1,
77 .devs_max = 0,
78 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080079 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080080 .devs_increment = 1,
81 .ncopies = 1,
82 },
83 [BTRFS_RAID_SINGLE] = {
84 .sub_stripes = 1,
85 .dev_stripes = 1,
86 .devs_max = 1,
87 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080088 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080089 .devs_increment = 1,
90 .ncopies = 1,
91 },
92 [BTRFS_RAID_RAID5] = {
93 .sub_stripes = 1,
94 .dev_stripes = 1,
95 .devs_max = 0,
96 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080097 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080098 .devs_increment = 1,
99 .ncopies = 2,
100 },
101 [BTRFS_RAID_RAID6] = {
102 .sub_stripes = 1,
103 .dev_stripes = 1,
104 .devs_max = 0,
105 .devs_min = 3,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800106 .tolerated_failures = 2,
Zhao Leiaf902042015-09-15 21:08:06 +0800107 .devs_increment = 1,
108 .ncopies = 3,
109 },
110};
111
Colin Ian Kingfb75d852016-01-19 00:05:28 +0000112const u64 btrfs_raid_group[BTRFS_NR_RAID_TYPES] = {
Zhao Leiaf902042015-09-15 21:08:06 +0800113 [BTRFS_RAID_RAID10] = BTRFS_BLOCK_GROUP_RAID10,
114 [BTRFS_RAID_RAID1] = BTRFS_BLOCK_GROUP_RAID1,
115 [BTRFS_RAID_DUP] = BTRFS_BLOCK_GROUP_DUP,
116 [BTRFS_RAID_RAID0] = BTRFS_BLOCK_GROUP_RAID0,
117 [BTRFS_RAID_SINGLE] = 0,
118 [BTRFS_RAID_RAID5] = BTRFS_BLOCK_GROUP_RAID5,
119 [BTRFS_RAID_RAID6] = BTRFS_BLOCK_GROUP_RAID6,
120};
121
David Sterba621292b2016-02-15 16:28:03 +0100122/*
123 * Table to convert BTRFS_RAID_* to the error code if minimum number of devices
124 * condition is not met. Zero means there's no corresponding
125 * BTRFS_ERROR_DEV_*_NOT_MET value.
126 */
127const int btrfs_raid_mindev_error[BTRFS_NR_RAID_TYPES] = {
128 [BTRFS_RAID_RAID10] = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
129 [BTRFS_RAID_RAID1] = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
130 [BTRFS_RAID_DUP] = 0,
131 [BTRFS_RAID_RAID0] = 0,
132 [BTRFS_RAID_SINGLE] = 0,
133 [BTRFS_RAID_RAID5] = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
134 [BTRFS_RAID_RAID6] = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
135};
136
Yan Zheng2b820322008-11-17 21:11:30 -0500137static int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +0100138 struct btrfs_fs_info *fs_info);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400139static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200140static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000141static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200142static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Liu Bo5ab56092017-03-14 13:33:57 -0700143static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
144 enum btrfs_map_op op,
145 u64 logical, u64 *length,
146 struct btrfs_bio **bbio_ret,
147 int mirror_num, int need_raid_map);
Yan Zheng2b820322008-11-17 21:11:30 -0500148
David Sterba9c6b1c42017-06-16 22:30:00 +0200149/*
150 * Device locking
151 * ==============
152 *
153 * There are several mutexes that protect manipulation of devices and low-level
154 * structures like chunks but not block groups, extents or files
155 *
156 * uuid_mutex (global lock)
157 * ------------------------
158 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
159 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
160 * device) or requested by the device= mount option
161 *
162 * the mutex can be very coarse and can cover long-running operations
163 *
164 * protects: updates to fs_devices counters like missing devices, rw devices,
165 * seeding, structure cloning, openning/closing devices at mount/umount time
166 *
167 * global::fs_devs - add, remove, updates to the global list
168 *
169 * does not protect: manipulation of the fs_devices::devices list!
170 *
171 * btrfs_device::name - renames (write side), read is RCU
172 *
173 * fs_devices::device_list_mutex (per-fs, with RCU)
174 * ------------------------------------------------
175 * protects updates to fs_devices::devices, ie. adding and deleting
176 *
177 * simple list traversal with read-only actions can be done with RCU protection
178 *
179 * may be used to exclude some operations from running concurrently without any
180 * modifications to the list (see write_all_supers)
181 *
182 * volume_mutex
183 * ------------
184 * coarse lock owned by a mounted filesystem; used to exclude some operations
185 * that cannot run in parallel and affect the higher-level properties of the
186 * filesystem like: device add/deleting/resize/replace, or balance
187 *
188 * balance_mutex
189 * -------------
190 * protects balance structures (status, state) and context accessed from
191 * several places (internally, ioctl)
192 *
193 * chunk_mutex
194 * -----------
195 * protects chunks, adding or removing during allocation, trim or when a new
196 * device is added/removed
197 *
198 * cleaner_mutex
199 * -------------
200 * a big lock that is held by the cleaner thread and prevents running subvolume
201 * cleaning together with relocation or delayed iputs
202 *
203 *
204 * Lock nesting
205 * ============
206 *
207 * uuid_mutex
208 * volume_mutex
209 * device_list_mutex
210 * chunk_mutex
211 * balance_mutex
212 */
213
Miao Xie67a2c452014-09-03 21:35:43 +0800214DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400215static LIST_HEAD(fs_uuids);
Anand Jainc73eccf2015-03-10 06:38:30 +0800216struct list_head *btrfs_get_fs_uuids(void)
217{
218 return &fs_uuids;
219}
Chris Mason8a4b83c2008-03-24 15:02:07 -0400220
David Sterba2dfeca92017-06-14 02:48:07 +0200221/*
222 * alloc_fs_devices - allocate struct btrfs_fs_devices
223 * @fsid: if not NULL, copy the uuid to fs_devices::fsid
224 *
225 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
226 * The returned struct is not linked onto any lists and can be destroyed with
227 * kfree() right away.
228 */
229static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
Ilya Dryomov2208a372013-08-12 14:33:03 +0300230{
231 struct btrfs_fs_devices *fs_devs;
232
David Sterba78f2c9e2016-02-11 14:25:38 +0100233 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300234 if (!fs_devs)
235 return ERR_PTR(-ENOMEM);
236
237 mutex_init(&fs_devs->device_list_mutex);
238
239 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +0800240 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300241 INIT_LIST_HEAD(&fs_devs->alloc_list);
242 INIT_LIST_HEAD(&fs_devs->list);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300243 if (fsid)
244 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300245
246 return fs_devs;
247}
248
David Sterba48dae9c2017-10-30 18:10:25 +0100249static void free_device(struct btrfs_device *device)
250{
251 rcu_string_free(device->name);
252 bio_put(device->flush_bio);
253 kfree(device);
254}
255
Yan Zhenge4404d62008-12-12 10:03:26 -0500256static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
257{
258 struct btrfs_device *device;
259 WARN_ON(fs_devices->opened);
260 while (!list_empty(&fs_devices->devices)) {
261 device = list_entry(fs_devices->devices.next,
262 struct btrfs_device, dev_list);
263 list_del(&device->dev_list);
David Sterba55de4802017-10-30 18:55:47 +0100264 free_device(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500265 }
266 kfree(fs_devices);
267}
268
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000269static void btrfs_kobject_uevent(struct block_device *bdev,
270 enum kobject_action action)
271{
272 int ret;
273
274 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
275 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500276 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000277 action,
278 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
279 &disk_to_dev(bdev->bd_disk)->kobj);
280}
281
David Sterbaffc5a372018-02-19 17:24:15 +0100282void __exit btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400283{
284 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400285
Yan Zheng2b820322008-11-17 21:11:30 -0500286 while (!list_empty(&fs_uuids)) {
287 fs_devices = list_entry(fs_uuids.next,
288 struct btrfs_fs_devices, list);
289 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500290 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400291 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400292}
293
David Sterba48dae9c2017-10-30 18:10:25 +0100294/*
295 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
296 * Returned struct is not linked onto any lists and must be destroyed using
297 * free_device.
298 */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300299static struct btrfs_device *__alloc_device(void)
300{
301 struct btrfs_device *dev;
302
David Sterba78f2c9e2016-02-11 14:25:38 +0100303 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300304 if (!dev)
305 return ERR_PTR(-ENOMEM);
306
David Sterbae0ae9992017-06-06 17:06:06 +0200307 /*
308 * Preallocate a bio that's always going to be used for flushing device
309 * barriers and matches the device lifespan
310 */
311 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
312 if (!dev->flush_bio) {
313 kfree(dev);
314 return ERR_PTR(-ENOMEM);
315 }
David Sterbae0ae9992017-06-06 17:06:06 +0200316
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300317 INIT_LIST_HEAD(&dev->dev_list);
318 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800319 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300320
321 spin_lock_init(&dev->io_lock);
322
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300323 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800324 atomic_set(&dev->dev_stats_ccnt, 0);
Sebastian Andrzej Siewior546bed62016-01-15 14:37:15 +0100325 btrfs_device_data_ordered_init(dev);
Chris Mason9bcaaea2017-05-04 16:08:08 -0700326 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Mel Gormand0164ad2015-11-06 16:28:21 -0800327 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300328
329 return dev;
330}
331
David Sterba35c70102017-06-15 19:51:51 +0200332/*
333 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
334 * return NULL.
335 *
336 * If devid and uuid are both specified, the match must be exact, otherwise
337 * only devid is used.
338 */
339static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
340 u64 devid, const u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400341{
David Sterba35c70102017-06-15 19:51:51 +0200342 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400343 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400344
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500345 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400346 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400347 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400348 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400349 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400350 }
351 return NULL;
352}
353
Chris Masona1b32a52008-09-05 16:09:51 -0400354static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400355{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400356 struct btrfs_fs_devices *fs_devices;
357
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500358 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400359 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
360 return fs_devices;
361 }
362 return NULL;
363}
364
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100365static int
366btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
367 int flush, struct block_device **bdev,
368 struct buffer_head **bh)
369{
370 int ret;
371
372 *bdev = blkdev_get_by_path(device_path, flags, holder);
373
374 if (IS_ERR(*bdev)) {
375 ret = PTR_ERR(*bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100376 goto error;
377 }
378
379 if (flush)
380 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
David Sterba9f6d2512017-06-16 01:48:05 +0200381 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100382 if (ret) {
383 blkdev_put(*bdev, flags);
384 goto error;
385 }
386 invalidate_bdev(*bdev);
387 *bh = btrfs_read_dev_super(*bdev);
Anand Jain92fc03f2015-08-14 18:32:51 +0800388 if (IS_ERR(*bh)) {
389 ret = PTR_ERR(*bh);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100390 blkdev_put(*bdev, flags);
391 goto error;
392 }
393
394 return 0;
395
396error:
397 *bdev = NULL;
398 *bh = NULL;
399 return ret;
400}
401
Chris Masonffbd5172009-04-20 15:50:09 -0400402static void requeue_list(struct btrfs_pending_bios *pending_bios,
403 struct bio *head, struct bio *tail)
404{
405
406 struct bio *old_head;
407
408 old_head = pending_bios->head;
409 pending_bios->head = head;
410 if (pending_bios->tail)
411 tail->bi_next = old_head;
412 else
413 pending_bios->tail = tail;
414}
415
Chris Mason8b712842008-06-11 16:50:36 -0400416/*
417 * we try to collect pending bios for a device so we don't get a large
418 * number of procs sending bios down to the same device. This greatly
419 * improves the schedulers ability to collect and merge the bios.
420 *
421 * But, it also turns into a long list of bios to process and that is sure
422 * to eventually make the worker thread block. The solution here is to
423 * make some progress and then put this work struct back at the end of
424 * the list if the block device is congested. This way, multiple devices
425 * can make progress from a single worker thread.
426 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100427static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400428{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400429 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -0400430 struct bio *pending;
431 struct backing_dev_info *bdi;
Chris Masonffbd5172009-04-20 15:50:09 -0400432 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400433 struct bio *tail;
434 struct bio *cur;
435 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400436 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400437 unsigned long batch_run = 0;
Chris Masonb765ead2009-04-03 10:27:10 -0400438 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400439 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000440 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400441 struct blk_plug plug;
442
443 /*
444 * this function runs all the bios we've collected for
445 * a particular device. We don't want to wander off to
446 * another device without first sending all of these down.
447 * So, setup a plug here and finish it off before we return
448 */
449 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400450
Jan Karaefa7c9f2017-02-02 15:56:53 +0100451 bdi = device->bdev->bd_bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400452
Chris Mason8b712842008-06-11 16:50:36 -0400453loop:
454 spin_lock(&device->io_lock);
455
Chris Masona6837052009-02-04 09:19:41 -0500456loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400457 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400458
Chris Mason8b712842008-06-11 16:50:36 -0400459 /* take all the bios off the list at once and process them
460 * later on (without the lock held). But, remember the
461 * tail and other pointers so the bios can be properly reinserted
462 * into the list if we hit congestion
463 */
Chris Masond84275c2009-06-09 15:39:08 -0400464 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400465 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400466 force_reg = 1;
467 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400468 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400469 force_reg = 0;
470 }
Chris Masonffbd5172009-04-20 15:50:09 -0400471
472 pending = pending_bios->head;
473 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400474 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400475
476 /*
477 * if pending was null this time around, no bios need processing
478 * at all and we can stop. Otherwise it'll loop back up again
479 * and do an additional check so no bios are missed.
480 *
481 * device->running_pending is used to synchronize with the
482 * schedule_bio code.
483 */
Chris Masonffbd5172009-04-20 15:50:09 -0400484 if (device->pending_sync_bios.head == NULL &&
485 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400486 again = 0;
487 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400488 } else {
489 again = 1;
490 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400491 }
Chris Masonffbd5172009-04-20 15:50:09 -0400492
493 pending_bios->head = NULL;
494 pending_bios->tail = NULL;
495
Chris Mason8b712842008-06-11 16:50:36 -0400496 spin_unlock(&device->io_lock);
497
Chris Masond3977122009-01-05 21:25:51 -0500498 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400499
500 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400501 /* we want to work on both lists, but do more bios on the
502 * sync list than the regular list
503 */
504 if ((num_run > 32 &&
505 pending_bios != &device->pending_sync_bios &&
506 device->pending_sync_bios.head) ||
507 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
508 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400509 spin_lock(&device->io_lock);
510 requeue_list(pending_bios, pending, tail);
511 goto loop_lock;
512 }
513
Chris Mason8b712842008-06-11 16:50:36 -0400514 cur = pending;
515 pending = pending->bi_next;
516 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400517
Jens Axboedac56212015-04-17 16:23:59 -0600518 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400519
Chris Mason2ab1ba62011-08-04 14:28:36 -0400520 /*
521 * if we're doing the sync list, record that our
522 * plug has some sync requests on it
523 *
524 * If we're doing the regular list and there are
525 * sync requests sitting around, unplug before
526 * we add more
527 */
528 if (pending_bios == &device->pending_sync_bios) {
529 sync_pending = 1;
530 } else if (sync_pending) {
531 blk_finish_plug(&plug);
532 blk_start_plug(&plug);
533 sync_pending = 0;
534 }
535
Mike Christie4e49ea42016-06-05 14:31:41 -0500536 btrfsic_submit_bio(cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400537 num_run++;
538 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100539
540 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400541
542 /*
543 * we made progress, there is more work to do and the bdi
544 * is now congested. Back off and let other work structs
545 * run instead
546 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400547 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500548 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400549 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400550
Chris Masonb765ead2009-04-03 10:27:10 -0400551 ioc = current->io_context;
552
553 /*
554 * the main goal here is that we don't want to
555 * block if we're going to be able to submit
556 * more requests without blocking.
557 *
558 * This code does two great things, it pokes into
559 * the elevator code from a filesystem _and_
560 * it makes assumptions about how batching works.
561 */
562 if (ioc && ioc->nr_batch_requests > 0 &&
563 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
564 (last_waited == 0 ||
565 ioc->last_waited == last_waited)) {
566 /*
567 * we want to go through our batch of
568 * requests and stop. So, we copy out
569 * the ioc->last_waited time and test
570 * against it before looping
571 */
572 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100573 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400574 continue;
575 }
Chris Mason8b712842008-06-11 16:50:36 -0400576 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400577 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500578 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400579
580 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800581 btrfs_queue_work(fs_info->submit_workers,
582 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400583 goto done;
584 }
585 }
Chris Masonffbd5172009-04-20 15:50:09 -0400586
Chris Mason51684082010-03-10 15:33:32 -0500587 cond_resched();
588 if (again)
589 goto loop;
590
591 spin_lock(&device->io_lock);
592 if (device->pending_bios.head || device->pending_sync_bios.head)
593 goto loop_lock;
594 spin_unlock(&device->io_lock);
595
Chris Mason8b712842008-06-11 16:50:36 -0400596done:
Chris Mason211588a2011-04-19 20:12:40 -0400597 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400598}
599
Christoph Hellwigb2950862008-12-02 09:54:17 -0500600static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400601{
602 struct btrfs_device *device;
603
604 device = container_of(work, struct btrfs_device, work);
605 run_scheduled_bios(device);
606}
607
Anand Jaind8367db2018-01-18 22:00:37 +0800608/*
609 * Search and remove all stale (devices which are not mounted) devices.
610 * When both inputs are NULL, it will search and release all stale devices.
611 * path: Optional. When provided will it release all unmounted devices
612 * matching this path only.
613 * skip_dev: Optional. Will skip this device when searching for the stale
614 * devices.
615 */
616static void btrfs_free_stale_devices(const char *path,
617 struct btrfs_device *skip_dev)
Anand Jain4fde46f2015-06-17 21:10:48 +0800618{
Anand Jain38cf6652018-01-18 22:00:34 +0800619 struct btrfs_fs_devices *fs_devs, *tmp_fs_devs;
620 struct btrfs_device *dev, *tmp_dev;
Anand Jain4fde46f2015-06-17 21:10:48 +0800621
Anand Jain38cf6652018-01-18 22:00:34 +0800622 list_for_each_entry_safe(fs_devs, tmp_fs_devs, &fs_uuids, list) {
Anand Jain4fde46f2015-06-17 21:10:48 +0800623
624 if (fs_devs->opened)
625 continue;
Anand Jain4fde46f2015-06-17 21:10:48 +0800626
Anand Jain38cf6652018-01-18 22:00:34 +0800627 list_for_each_entry_safe(dev, tmp_dev,
628 &fs_devs->devices, dev_list) {
Anand Jain522f1b42018-01-18 22:00:35 +0800629 int not_found = 0;
Anand Jain4fde46f2015-06-17 21:10:48 +0800630
Anand Jaind8367db2018-01-18 22:00:37 +0800631 if (skip_dev && skip_dev == dev)
632 continue;
633 if (path && !dev->name)
Anand Jain4fde46f2015-06-17 21:10:48 +0800634 continue;
635
Anand Jain4fde46f2015-06-17 21:10:48 +0800636 rcu_read_lock();
Anand Jaind8367db2018-01-18 22:00:37 +0800637 if (path)
Anand Jain522f1b42018-01-18 22:00:35 +0800638 not_found = strcmp(rcu_str_deref(dev->name),
Anand Jaind8367db2018-01-18 22:00:37 +0800639 path);
Anand Jain4fde46f2015-06-17 21:10:48 +0800640 rcu_read_unlock();
Anand Jain38cf6652018-01-18 22:00:34 +0800641 if (not_found)
642 continue;
Anand Jain4fde46f2015-06-17 21:10:48 +0800643
Anand Jain4fde46f2015-06-17 21:10:48 +0800644 /* delete the stale device */
645 if (fs_devs->num_devices == 1) {
646 btrfs_sysfs_remove_fsid(fs_devs);
647 list_del(&fs_devs->list);
648 free_fs_devices(fs_devs);
Nikolay Borisovfd649f12018-01-30 16:07:37 +0200649 break;
Anand Jain4fde46f2015-06-17 21:10:48 +0800650 } else {
651 fs_devs->num_devices--;
652 list_del(&dev->dev_list);
David Sterba55de4802017-10-30 18:55:47 +0100653 free_device(dev);
Anand Jain4fde46f2015-06-17 21:10:48 +0800654 }
Anand Jain4fde46f2015-06-17 21:10:48 +0800655 }
656 }
657}
658
Anand Jain0fb08bc2017-11-09 23:45:24 +0800659static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
660 struct btrfs_device *device, fmode_t flags,
661 void *holder)
662{
663 struct request_queue *q;
664 struct block_device *bdev;
665 struct buffer_head *bh;
666 struct btrfs_super_block *disk_super;
667 u64 devid;
668 int ret;
669
670 if (device->bdev)
671 return -EINVAL;
672 if (!device->name)
673 return -EINVAL;
674
675 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
676 &bdev, &bh);
677 if (ret)
678 return ret;
679
680 disk_super = (struct btrfs_super_block *)bh->b_data;
681 devid = btrfs_stack_device_id(&disk_super->dev_item);
682 if (devid != device->devid)
683 goto error_brelse;
684
685 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
686 goto error_brelse;
687
688 device->generation = btrfs_super_generation(disk_super);
689
690 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
Anand Jainebbede42017-12-04 12:54:52 +0800691 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800692 fs_devices->seeding = 1;
693 } else {
Anand Jainebbede42017-12-04 12:54:52 +0800694 if (bdev_read_only(bdev))
695 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
696 else
697 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800698 }
699
700 q = bdev_get_queue(bdev);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800701 if (!blk_queue_nonrot(q))
702 fs_devices->rotating = 1;
703
704 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +0800705 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800706 device->mode = flags;
707
708 fs_devices->open_devices++;
Anand Jainebbede42017-12-04 12:54:52 +0800709 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
710 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Anand Jain0fb08bc2017-11-09 23:45:24 +0800711 fs_devices->rw_devices++;
Anand Jainb1b8e382018-01-22 14:49:37 -0800712 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800713 }
714 brelse(bh);
715
716 return 0;
717
718error_brelse:
719 brelse(bh);
720 blkdev_put(bdev, flags);
721
722 return -EINVAL;
723}
724
David Sterba60999ca2014-03-26 18:26:36 +0100725/*
726 * Add new device to list of registered devices
727 *
728 * Returns:
Anand Jaine124ece2018-01-18 22:02:35 +0800729 * device pointer which was just added or updated when successful
730 * error pointer when failed
David Sterba60999ca2014-03-26 18:26:36 +0100731 */
Anand Jaine124ece2018-01-18 22:02:35 +0800732static noinline struct btrfs_device *device_list_add(const char *path,
Anand Jain3acbcbf2018-01-18 22:02:36 +0800733 struct btrfs_super_block *disk_super)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400734{
735 struct btrfs_device *device;
736 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400737 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400738 u64 found_transid = btrfs_super_generation(disk_super);
Anand Jain3acbcbf2018-01-18 22:02:36 +0800739 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400740
741 fs_devices = find_fsid(disk_super->fsid);
742 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300743 fs_devices = alloc_fs_devices(disk_super->fsid);
744 if (IS_ERR(fs_devices))
Anand Jaine124ece2018-01-18 22:02:35 +0800745 return ERR_CAST(fs_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300746
Chris Mason8a4b83c2008-03-24 15:02:07 -0400747 list_add(&fs_devices->list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300748
Chris Mason8a4b83c2008-03-24 15:02:07 -0400749 device = NULL;
750 } else {
David Sterba35c70102017-06-15 19:51:51 +0200751 device = find_device(fs_devices, devid,
752 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400753 }
Miao Xie443f24f2014-07-24 11:37:15 +0800754
Chris Mason8a4b83c2008-03-24 15:02:07 -0400755 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500756 if (fs_devices->opened)
Anand Jaine124ece2018-01-18 22:02:35 +0800757 return ERR_PTR(-EBUSY);
Yan Zheng2b820322008-11-17 21:11:30 -0500758
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300759 device = btrfs_alloc_device(NULL, &devid,
760 disk_super->dev_item.uuid);
761 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400762 /* we can safely leave the fs_devices entry around */
Anand Jaine124ece2018-01-18 22:02:35 +0800763 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400764 }
Josef Bacik606686e2012-06-04 14:03:51 -0400765
766 name = rcu_string_strdup(path, GFP_NOFS);
767 if (!name) {
David Sterba55de4802017-10-30 18:55:47 +0100768 free_device(device);
Anand Jaine124ece2018-01-18 22:02:35 +0800769 return ERR_PTR(-ENOMEM);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400770 }
Josef Bacik606686e2012-06-04 14:03:51 -0400771 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200772
Chris Masone5e9a522009-06-10 15:17:02 -0400773 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000774 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100775 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400776 mutex_unlock(&fs_devices->device_list_mutex);
777
Yan Zheng2b820322008-11-17 21:11:30 -0500778 device->fs_devices = fs_devices;
Anand Jaind8367db2018-01-18 22:00:37 +0800779 btrfs_free_stale_devices(path, device);
Anand Jain327f18c2018-01-18 22:02:33 +0800780
781 if (disk_super->label[0])
782 pr_info("BTRFS: device label %s devid %llu transid %llu %s\n",
783 disk_super->label, devid, found_transid, path);
784 else
785 pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n",
786 disk_super->fsid, devid, found_transid, path);
787
Josef Bacik606686e2012-06-04 14:03:51 -0400788 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800789 /*
790 * When FS is already mounted.
791 * 1. If you are here and if the device->name is NULL that
792 * means this device was missing at time of FS mount.
793 * 2. If you are here and if the device->name is different
794 * from 'path' that means either
795 * a. The same device disappeared and reappeared with
796 * different name. or
797 * b. The missing-disk-which-was-replaced, has
798 * reappeared now.
799 *
800 * We must allow 1 and 2a above. But 2b would be a spurious
801 * and unintentional.
802 *
803 * Further in case of 1 and 2a above, the disk at 'path'
804 * would have missed some transaction when it was away and
805 * in case of 2a the stale bdev has to be updated as well.
806 * 2b must not be allowed at all time.
807 */
808
809 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700810 * For now, we do allow update to btrfs_fs_device through the
811 * btrfs dev scan cli after FS has been mounted. We're still
812 * tracking a problem where systems fail mount by subvolume id
813 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800814 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700815 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800816 /*
817 * That is if the FS is _not_ mounted and if you
818 * are here, that means there is more than one
819 * disk with same uuid and devid.We keep the one
820 * with larger generation number or the last-in if
821 * generation are equal.
822 */
Anand Jaine124ece2018-01-18 22:02:35 +0800823 return ERR_PTR(-EEXIST);
Anand Jain77bdae42014-07-03 18:22:06 +0800824 }
Anand Jainb96de002014-07-03 18:22:05 +0800825
Josef Bacik606686e2012-06-04 14:03:51 -0400826 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000827 if (!name)
Anand Jaine124ece2018-01-18 22:02:35 +0800828 return ERR_PTR(-ENOMEM);
Josef Bacik606686e2012-06-04 14:03:51 -0400829 rcu_string_free(device->name);
830 rcu_assign_pointer(device->name, name);
Anand Jaine6e674b2017-12-04 12:54:54 +0800831 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -0500832 fs_devices->missing_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +0800833 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -0500834 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400835 }
836
Anand Jain77bdae42014-07-03 18:22:06 +0800837 /*
838 * Unmount does not free the btrfs_device struct but would zero
839 * generation along with most of the other members. So just update
840 * it back. We need it to pick the disk with largest generation
841 * (as above).
842 */
843 if (!fs_devices->opened)
844 device->generation = found_transid;
845
Anand Jainf2788d22018-01-18 22:02:34 +0800846 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
847
Anand Jaine124ece2018-01-18 22:02:35 +0800848 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400849}
850
Yan Zhenge4404d62008-12-12 10:03:26 -0500851static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
852{
853 struct btrfs_fs_devices *fs_devices;
854 struct btrfs_device *device;
855 struct btrfs_device *orig_dev;
856
Ilya Dryomov2208a372013-08-12 14:33:03 +0300857 fs_devices = alloc_fs_devices(orig->fsid);
858 if (IS_ERR(fs_devices))
859 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500860
Miao Xieadbbb862014-09-03 21:35:42 +0800861 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400862 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500863
Xiao Guangrong46224702011-04-20 10:08:47 +0000864 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500865 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400866 struct rcu_string *name;
867
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300868 device = btrfs_alloc_device(NULL, &orig_dev->devid,
869 orig_dev->uuid);
870 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500871 goto error;
872
Josef Bacik606686e2012-06-04 14:03:51 -0400873 /*
874 * This is ok to do without rcu read locked because we hold the
875 * uuid mutex so nothing we touch in here is going to disappear.
876 */
Anand Jaine755f782014-06-30 17:12:47 +0800877 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +0100878 name = rcu_string_strdup(orig_dev->name->str,
879 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +0800880 if (!name) {
David Sterba55de4802017-10-30 18:55:47 +0100881 free_device(device);
Anand Jaine755f782014-06-30 17:12:47 +0800882 goto error;
883 }
884 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400885 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500886
Yan Zhenge4404d62008-12-12 10:03:26 -0500887 list_add(&device->dev_list, &fs_devices->devices);
888 device->fs_devices = fs_devices;
889 fs_devices->num_devices++;
890 }
Miao Xieadbbb862014-09-03 21:35:42 +0800891 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500892 return fs_devices;
893error:
Miao Xieadbbb862014-09-03 21:35:42 +0800894 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500895 free_fs_devices(fs_devices);
896 return ERR_PTR(-ENOMEM);
897}
898
Anand Jain9b99b112018-02-27 12:41:59 +0800899/*
900 * After we have read the system tree and know devids belonging to
901 * this filesystem, remove the device which does not belong there.
902 */
903void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400904{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500905 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800906 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500907
Chris Masondfe25022008-05-13 13:46:40 -0400908 mutex_lock(&uuid_mutex);
909again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000910 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500911 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +0800912 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
913 &device->dev_state)) {
Anand Jain401e29c2017-12-04 12:54:55 +0800914 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
915 &device->dev_state) &&
916 (!latest_dev ||
917 device->generation > latest_dev->generation)) {
Miao Xie443f24f2014-07-24 11:37:15 +0800918 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500919 }
Yan Zheng2b820322008-11-17 21:11:30 -0500920 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500921 }
Yan Zheng2b820322008-11-17 21:11:30 -0500922
Stefan Behrens8dabb742012-11-06 13:15:27 +0100923 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
924 /*
925 * In the first step, keep the device which has
926 * the correct fsid and the devid that is used
927 * for the dev_replace procedure.
928 * In the second step, the dev_replace state is
929 * read from the device tree and it is known
930 * whether the procedure is really active or
931 * not, which means whether this device is
932 * used or whether it should be removed.
933 */
Anand Jain401e29c2017-12-04 12:54:55 +0800934 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
935 &device->dev_state)) {
Stefan Behrens8dabb742012-11-06 13:15:27 +0100936 continue;
937 }
938 }
Yan Zheng2b820322008-11-17 21:11:30 -0500939 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100940 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500941 device->bdev = NULL;
942 fs_devices->open_devices--;
943 }
Anand Jainebbede42017-12-04 12:54:52 +0800944 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -0500945 list_del_init(&device->dev_alloc_list);
Anand Jainebbede42017-12-04 12:54:52 +0800946 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +0800947 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
948 &device->dev_state))
Stefan Behrens8dabb742012-11-06 13:15:27 +0100949 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500950 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500951 list_del_init(&device->dev_list);
952 fs_devices->num_devices--;
David Sterba55de4802017-10-30 18:55:47 +0100953 free_device(device);
Chris Masondfe25022008-05-13 13:46:40 -0400954 }
Yan Zheng2b820322008-11-17 21:11:30 -0500955
956 if (fs_devices->seed) {
957 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500958 goto again;
959 }
960
Miao Xie443f24f2014-07-24 11:37:15 +0800961 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500962
Chris Masondfe25022008-05-13 13:46:40 -0400963 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400964}
Chris Masona0af4692008-05-13 16:03:06 -0400965
David Sterbaf06c5962017-06-06 17:08:23 +0200966static void free_device_rcu(struct rcu_head *head)
Xiao Guangrong1f781602011-04-20 10:09:16 +0000967{
968 struct btrfs_device *device;
969
970 device = container_of(head, struct btrfs_device, rcu);
David Sterba55de4802017-10-30 18:55:47 +0100971 free_device(device);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000972}
973
Anand Jain14238812016-07-22 06:04:53 +0800974static void btrfs_close_bdev(struct btrfs_device *device)
975{
David Sterba08ffcae2017-06-19 16:55:35 +0200976 if (!device->bdev)
977 return;
978
Anand Jainebbede42017-12-04 12:54:52 +0800979 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Anand Jain14238812016-07-22 06:04:53 +0800980 sync_blockdev(device->bdev);
981 invalidate_bdev(device->bdev);
982 }
983
David Sterba08ffcae2017-06-19 16:55:35 +0200984 blkdev_put(device->bdev, device->mode);
Anand Jain14238812016-07-22 06:04:53 +0800985}
986
Anand Jain0ccd0522016-09-22 12:56:13 +0800987static void btrfs_prepare_close_one_device(struct btrfs_device *device)
Anand Jainf448341a2016-06-14 18:55:25 +0800988{
989 struct btrfs_fs_devices *fs_devices = device->fs_devices;
990 struct btrfs_device *new_device;
991 struct rcu_string *name;
992
993 if (device->bdev)
994 fs_devices->open_devices--;
995
Anand Jainebbede42017-12-04 12:54:52 +0800996 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jainf448341a2016-06-14 18:55:25 +0800997 device->devid != BTRFS_DEV_REPLACE_DEVID) {
998 list_del_init(&device->dev_alloc_list);
999 fs_devices->rw_devices--;
1000 }
1001
Anand Jaine6e674b2017-12-04 12:54:54 +08001002 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jainf448341a2016-06-14 18:55:25 +08001003 fs_devices->missing_devices--;
1004
1005 new_device = btrfs_alloc_device(NULL, &device->devid,
1006 device->uuid);
1007 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
1008
1009 /* Safe because we are under uuid_mutex */
1010 if (device->name) {
1011 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1012 BUG_ON(!name); /* -ENOMEM */
1013 rcu_assign_pointer(new_device->name, name);
1014 }
1015
1016 list_replace_rcu(&device->dev_list, &new_device->dev_list);
1017 new_device->fs_devices = device->fs_devices;
Anand Jainf448341a2016-06-14 18:55:25 +08001018}
1019
Yan Zheng2b820322008-11-17 21:11:30 -05001020static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001021{
Sasha Levin2037a092015-05-12 19:31:37 -04001022 struct btrfs_device *device, *tmp;
Anand Jain0ccd0522016-09-22 12:56:13 +08001023 struct list_head pending_put;
1024
1025 INIT_LIST_HEAD(&pending_put);
Yan Zhenge4404d62008-12-12 10:03:26 -05001026
Yan Zheng2b820322008-11-17 21:11:30 -05001027 if (--fs_devices->opened > 0)
1028 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001029
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001030 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -04001031 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Anand Jain0ccd0522016-09-22 12:56:13 +08001032 btrfs_prepare_close_one_device(device);
1033 list_add(&device->dev_list, &pending_put);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001034 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001035 mutex_unlock(&fs_devices->device_list_mutex);
1036
Anand Jain0ccd0522016-09-22 12:56:13 +08001037 /*
1038 * btrfs_show_devname() is using the device_list_mutex,
1039 * sometimes call to blkdev_put() leads vfs calling
1040 * into this func. So do put outside of device_list_mutex,
1041 * as of now.
1042 */
1043 while (!list_empty(&pending_put)) {
1044 device = list_first_entry(&pending_put,
1045 struct btrfs_device, dev_list);
1046 list_del(&device->dev_list);
1047 btrfs_close_bdev(device);
David Sterbaf06c5962017-06-06 17:08:23 +02001048 call_rcu(&device->rcu, free_device_rcu);
Anand Jain0ccd0522016-09-22 12:56:13 +08001049 }
1050
Yan Zhenge4404d62008-12-12 10:03:26 -05001051 WARN_ON(fs_devices->open_devices);
1052 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001053 fs_devices->opened = 0;
1054 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001055
Chris Mason8a4b83c2008-03-24 15:02:07 -04001056 return 0;
1057}
1058
Yan Zheng2b820322008-11-17 21:11:30 -05001059int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1060{
Yan Zhenge4404d62008-12-12 10:03:26 -05001061 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001062 int ret;
1063
1064 mutex_lock(&uuid_mutex);
1065 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001066 if (!fs_devices->opened) {
1067 seed_devices = fs_devices->seed;
1068 fs_devices->seed = NULL;
1069 }
Yan Zheng2b820322008-11-17 21:11:30 -05001070 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001071
1072 while (seed_devices) {
1073 fs_devices = seed_devices;
1074 seed_devices = fs_devices->seed;
1075 __btrfs_close_devices(fs_devices);
1076 free_fs_devices(fs_devices);
1077 }
Yan Zheng2b820322008-11-17 21:11:30 -05001078 return ret;
1079}
1080
Yan Zhenge4404d62008-12-12 10:03:26 -05001081static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
1082 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001083{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001084 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001085 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +08001086 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -04001087 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001088
Tejun Heod4d77622010-11-13 11:55:18 +01001089 flags |= FMODE_EXCL;
1090
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001091 list_for_each_entry(device, head, dev_list) {
Eric Sandeenf63e0cc2013-04-04 20:45:08 +00001092 /* Just open everything we can; ignore failures here */
Anand Jain0fb08bc2017-11-09 23:45:24 +08001093 if (btrfs_open_one_device(fs_devices, device, flags, holder))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001094 continue;
Chris Masona0af4692008-05-13 16:03:06 -04001095
Anand Jain9f050db2017-11-09 23:45:25 +08001096 if (!latest_dev ||
1097 device->generation > latest_dev->generation)
1098 latest_dev = device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001099 }
Chris Masona0af4692008-05-13 16:03:06 -04001100 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +03001101 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -04001102 goto out;
1103 }
Yan Zheng2b820322008-11-17 21:11:30 -05001104 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +08001105 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -05001106 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -04001107out:
Yan Zheng2b820322008-11-17 21:11:30 -05001108 return ret;
1109}
1110
Anand Jainf8e10cd2018-01-22 14:49:36 -08001111static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1112{
1113 struct btrfs_device *dev1, *dev2;
1114
1115 dev1 = list_entry(a, struct btrfs_device, dev_list);
1116 dev2 = list_entry(b, struct btrfs_device, dev_list);
1117
1118 if (dev1->devid < dev2->devid)
1119 return -1;
1120 else if (dev1->devid > dev2->devid)
1121 return 1;
1122 return 0;
1123}
1124
Yan Zheng2b820322008-11-17 21:11:30 -05001125int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001126 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001127{
1128 int ret;
1129
1130 mutex_lock(&uuid_mutex);
1131 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001132 fs_devices->opened++;
1133 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001134 } else {
Anand Jainf8e10cd2018-01-22 14:49:36 -08001135 list_sort(NULL, &fs_devices->devices, devid_cmp);
Chris Mason15916de2008-11-19 21:17:22 -05001136 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001137 }
Chris Mason8a4b83c2008-03-24 15:02:07 -04001138 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001139 return ret;
1140}
1141
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001142static void btrfs_release_disk_super(struct page *page)
Anand Jain6cf86a002016-02-13 10:01:29 +08001143{
1144 kunmap(page);
1145 put_page(page);
1146}
1147
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001148static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1149 struct page **page,
1150 struct btrfs_super_block **disk_super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001151{
1152 void *p;
1153 pgoff_t index;
1154
1155 /* make sure our super fits in the device */
1156 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1157 return 1;
1158
1159 /* make sure our super fits in the page */
1160 if (sizeof(**disk_super) > PAGE_SIZE)
1161 return 1;
1162
1163 /* make sure our super doesn't straddle pages on disk */
1164 index = bytenr >> PAGE_SHIFT;
1165 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1166 return 1;
1167
1168 /* pull in the page with our super */
1169 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1170 index, GFP_KERNEL);
1171
1172 if (IS_ERR_OR_NULL(*page))
1173 return 1;
1174
1175 p = kmap(*page);
1176
1177 /* align our pointer to the offset of the super block */
1178 *disk_super = p + (bytenr & ~PAGE_MASK);
1179
1180 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1181 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1182 btrfs_release_disk_super(*page);
1183 return 1;
1184 }
1185
1186 if ((*disk_super)->label[0] &&
1187 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1188 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1189
1190 return 0;
1191}
1192
David Sterba6f60cbd2013-02-15 11:31:02 -07001193/*
1194 * Look for a btrfs signature on a device. This may be called out of the mount path
1195 * and we are not allowed to call set_blocksize during the scan. The superblock
1196 * is read via pagecache
1197 */
Christoph Hellwig97288f22008-12-02 06:36:09 -05001198int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -04001199 struct btrfs_fs_devices **fs_devices_ret)
1200{
1201 struct btrfs_super_block *disk_super;
Anand Jaine124ece2018-01-18 22:02:35 +08001202 struct btrfs_device *device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001203 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001204 struct page *page;
Anand Jaine124ece2018-01-18 22:02:35 +08001205 int ret = 0;
David Sterba6f60cbd2013-02-15 11:31:02 -07001206 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001207
David Sterba6f60cbd2013-02-15 11:31:02 -07001208 /*
1209 * we would like to check all the supers, but that would make
1210 * a btrfs mount succeed after a mkfs from a different FS.
1211 * So, we need to add a special mount option to scan for
1212 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1213 */
1214 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001215 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -05001216 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -07001217
1218 bdev = blkdev_get_by_path(path, flags, holder);
David Sterba6f60cbd2013-02-15 11:31:02 -07001219 if (IS_ERR(bdev)) {
1220 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001221 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -07001222 }
1223
Anand Jain05a5c552017-12-15 15:40:16 +08001224 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
1225 ret = -EINVAL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001226 goto error_bdev_put;
Anand Jain05a5c552017-12-15 15:40:16 +08001227 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001228
Anand Jain3acbcbf2018-01-18 22:02:36 +08001229 device = device_list_add(path, disk_super);
Anand Jaine124ece2018-01-18 22:02:35 +08001230 if (IS_ERR(device))
1231 ret = PTR_ERR(device);
1232 else
1233 *fs_devices_ret = device->fs_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001234
Anand Jain6cf86a002016-02-13 10:01:29 +08001235 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001236
1237error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001238 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001239error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001240 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001241 return ret;
1242}
Chris Mason0b86a832008-03-24 15:01:56 -04001243
Miao Xie6d07bce2011-01-05 10:07:31 +00001244/* helper to account the used device space in the range */
1245int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1246 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -04001247{
1248 struct btrfs_key key;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001249 struct btrfs_root *root = device->fs_info->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +00001250 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001251 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +00001252 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001253 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +00001254 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001255 struct extent_buffer *l;
1256
Miao Xie6d07bce2011-01-05 10:07:31 +00001257 *length = 0;
1258
Anand Jain401e29c2017-12-04 12:54:55 +08001259 if (start >= device->total_bytes ||
1260 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Miao Xie6d07bce2011-01-05 10:07:31 +00001261 return 0;
1262
Yan Zheng2b820322008-11-17 21:11:30 -05001263 path = btrfs_alloc_path();
1264 if (!path)
1265 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01001266 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04001267
Chris Mason0b86a832008-03-24 15:01:56 -04001268 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001269 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001270 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001271
1272 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001273 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001274 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001275 if (ret > 0) {
1276 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1277 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001278 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001279 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001280
Chris Mason0b86a832008-03-24 15:01:56 -04001281 while (1) {
1282 l = path->nodes[0];
1283 slot = path->slots[0];
1284 if (slot >= btrfs_header_nritems(l)) {
1285 ret = btrfs_next_leaf(root, path);
1286 if (ret == 0)
1287 continue;
1288 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001289 goto out;
1290
1291 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001292 }
1293 btrfs_item_key_to_cpu(l, &key, slot);
1294
1295 if (key.objectid < device->devid)
1296 goto next;
1297
1298 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001299 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001300
David Sterba962a2982014-06-04 18:41:45 +02001301 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001302 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001303
Chris Mason0b86a832008-03-24 15:01:56 -04001304 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001305 extent_end = key.offset + btrfs_dev_extent_length(l,
1306 dev_extent);
1307 if (key.offset <= start && extent_end > end) {
1308 *length = end - start + 1;
1309 break;
1310 } else if (key.offset <= start && extent_end > start)
1311 *length += extent_end - start;
1312 else if (key.offset > start && extent_end <= end)
1313 *length += extent_end - key.offset;
1314 else if (key.offset > start && key.offset <= end) {
1315 *length += end - key.offset + 1;
1316 break;
1317 } else if (key.offset > end)
1318 break;
1319
1320next:
1321 path->slots[0]++;
1322 }
1323 ret = 0;
1324out:
1325 btrfs_free_path(path);
1326 return ret;
1327}
1328
Jeff Mahoney499f3772015-06-15 09:41:17 -04001329static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001330 struct btrfs_device *device,
1331 u64 *start, u64 len)
1332{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001333 struct btrfs_fs_info *fs_info = device->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001334 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001335 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001336 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001337 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001338
Jeff Mahoney499f3772015-06-15 09:41:17 -04001339 if (transaction)
1340 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001341again:
1342 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001343 struct map_lookup *map;
1344 int i;
1345
Jeff Mahoney95617d62015-06-03 10:55:48 -04001346 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04001347 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001348 u64 end;
1349
Josef Bacik6df9a952013-06-27 13:22:46 -04001350 if (map->stripes[i].dev != device)
1351 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001352 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001353 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001354 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001355 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001356 /*
1357 * Make sure that while processing the pinned list we do
1358 * not override our *start with a lower value, because
1359 * we can have pinned chunks that fall within this
1360 * device hole and that have lower physical addresses
1361 * than the pending chunks we processed before. If we
1362 * do not take this special care we can end up getting
1363 * 2 pending chunks that start at the same physical
1364 * device offsets because the end offset of a pinned
1365 * chunk can be equal to the start offset of some
1366 * pending chunk.
1367 */
1368 end = map->stripes[i].physical + em->orig_block_len;
1369 if (end > *start) {
1370 *start = end;
1371 ret = 1;
1372 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001373 }
1374 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001375 if (search_list != &fs_info->pinned_chunks) {
1376 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001377 goto again;
1378 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001379
1380 return ret;
1381}
1382
1383
Chris Mason0b86a832008-03-24 15:01:56 -04001384/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001385 * find_free_dev_extent_start - find free space in the specified device
1386 * @device: the device which we search the free space in
1387 * @num_bytes: the size of the free space that we need
1388 * @search_start: the position from which to begin the search
1389 * @start: store the start of the free space.
1390 * @len: the size of the free space. that we find, or the size
1391 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001392 *
Chris Mason0b86a832008-03-24 15:01:56 -04001393 * this uses a pretty simple search, the expectation is that it is
1394 * called very infrequently and that a given device has a small number
1395 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001396 *
1397 * @start is used to store the start of the free space if we find. But if we
1398 * don't find suitable free space, it will be used to store the start position
1399 * of the max free space.
1400 *
1401 * @len is used to store the size of the free space that we find.
1402 * But if we don't find suitable free space, it is used to store the size of
1403 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001404 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001405int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1406 struct btrfs_device *device, u64 num_bytes,
1407 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001408{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001409 struct btrfs_fs_info *fs_info = device->fs_info;
1410 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001411 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001412 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001413 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001414 u64 hole_size;
1415 u64 max_hole_start;
1416 u64 max_hole_size;
1417 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001418 u64 search_end = device->total_bytes;
1419 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001420 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001421 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001422
1423 /*
1424 * We don't want to overwrite the superblock on the drive nor any area
1425 * used by the boot loader (grub for example), so we make sure to start
1426 * at an offset of at least 1MB.
1427 */
David Sterba0d0c71b2017-06-15 01:30:06 +02001428 search_start = max_t(u64, search_start, SZ_1M);
Chris Mason0b86a832008-03-24 15:01:56 -04001429
Josef Bacik6df9a952013-06-27 13:22:46 -04001430 path = btrfs_alloc_path();
1431 if (!path)
1432 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001433
Miao Xie7bfc8372011-01-05 10:07:26 +00001434 max_hole_start = search_start;
1435 max_hole_size = 0;
1436
Zhao Leif2ab7612015-02-16 18:52:17 +08001437again:
Anand Jain401e29c2017-12-04 12:54:55 +08001438 if (search_start >= search_end ||
1439 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001440 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001441 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001442 }
1443
David Sterbae4058b52015-11-27 16:31:35 +01001444 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001445 path->search_commit_root = 1;
1446 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001447
Chris Mason0b86a832008-03-24 15:01:56 -04001448 key.objectid = device->devid;
1449 key.offset = search_start;
1450 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001451
Li Zefan125ccb02011-12-08 15:07:24 +08001452 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001453 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001454 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001455 if (ret > 0) {
1456 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1457 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001458 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001459 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001460
Chris Mason0b86a832008-03-24 15:01:56 -04001461 while (1) {
1462 l = path->nodes[0];
1463 slot = path->slots[0];
1464 if (slot >= btrfs_header_nritems(l)) {
1465 ret = btrfs_next_leaf(root, path);
1466 if (ret == 0)
1467 continue;
1468 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001469 goto out;
1470
1471 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001472 }
1473 btrfs_item_key_to_cpu(l, &key, slot);
1474
1475 if (key.objectid < device->devid)
1476 goto next;
1477
1478 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001479 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001480
David Sterba962a2982014-06-04 18:41:45 +02001481 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001482 goto next;
1483
Miao Xie7bfc8372011-01-05 10:07:26 +00001484 if (key.offset > search_start) {
1485 hole_size = key.offset - search_start;
1486
Josef Bacik6df9a952013-06-27 13:22:46 -04001487 /*
1488 * Have to check before we set max_hole_start, otherwise
1489 * we could end up sending back this offset anyway.
1490 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001491 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001492 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001493 hole_size)) {
1494 if (key.offset >= search_start) {
1495 hole_size = key.offset - search_start;
1496 } else {
1497 WARN_ON_ONCE(1);
1498 hole_size = 0;
1499 }
1500 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001501
Miao Xie7bfc8372011-01-05 10:07:26 +00001502 if (hole_size > max_hole_size) {
1503 max_hole_start = search_start;
1504 max_hole_size = hole_size;
1505 }
1506
1507 /*
1508 * If this free space is greater than which we need,
1509 * it must be the max free space that we have found
1510 * until now, so max_hole_start must point to the start
1511 * of this free space and the length of this free space
1512 * is stored in max_hole_size. Thus, we return
1513 * max_hole_start and max_hole_size and go back to the
1514 * caller.
1515 */
1516 if (hole_size >= num_bytes) {
1517 ret = 0;
1518 goto out;
1519 }
1520 }
1521
Chris Mason0b86a832008-03-24 15:01:56 -04001522 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001523 extent_end = key.offset + btrfs_dev_extent_length(l,
1524 dev_extent);
1525 if (extent_end > search_start)
1526 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001527next:
1528 path->slots[0]++;
1529 cond_resched();
1530 }
Chris Mason0b86a832008-03-24 15:01:56 -04001531
liubo38c01b92011-08-02 02:39:03 +00001532 /*
1533 * At this point, search_start should be the end of
1534 * allocated dev extents, and when shrinking the device,
1535 * search_end may be smaller than search_start.
1536 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001537 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001538 hole_size = search_end - search_start;
1539
Jeff Mahoney499f3772015-06-15 09:41:17 -04001540 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001541 hole_size)) {
1542 btrfs_release_path(path);
1543 goto again;
1544 }
Chris Mason0b86a832008-03-24 15:01:56 -04001545
Zhao Leif2ab7612015-02-16 18:52:17 +08001546 if (hole_size > max_hole_size) {
1547 max_hole_start = search_start;
1548 max_hole_size = hole_size;
1549 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001550 }
1551
Miao Xie7bfc8372011-01-05 10:07:26 +00001552 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001553 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001554 ret = -ENOSPC;
1555 else
1556 ret = 0;
1557
1558out:
Yan Zheng2b820322008-11-17 21:11:30 -05001559 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001560 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001561 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001562 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001563 return ret;
1564}
1565
Jeff Mahoney499f3772015-06-15 09:41:17 -04001566int find_free_dev_extent(struct btrfs_trans_handle *trans,
1567 struct btrfs_device *device, u64 num_bytes,
1568 u64 *start, u64 *len)
1569{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001570 /* FIXME use last free of some kind */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001571 return find_free_dev_extent_start(trans->transaction, device,
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001572 num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001573}
1574
Christoph Hellwigb2950862008-12-02 09:54:17 -05001575static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001576 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001577 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001578{
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 Mason8f18cf12008-04-25 16:53:30 -04001581 int ret;
1582 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001583 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001584 struct btrfs_key found_key;
1585 struct extent_buffer *leaf = NULL;
1586 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001587
1588 path = btrfs_alloc_path();
1589 if (!path)
1590 return -ENOMEM;
1591
1592 key.objectid = device->devid;
1593 key.offset = start;
1594 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001595again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001596 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001597 if (ret > 0) {
1598 ret = btrfs_previous_item(root, path, key.objectid,
1599 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001600 if (ret)
1601 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001602 leaf = path->nodes[0];
1603 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1604 extent = btrfs_item_ptr(leaf, path->slots[0],
1605 struct btrfs_dev_extent);
1606 BUG_ON(found_key.offset > start || found_key.offset +
1607 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001608 key = found_key;
1609 btrfs_release_path(path);
1610 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001611 } else if (ret == 0) {
1612 leaf = path->nodes[0];
1613 extent = btrfs_item_ptr(leaf, path->slots[0],
1614 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001615 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001616 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001617 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001618 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001619
Miao Xie2196d6e2014-09-03 21:35:41 +08001620 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1621
Chris Mason8f18cf12008-04-25 16:53:30 -04001622 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001623 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001624 btrfs_handle_fs_error(fs_info, ret,
1625 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001626 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001627 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001628 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001629out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001630 btrfs_free_path(path);
1631 return ret;
1632}
1633
Eric Sandeen48a3b632013-04-25 20:41:01 +00001634static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1635 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001636 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001637{
1638 int ret;
1639 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001640 struct btrfs_fs_info *fs_info = device->fs_info;
1641 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001642 struct btrfs_dev_extent *extent;
1643 struct extent_buffer *leaf;
1644 struct btrfs_key key;
1645
Anand Jaine12c9622017-12-04 12:54:53 +08001646 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
Anand Jain401e29c2017-12-04 12:54:55 +08001647 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
Chris Mason0b86a832008-03-24 15:01:56 -04001648 path = btrfs_alloc_path();
1649 if (!path)
1650 return -ENOMEM;
1651
Chris Mason0b86a832008-03-24 15:01:56 -04001652 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001653 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001654 key.type = BTRFS_DEV_EXTENT_KEY;
1655 ret = btrfs_insert_empty_item(trans, root, path, &key,
1656 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001657 if (ret)
1658 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001659
1660 leaf = path->nodes[0];
1661 extent = btrfs_item_ptr(leaf, path->slots[0],
1662 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001663 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1664 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001665 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1666 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001667 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1668
Chris Mason0b86a832008-03-24 15:01:56 -04001669 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1670 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001671out:
Chris Mason0b86a832008-03-24 15:01:56 -04001672 btrfs_free_path(path);
1673 return ret;
1674}
1675
Josef Bacik6df9a952013-06-27 13:22:46 -04001676static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001677{
Josef Bacik6df9a952013-06-27 13:22:46 -04001678 struct extent_map_tree *em_tree;
1679 struct extent_map *em;
1680 struct rb_node *n;
1681 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001682
Josef Bacik6df9a952013-06-27 13:22:46 -04001683 em_tree = &fs_info->mapping_tree.map_tree;
1684 read_lock(&em_tree->lock);
1685 n = rb_last(&em_tree->map);
1686 if (n) {
1687 em = rb_entry(n, struct extent_map, rb_node);
1688 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001689 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001690 read_unlock(&em_tree->lock);
1691
Chris Mason0b86a832008-03-24 15:01:56 -04001692 return ret;
1693}
1694
Ilya Dryomov53f10652013-08-12 14:33:01 +03001695static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1696 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001697{
1698 int ret;
1699 struct btrfs_key key;
1700 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001701 struct btrfs_path *path;
1702
Yan Zheng2b820322008-11-17 21:11:30 -05001703 path = btrfs_alloc_path();
1704 if (!path)
1705 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001706
1707 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1708 key.type = BTRFS_DEV_ITEM_KEY;
1709 key.offset = (u64)-1;
1710
Ilya Dryomov53f10652013-08-12 14:33:01 +03001711 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001712 if (ret < 0)
1713 goto error;
1714
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001715 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001716
Ilya Dryomov53f10652013-08-12 14:33:01 +03001717 ret = btrfs_previous_item(fs_info->chunk_root, path,
1718 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001719 BTRFS_DEV_ITEM_KEY);
1720 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001721 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001722 } else {
1723 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1724 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001725 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001726 }
1727 ret = 0;
1728error:
Yan Zheng2b820322008-11-17 21:11:30 -05001729 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001730 return ret;
1731}
1732
1733/*
1734 * the device information is stored in the chunk root
1735 * the btrfs_device struct should be fully filled in
1736 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001737static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001738 struct btrfs_fs_info *fs_info,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001739 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001740{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001741 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001742 int ret;
1743 struct btrfs_path *path;
1744 struct btrfs_dev_item *dev_item;
1745 struct extent_buffer *leaf;
1746 struct btrfs_key key;
1747 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001748
Chris Mason0b86a832008-03-24 15:01:56 -04001749 path = btrfs_alloc_path();
1750 if (!path)
1751 return -ENOMEM;
1752
Chris Mason0b86a832008-03-24 15:01:56 -04001753 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1754 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001755 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001756
1757 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001758 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001759 if (ret)
1760 goto out;
1761
1762 leaf = path->nodes[0];
1763 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1764
1765 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001766 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001767 btrfs_set_device_type(leaf, dev_item, device->type);
1768 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1769 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1770 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001771 btrfs_set_device_total_bytes(leaf, dev_item,
1772 btrfs_device_get_disk_total_bytes(device));
1773 btrfs_set_device_bytes_used(leaf, dev_item,
1774 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001775 btrfs_set_device_group(leaf, dev_item, 0);
1776 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1777 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001778 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001779
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001780 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001781 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001782 ptr = btrfs_device_fsid(dev_item);
Anand Jain44880fd2017-07-29 17:50:09 +08001783 write_extent_buffer(leaf, fs_info->fsid, ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001784 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001785
Yan Zheng2b820322008-11-17 21:11:30 -05001786 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001787out:
1788 btrfs_free_path(path);
1789 return ret;
1790}
Chris Mason8f18cf12008-04-25 16:53:30 -04001791
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001792/*
1793 * Function to update ctime/mtime for a given device path.
1794 * Mainly used for ctime/mtime based probe like libblkid.
1795 */
David Sterbada353f62017-02-14 17:55:53 +01001796static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001797{
1798 struct file *filp;
1799
1800 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001801 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001802 return;
1803 file_update_time(filp);
1804 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001805}
1806
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001807static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
Chris Masona061fc82008-05-07 11:43:44 -04001808 struct btrfs_device *device)
1809{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001810 struct btrfs_root *root = fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001811 int ret;
1812 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001813 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001814 struct btrfs_trans_handle *trans;
1815
Chris Masona061fc82008-05-07 11:43:44 -04001816 path = btrfs_alloc_path();
1817 if (!path)
1818 return -ENOMEM;
1819
Yan, Zhenga22285a2010-05-16 10:48:46 -04001820 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001821 if (IS_ERR(trans)) {
1822 btrfs_free_path(path);
1823 return PTR_ERR(trans);
1824 }
Chris Masona061fc82008-05-07 11:43:44 -04001825 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1826 key.type = BTRFS_DEV_ITEM_KEY;
1827 key.offset = device->devid;
1828
1829 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001830 if (ret) {
1831 if (ret > 0)
1832 ret = -ENOENT;
1833 btrfs_abort_transaction(trans, ret);
1834 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001835 goto out;
1836 }
1837
1838 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001839 if (ret) {
1840 btrfs_abort_transaction(trans, ret);
1841 btrfs_end_transaction(trans);
1842 }
1843
Chris Masona061fc82008-05-07 11:43:44 -04001844out:
1845 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001846 if (!ret)
1847 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001848 return ret;
1849}
1850
David Sterba3cc31a02016-02-15 16:00:26 +01001851/*
1852 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1853 * filesystem. It's up to the caller to adjust that number regarding eg. device
1854 * replace.
1855 */
1856static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1857 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001858{
Chris Masona061fc82008-05-07 11:43:44 -04001859 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00001860 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01001861 int i;
Chris Masona061fc82008-05-07 11:43:44 -04001862
Miao Xiede98ced2013-01-29 10:13:12 +00001863 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001864 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00001865
Anand Jainbd45ffb2016-02-13 10:01:34 +08001866 all_avail = fs_info->avail_data_alloc_bits |
1867 fs_info->avail_system_alloc_bits |
1868 fs_info->avail_metadata_alloc_bits;
1869 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08001870
David Sterba418775a2016-02-15 16:28:14 +01001871 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1872 if (!(all_avail & btrfs_raid_group[i]))
1873 continue;
Chris Masona061fc82008-05-07 11:43:44 -04001874
David Sterba418775a2016-02-15 16:28:14 +01001875 if (num_devices < btrfs_raid_array[i].devs_min) {
1876 int ret = btrfs_raid_mindev_error[i];
Chris Masona061fc82008-05-07 11:43:44 -04001877
David Sterba418775a2016-02-15 16:28:14 +01001878 if (ret)
1879 return ret;
1880 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08001881 }
1882
1883 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001884}
1885
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001886static struct btrfs_device * btrfs_find_next_active_device(
1887 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08001888{
1889 struct btrfs_device *next_device;
1890
1891 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1892 if (next_device != device &&
Anand Jaine6e674b2017-12-04 12:54:54 +08001893 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
1894 && next_device->bdev)
Anand Jain88acff62016-05-03 17:44:43 +08001895 return next_device;
1896 }
1897
1898 return NULL;
1899}
1900
1901/*
1902 * Helper function to check if the given device is part of s_bdev / latest_bdev
1903 * and replace it with the provided or the next active device, in the context
1904 * where this function called, there should be always be another device (or
1905 * this_dev) which is active.
1906 */
1907void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info,
1908 struct btrfs_device *device, struct btrfs_device *this_dev)
1909{
1910 struct btrfs_device *next_device;
1911
1912 if (this_dev)
1913 next_device = this_dev;
1914 else
1915 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1916 device);
1917 ASSERT(next_device);
1918
1919 if (fs_info->sb->s_bdev &&
1920 (fs_info->sb->s_bdev == device->bdev))
1921 fs_info->sb->s_bdev = next_device->bdev;
1922
1923 if (fs_info->fs_devices->latest_bdev == device->bdev)
1924 fs_info->fs_devices->latest_bdev = next_device->bdev;
1925}
1926
David Sterbada353f62017-02-14 17:55:53 +01001927int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
1928 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08001929{
1930 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001931 struct btrfs_fs_devices *cur_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001932 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001933 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001934
Anand Jain2c997382017-11-06 10:28:00 +08001935 mutex_lock(&fs_info->volume_mutex);
Anand Jainf1fa7f22016-02-13 10:01:33 +08001936 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001937
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001938 num_devices = fs_info->fs_devices->num_devices;
1939 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
1940 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
Chris Masona061fc82008-05-07 11:43:44 -04001941 WARN_ON(num_devices < 1);
1942 num_devices--;
1943 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001944 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Chris Masona061fc82008-05-07 11:43:44 -04001945
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001946 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08001947 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001948 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001949
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001950 ret = btrfs_find_device_by_devspec(fs_info, devid, device_path,
1951 &device);
Anand Jain24fc5722016-02-13 10:01:36 +08001952 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001953 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001954
Anand Jain401e29c2017-12-04 12:54:55 +08001955 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Anand Jain183860f2013-05-17 10:52:45 +00001956 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08001957 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001958 }
1959
Anand Jainebbede42017-12-04 12:54:52 +08001960 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1961 fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001962 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08001963 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001964 }
1965
Anand Jainebbede42017-12-04 12:54:52 +08001966 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02001967 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001968 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001969 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02001970 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001971 }
Chris Masona061fc82008-05-07 11:43:44 -04001972
Carey Underwoodd7901552013-03-04 16:37:06 -06001973 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001974 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001975 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001976 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001977 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001978
Stefan Behrens63a212a2012-11-05 18:29:28 +01001979 /*
1980 * TODO: the superblock still includes this device in its num_devices
1981 * counter although write_all_supers() is not locked out. This
1982 * could give a filesystem state which requires a degraded mount.
1983 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001984 ret = btrfs_rm_dev_item(fs_info, device);
Chris Masona061fc82008-05-07 11:43:44 -04001985 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001986 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001987
Anand Jaine12c9622017-12-04 12:54:53 +08001988 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001989 btrfs_scrub_cancel_dev(fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001990
1991 /*
1992 * the device list mutex makes sure that we don't change
1993 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001994 * the device supers. Whoever is writing all supers, should
1995 * lock the device list mutex before getting the number of
1996 * devices in the super block (super_copy). Conversely,
1997 * whoever updates the number of devices in the super block
1998 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001999 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002000
2001 cur_devices = device->fs_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002002 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002003 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002004
Yan Zhenge4404d62008-12-12 10:03:26 -05002005 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04002006 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05002007
Anand Jaine6e674b2017-12-04 12:54:54 +08002008 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Miao Xie3a7d55c2014-07-16 18:38:01 +08002009 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05002010
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002011 btrfs_assign_next_active_device(fs_info, device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05002012
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002013 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002014 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002015 /* remove sysfs entry */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002016 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002017 }
Anand Jain99994cd2014-06-03 11:36:00 +08002018
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002019 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2020 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
2021 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002022
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002023 /*
2024 * at this point, the device is zero sized and detached from
2025 * the devices list. All that's left is to zero out the old
2026 * supers and free the device.
2027 */
Anand Jainebbede42017-12-04 12:54:52 +08002028 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002029 btrfs_scratch_superblocks(device->bdev, device->name->str);
2030
2031 btrfs_close_bdev(device);
David Sterbaf06c5962017-06-06 17:08:23 +02002032 call_rcu(&device->rcu, free_device_rcu);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002033
Xiao Guangrong1f781602011-04-20 10:09:16 +00002034 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002035 struct btrfs_fs_devices *fs_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002036 fs_devices = fs_info->fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002037 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002038 if (fs_devices->seed == cur_devices) {
2039 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05002040 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002041 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002042 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05002043 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00002044 cur_devices->seed = NULL;
Xiao Guangrong1f781602011-04-20 10:09:16 +00002045 __btrfs_close_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002046 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002047 }
2048
Chris Masona061fc82008-05-07 11:43:44 -04002049out:
2050 mutex_unlock(&uuid_mutex);
Anand Jain2c997382017-11-06 10:28:00 +08002051 mutex_unlock(&fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002052 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08002053
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002054error_undo:
Anand Jainebbede42017-12-04 12:54:52 +08002055 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002056 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002057 list_add(&device->dev_alloc_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002058 &fs_info->fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002059 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002060 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002061 }
Anand Jain24fc5722016-02-13 10:01:36 +08002062 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002063}
2064
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002065void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
2066 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002067{
Anand Jaind51908c2014-08-13 14:24:19 +08002068 struct btrfs_fs_devices *fs_devices;
2069
Stefan Behrense93c89c2012-11-05 17:33:06 +01002070 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03002071
Anand Jain25e8e912014-08-20 10:56:56 +08002072 /*
2073 * in case of fs with no seed, srcdev->fs_devices will point
2074 * to fs_devices of fs_info. However when the dev being replaced is
2075 * a seed dev it will point to the seed's local fs_devices. In short
2076 * srcdev will have its correct fs_devices in both the cases.
2077 */
2078 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002079
Stefan Behrense93c89c2012-11-05 17:33:06 +01002080 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002081 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002082 fs_devices->num_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08002083 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
Anand Jaind51908c2014-08-13 14:24:19 +08002084 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002085
Anand Jainebbede42017-12-04 12:54:52 +08002086 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
Miao Xie82372bc2014-09-03 21:35:44 +08002087 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002088
Miao Xie82372bc2014-09-03 21:35:44 +08002089 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002090 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002091}
2092
2093void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2094 struct btrfs_device *srcdev)
2095{
2096 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002097
Anand Jainebbede42017-12-04 12:54:52 +08002098 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
Anand Jain48b3b9d2016-04-12 21:36:16 +08002099 /* zero out the old super if it is writable */
2100 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2101 }
Anand Jain14238812016-07-22 06:04:53 +08002102
2103 btrfs_close_bdev(srcdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002104 call_rcu(&srcdev->rcu, free_device_rcu);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002105
Anand Jain94d5f0c2014-08-13 14:24:22 +08002106 /* if this is no devs we rather delete the fs_devices */
2107 if (!fs_devices->num_devices) {
2108 struct btrfs_fs_devices *tmp_fs_devices;
2109
Anand Jain6dd38f82017-10-17 06:53:50 +08002110 /*
2111 * On a mounted FS, num_devices can't be zero unless it's a
2112 * seed. In case of a seed device being replaced, the replace
2113 * target added to the sprout FS, so there will be no more
2114 * device left under the seed FS.
2115 */
2116 ASSERT(fs_devices->seeding);
2117
Anand Jain94d5f0c2014-08-13 14:24:22 +08002118 tmp_fs_devices = fs_info->fs_devices;
2119 while (tmp_fs_devices) {
2120 if (tmp_fs_devices->seed == fs_devices) {
2121 tmp_fs_devices->seed = fs_devices->seed;
2122 break;
2123 }
2124 tmp_fs_devices = tmp_fs_devices->seed;
2125 }
2126 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08002127 __btrfs_close_devices(fs_devices);
2128 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002129 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002130}
2131
2132void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2133 struct btrfs_device *tgtdev)
2134{
Miao Xie67a2c452014-09-03 21:35:43 +08002135 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002136 WARN_ON(!tgtdev);
2137 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002138
Anand Jain32576042015-08-14 18:32:49 +08002139 btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002140
Anand Jain779bf3fe2016-04-18 16:51:23 +08002141 if (tgtdev->bdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002142 fs_info->fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002143
Stefan Behrense93c89c2012-11-05 17:33:06 +01002144 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002145
Anand Jain88acff62016-05-03 17:44:43 +08002146 btrfs_assign_next_active_device(fs_info, tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002147
Stefan Behrense93c89c2012-11-05 17:33:06 +01002148 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002149
2150 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08002151 mutex_unlock(&uuid_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002152
2153 /*
2154 * The update_dev_time() with in btrfs_scratch_superblocks()
2155 * may lead to a call to btrfs_show_devname() which will try
2156 * to hold device_list_mutex. And here this device
2157 * is already out of device list, so we don't have to hold
2158 * the device_list_mutex lock.
2159 */
2160 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002161
2162 btrfs_close_bdev(tgtdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002163 call_rcu(&tgtdev->rcu, free_device_rcu);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002164}
2165
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002166static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002167 const char *device_path,
Eric Sandeen48a3b632013-04-25 20:41:01 +00002168 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002169{
2170 int ret = 0;
2171 struct btrfs_super_block *disk_super;
2172 u64 devid;
2173 u8 *dev_uuid;
2174 struct block_device *bdev;
2175 struct buffer_head *bh;
2176
2177 *device = NULL;
2178 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002179 fs_info->bdev_holder, 0, &bdev, &bh);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002180 if (ret)
2181 return ret;
2182 disk_super = (struct btrfs_super_block *)bh->b_data;
2183 devid = btrfs_stack_device_id(&disk_super->dev_item);
2184 dev_uuid = disk_super->dev_item.uuid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002185 *device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002186 brelse(bh);
2187 if (!*device)
2188 ret = -ENOENT;
2189 blkdev_put(bdev, FMODE_READ);
2190 return ret;
2191}
2192
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002193int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002194 const char *device_path,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002195 struct btrfs_device **device)
2196{
2197 *device = NULL;
2198 if (strcmp(device_path, "missing") == 0) {
2199 struct list_head *devices;
2200 struct btrfs_device *tmp;
2201
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002202 devices = &fs_info->fs_devices->devices;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002203 /*
2204 * It is safe to read the devices since the volume_mutex
2205 * is held by the caller.
2206 */
2207 list_for_each_entry(tmp, devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +08002208 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2209 &tmp->dev_state) && !tmp->bdev) {
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002210 *device = tmp;
2211 break;
2212 }
2213 }
2214
Anand Jaind74a6252015-08-14 18:32:56 +08002215 if (!*device)
2216 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002217
2218 return 0;
2219 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002220 return btrfs_find_device_by_path(fs_info, device_path, device);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002221 }
2222}
2223
Yan Zheng2b820322008-11-17 21:11:30 -05002224/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002225 * Lookup a device given by device id, or the path if the id is 0.
2226 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002227int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid,
David Sterbada353f62017-02-14 17:55:53 +01002228 const char *devpath,
2229 struct btrfs_device **device)
Anand Jain24e04742016-02-13 10:01:35 +08002230{
2231 int ret;
2232
David Sterba5c5c0df2016-02-15 16:39:55 +01002233 if (devid) {
Anand Jain24e04742016-02-13 10:01:35 +08002234 ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002235 *device = btrfs_find_device(fs_info, devid, NULL, NULL);
Anand Jain24e04742016-02-13 10:01:35 +08002236 if (!*device)
2237 ret = -ENOENT;
2238 } else {
David Sterba5c5c0df2016-02-15 16:39:55 +01002239 if (!devpath || !devpath[0])
Anand Jainb3d1b152016-02-13 10:01:37 +08002240 return -EINVAL;
2241
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002242 ret = btrfs_find_device_missing_or_by_path(fs_info, devpath,
Anand Jain24e04742016-02-13 10:01:35 +08002243 device);
2244 }
2245 return ret;
2246}
2247
Yan Zheng2b820322008-11-17 21:11:30 -05002248/*
2249 * does all the dirty work required for changing file system's UUID.
2250 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002251static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002252{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002253 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002254 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002255 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002256 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002257 struct btrfs_device *device;
2258 u64 super_flags;
2259
2260 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05002261 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002262 return -EINVAL;
2263
David Sterba2dfeca92017-06-14 02:48:07 +02002264 seed_devices = alloc_fs_devices(NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002265 if (IS_ERR(seed_devices))
2266 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002267
Yan Zhenge4404d62008-12-12 10:03:26 -05002268 old_devices = clone_fs_devices(fs_devices);
2269 if (IS_ERR(old_devices)) {
2270 kfree(seed_devices);
2271 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002272 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002273
Yan Zheng2b820322008-11-17 21:11:30 -05002274 list_add(&old_devices->list, &fs_uuids);
2275
Yan Zhenge4404d62008-12-12 10:03:26 -05002276 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2277 seed_devices->opened = 1;
2278 INIT_LIST_HEAD(&seed_devices->devices);
2279 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002280 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002281
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002282 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002283 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2284 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002285 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002286 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002287
David Sterba34441362016-10-04 19:34:27 +02002288 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002289 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002290 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002291
Yan Zheng2b820322008-11-17 21:11:30 -05002292 fs_devices->seeding = 0;
2293 fs_devices->num_devices = 0;
2294 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002295 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002296 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002297 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002298
2299 generate_random_uuid(fs_devices->fsid);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002300 memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002301 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002302 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002303
Yan Zheng2b820322008-11-17 21:11:30 -05002304 super_flags = btrfs_super_flags(disk_super) &
2305 ~BTRFS_SUPER_FLAG_SEEDING;
2306 btrfs_set_super_flags(disk_super, super_flags);
2307
2308 return 0;
2309}
2310
2311/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002312 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002313 */
2314static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002315 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002316{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002317 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002318 struct btrfs_path *path;
2319 struct extent_buffer *leaf;
2320 struct btrfs_dev_item *dev_item;
2321 struct btrfs_device *device;
2322 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002323 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002324 u8 dev_uuid[BTRFS_UUID_SIZE];
2325 u64 devid;
2326 int ret;
2327
2328 path = btrfs_alloc_path();
2329 if (!path)
2330 return -ENOMEM;
2331
Yan Zheng2b820322008-11-17 21:11:30 -05002332 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2333 key.offset = 0;
2334 key.type = BTRFS_DEV_ITEM_KEY;
2335
2336 while (1) {
2337 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2338 if (ret < 0)
2339 goto error;
2340
2341 leaf = path->nodes[0];
2342next_slot:
2343 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2344 ret = btrfs_next_leaf(root, path);
2345 if (ret > 0)
2346 break;
2347 if (ret < 0)
2348 goto error;
2349 leaf = path->nodes[0];
2350 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002351 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002352 continue;
2353 }
2354
2355 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2356 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2357 key.type != BTRFS_DEV_ITEM_KEY)
2358 break;
2359
2360 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2361 struct btrfs_dev_item);
2362 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002363 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002364 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002365 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002366 BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002367 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002368 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002369
2370 if (device->fs_devices->seeding) {
2371 btrfs_set_device_generation(leaf, dev_item,
2372 device->generation);
2373 btrfs_mark_buffer_dirty(leaf);
2374 }
2375
2376 path->slots[0]++;
2377 goto next_slot;
2378 }
2379 ret = 0;
2380error:
2381 btrfs_free_path(path);
2382 return ret;
2383}
2384
David Sterbada353f62017-02-14 17:55:53 +01002385int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002386{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002387 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002388 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002389 struct btrfs_trans_handle *trans;
2390 struct btrfs_device *device;
2391 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002392 struct list_head *devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002393 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002394 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002395 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002396 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002397 int ret = 0;
Anand Jain7132a262017-09-28 14:51:11 +08002398 bool unlocked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002399
David Howellsbc98a422017-07-17 08:45:34 +01002400 if (sb_rdonly(sb) && !fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002401 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002402
Li Zefana5d16332011-12-07 20:08:40 -05002403 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002404 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002405 if (IS_ERR(bdev))
2406 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002407
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002408 if (fs_info->fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002409 seeding_dev = 1;
2410 down_write(&sb->s_umount);
2411 mutex_lock(&uuid_mutex);
2412 }
2413
Chris Mason8c8bee12008-09-29 11:19:10 -04002414 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002415
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002416 devices = &fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002417
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002418 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002419 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002420 if (device->bdev == bdev) {
2421 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002422 mutex_unlock(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002423 &fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002424 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002425 }
2426 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002427 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002428
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002429 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002430 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002431 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002432 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002433 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002434 }
2435
David Sterba78f2c9e2016-02-11 14:25:38 +01002436 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002437 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002438 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002439 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002440 }
Josef Bacik606686e2012-06-04 14:03:51 -04002441 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002442
Yan, Zhenga22285a2010-05-16 10:48:46 -04002443 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002444 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002445 ret = PTR_ERR(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002446 goto error_free_device;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002447 }
2448
Josef Bacikd5e20032011-08-04 14:52:27 +00002449 q = bdev_get_queue(bdev);
Anand Jainebbede42017-12-04 12:54:52 +08002450 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05002451 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002452 device->io_width = fs_info->sectorsize;
2453 device->io_align = fs_info->sectorsize;
2454 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002455 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2456 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002457 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002458 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002459 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002460 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002461 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002462 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002463 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002464 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002465 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002466
Yan Zheng2b820322008-11-17 21:11:30 -05002467 if (seeding_dev) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002468 sb->s_flags &= ~SB_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002469 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002470 if (ret) {
2471 btrfs_abort_transaction(trans, ret);
2472 goto error_trans;
2473 }
Yan Zheng2b820322008-11-17 21:11:30 -05002474 }
2475
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002476 device->fs_devices = fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002477
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002478 mutex_lock(&fs_info->fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002479 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002480 list_add_rcu(&device->dev_list, &fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002481 list_add(&device->dev_alloc_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002482 &fs_info->fs_devices->alloc_list);
2483 fs_info->fs_devices->num_devices++;
2484 fs_info->fs_devices->open_devices++;
2485 fs_info->fs_devices->rw_devices++;
2486 fs_info->fs_devices->total_devices++;
2487 fs_info->fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002488
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002489 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002490
Anand Jaine884f4f2017-04-04 18:40:19 +08002491 if (!blk_queue_nonrot(q))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002492 fs_info->fs_devices->rotating = 1;
Chris Masonc2898112009-06-10 09:51:32 -04002493
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002494 tmp = btrfs_super_total_bytes(fs_info->super_copy);
2495 btrfs_set_super_total_bytes(fs_info->super_copy,
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002496 round_down(tmp + device->total_bytes, fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002497
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002498 tmp = btrfs_super_num_devices(fs_info->super_copy);
2499 btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002500
2501 /* add sysfs device entry */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002502 btrfs_sysfs_add_device_link(fs_info->fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002503
Miao Xie2196d6e2014-09-03 21:35:41 +08002504 /*
2505 * we've got more storage, clear any full flags on the space
2506 * infos
2507 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002508 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002509
David Sterba34441362016-10-04 19:34:27 +02002510 mutex_unlock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002511 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002512
Yan Zheng2b820322008-11-17 21:11:30 -05002513 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002514 mutex_lock(&fs_info->chunk_mutex);
David Sterbae4a4dce2017-02-10 19:49:01 +01002515 ret = init_first_rw_device(trans, fs_info);
David Sterba34441362016-10-04 19:34:27 +02002516 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002517 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002518 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002519 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002520 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002521 }
2522
Anand Jainc74a0b02017-11-06 16:36:15 +08002523 ret = btrfs_add_dev_item(trans, fs_info, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002524 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002525 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002526 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002527 }
2528
2529 if (seeding_dev) {
2530 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2531
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002532 ret = btrfs_finish_sprout(trans, fs_info);
David Sterba005d6422012-09-18 07:52:32 -06002533 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002534 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002535 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002536 }
Anand Jainb2373f22014-06-03 11:36:03 +08002537
2538 /* Sprouting would change fsid of the mounted root,
2539 * so rename the fsid on the sysfs
2540 */
2541 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002542 fs_info->fsid);
2543 if (kobject_rename(&fs_info->fs_devices->fsid_kobj, fsid_buf))
2544 btrfs_warn(fs_info,
2545 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002546 }
2547
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002548 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002549
2550 if (seeding_dev) {
2551 mutex_unlock(&uuid_mutex);
2552 up_write(&sb->s_umount);
Anand Jain7132a262017-09-28 14:51:11 +08002553 unlocked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002554
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002555 if (ret) /* transaction commit */
2556 return ret;
2557
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002558 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002559 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002560 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002561 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002562 trans = btrfs_attach_transaction(root);
2563 if (IS_ERR(trans)) {
2564 if (PTR_ERR(trans) == -ENOENT)
2565 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002566 ret = PTR_ERR(trans);
2567 trans = NULL;
2568 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002569 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002570 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002571 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002572
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002573 /* Update ctime/mtime for libblkid */
2574 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002575 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002576
Anand Jaind31c32f2017-09-28 14:51:10 +08002577error_sysfs:
2578 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002579error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002580 if (seeding_dev)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002581 sb->s_flags |= SB_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002582 if (trans)
2583 btrfs_end_transaction(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002584error_free_device:
David Sterba55de4802017-10-30 18:55:47 +01002585 free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002586error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002587 blkdev_put(bdev, FMODE_EXCL);
Anand Jain7132a262017-09-28 14:51:11 +08002588 if (seeding_dev && !unlocked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002589 mutex_unlock(&uuid_mutex);
2590 up_write(&sb->s_umount);
2591 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002592 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002593}
2594
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002595int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002596 const char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002597 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002598 struct btrfs_device **device_out)
2599{
Stefan Behrense93c89c2012-11-05 17:33:06 +01002600 struct btrfs_device *device;
2601 struct block_device *bdev;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002602 struct list_head *devices;
2603 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002604 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002605 int ret = 0;
2606
2607 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002608 if (fs_info->fs_devices->seeding) {
2609 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002610 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002611 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002612
2613 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2614 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002615 if (IS_ERR(bdev)) {
2616 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002617 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002618 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002619
2620 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2621
2622 devices = &fs_info->fs_devices->devices;
2623 list_for_each_entry(device, devices, dev_list) {
2624 if (device->bdev == bdev) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002625 btrfs_err(fs_info,
2626 "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002627 ret = -EEXIST;
2628 goto error;
2629 }
2630 }
2631
Miao Xie1c433662014-09-03 21:35:32 +08002632
Miao Xie7cc8e582014-09-03 21:35:38 +08002633 if (i_size_read(bdev->bd_inode) <
2634 btrfs_device_get_total_bytes(srcdev)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002635 btrfs_err(fs_info,
2636 "target device is smaller than source device!");
Miao Xie1c433662014-09-03 21:35:32 +08002637 ret = -EINVAL;
2638 goto error;
2639 }
2640
2641
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002642 device = btrfs_alloc_device(NULL, &devid, NULL);
2643 if (IS_ERR(device)) {
2644 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002645 goto error;
2646 }
2647
David Sterba61655722017-06-15 17:16:43 +02002648 name = rcu_string_strdup(device_path, GFP_KERNEL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002649 if (!name) {
David Sterba55de4802017-10-30 18:55:47 +01002650 free_device(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002651 ret = -ENOMEM;
2652 goto error;
2653 }
2654 rcu_assign_pointer(device->name, name);
2655
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002656 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jainebbede42017-12-04 12:54:52 +08002657 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002658 device->generation = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002659 device->io_width = fs_info->sectorsize;
2660 device->io_align = fs_info->sectorsize;
2661 device->sector_size = fs_info->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002662 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2663 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2664 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002665 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002666 device->commit_bytes_used = device->bytes_used;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002667 device->fs_info = fs_info;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002668 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002669 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002670 set_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002671 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002672 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002673 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002674 device->fs_devices = fs_info->fs_devices;
2675 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2676 fs_info->fs_devices->num_devices++;
2677 fs_info->fs_devices->open_devices++;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002678 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002679
2680 *device_out = device;
2681 return ret;
2682
2683error:
2684 blkdev_put(bdev, FMODE_EXCL);
2685 return ret;
2686}
2687
Chris Masond3977122009-01-05 21:25:51 -05002688static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2689 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002690{
2691 int ret;
2692 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002693 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002694 struct btrfs_dev_item *dev_item;
2695 struct extent_buffer *leaf;
2696 struct btrfs_key key;
2697
Chris Mason0b86a832008-03-24 15:01:56 -04002698 path = btrfs_alloc_path();
2699 if (!path)
2700 return -ENOMEM;
2701
2702 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2703 key.type = BTRFS_DEV_ITEM_KEY;
2704 key.offset = device->devid;
2705
2706 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2707 if (ret < 0)
2708 goto out;
2709
2710 if (ret > 0) {
2711 ret = -ENOENT;
2712 goto out;
2713 }
2714
2715 leaf = path->nodes[0];
2716 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2717
2718 btrfs_set_device_id(leaf, dev_item, device->devid);
2719 btrfs_set_device_type(leaf, dev_item, device->type);
2720 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2721 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2722 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002723 btrfs_set_device_total_bytes(leaf, dev_item,
2724 btrfs_device_get_disk_total_bytes(device));
2725 btrfs_set_device_bytes_used(leaf, dev_item,
2726 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002727 btrfs_mark_buffer_dirty(leaf);
2728
2729out:
2730 btrfs_free_path(path);
2731 return ret;
2732}
2733
Miao Xie2196d6e2014-09-03 21:35:41 +08002734int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002735 struct btrfs_device *device, u64 new_size)
2736{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002737 struct btrfs_fs_info *fs_info = device->fs_info;
2738 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002739 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002740 u64 old_total;
2741 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002742
Anand Jainebbede42017-12-04 12:54:52 +08002743 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Yan Zheng2b820322008-11-17 21:11:30 -05002744 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002745
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002746 new_size = round_down(new_size, fs_info->sectorsize);
2747
David Sterba34441362016-10-04 19:34:27 +02002748 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002749 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002750 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002751
Stefan Behrens63a212a2012-11-05 18:29:28 +01002752 if (new_size <= device->total_bytes ||
Anand Jain401e29c2017-12-04 12:54:55 +08002753 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002754 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002755 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002756 }
Yan Zheng2b820322008-11-17 21:11:30 -05002757
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002758 fs_devices = fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002759
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002760 btrfs_set_super_total_bytes(super_copy,
2761 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002762 device->fs_devices->total_rw_bytes += diff;
2763
Miao Xie7cc8e582014-09-03 21:35:38 +08002764 btrfs_device_set_total_bytes(device, new_size);
2765 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002766 btrfs_clear_space_info_full(device->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002767 if (list_empty(&device->resized_list))
2768 list_add_tail(&device->resized_list,
2769 &fs_devices->resized_devices);
David Sterba34441362016-10-04 19:34:27 +02002770 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002771
Chris Mason8f18cf12008-04-25 16:53:30 -04002772 return btrfs_update_device(trans, device);
2773}
2774
2775static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002776 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002777{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002778 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002779 int ret;
2780 struct btrfs_path *path;
2781 struct btrfs_key key;
2782
Chris Mason8f18cf12008-04-25 16:53:30 -04002783 path = btrfs_alloc_path();
2784 if (!path)
2785 return -ENOMEM;
2786
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002787 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002788 key.offset = chunk_offset;
2789 key.type = BTRFS_CHUNK_ITEM_KEY;
2790
2791 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002792 if (ret < 0)
2793 goto out;
2794 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002795 btrfs_handle_fs_error(fs_info, -ENOENT,
2796 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002797 ret = -ENOENT;
2798 goto out;
2799 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002800
2801 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002802 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002803 btrfs_handle_fs_error(fs_info, ret,
2804 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002805out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002806 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002807 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002808}
2809
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002810static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002811{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002812 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002813 struct btrfs_disk_key *disk_key;
2814 struct btrfs_chunk *chunk;
2815 u8 *ptr;
2816 int ret = 0;
2817 u32 num_stripes;
2818 u32 array_size;
2819 u32 len = 0;
2820 u32 cur;
2821 struct btrfs_key key;
2822
David Sterba34441362016-10-04 19:34:27 +02002823 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002824 array_size = btrfs_super_sys_array_size(super_copy);
2825
2826 ptr = super_copy->sys_chunk_array;
2827 cur = 0;
2828
2829 while (cur < array_size) {
2830 disk_key = (struct btrfs_disk_key *)ptr;
2831 btrfs_disk_key_to_cpu(&key, disk_key);
2832
2833 len = sizeof(*disk_key);
2834
2835 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2836 chunk = (struct btrfs_chunk *)(ptr + len);
2837 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2838 len += btrfs_chunk_item_size(num_stripes);
2839 } else {
2840 ret = -EIO;
2841 break;
2842 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002843 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002844 key.offset == chunk_offset) {
2845 memmove(ptr, ptr + len, array_size - (cur + len));
2846 array_size -= len;
2847 btrfs_set_super_sys_array_size(super_copy, array_size);
2848 } else {
2849 ptr += len;
2850 cur += len;
2851 }
2852 }
David Sterba34441362016-10-04 19:34:27 +02002853 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002854 return ret;
2855}
2856
Liu Bo592d92e2017-03-14 13:33:55 -07002857static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info,
2858 u64 logical, u64 length)
2859{
2860 struct extent_map_tree *em_tree;
2861 struct extent_map *em;
2862
2863 em_tree = &fs_info->mapping_tree.map_tree;
2864 read_lock(&em_tree->lock);
2865 em = lookup_extent_mapping(em_tree, logical, length);
2866 read_unlock(&em_tree->lock);
2867
2868 if (!em) {
2869 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2870 logical, length);
2871 return ERR_PTR(-EINVAL);
2872 }
2873
2874 if (em->start > logical || em->start + em->len < logical) {
2875 btrfs_crit(fs_info,
2876 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2877 logical, length, em->start, em->start + em->len);
2878 free_extent_map(em);
2879 return ERR_PTR(-EINVAL);
2880 }
2881
2882 /* callers are responsible for dropping em's ref. */
2883 return em;
2884}
2885
Josef Bacik47ab2a62014-09-18 11:20:02 -04002886int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002887 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04002888{
Josef Bacik47ab2a62014-09-18 11:20:02 -04002889 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002890 struct map_lookup *map;
2891 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002892 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002893 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002894
Liu Bo592d92e2017-03-14 13:33:55 -07002895 em = get_chunk_map(fs_info, chunk_offset, 1);
2896 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04002897 /*
2898 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002899 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002900 * do anything we still error out.
2901 */
2902 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07002903 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002904 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002905 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02002906 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002907 check_system_chunk(trans, fs_info, map->type);
David Sterba34441362016-10-04 19:34:27 +02002908 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002909
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002910 /*
2911 * Take the device list mutex to prevent races with the final phase of
2912 * a device replace operation that replaces the device object associated
2913 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2914 */
2915 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002916 for (i = 0; i < map->num_stripes; i++) {
2917 struct btrfs_device *device = map->stripes[i].dev;
2918 ret = btrfs_free_dev_extent(trans, device,
2919 map->stripes[i].physical,
2920 &dev_extent_len);
2921 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002922 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002923 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002924 goto out;
2925 }
2926
2927 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02002928 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002929 btrfs_device_set_bytes_used(device,
2930 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002931 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002932 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02002933 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002934 }
2935
2936 if (map->stripes[i].dev) {
2937 ret = btrfs_update_device(trans, map->stripes[i].dev);
2938 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002939 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002940 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002941 goto out;
2942 }
2943 }
2944 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002945 mutex_unlock(&fs_devices->device_list_mutex);
2946
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002947 ret = btrfs_free_chunk(trans, fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002948 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002949 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002950 goto out;
2951 }
2952
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002953 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002954
2955 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002956 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002957 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002958 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002959 goto out;
2960 }
2961 }
2962
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002963 ret = btrfs_remove_block_group(trans, fs_info, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002964 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002965 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002966 goto out;
2967 }
2968
Josef Bacik47ab2a62014-09-18 11:20:02 -04002969out:
2970 /* once for us */
2971 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002972 return ret;
2973}
2974
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002975static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002976{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002977 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07002978 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002979 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002980
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002981 /*
2982 * Prevent races with automatic removal of unused block groups.
2983 * After we relocate and before we remove the chunk with offset
2984 * chunk_offset, automatic removal of the block group can kick in,
2985 * resulting in a failure when calling btrfs_remove_chunk() below.
2986 *
2987 * Make sure to acquire this mutex before doing a tree search (dev
2988 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2989 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2990 * we release the path used to search the chunk/dev tree and before
2991 * the current task acquires this mutex and calls us.
2992 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002993 ASSERT(mutex_is_locked(&fs_info->delete_unused_bgs_mutex));
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002994
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002995 ret = btrfs_can_relocate(fs_info, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002996 if (ret)
2997 return -ENOSPC;
2998
Chris Mason8f18cf12008-04-25 16:53:30 -04002999 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003000 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003001 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003002 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003003 if (ret)
3004 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003005
Chris Mason19c4d2f2016-10-10 13:43:31 -07003006 trans = btrfs_start_trans_remove_block_group(root->fs_info,
3007 chunk_offset);
3008 if (IS_ERR(trans)) {
3009 ret = PTR_ERR(trans);
3010 btrfs_handle_fs_error(root->fs_info, ret, NULL);
3011 return ret;
3012 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09003013
Chris Mason19c4d2f2016-10-10 13:43:31 -07003014 /*
3015 * step two, delete the device extents and the
3016 * chunk tree entries
3017 */
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003018 ret = btrfs_remove_chunk(trans, fs_info, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003019 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07003020 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003021}
3022
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003023static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05003024{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003025 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05003026 struct btrfs_path *path;
3027 struct extent_buffer *leaf;
3028 struct btrfs_chunk *chunk;
3029 struct btrfs_key key;
3030 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05003031 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04003032 bool retried = false;
3033 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05003034 int ret;
3035
3036 path = btrfs_alloc_path();
3037 if (!path)
3038 return -ENOMEM;
3039
Josef Bacikba1bf482009-09-11 16:11:19 -04003040again:
Yan Zheng2b820322008-11-17 21:11:30 -05003041 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3042 key.offset = (u64)-1;
3043 key.type = BTRFS_CHUNK_ITEM_KEY;
3044
3045 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003046 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003047 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003048 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003049 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003050 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003051 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003052 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05003053
3054 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3055 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003056 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003057 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003058 if (ret < 0)
3059 goto error;
3060 if (ret > 0)
3061 break;
3062
3063 leaf = path->nodes[0];
3064 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3065
3066 chunk = btrfs_item_ptr(leaf, path->slots[0],
3067 struct btrfs_chunk);
3068 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003069 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003070
3071 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003072 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003073 if (ret == -ENOSPC)
3074 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303075 else
3076 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003077 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003078 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003079
3080 if (found_key.offset == 0)
3081 break;
3082 key.offset = found_key.offset - 1;
3083 }
3084 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003085 if (failed && !retried) {
3086 failed = 0;
3087 retried = true;
3088 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303089 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003090 ret = -ENOSPC;
3091 }
Yan Zheng2b820322008-11-17 21:11:30 -05003092error:
3093 btrfs_free_path(path);
3094 return ret;
3095}
3096
Liu Boa6f93c72017-11-15 16:28:11 -07003097/*
3098 * return 1 : allocate a data chunk successfully,
3099 * return <0: errors during allocating a data chunk,
3100 * return 0 : no need to allocate a data chunk.
3101 */
3102static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3103 u64 chunk_offset)
3104{
3105 struct btrfs_block_group_cache *cache;
3106 u64 bytes_used;
3107 u64 chunk_type;
3108
3109 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3110 ASSERT(cache);
3111 chunk_type = cache->flags;
3112 btrfs_put_block_group(cache);
3113
3114 if (chunk_type & BTRFS_BLOCK_GROUP_DATA) {
3115 spin_lock(&fs_info->data_sinfo->lock);
3116 bytes_used = fs_info->data_sinfo->bytes_used;
3117 spin_unlock(&fs_info->data_sinfo->lock);
3118
3119 if (!bytes_used) {
3120 struct btrfs_trans_handle *trans;
3121 int ret;
3122
3123 trans = btrfs_join_transaction(fs_info->tree_root);
3124 if (IS_ERR(trans))
3125 return PTR_ERR(trans);
3126
3127 ret = btrfs_force_chunk_alloc(trans, fs_info,
3128 BTRFS_BLOCK_GROUP_DATA);
3129 btrfs_end_transaction(trans);
3130 if (ret < 0)
3131 return ret;
3132
3133 return 1;
3134 }
3135 }
3136 return 0;
3137}
3138
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003139static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003140 struct btrfs_balance_control *bctl)
3141{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003142 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003143 struct btrfs_trans_handle *trans;
3144 struct btrfs_balance_item *item;
3145 struct btrfs_disk_balance_args disk_bargs;
3146 struct btrfs_path *path;
3147 struct extent_buffer *leaf;
3148 struct btrfs_key key;
3149 int ret, err;
3150
3151 path = btrfs_alloc_path();
3152 if (!path)
3153 return -ENOMEM;
3154
3155 trans = btrfs_start_transaction(root, 0);
3156 if (IS_ERR(trans)) {
3157 btrfs_free_path(path);
3158 return PTR_ERR(trans);
3159 }
3160
3161 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003162 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003163 key.offset = 0;
3164
3165 ret = btrfs_insert_empty_item(trans, root, path, &key,
3166 sizeof(*item));
3167 if (ret)
3168 goto out;
3169
3170 leaf = path->nodes[0];
3171 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3172
David Sterbab159fa22016-11-08 18:09:03 +01003173 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003174
3175 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3176 btrfs_set_balance_data(leaf, item, &disk_bargs);
3177 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3178 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3179 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3180 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3181
3182 btrfs_set_balance_flags(leaf, item, bctl->flags);
3183
3184 btrfs_mark_buffer_dirty(leaf);
3185out:
3186 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003187 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003188 if (err && !ret)
3189 ret = err;
3190 return ret;
3191}
3192
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003193static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003194{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003195 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003196 struct btrfs_trans_handle *trans;
3197 struct btrfs_path *path;
3198 struct btrfs_key key;
3199 int ret, err;
3200
3201 path = btrfs_alloc_path();
3202 if (!path)
3203 return -ENOMEM;
3204
3205 trans = btrfs_start_transaction(root, 0);
3206 if (IS_ERR(trans)) {
3207 btrfs_free_path(path);
3208 return PTR_ERR(trans);
3209 }
3210
3211 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003212 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003213 key.offset = 0;
3214
3215 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3216 if (ret < 0)
3217 goto out;
3218 if (ret > 0) {
3219 ret = -ENOENT;
3220 goto out;
3221 }
3222
3223 ret = btrfs_del_item(trans, root, path);
3224out:
3225 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003226 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003227 if (err && !ret)
3228 ret = err;
3229 return ret;
3230}
3231
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003232/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003233 * This is a heuristic used to reduce the number of chunks balanced on
3234 * resume after balance was interrupted.
3235 */
3236static void update_balance_args(struct btrfs_balance_control *bctl)
3237{
3238 /*
3239 * Turn on soft mode for chunk types that were being converted.
3240 */
3241 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3242 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3243 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3244 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3245 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3246 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3247
3248 /*
3249 * Turn on usage filter if is not already used. The idea is
3250 * that chunks that we have already balanced should be
3251 * reasonably full. Don't do it for chunks that are being
3252 * converted - that will keep us from relocating unconverted
3253 * (albeit full) chunks.
3254 */
3255 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003256 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003257 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3258 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3259 bctl->data.usage = 90;
3260 }
3261 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003262 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003263 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3264 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3265 bctl->sys.usage = 90;
3266 }
3267 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003268 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003269 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3270 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3271 bctl->meta.usage = 90;
3272 }
3273}
3274
3275/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003276 * Should be called with both balance and volume mutexes held to
3277 * serialize other volume operations (add_dev/rm_dev/resize) with
3278 * restriper. Same goes for unset_balance_control.
3279 */
3280static void set_balance_control(struct btrfs_balance_control *bctl)
3281{
3282 struct btrfs_fs_info *fs_info = bctl->fs_info;
3283
3284 BUG_ON(fs_info->balance_ctl);
3285
3286 spin_lock(&fs_info->balance_lock);
3287 fs_info->balance_ctl = bctl;
3288 spin_unlock(&fs_info->balance_lock);
3289}
3290
3291static void unset_balance_control(struct btrfs_fs_info *fs_info)
3292{
3293 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3294
3295 BUG_ON(!fs_info->balance_ctl);
3296
3297 spin_lock(&fs_info->balance_lock);
3298 fs_info->balance_ctl = NULL;
3299 spin_unlock(&fs_info->balance_lock);
3300
3301 kfree(bctl);
3302}
3303
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003304/*
3305 * Balance filters. Return 1 if chunk should be filtered out
3306 * (should not be balanced).
3307 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003308static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003309 struct btrfs_balance_args *bargs)
3310{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003311 chunk_type = chunk_to_extended(chunk_type) &
3312 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003313
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003314 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003315 return 0;
3316
3317 return 1;
3318}
3319
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003320static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003321 struct btrfs_balance_args *bargs)
3322{
3323 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003324 u64 chunk_used;
3325 u64 user_thresh_min;
3326 u64 user_thresh_max;
3327 int ret = 1;
3328
3329 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3330 chunk_used = btrfs_block_group_used(&cache->item);
3331
3332 if (bargs->usage_min == 0)
3333 user_thresh_min = 0;
3334 else
3335 user_thresh_min = div_factor_fine(cache->key.offset,
3336 bargs->usage_min);
3337
3338 if (bargs->usage_max == 0)
3339 user_thresh_max = 1;
3340 else if (bargs->usage_max > 100)
3341 user_thresh_max = cache->key.offset;
3342 else
3343 user_thresh_max = div_factor_fine(cache->key.offset,
3344 bargs->usage_max);
3345
3346 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3347 ret = 0;
3348
3349 btrfs_put_block_group(cache);
3350 return ret;
3351}
3352
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003353static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003354 u64 chunk_offset, struct btrfs_balance_args *bargs)
3355{
3356 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003357 u64 chunk_used, user_thresh;
3358 int ret = 1;
3359
3360 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3361 chunk_used = btrfs_block_group_used(&cache->item);
3362
David Sterbabc309462015-10-20 18:22:13 +02003363 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003364 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003365 else if (bargs->usage > 100)
3366 user_thresh = cache->key.offset;
3367 else
3368 user_thresh = div_factor_fine(cache->key.offset,
3369 bargs->usage);
3370
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003371 if (chunk_used < user_thresh)
3372 ret = 0;
3373
3374 btrfs_put_block_group(cache);
3375 return ret;
3376}
3377
Ilya Dryomov409d4042012-01-16 22:04:47 +02003378static int chunk_devid_filter(struct extent_buffer *leaf,
3379 struct btrfs_chunk *chunk,
3380 struct btrfs_balance_args *bargs)
3381{
3382 struct btrfs_stripe *stripe;
3383 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3384 int i;
3385
3386 for (i = 0; i < num_stripes; i++) {
3387 stripe = btrfs_stripe_nr(chunk, i);
3388 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3389 return 0;
3390 }
3391
3392 return 1;
3393}
3394
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003395/* [pstart, pend) */
3396static int chunk_drange_filter(struct extent_buffer *leaf,
3397 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003398 struct btrfs_balance_args *bargs)
3399{
3400 struct btrfs_stripe *stripe;
3401 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3402 u64 stripe_offset;
3403 u64 stripe_length;
3404 int factor;
3405 int i;
3406
3407 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3408 return 0;
3409
3410 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003411 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3412 factor = num_stripes / 2;
3413 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3414 factor = num_stripes - 1;
3415 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3416 factor = num_stripes - 2;
3417 } else {
3418 factor = num_stripes;
3419 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003420
3421 for (i = 0; i < num_stripes; i++) {
3422 stripe = btrfs_stripe_nr(chunk, i);
3423 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3424 continue;
3425
3426 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3427 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003428 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003429
3430 if (stripe_offset < bargs->pend &&
3431 stripe_offset + stripe_length > bargs->pstart)
3432 return 0;
3433 }
3434
3435 return 1;
3436}
3437
Ilya Dryomovea671762012-01-16 22:04:48 +02003438/* [vstart, vend) */
3439static int chunk_vrange_filter(struct extent_buffer *leaf,
3440 struct btrfs_chunk *chunk,
3441 u64 chunk_offset,
3442 struct btrfs_balance_args *bargs)
3443{
3444 if (chunk_offset < bargs->vend &&
3445 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3446 /* at least part of the chunk is inside this vrange */
3447 return 0;
3448
3449 return 1;
3450}
3451
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003452static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3453 struct btrfs_chunk *chunk,
3454 struct btrfs_balance_args *bargs)
3455{
3456 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3457
3458 if (bargs->stripes_min <= num_stripes
3459 && num_stripes <= bargs->stripes_max)
3460 return 0;
3461
3462 return 1;
3463}
3464
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003465static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003466 struct btrfs_balance_args *bargs)
3467{
3468 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3469 return 0;
3470
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003471 chunk_type = chunk_to_extended(chunk_type) &
3472 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003473
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003474 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003475 return 1;
3476
3477 return 0;
3478}
3479
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003480static int should_balance_chunk(struct btrfs_fs_info *fs_info,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003481 struct extent_buffer *leaf,
3482 struct btrfs_chunk *chunk, u64 chunk_offset)
3483{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003484 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003485 struct btrfs_balance_args *bargs = NULL;
3486 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3487
3488 /* type filter */
3489 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3490 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3491 return 0;
3492 }
3493
3494 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3495 bargs = &bctl->data;
3496 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3497 bargs = &bctl->sys;
3498 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3499 bargs = &bctl->meta;
3500
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003501 /* profiles filter */
3502 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3503 chunk_profiles_filter(chunk_type, bargs)) {
3504 return 0;
3505 }
3506
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003507 /* usage filter */
3508 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003509 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003510 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003511 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003512 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003513 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003514 }
3515
Ilya Dryomov409d4042012-01-16 22:04:47 +02003516 /* devid filter */
3517 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3518 chunk_devid_filter(leaf, chunk, bargs)) {
3519 return 0;
3520 }
3521
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003522 /* drange filter, makes sense only with devid filter */
3523 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003524 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003525 return 0;
3526 }
3527
Ilya Dryomovea671762012-01-16 22:04:48 +02003528 /* vrange filter */
3529 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3530 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3531 return 0;
3532 }
3533
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003534 /* stripes filter */
3535 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3536 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3537 return 0;
3538 }
3539
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003540 /* soft profile changing mode */
3541 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3542 chunk_soft_convert_filter(chunk_type, bargs)) {
3543 return 0;
3544 }
3545
David Sterba7d824b62014-05-07 17:37:51 +02003546 /*
3547 * limited by count, must be the last filter
3548 */
3549 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3550 if (bargs->limit == 0)
3551 return 0;
3552 else
3553 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003554 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3555 /*
3556 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003557 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003558 * about the count of all chunks that satisfy the filters.
3559 */
3560 if (bargs->limit_max == 0)
3561 return 0;
3562 else
3563 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003564 }
3565
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003566 return 1;
3567}
3568
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003569static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003570{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003571 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003572 struct btrfs_root *chunk_root = fs_info->chunk_root;
3573 struct btrfs_root *dev_root = fs_info->dev_root;
3574 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003575 struct btrfs_device *device;
3576 u64 old_size;
3577 u64 size_to_free;
David Sterba12907fc2015-10-10 17:16:50 +02003578 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003579 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003580 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003581 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003582 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003583 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003584 struct extent_buffer *leaf;
3585 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003586 int ret;
3587 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003588 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003589 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003590 u64 limit_data = bctl->data.limit;
3591 u64 limit_meta = bctl->meta.limit;
3592 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003593 u32 count_data = 0;
3594 u32 count_meta = 0;
3595 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003596 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003597
Chris Masonec44a352008-04-28 15:29:52 -04003598 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003599 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003600 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003601 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003602 size_to_free = div_factor(old_size, 1);
Byongho Leeee221842015-12-15 01:42:10 +09003603 size_to_free = min_t(u64, size_to_free, SZ_1M);
Anand Jainebbede42017-12-04 12:54:52 +08003604 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003605 btrfs_device_get_total_bytes(device) -
3606 btrfs_device_get_bytes_used(device) > size_to_free ||
Anand Jain401e29c2017-12-04 12:54:55 +08003607 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Chris Masonec44a352008-04-28 15:29:52 -04003608 continue;
3609
3610 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003611 if (ret == -ENOSPC)
3612 break;
Liu Bo5a488b92016-07-12 11:24:21 -07003613 if (ret) {
3614 /* btrfs_shrink_device never returns ret > 0 */
3615 WARN_ON(ret > 0);
3616 goto error;
3617 }
Chris Masonec44a352008-04-28 15:29:52 -04003618
Yan, Zhenga22285a2010-05-16 10:48:46 -04003619 trans = btrfs_start_transaction(dev_root, 0);
Liu Bo5a488b92016-07-12 11:24:21 -07003620 if (IS_ERR(trans)) {
3621 ret = PTR_ERR(trans);
3622 btrfs_info_in_rcu(fs_info,
3623 "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu",
3624 rcu_str_deref(device->name), ret,
3625 old_size, old_size - size_to_free);
3626 goto error;
3627 }
Chris Masonec44a352008-04-28 15:29:52 -04003628
3629 ret = btrfs_grow_device(trans, device, old_size);
Liu Bo5a488b92016-07-12 11:24:21 -07003630 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003631 btrfs_end_transaction(trans);
Liu Bo5a488b92016-07-12 11:24:21 -07003632 /* btrfs_grow_device never returns ret > 0 */
3633 WARN_ON(ret > 0);
3634 btrfs_info_in_rcu(fs_info,
3635 "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu",
3636 rcu_str_deref(device->name), ret,
3637 old_size, old_size - size_to_free);
3638 goto error;
3639 }
Chris Masonec44a352008-04-28 15:29:52 -04003640
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003641 btrfs_end_transaction(trans);
Chris Masonec44a352008-04-28 15:29:52 -04003642 }
3643
3644 /* step two, relocate all the chunks */
3645 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003646 if (!path) {
3647 ret = -ENOMEM;
3648 goto error;
3649 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003650
3651 /* zero out stat counters */
3652 spin_lock(&fs_info->balance_lock);
3653 memset(&bctl->stat, 0, sizeof(bctl->stat));
3654 spin_unlock(&fs_info->balance_lock);
3655again:
David Sterba7d824b62014-05-07 17:37:51 +02003656 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003657 /*
3658 * The single value limit and min/max limits use the same bytes
3659 * in the
3660 */
David Sterba7d824b62014-05-07 17:37:51 +02003661 bctl->data.limit = limit_data;
3662 bctl->meta.limit = limit_meta;
3663 bctl->sys.limit = limit_sys;
3664 }
Chris Masonec44a352008-04-28 15:29:52 -04003665 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3666 key.offset = (u64)-1;
3667 key.type = BTRFS_CHUNK_ITEM_KEY;
3668
Chris Masond3977122009-01-05 21:25:51 -05003669 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003670 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003671 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003672 ret = -ECANCELED;
3673 goto error;
3674 }
3675
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003676 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003677 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003678 if (ret < 0) {
3679 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003680 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003681 }
Chris Masonec44a352008-04-28 15:29:52 -04003682
3683 /*
3684 * this shouldn't happen, it means the last relocate
3685 * failed
3686 */
3687 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003688 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003689
3690 ret = btrfs_previous_item(chunk_root, path, 0,
3691 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003692 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003693 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003694 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003695 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003696 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003697
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003698 leaf = path->nodes[0];
3699 slot = path->slots[0];
3700 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3701
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003702 if (found_key.objectid != key.objectid) {
3703 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003704 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003705 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003706
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003707 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003708 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003709
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003710 if (!counting) {
3711 spin_lock(&fs_info->balance_lock);
3712 bctl->stat.considered++;
3713 spin_unlock(&fs_info->balance_lock);
3714 }
3715
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003716 ret = should_balance_chunk(fs_info, leaf, chunk,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003717 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003718
David Sterbab3b4aa72011-04-21 01:20:15 +02003719 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003720 if (!ret) {
3721 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003722 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003723 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003724
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003725 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003726 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003727 spin_lock(&fs_info->balance_lock);
3728 bctl->stat.expected++;
3729 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003730
3731 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3732 count_data++;
3733 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3734 count_sys++;
3735 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3736 count_meta++;
3737
3738 goto loop;
3739 }
3740
3741 /*
3742 * Apply limit_min filter, no need to check if the LIMITS
3743 * filter is used, limit_min is 0 by default
3744 */
3745 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3746 count_data < bctl->data.limit_min)
3747 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3748 count_meta < bctl->meta.limit_min)
3749 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3750 count_sys < bctl->sys.limit_min)) {
3751 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003752 goto loop;
3753 }
3754
Liu Boa6f93c72017-11-15 16:28:11 -07003755 if (!chunk_reserved) {
3756 /*
3757 * We may be relocating the only data chunk we have,
3758 * which could potentially end up with losing data's
3759 * raid profile, so lets allocate an empty one in
3760 * advance.
3761 */
3762 ret = btrfs_may_alloc_data_chunk(fs_info,
3763 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003764 if (ret < 0) {
3765 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3766 goto error;
Liu Boa6f93c72017-11-15 16:28:11 -07003767 } else if (ret == 1) {
3768 chunk_reserved = 1;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003769 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003770 }
3771
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003772 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003773 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik508794e2011-07-02 21:24:41 +00003774 if (ret && ret != -ENOSPC)
3775 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003776 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003777 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003778 } else {
3779 spin_lock(&fs_info->balance_lock);
3780 bctl->stat.completed++;
3781 spin_unlock(&fs_info->balance_lock);
3782 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003783loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003784 if (found_key.offset == 0)
3785 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003786 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003787 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003788
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003789 if (counting) {
3790 btrfs_release_path(path);
3791 counting = false;
3792 goto again;
3793 }
Chris Masonec44a352008-04-28 15:29:52 -04003794error:
3795 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003796 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003797 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003798 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003799 if (!ret)
3800 ret = -ENOSPC;
3801 }
3802
Chris Masonec44a352008-04-28 15:29:52 -04003803 return ret;
3804}
3805
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003806/**
3807 * alloc_profile_is_valid - see if a given profile is valid and reduced
3808 * @flags: profile to validate
3809 * @extended: if true @flags is treated as an extended profile
3810 */
3811static int alloc_profile_is_valid(u64 flags, int extended)
3812{
3813 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3814 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3815
3816 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3817
3818 /* 1) check that all other bits are zeroed */
3819 if (flags & ~mask)
3820 return 0;
3821
3822 /* 2) see if profile is reduced */
3823 if (flags == 0)
3824 return !extended; /* "0" is valid for usual profiles */
3825
3826 /* true if exactly one bit set */
3827 return (flags & (flags - 1)) == 0;
3828}
3829
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003830static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3831{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003832 /* cancel requested || normal exit path */
3833 return atomic_read(&fs_info->balance_cancel_req) ||
3834 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3835 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003836}
3837
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003838static void __cancel_balance(struct btrfs_fs_info *fs_info)
3839{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003840 int ret;
3841
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003842 unset_balance_control(fs_info);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003843 ret = del_balance_item(fs_info);
Liu Bo0f788c52013-03-04 16:25:40 +00003844 if (ret)
Anand Jain34d97002016-03-16 16:43:06 +08003845 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003846
David Sterba171938e2017-03-28 14:44:21 +02003847 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003848}
3849
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003850/* Non-zero return value signifies invalidity */
3851static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3852 u64 allowed)
3853{
3854 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3855 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3856 (bctl_arg->target & ~allowed)));
3857}
3858
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003859/*
3860 * Should be called with both balance and volume mutexes held
3861 */
3862int btrfs_balance(struct btrfs_balance_control *bctl,
3863 struct btrfs_ioctl_balance_args *bargs)
3864{
3865 struct btrfs_fs_info *fs_info = bctl->fs_info;
Adam Borowski14506122017-03-07 23:34:44 +01003866 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003867 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003868 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003869 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003870 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003871 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003872
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003873 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003874 atomic_read(&fs_info->balance_pause_req) ||
3875 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003876 ret = -EINVAL;
3877 goto out;
3878 }
3879
Ilya Dryomove4837f82012-03-27 17:09:17 +03003880 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3881 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3882 mixed = 1;
3883
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003884 /*
3885 * In case of mixed groups both data and meta should be picked,
3886 * and identical options should be given for both of them.
3887 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003888 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3889 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003890 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3891 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3892 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003893 btrfs_err(fs_info,
3894 "with mixed groups data and metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003895 ret = -EINVAL;
3896 goto out;
3897 }
3898 }
3899
Stefan Behrens8dabb742012-11-06 13:15:27 +01003900 num_devices = fs_info->fs_devices->num_devices;
Liu Bo73beece2015-07-17 16:49:19 +08003901 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Stefan Behrens8dabb742012-11-06 13:15:27 +01003902 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3903 BUG_ON(num_devices < 1);
3904 num_devices--;
3905 }
Liu Bo73beece2015-07-17 16:49:19 +08003906 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Austin S. Hemmelgarn88be1592016-03-23 14:22:59 -04003907 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
3908 if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003909 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003910 if (num_devices > 2)
3911 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3912 if (num_devices > 3)
3913 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3914 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003915 if (validate_convert_profile(&bctl->data, allowed)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003916 btrfs_err(fs_info,
3917 "unable to start balance with target data profile %llu",
3918 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003919 ret = -EINVAL;
3920 goto out;
3921 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003922 if (validate_convert_profile(&bctl->meta, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003923 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003924 "unable to start balance with target metadata profile %llu",
3925 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003926 ret = -EINVAL;
3927 goto out;
3928 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003929 if (validate_convert_profile(&bctl->sys, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003930 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003931 "unable to start balance with target system profile %llu",
3932 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003933 ret = -EINVAL;
3934 goto out;
3935 }
3936
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003937 /* allow to reduce meta or sys integrity only if force set */
3938 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003939 BTRFS_BLOCK_GROUP_RAID10 |
3940 BTRFS_BLOCK_GROUP_RAID5 |
3941 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003942 do {
3943 seq = read_seqbegin(&fs_info->profiles_lock);
3944
3945 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3946 (fs_info->avail_system_alloc_bits & allowed) &&
3947 !(bctl->sys.target & allowed)) ||
3948 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3949 (fs_info->avail_metadata_alloc_bits & allowed) &&
3950 !(bctl->meta.target & allowed))) {
3951 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003952 btrfs_info(fs_info,
3953 "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003954 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003955 btrfs_err(fs_info,
3956 "balance will reduce metadata integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003957 ret = -EINVAL;
3958 goto out;
3959 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003960 }
Miao Xiede98ced2013-01-29 10:13:12 +00003961 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003962
Adam Borowski14506122017-03-07 23:34:44 +01003963 /* if we're not converting, the target field is uninitialized */
3964 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3965 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
3966 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3967 bctl->data.target : fs_info->avail_data_alloc_bits;
3968 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
3969 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Sam Tygieree592d02016-01-06 08:46:12 +00003970 btrfs_warn(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003971 "metadata profile 0x%llx has lower redundancy than data profile 0x%llx",
Adam Borowski14506122017-03-07 23:34:44 +01003972 meta_target, data_target);
Sam Tygieree592d02016-01-06 08:46:12 +00003973 }
3974
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003975 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003976 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003977 goto out;
3978
Ilya Dryomov59641012012-01-16 22:04:48 +02003979 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3980 BUG_ON(ret == -EEXIST);
3981 set_balance_control(bctl);
3982 } else {
3983 BUG_ON(ret != -EEXIST);
3984 spin_lock(&fs_info->balance_lock);
3985 update_balance_args(bctl);
3986 spin_unlock(&fs_info->balance_lock);
3987 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003988
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003989 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003990 mutex_unlock(&fs_info->balance_mutex);
3991
3992 ret = __btrfs_balance(fs_info);
3993
3994 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003995 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003996
3997 if (bargs) {
3998 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003999 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004000 }
4001
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004002 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
4003 balance_need_close(fs_info)) {
4004 __cancel_balance(fs_info);
4005 }
4006
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004007 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004008
4009 return ret;
4010out:
Ilya Dryomov59641012012-01-16 22:04:48 +02004011 if (bctl->flags & BTRFS_BALANCE_RESUME)
4012 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004013 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02004014 kfree(bctl);
David Sterba171938e2017-03-28 14:44:21 +02004015 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004016 }
Ilya Dryomov59641012012-01-16 22:04:48 +02004017 return ret;
4018}
4019
4020static int balance_kthread(void *data)
4021{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004022 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02004023 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02004024
4025 mutex_lock(&fs_info->volume_mutex);
4026 mutex_lock(&fs_info->balance_mutex);
4027
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004028 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004029 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004030 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02004031 }
Ilya Dryomov59641012012-01-16 22:04:48 +02004032
4033 mutex_unlock(&fs_info->balance_mutex);
4034 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004035
Ilya Dryomov59641012012-01-16 22:04:48 +02004036 return ret;
4037}
4038
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004039int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
4040{
4041 struct task_struct *tsk;
4042
4043 spin_lock(&fs_info->balance_lock);
4044 if (!fs_info->balance_ctl) {
4045 spin_unlock(&fs_info->balance_lock);
4046 return 0;
4047 }
4048 spin_unlock(&fs_info->balance_lock);
4049
Jeff Mahoney3cdde222016-06-09 21:38:35 -04004050 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004051 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004052 return 0;
4053 }
4054
4055 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05304056 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004057}
4058
Ilya Dryomov68310a52012-06-22 12:24:12 -06004059int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02004060{
Ilya Dryomov59641012012-01-16 22:04:48 +02004061 struct btrfs_balance_control *bctl;
4062 struct btrfs_balance_item *item;
4063 struct btrfs_disk_balance_args disk_bargs;
4064 struct btrfs_path *path;
4065 struct extent_buffer *leaf;
4066 struct btrfs_key key;
4067 int ret;
4068
4069 path = btrfs_alloc_path();
4070 if (!path)
4071 return -ENOMEM;
4072
Ilya Dryomov68310a52012-06-22 12:24:12 -06004073 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01004074 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06004075 key.offset = 0;
4076
4077 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4078 if (ret < 0)
4079 goto out;
4080 if (ret > 0) { /* ret = -ENOENT; */
4081 ret = 0;
4082 goto out;
4083 }
4084
Ilya Dryomov59641012012-01-16 22:04:48 +02004085 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4086 if (!bctl) {
4087 ret = -ENOMEM;
4088 goto out;
4089 }
4090
Ilya Dryomov59641012012-01-16 22:04:48 +02004091 leaf = path->nodes[0];
4092 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4093
Ilya Dryomov68310a52012-06-22 12:24:12 -06004094 bctl->fs_info = fs_info;
4095 bctl->flags = btrfs_balance_flags(leaf, item);
4096 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004097
4098 btrfs_balance_data(leaf, item, &disk_bargs);
4099 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4100 btrfs_balance_meta(leaf, item, &disk_bargs);
4101 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4102 btrfs_balance_sys(leaf, item, &disk_bargs);
4103 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4104
David Sterba171938e2017-03-28 14:44:21 +02004105 WARN_ON(test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004106
Ilya Dryomov68310a52012-06-22 12:24:12 -06004107 mutex_lock(&fs_info->volume_mutex);
4108 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004109
Ilya Dryomov68310a52012-06-22 12:24:12 -06004110 set_balance_control(bctl);
4111
4112 mutex_unlock(&fs_info->balance_mutex);
4113 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004114out:
4115 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004116 return ret;
4117}
4118
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004119int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4120{
4121 int ret = 0;
4122
4123 mutex_lock(&fs_info->balance_mutex);
4124 if (!fs_info->balance_ctl) {
4125 mutex_unlock(&fs_info->balance_mutex);
4126 return -ENOTCONN;
4127 }
4128
4129 if (atomic_read(&fs_info->balance_running)) {
4130 atomic_inc(&fs_info->balance_pause_req);
4131 mutex_unlock(&fs_info->balance_mutex);
4132
4133 wait_event(fs_info->balance_wait_q,
4134 atomic_read(&fs_info->balance_running) == 0);
4135
4136 mutex_lock(&fs_info->balance_mutex);
4137 /* we are good with balance_ctl ripped off from under us */
4138 BUG_ON(atomic_read(&fs_info->balance_running));
4139 atomic_dec(&fs_info->balance_pause_req);
4140 } else {
4141 ret = -ENOTCONN;
4142 }
4143
4144 mutex_unlock(&fs_info->balance_mutex);
4145 return ret;
4146}
4147
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004148int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4149{
David Howellsbc98a422017-07-17 08:45:34 +01004150 if (sb_rdonly(fs_info->sb))
Ilya Dryomove649e582013-10-10 20:40:21 +03004151 return -EROFS;
4152
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004153 mutex_lock(&fs_info->balance_mutex);
4154 if (!fs_info->balance_ctl) {
4155 mutex_unlock(&fs_info->balance_mutex);
4156 return -ENOTCONN;
4157 }
4158
4159 atomic_inc(&fs_info->balance_cancel_req);
4160 /*
4161 * if we are running just wait and return, balance item is
4162 * deleted in btrfs_balance in this case
4163 */
4164 if (atomic_read(&fs_info->balance_running)) {
4165 mutex_unlock(&fs_info->balance_mutex);
4166 wait_event(fs_info->balance_wait_q,
4167 atomic_read(&fs_info->balance_running) == 0);
4168 mutex_lock(&fs_info->balance_mutex);
4169 } else {
4170 /* __cancel_balance needs volume_mutex */
4171 mutex_unlock(&fs_info->balance_mutex);
4172 mutex_lock(&fs_info->volume_mutex);
4173 mutex_lock(&fs_info->balance_mutex);
4174
4175 if (fs_info->balance_ctl)
4176 __cancel_balance(fs_info);
4177
4178 mutex_unlock(&fs_info->volume_mutex);
4179 }
4180
4181 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
4182 atomic_dec(&fs_info->balance_cancel_req);
4183 mutex_unlock(&fs_info->balance_mutex);
4184 return 0;
4185}
4186
Stefan Behrens803b2f52013-08-15 17:11:21 +02004187static int btrfs_uuid_scan_kthread(void *data)
4188{
4189 struct btrfs_fs_info *fs_info = data;
4190 struct btrfs_root *root = fs_info->tree_root;
4191 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004192 struct btrfs_path *path = NULL;
4193 int ret = 0;
4194 struct extent_buffer *eb;
4195 int slot;
4196 struct btrfs_root_item root_item;
4197 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004198 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004199
4200 path = btrfs_alloc_path();
4201 if (!path) {
4202 ret = -ENOMEM;
4203 goto out;
4204 }
4205
4206 key.objectid = 0;
4207 key.type = BTRFS_ROOT_ITEM_KEY;
4208 key.offset = 0;
4209
Stefan Behrens803b2f52013-08-15 17:11:21 +02004210 while (1) {
Anand Jain7c829b72018-03-07 17:29:18 +08004211 ret = btrfs_search_forward(root, &key, path,
4212 BTRFS_OLDEST_GENERATION);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004213 if (ret) {
4214 if (ret > 0)
4215 ret = 0;
4216 break;
4217 }
4218
4219 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4220 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4221 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4222 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4223 goto skip;
4224
4225 eb = path->nodes[0];
4226 slot = path->slots[0];
4227 item_size = btrfs_item_size_nr(eb, slot);
4228 if (item_size < sizeof(root_item))
4229 goto skip;
4230
Stefan Behrens803b2f52013-08-15 17:11:21 +02004231 read_extent_buffer(eb, &root_item,
4232 btrfs_item_ptr_offset(eb, slot),
4233 (int)sizeof(root_item));
4234 if (btrfs_root_refs(&root_item) == 0)
4235 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004236
4237 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4238 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4239 if (trans)
4240 goto update_tree;
4241
4242 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004243 /*
4244 * 1 - subvol uuid item
4245 * 1 - received_subvol uuid item
4246 */
4247 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4248 if (IS_ERR(trans)) {
4249 ret = PTR_ERR(trans);
4250 break;
4251 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004252 continue;
4253 } else {
4254 goto skip;
4255 }
4256update_tree:
4257 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004258 ret = btrfs_uuid_tree_add(trans, fs_info,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004259 root_item.uuid,
4260 BTRFS_UUID_KEY_SUBVOL,
4261 key.objectid);
4262 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004263 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004264 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004265 break;
4266 }
4267 }
4268
4269 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004270 ret = btrfs_uuid_tree_add(trans, fs_info,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004271 root_item.received_uuid,
4272 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4273 key.objectid);
4274 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004275 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004276 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004277 break;
4278 }
4279 }
4280
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004281skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004282 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004283 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004284 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004285 if (ret)
4286 break;
4287 }
4288
Stefan Behrens803b2f52013-08-15 17:11:21 +02004289 btrfs_release_path(path);
4290 if (key.offset < (u64)-1) {
4291 key.offset++;
4292 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4293 key.offset = 0;
4294 key.type = BTRFS_ROOT_ITEM_KEY;
4295 } else if (key.objectid < (u64)-1) {
4296 key.offset = 0;
4297 key.type = BTRFS_ROOT_ITEM_KEY;
4298 key.objectid++;
4299 } else {
4300 break;
4301 }
4302 cond_resched();
4303 }
4304
4305out:
4306 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004307 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004308 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004309 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004310 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004311 else
Josef Bacikafcdd122016-09-02 15:40:02 -04004312 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004313 up(&fs_info->uuid_tree_rescan_sem);
4314 return 0;
4315}
4316
Stefan Behrens70f80172013-08-15 17:11:23 +02004317/*
4318 * Callback for btrfs_uuid_tree_iterate().
4319 * returns:
4320 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004321 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004322 * > 0 if the check failed, which means the caller shall remove the entry.
4323 */
4324static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4325 u8 *uuid, u8 type, u64 subid)
4326{
4327 struct btrfs_key key;
4328 int ret = 0;
4329 struct btrfs_root *subvol_root;
4330
4331 if (type != BTRFS_UUID_KEY_SUBVOL &&
4332 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4333 goto out;
4334
4335 key.objectid = subid;
4336 key.type = BTRFS_ROOT_ITEM_KEY;
4337 key.offset = (u64)-1;
4338 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4339 if (IS_ERR(subvol_root)) {
4340 ret = PTR_ERR(subvol_root);
4341 if (ret == -ENOENT)
4342 ret = 1;
4343 goto out;
4344 }
4345
4346 switch (type) {
4347 case BTRFS_UUID_KEY_SUBVOL:
4348 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4349 ret = 1;
4350 break;
4351 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4352 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4353 BTRFS_UUID_SIZE))
4354 ret = 1;
4355 break;
4356 }
4357
4358out:
4359 return ret;
4360}
4361
4362static int btrfs_uuid_rescan_kthread(void *data)
4363{
4364 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4365 int ret;
4366
4367 /*
4368 * 1st step is to iterate through the existing UUID tree and
4369 * to delete all entries that contain outdated data.
4370 * 2nd step is to add all missing entries to the UUID tree.
4371 */
4372 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4373 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004374 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004375 up(&fs_info->uuid_tree_rescan_sem);
4376 return ret;
4377 }
4378 return btrfs_uuid_scan_kthread(data);
4379}
4380
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004381int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4382{
4383 struct btrfs_trans_handle *trans;
4384 struct btrfs_root *tree_root = fs_info->tree_root;
4385 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004386 struct task_struct *task;
4387 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004388
4389 /*
4390 * 1 - root node
4391 * 1 - root item
4392 */
4393 trans = btrfs_start_transaction(tree_root, 2);
4394 if (IS_ERR(trans))
4395 return PTR_ERR(trans);
4396
4397 uuid_root = btrfs_create_tree(trans, fs_info,
4398 BTRFS_UUID_TREE_OBJECTID);
4399 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004400 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004401 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004402 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004403 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004404 }
4405
4406 fs_info->uuid_root = uuid_root;
4407
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004408 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004409 if (ret)
4410 return ret;
4411
4412 down(&fs_info->uuid_tree_rescan_sem);
4413 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4414 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004415 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004416 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004417 up(&fs_info->uuid_tree_rescan_sem);
4418 return PTR_ERR(task);
4419 }
4420
4421 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004422}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004423
Stefan Behrens70f80172013-08-15 17:11:23 +02004424int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4425{
4426 struct task_struct *task;
4427
4428 down(&fs_info->uuid_tree_rescan_sem);
4429 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4430 if (IS_ERR(task)) {
4431 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004432 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004433 up(&fs_info->uuid_tree_rescan_sem);
4434 return PTR_ERR(task);
4435 }
4436
4437 return 0;
4438}
4439
Chris Mason8f18cf12008-04-25 16:53:30 -04004440/*
4441 * shrinking a device means finding all of the device extents past
4442 * the new size, and then following the back refs to the chunks.
4443 * The chunk relocation code actually frees the device extent
4444 */
4445int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4446{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004447 struct btrfs_fs_info *fs_info = device->fs_info;
4448 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004449 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004450 struct btrfs_dev_extent *dev_extent = NULL;
4451 struct btrfs_path *path;
4452 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004453 u64 chunk_offset;
4454 int ret;
4455 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004456 int failed = 0;
4457 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004458 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004459 struct extent_buffer *l;
4460 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004461 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004462 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004463 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004464 u64 diff;
4465
4466 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004467 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004468
Anand Jain401e29c2017-12-04 12:54:55 +08004469 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Stefan Behrens63a212a2012-11-05 18:29:28 +01004470 return -EINVAL;
4471
Chris Mason8f18cf12008-04-25 16:53:30 -04004472 path = btrfs_alloc_path();
4473 if (!path)
4474 return -ENOMEM;
4475
David Sterbae4058b52015-11-27 16:31:35 +01004476 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04004477
David Sterba34441362016-10-04 19:34:27 +02004478 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004479
Miao Xie7cc8e582014-09-03 21:35:38 +08004480 btrfs_device_set_total_bytes(device, new_size);
Anand Jainebbede42017-12-04 12:54:52 +08004481 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05004482 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004483 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004484 }
David Sterba34441362016-10-04 19:34:27 +02004485 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004486
Josef Bacikba1bf482009-09-11 16:11:19 -04004487again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004488 key.objectid = device->devid;
4489 key.offset = (u64)-1;
4490 key.type = BTRFS_DEV_EXTENT_KEY;
4491
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004492 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004493 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004494 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004495 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004496 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004497 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004498 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004499
4500 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004501 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004502 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004503 if (ret < 0)
4504 goto done;
4505 if (ret) {
4506 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004507 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004508 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004509 }
4510
4511 l = path->nodes[0];
4512 slot = path->slots[0];
4513 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4514
Josef Bacikba1bf482009-09-11 16:11:19 -04004515 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004516 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004517 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004518 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004519 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004520
4521 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4522 length = btrfs_dev_extent_length(l, dev_extent);
4523
Josef Bacikba1bf482009-09-11 16:11:19 -04004524 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004525 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004526 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004527 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004528 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004529
Chris Mason8f18cf12008-04-25 16:53:30 -04004530 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004531 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004532
Liu Boa6f93c72017-11-15 16:28:11 -07004533 /*
4534 * We may be relocating the only data chunk we have,
4535 * which could potentially end up with losing data's
4536 * raid profile, so lets allocate an empty one in
4537 * advance.
4538 */
4539 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4540 if (ret < 0) {
4541 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4542 goto done;
4543 }
4544
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004545 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4546 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04004547 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004548 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004549 if (ret == -ENOSPC)
4550 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004551 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004552
4553 if (failed && !retried) {
4554 failed = 0;
4555 retried = true;
4556 goto again;
4557 } else if (failed && retried) {
4558 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004559 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004560 }
4561
Chris Balld6397ba2009-04-27 07:29:03 -04004562 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004563 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004564 if (IS_ERR(trans)) {
4565 ret = PTR_ERR(trans);
4566 goto done;
4567 }
4568
David Sterba34441362016-10-04 19:34:27 +02004569 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004570
4571 /*
4572 * We checked in the above loop all device extents that were already in
4573 * the device tree. However before we have updated the device's
4574 * total_bytes to the new size, we might have had chunk allocations that
4575 * have not complete yet (new block groups attached to transaction
4576 * handles), and therefore their device extents were not yet in the
4577 * device tree and we missed them in the loop above. So if we have any
4578 * pending chunk using a device extent that overlaps the device range
4579 * that we can not use anymore, commit the current transaction and
4580 * repeat the search on the device tree - this way we guarantee we will
4581 * not have chunks using device extents that end beyond 'new_size'.
4582 */
4583 if (!checked_pending_chunks) {
4584 u64 start = new_size;
4585 u64 len = old_size - new_size;
4586
Jeff Mahoney499f3772015-06-15 09:41:17 -04004587 if (contains_pending_extent(trans->transaction, device,
4588 &start, len)) {
David Sterba34441362016-10-04 19:34:27 +02004589 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004590 checked_pending_chunks = true;
4591 failed = 0;
4592 retried = false;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004593 ret = btrfs_commit_transaction(trans);
Filipe Manana53e489b2015-06-02 14:43:21 +01004594 if (ret)
4595 goto done;
4596 goto again;
4597 }
4598 }
4599
Miao Xie7cc8e582014-09-03 21:35:38 +08004600 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004601 if (list_empty(&device->resized_list))
4602 list_add_tail(&device->resized_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004603 &fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004604
Chris Balld6397ba2009-04-27 07:29:03 -04004605 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004606 btrfs_set_super_total_bytes(super_copy,
4607 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004608 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004609
4610 /* Now btrfs_update_device() will change the on-disk size. */
4611 ret = btrfs_update_device(trans, device);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004612 btrfs_end_transaction(trans);
Chris Mason8f18cf12008-04-25 16:53:30 -04004613done:
4614 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004615 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004616 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004617 btrfs_device_set_total_bytes(device, old_size);
Anand Jainebbede42017-12-04 12:54:52 +08004618 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Filipe Manana53e489b2015-06-02 14:43:21 +01004619 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004620 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004621 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004622 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004623 return ret;
4624}
4625
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004626static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004627 struct btrfs_key *key,
4628 struct btrfs_chunk *chunk, int item_size)
4629{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004630 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004631 struct btrfs_disk_key disk_key;
4632 u32 array_size;
4633 u8 *ptr;
4634
David Sterba34441362016-10-04 19:34:27 +02004635 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004636 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004637 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004638 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004639 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004640 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004641 }
Chris Mason0b86a832008-03-24 15:01:56 -04004642
4643 ptr = super_copy->sys_chunk_array + array_size;
4644 btrfs_cpu_key_to_disk(&disk_key, key);
4645 memcpy(ptr, &disk_key, sizeof(disk_key));
4646 ptr += sizeof(disk_key);
4647 memcpy(ptr, chunk, item_size);
4648 item_size += sizeof(disk_key);
4649 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004650 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004651
Chris Mason0b86a832008-03-24 15:01:56 -04004652 return 0;
4653}
4654
Miao Xieb2117a32011-01-05 10:07:28 +00004655/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004656 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004657 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004658static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004659{
Arne Jansen73c5de02011-04-12 12:07:57 +02004660 const struct btrfs_device_info *di_a = a;
4661 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004662
Arne Jansen73c5de02011-04-12 12:07:57 +02004663 if (di_a->max_avail > di_b->max_avail)
4664 return -1;
4665 if (di_a->max_avail < di_b->max_avail)
4666 return 1;
4667 if (di_a->total_avail > di_b->total_avail)
4668 return -1;
4669 if (di_a->total_avail < di_b->total_avail)
4670 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004671 return 0;
4672}
4673
David Woodhouse53b381b2013-01-29 18:40:14 -05004674static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4675{
Zhao Leiffe2d202015-01-20 15:11:44 +08004676 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004677 return;
4678
Miao Xieceda0862013-04-11 10:30:16 +00004679 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004680}
4681
Qu Wenruo062d4d12018-01-30 18:20:46 +08004682#define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004683 - sizeof(struct btrfs_chunk)) \
4684 / sizeof(struct btrfs_stripe) + 1)
4685
4686#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4687 - 2 * sizeof(struct btrfs_disk_key) \
4688 - 2 * sizeof(struct btrfs_chunk)) \
4689 / sizeof(struct btrfs_stripe) + 1)
4690
Miao Xieb2117a32011-01-05 10:07:28 +00004691static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
David Sterba72b468c2017-02-10 19:46:27 +01004692 u64 start, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004693{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004694 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004695 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004696 struct btrfs_device *device;
Arne Jansen73c5de02011-04-12 12:07:57 +02004697 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004698 struct extent_map_tree *em_tree;
4699 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004700 struct btrfs_device_info *devices_info = NULL;
4701 u64 total_avail;
4702 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004703 int data_stripes; /* number of stripes that count for
4704 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004705 int sub_stripes; /* sub_stripes info for map */
4706 int dev_stripes; /* stripes per dev */
4707 int devs_max; /* max devs to use */
4708 int devs_min; /* min devs needed */
4709 int devs_increment; /* ndevs has to be a multiple of this */
4710 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004711 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004712 u64 max_stripe_size;
4713 u64 max_chunk_size;
4714 u64 stripe_size;
4715 u64 num_bytes;
4716 int ndevs;
4717 int i;
4718 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004719 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004720
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004721 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004722
Qu Wenruo4117f202018-01-22 13:50:54 +08004723 if (list_empty(&fs_devices->alloc_list)) {
4724 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4725 btrfs_debug(info, "%s: no writable device", __func__);
Miao Xieb2117a32011-01-05 10:07:28 +00004726 return -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08004727 }
Miao Xieb2117a32011-01-05 10:07:28 +00004728
Qu Wenruo3e72ee82018-01-30 18:20:45 +08004729 index = btrfs_bg_flags_to_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004730
Liu Bo31e50222012-11-21 14:18:10 +00004731 sub_stripes = btrfs_raid_array[index].sub_stripes;
4732 dev_stripes = btrfs_raid_array[index].dev_stripes;
4733 devs_max = btrfs_raid_array[index].devs_max;
4734 devs_min = btrfs_raid_array[index].devs_min;
4735 devs_increment = btrfs_raid_array[index].devs_increment;
4736 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004737
4738 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09004739 max_stripe_size = SZ_1G;
Arne Jansen73c5de02011-04-12 12:07:57 +02004740 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004741 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08004742 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02004743 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004744 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09004745 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4746 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05004747 else
Byongho Leeee221842015-12-15 01:42:10 +09004748 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004749 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004750 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08004751 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02004752 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09004753 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004754 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004755 if (!devs_max)
4756 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004757 } else {
David Sterba351fd352014-05-15 16:48:20 +02004758 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004759 type);
4760 BUG_ON(1);
4761 }
4762
4763 /* we don't want a chunk larger than 10% of writeable space */
4764 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4765 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004766
David Sterba31e818f2015-02-20 18:00:26 +01004767 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004768 GFP_NOFS);
4769 if (!devices_info)
4770 return -ENOMEM;
4771
Arne Jansen73c5de02011-04-12 12:07:57 +02004772 /*
4773 * in the first pass through the devices list, we gather information
4774 * about the available holes on each device.
4775 */
4776 ndevs = 0;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004777 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004778 u64 max_avail;
4779 u64 dev_offset;
4780
Anand Jainebbede42017-12-04 12:54:52 +08004781 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004782 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004783 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004784 continue;
4785 }
4786
Anand Jaine12c9622017-12-04 12:54:53 +08004787 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
4788 &device->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08004789 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Arne Jansen73c5de02011-04-12 12:07:57 +02004790 continue;
4791
4792 if (device->total_bytes > device->bytes_used)
4793 total_avail = device->total_bytes - device->bytes_used;
4794 else
4795 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004796
4797 /* If there is no space on this device, skip it. */
4798 if (total_avail == 0)
4799 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004800
Josef Bacik6df9a952013-06-27 13:22:46 -04004801 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004802 max_stripe_size * dev_stripes,
4803 &dev_offset, &max_avail);
4804 if (ret && ret != -ENOSPC)
4805 goto error;
4806
4807 if (ret == 0)
4808 max_avail = max_stripe_size * dev_stripes;
4809
Qu Wenruo4117f202018-01-22 13:50:54 +08004810 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) {
4811 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4812 btrfs_debug(info,
4813 "%s: devid %llu has no free space, have=%llu want=%u",
4814 __func__, device->devid, max_avail,
4815 BTRFS_STRIPE_LEN * dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02004816 continue;
Qu Wenruo4117f202018-01-22 13:50:54 +08004817 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004818
Eric Sandeen063d0062013-01-31 00:55:01 +00004819 if (ndevs == fs_devices->rw_devices) {
4820 WARN(1, "%s: found more than %llu devices\n",
4821 __func__, fs_devices->rw_devices);
4822 break;
4823 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004824 devices_info[ndevs].dev_offset = dev_offset;
4825 devices_info[ndevs].max_avail = max_avail;
4826 devices_info[ndevs].total_avail = total_avail;
4827 devices_info[ndevs].dev = device;
4828 ++ndevs;
4829 }
4830
4831 /*
4832 * now sort the devices by hole size / available space
4833 */
4834 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4835 btrfs_cmp_device_info, NULL);
4836
4837 /* round down to number of usable stripes */
Nikolay Borisove5600fd2017-06-27 10:02:25 +03004838 ndevs = round_down(ndevs, devs_increment);
Arne Jansen73c5de02011-04-12 12:07:57 +02004839
Qu Wenruoba89b802018-01-31 13:56:15 +08004840 if (ndevs < devs_min) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004841 ret = -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08004842 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
4843 btrfs_debug(info,
4844 "%s: not enough devices with free space: have=%d minimum required=%d",
Qu Wenruoba89b802018-01-31 13:56:15 +08004845 __func__, ndevs, devs_min);
Qu Wenruo4117f202018-01-22 13:50:54 +08004846 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004847 goto error;
4848 }
4849
Nikolay Borisovf148ef42017-06-27 10:02:26 +03004850 ndevs = min(ndevs, devs_max);
4851
Arne Jansen73c5de02011-04-12 12:07:57 +02004852 /*
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01004853 * The primary goal is to maximize the number of stripes, so use as
4854 * many devices as possible, even if the stripes are not maximum sized.
4855 *
4856 * The DUP profile stores more than one stripe per device, the
4857 * max_avail is the total size so we have to adjust.
Arne Jansen73c5de02011-04-12 12:07:57 +02004858 */
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01004859 stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02004860 num_stripes = ndevs * dev_stripes;
4861
David Woodhouse53b381b2013-01-29 18:40:14 -05004862 /*
4863 * this will have to be fixed for RAID1 and RAID10 over
4864 * more drives
4865 */
4866 data_stripes = num_stripes / ncopies;
4867
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004868 if (type & BTRFS_BLOCK_GROUP_RAID5)
David Woodhouse53b381b2013-01-29 18:40:14 -05004869 data_stripes = num_stripes - 1;
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004870
4871 if (type & BTRFS_BLOCK_GROUP_RAID6)
David Woodhouse53b381b2013-01-29 18:40:14 -05004872 data_stripes = num_stripes - 2;
Chris Mason86db2572013-02-20 16:23:40 -05004873
4874 /*
4875 * Use the number of data stripes to figure out how big this chunk
4876 * is really going to be in terms of logical address space,
4877 * and compare that answer with the max chunk size
4878 */
4879 if (stripe_size * data_stripes > max_chunk_size) {
David Sterbab8b93ad2015-01-16 17:26:13 +01004880 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004881
4882 /* bump the answer up to a 16MB boundary */
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004883 stripe_size = round_up(stripe_size, SZ_16M);
Chris Mason86db2572013-02-20 16:23:40 -05004884
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004885 /*
4886 * But don't go higher than the limits we found while searching
4887 * for free extents
Chris Mason86db2572013-02-20 16:23:40 -05004888 */
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004889 stripe_size = min(devices_info[ndevs - 1].max_avail,
4890 stripe_size);
Chris Mason86db2572013-02-20 16:23:40 -05004891 }
4892
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004893 /* align to BTRFS_STRIPE_LEN */
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004894 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
Arne Jansen73c5de02011-04-12 12:07:57 +02004895
Miao Xieb2117a32011-01-05 10:07:28 +00004896 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4897 if (!map) {
4898 ret = -ENOMEM;
4899 goto error;
4900 }
4901 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004902
Arne Jansen73c5de02011-04-12 12:07:57 +02004903 for (i = 0; i < ndevs; ++i) {
4904 for (j = 0; j < dev_stripes; ++j) {
4905 int s = i * dev_stripes + j;
4906 map->stripes[s].dev = devices_info[i].dev;
4907 map->stripes[s].physical = devices_info[i].dev_offset +
4908 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004909 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004910 }
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004911 map->stripe_len = BTRFS_STRIPE_LEN;
4912 map->io_align = BTRFS_STRIPE_LEN;
4913 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04004914 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004915 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004916
David Woodhouse53b381b2013-01-29 18:40:14 -05004917 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004918
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004919 trace_btrfs_chunk_alloc(info, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004920
David Sterba172ddd62011-04-21 00:48:27 +02004921 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004922 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004923 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004924 ret = -ENOMEM;
4925 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004926 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004927 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04004928 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05004929 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004930 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004931 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004932 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004933 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004934
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004935 em_tree = &info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004936 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004937 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004938 if (ret) {
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03004939 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004940 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004941 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004942 }
Yan Zheng2b820322008-11-17 21:11:30 -05004943
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03004944 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4945 refcount_inc(&em->refs);
4946 write_unlock(&em_tree->lock);
4947
Nikolay Borisov01744842017-07-27 14:22:11 +03004948 ret = btrfs_make_block_group(trans, info, 0, type, start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004949 if (ret)
4950 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004951
Miao Xie7cc8e582014-09-03 21:35:38 +08004952 for (i = 0; i < map->num_stripes; i++) {
4953 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4954 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4955 }
Miao Xie43530c42014-09-03 21:35:36 +08004956
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004957 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
Miao Xie1c116182014-09-03 21:35:37 +08004958
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004959 free_extent_map(em);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004960 check_raid56_incompat_flag(info, type);
David Woodhouse53b381b2013-01-29 18:40:14 -05004961
Miao Xieb2117a32011-01-05 10:07:28 +00004962 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004963 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004964
Josef Bacik6df9a952013-06-27 13:22:46 -04004965error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004966 write_lock(&em_tree->lock);
4967 remove_extent_mapping(em_tree, em);
4968 write_unlock(&em_tree->lock);
4969
4970 /* One for our allocation */
4971 free_extent_map(em);
4972 /* One for the tree reference */
4973 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004974 /* One for the pending_chunks list reference */
4975 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004976error:
Miao Xieb2117a32011-01-05 10:07:28 +00004977 kfree(devices_info);
4978 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004979}
4980
Josef Bacik6df9a952013-06-27 13:22:46 -04004981int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004982 struct btrfs_fs_info *fs_info,
Josef Bacik6df9a952013-06-27 13:22:46 -04004983 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004984{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004985 struct btrfs_root *extent_root = fs_info->extent_root;
4986 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05004987 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05004988 struct btrfs_device *device;
4989 struct btrfs_chunk *chunk;
4990 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004991 struct extent_map *em;
4992 struct map_lookup *map;
4993 size_t item_size;
4994 u64 dev_offset;
4995 u64 stripe_size;
4996 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08004997 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004998
Liu Bo592d92e2017-03-14 13:33:55 -07004999 em = get_chunk_map(fs_info, chunk_offset, chunk_size);
5000 if (IS_ERR(em))
5001 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04005002
Jeff Mahoney95617d62015-06-03 10:55:48 -04005003 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04005004 item_size = btrfs_chunk_item_size(map->num_stripes);
5005 stripe_size = em->orig_block_len;
5006
5007 chunk = kzalloc(item_size, GFP_NOFS);
5008 if (!chunk) {
5009 ret = -ENOMEM;
5010 goto out;
5011 }
5012
Filipe Manana50460e32015-11-20 10:42:47 +00005013 /*
5014 * Take the device list mutex to prevent races with the final phase of
5015 * a device replace operation that replaces the device object associated
5016 * with the map's stripes, because the device object's id can change
5017 * at any time during that final phase of the device replace operation
5018 * (dev-replace.c:btrfs_dev_replace_finishing()).
5019 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005020 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04005021 for (i = 0; i < map->num_stripes; i++) {
5022 device = map->stripes[i].dev;
5023 dev_offset = map->stripes[i].physical;
5024
Yan Zheng2b820322008-11-17 21:11:30 -05005025 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07005026 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005027 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03005028 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
5029 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005030 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005031 break;
5032 }
5033 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005034 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00005035 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05005036 }
5037
Yan Zheng2b820322008-11-17 21:11:30 -05005038 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005039 for (i = 0; i < map->num_stripes; i++) {
5040 device = map->stripes[i].dev;
5041 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05005042
5043 btrfs_set_stack_stripe_devid(stripe, device->devid);
5044 btrfs_set_stack_stripe_offset(stripe, dev_offset);
5045 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
5046 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05005047 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005048 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05005049
5050 btrfs_set_stack_chunk_length(chunk, chunk_size);
5051 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
5052 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
5053 btrfs_set_stack_chunk_type(chunk, map->type);
5054 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
5055 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
5056 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005057 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05005058 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
5059
5060 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5061 key.type = BTRFS_CHUNK_ITEM_KEY;
5062 key.offset = chunk_offset;
5063
5064 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005065 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5066 /*
5067 * TODO: Cleanup of inserted chunk root in case of
5068 * failure.
5069 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005070 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05005071 }
liubo1abe9b82011-03-24 11:18:59 +00005072
Josef Bacik6df9a952013-06-27 13:22:46 -04005073out:
Yan Zheng2b820322008-11-17 21:11:30 -05005074 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04005075 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005076 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005077}
5078
5079/*
5080 * Chunk allocation falls into two parts. The first part does works
5081 * that make the new allocated chunk useable, but not do any operation
5082 * that modifies the chunk tree. The second part does the works that
5083 * require modifying the chunk tree. This division is important for the
5084 * bootstrap process of adding storage to a seed btrfs.
5085 */
5086int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005087 struct btrfs_fs_info *fs_info, u64 type)
Yan Zheng2b820322008-11-17 21:11:30 -05005088{
5089 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005090
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005091 ASSERT(mutex_is_locked(&fs_info->chunk_mutex));
5092 chunk_offset = find_next_chunk(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005093 return __btrfs_alloc_chunk(trans, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05005094}
5095
Chris Masond3977122009-01-05 21:25:51 -05005096static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +01005097 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05005098{
5099 u64 chunk_offset;
5100 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005101 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05005102 int ret;
5103
Josef Bacik6df9a952013-06-27 13:22:46 -04005104 chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005105 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005106 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005107 if (ret)
5108 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005109
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005110 sys_chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005111 alloc_profile = btrfs_system_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005112 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005113 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005114}
5115
Miao Xied20983b2014-07-03 18:22:13 +08005116static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5117{
5118 int max_errors;
5119
5120 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5121 BTRFS_BLOCK_GROUP_RAID10 |
5122 BTRFS_BLOCK_GROUP_RAID5 |
5123 BTRFS_BLOCK_GROUP_DUP)) {
5124 max_errors = 1;
5125 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5126 max_errors = 2;
5127 } else {
5128 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005129 }
5130
Miao Xied20983b2014-07-03 18:22:13 +08005131 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05005132}
5133
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005134int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005135{
5136 struct extent_map *em;
5137 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005138 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005139 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005140 int i;
5141
Liu Bo592d92e2017-03-14 13:33:55 -07005142 em = get_chunk_map(fs_info, chunk_offset, 1);
5143 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005144 return 1;
5145
Jeff Mahoney95617d62015-06-03 10:55:48 -04005146 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005147 for (i = 0; i < map->num_stripes; i++) {
Anand Jaine6e674b2017-12-04 12:54:54 +08005148 if (test_bit(BTRFS_DEV_STATE_MISSING,
5149 &map->stripes[i].dev->dev_state)) {
Miao Xied20983b2014-07-03 18:22:13 +08005150 miss_ndevs++;
5151 continue;
5152 }
Anand Jainebbede42017-12-04 12:54:52 +08005153 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5154 &map->stripes[i].dev->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05005155 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005156 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005157 }
5158 }
Miao Xied20983b2014-07-03 18:22:13 +08005159
5160 /*
5161 * If the number of missing devices is larger than max errors,
5162 * we can not write the data into that chunk successfully, so
5163 * set it readonly.
5164 */
5165 if (miss_ndevs > btrfs_chunk_max_errors(map))
5166 readonly = 1;
5167end:
Yan Zheng2b820322008-11-17 21:11:30 -05005168 free_extent_map(em);
5169 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005170}
5171
5172void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
5173{
David Sterbaa8067e02011-04-21 00:34:43 +02005174 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04005175}
5176
5177void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
5178{
5179 struct extent_map *em;
5180
Chris Masond3977122009-01-05 21:25:51 -05005181 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04005182 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005183 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
5184 if (em)
5185 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005186 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005187 if (!em)
5188 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005189 /* once for us */
5190 free_extent_map(em);
5191 /* once for the tree */
5192 free_extent_map(em);
5193 }
5194}
5195
Stefan Behrens5d964052012-11-05 14:59:07 +01005196int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005197{
5198 struct extent_map *em;
5199 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005200 int ret;
5201
Liu Bo592d92e2017-03-14 13:33:55 -07005202 em = get_chunk_map(fs_info, logical, len);
5203 if (IS_ERR(em))
5204 /*
5205 * We could return errors for these cases, but that could get
5206 * ugly and we'd probably do the same thing which is just not do
5207 * anything else and exit, so return 1 so the callers don't try
5208 * to use other copies.
5209 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005210 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005211
Jeff Mahoney95617d62015-06-03 10:55:48 -04005212 map = em->map_lookup;
Chris Masonf1885912008-04-09 16:28:12 -04005213 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5214 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005215 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5216 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005217 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5218 ret = 2;
5219 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
Liu Bo8810f752018-01-02 13:36:41 -07005220 /*
5221 * There could be two corrupted data stripes, we need
5222 * to loop retry in order to rebuild the correct data.
5223 *
5224 * Fail a stripe at a time on every retry except the
5225 * stripe under reconstruction.
5226 */
5227 ret = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04005228 else
5229 ret = 1;
5230 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005231
Liu Bo73beece2015-07-17 16:49:19 +08005232 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Liu Bo6fad8232017-03-14 13:33:59 -07005233 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5234 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005235 ret++;
Liu Bo73beece2015-07-17 16:49:19 +08005236 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005237
Chris Masonf1885912008-04-09 16:28:12 -04005238 return ret;
5239}
5240
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005241unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005242 u64 logical)
5243{
5244 struct extent_map *em;
5245 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005246 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005247
Liu Bo592d92e2017-03-14 13:33:55 -07005248 em = get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005249
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005250 if (!WARN_ON(IS_ERR(em))) {
5251 map = em->map_lookup;
5252 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5253 len = map->stripe_len * nr_data_stripes(map);
5254 free_extent_map(em);
5255 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005256 return len;
5257}
5258
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005259int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005260{
5261 struct extent_map *em;
5262 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005263 int ret = 0;
5264
Liu Bo592d92e2017-03-14 13:33:55 -07005265 em = get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005266
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005267 if(!WARN_ON(IS_ERR(em))) {
5268 map = em->map_lookup;
5269 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5270 ret = 1;
5271 free_extent_map(em);
5272 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005273 return ret;
5274}
5275
Stefan Behrens30d98612012-11-06 14:52:18 +01005276static int find_live_mirror(struct btrfs_fs_info *fs_info,
Anand Jain99f92a72018-03-14 16:29:12 +08005277 struct map_lookup *map, int first,
Stefan Behrens30d98612012-11-06 14:52:18 +01005278 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005279{
5280 int i;
Anand Jain99f92a72018-03-14 16:29:12 +08005281 int num_stripes;
Stefan Behrens30d98612012-11-06 14:52:18 +01005282 int tolerance;
5283 struct btrfs_device *srcdev;
5284
Anand Jain99f92a72018-03-14 16:29:12 +08005285 ASSERT((map->type &
5286 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)));
5287
5288 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5289 num_stripes = map->sub_stripes;
5290 else
5291 num_stripes = map->num_stripes;
5292
Stefan Behrens30d98612012-11-06 14:52:18 +01005293 if (dev_replace_is_ongoing &&
5294 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5295 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5296 srcdev = fs_info->dev_replace.srcdev;
5297 else
5298 srcdev = NULL;
5299
5300 /*
5301 * try to avoid the drive that is the source drive for a
5302 * dev-replace procedure, only choose it if no other non-missing
5303 * mirror is available
5304 */
5305 for (tolerance = 0; tolerance < 2; tolerance++) {
5306 if (map->stripes[optimal].dev->bdev &&
5307 (tolerance || map->stripes[optimal].dev != srcdev))
5308 return optimal;
Anand Jain99f92a72018-03-14 16:29:12 +08005309 for (i = first; i < first + num_stripes; i++) {
Stefan Behrens30d98612012-11-06 14:52:18 +01005310 if (map->stripes[i].dev->bdev &&
5311 (tolerance || map->stripes[i].dev != srcdev))
5312 return i;
5313 }
Chris Masondfe25022008-05-13 13:46:40 -04005314 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005315
Chris Masondfe25022008-05-13 13:46:40 -04005316 /* we couldn't find one that doesn't fail. Just return something
5317 * and the io error handling code will clean up eventually
5318 */
5319 return optimal;
5320}
5321
David Woodhouse53b381b2013-01-29 18:40:14 -05005322static inline int parity_smaller(u64 a, u64 b)
5323{
5324 return a > b;
5325}
5326
5327/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005328static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005329{
5330 struct btrfs_bio_stripe s;
5331 int i;
5332 u64 l;
5333 int again = 1;
5334
5335 while (again) {
5336 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005337 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005338 if (parity_smaller(bbio->raid_map[i],
5339 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005340 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005341 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005342 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005343 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005344 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005345 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005346
David Woodhouse53b381b2013-01-29 18:40:14 -05005347 again = 1;
5348 }
5349 }
5350 }
5351}
5352
Zhao Lei6e9606d2015-01-20 15:11:34 +08005353static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5354{
5355 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005356 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005357 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005358 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005359 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005360 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005361 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005362 /*
5363 * plus the raid_map, which includes both the tgt dev
5364 * and the stripes
5365 */
5366 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005367 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005368
5369 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005370 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005371
5372 return bbio;
5373}
5374
5375void btrfs_get_bbio(struct btrfs_bio *bbio)
5376{
Elena Reshetova140475a2017-03-03 10:55:10 +02005377 WARN_ON(!refcount_read(&bbio->refs));
5378 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005379}
5380
5381void btrfs_put_bbio(struct btrfs_bio *bbio)
5382{
5383 if (!bbio)
5384 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005385 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005386 kfree(bbio);
5387}
5388
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005389/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5390/*
5391 * Please note that, discard won't be sent to target device of device
5392 * replace.
5393 */
5394static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5395 u64 logical, u64 length,
5396 struct btrfs_bio **bbio_ret)
5397{
5398 struct extent_map *em;
5399 struct map_lookup *map;
5400 struct btrfs_bio *bbio;
5401 u64 offset;
5402 u64 stripe_nr;
5403 u64 stripe_nr_end;
5404 u64 stripe_end_offset;
5405 u64 stripe_cnt;
5406 u64 stripe_len;
5407 u64 stripe_offset;
5408 u64 num_stripes;
5409 u32 stripe_index;
5410 u32 factor = 0;
5411 u32 sub_stripes = 0;
5412 u64 stripes_per_dev = 0;
5413 u32 remaining_stripes = 0;
5414 u32 last_stripe = 0;
5415 int ret = 0;
5416 int i;
5417
5418 /* discard always return a bbio */
5419 ASSERT(bbio_ret);
5420
5421 em = get_chunk_map(fs_info, logical, length);
5422 if (IS_ERR(em))
5423 return PTR_ERR(em);
5424
5425 map = em->map_lookup;
5426 /* we don't discard raid56 yet */
5427 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5428 ret = -EOPNOTSUPP;
5429 goto out;
5430 }
5431
5432 offset = logical - em->start;
5433 length = min_t(u64, em->len - offset, length);
5434
5435 stripe_len = map->stripe_len;
5436 /*
5437 * stripe_nr counts the total number of stripes we have to stride
5438 * to get to this block
5439 */
5440 stripe_nr = div64_u64(offset, stripe_len);
5441
5442 /* stripe_offset is the offset of this block in its stripe */
5443 stripe_offset = offset - stripe_nr * stripe_len;
5444
5445 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005446 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005447 stripe_cnt = stripe_nr_end - stripe_nr;
5448 stripe_end_offset = stripe_nr_end * map->stripe_len -
5449 (offset + length);
5450 /*
5451 * after this, stripe_nr is the number of stripes on this
5452 * device we have to walk to find the data, and stripe_index is
5453 * the number of our device in the stripe array
5454 */
5455 num_stripes = 1;
5456 stripe_index = 0;
5457 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5458 BTRFS_BLOCK_GROUP_RAID10)) {
5459 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5460 sub_stripes = 1;
5461 else
5462 sub_stripes = map->sub_stripes;
5463
5464 factor = map->num_stripes / sub_stripes;
5465 num_stripes = min_t(u64, map->num_stripes,
5466 sub_stripes * stripe_cnt);
5467 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5468 stripe_index *= sub_stripes;
5469 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5470 &remaining_stripes);
5471 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5472 last_stripe *= sub_stripes;
5473 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5474 BTRFS_BLOCK_GROUP_DUP)) {
5475 num_stripes = map->num_stripes;
5476 } else {
5477 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5478 &stripe_index);
5479 }
5480
5481 bbio = alloc_btrfs_bio(num_stripes, 0);
5482 if (!bbio) {
5483 ret = -ENOMEM;
5484 goto out;
5485 }
5486
5487 for (i = 0; i < num_stripes; i++) {
5488 bbio->stripes[i].physical =
5489 map->stripes[stripe_index].physical +
5490 stripe_offset + stripe_nr * map->stripe_len;
5491 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5492
5493 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5494 BTRFS_BLOCK_GROUP_RAID10)) {
5495 bbio->stripes[i].length = stripes_per_dev *
5496 map->stripe_len;
5497
5498 if (i / sub_stripes < remaining_stripes)
5499 bbio->stripes[i].length +=
5500 map->stripe_len;
5501
5502 /*
5503 * Special for the first stripe and
5504 * the last stripe:
5505 *
5506 * |-------|...|-------|
5507 * |----------|
5508 * off end_off
5509 */
5510 if (i < sub_stripes)
5511 bbio->stripes[i].length -=
5512 stripe_offset;
5513
5514 if (stripe_index >= last_stripe &&
5515 stripe_index <= (last_stripe +
5516 sub_stripes - 1))
5517 bbio->stripes[i].length -=
5518 stripe_end_offset;
5519
5520 if (i == sub_stripes - 1)
5521 stripe_offset = 0;
5522 } else {
5523 bbio->stripes[i].length = length;
5524 }
5525
5526 stripe_index++;
5527 if (stripe_index == map->num_stripes) {
5528 stripe_index = 0;
5529 stripe_nr++;
5530 }
5531 }
5532
5533 *bbio_ret = bbio;
5534 bbio->map_type = map->type;
5535 bbio->num_stripes = num_stripes;
5536out:
5537 free_extent_map(em);
5538 return ret;
5539}
5540
Liu Bo5ab56092017-03-14 13:33:57 -07005541/*
5542 * In dev-replace case, for repair case (that's the only case where the mirror
5543 * is selected explicitly when calling btrfs_map_block), blocks left of the
5544 * left cursor can also be read from the target drive.
5545 *
5546 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5547 * array of stripes.
5548 * For READ, it also needs to be supported using the same mirror number.
5549 *
5550 * If the requested block is not left of the left cursor, EIO is returned. This
5551 * can happen because btrfs_num_copies() returns one more in the dev-replace
5552 * case.
5553 */
5554static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5555 u64 logical, u64 length,
5556 u64 srcdev_devid, int *mirror_num,
5557 u64 *physical)
5558{
5559 struct btrfs_bio *bbio = NULL;
5560 int num_stripes;
5561 int index_srcdev = 0;
5562 int found = 0;
5563 u64 physical_of_found = 0;
5564 int i;
5565 int ret = 0;
5566
5567 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5568 logical, &length, &bbio, 0, 0);
5569 if (ret) {
5570 ASSERT(bbio == NULL);
5571 return ret;
5572 }
5573
5574 num_stripes = bbio->num_stripes;
5575 if (*mirror_num > num_stripes) {
5576 /*
5577 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5578 * that means that the requested area is not left of the left
5579 * cursor
5580 */
5581 btrfs_put_bbio(bbio);
5582 return -EIO;
5583 }
5584
5585 /*
5586 * process the rest of the function using the mirror_num of the source
5587 * drive. Therefore look it up first. At the end, patch the device
5588 * pointer to the one of the target drive.
5589 */
5590 for (i = 0; i < num_stripes; i++) {
5591 if (bbio->stripes[i].dev->devid != srcdev_devid)
5592 continue;
5593
5594 /*
5595 * In case of DUP, in order to keep it simple, only add the
5596 * mirror with the lowest physical address
5597 */
5598 if (found &&
5599 physical_of_found <= bbio->stripes[i].physical)
5600 continue;
5601
5602 index_srcdev = i;
5603 found = 1;
5604 physical_of_found = bbio->stripes[i].physical;
5605 }
5606
5607 btrfs_put_bbio(bbio);
5608
5609 ASSERT(found);
5610 if (!found)
5611 return -EIO;
5612
5613 *mirror_num = index_srcdev + 1;
5614 *physical = physical_of_found;
5615 return ret;
5616}
5617
Liu Bo73c0f222017-03-14 13:33:58 -07005618static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5619 struct btrfs_bio **bbio_ret,
5620 struct btrfs_dev_replace *dev_replace,
5621 int *num_stripes_ret, int *max_errors_ret)
5622{
5623 struct btrfs_bio *bbio = *bbio_ret;
5624 u64 srcdev_devid = dev_replace->srcdev->devid;
5625 int tgtdev_indexes = 0;
5626 int num_stripes = *num_stripes_ret;
5627 int max_errors = *max_errors_ret;
5628 int i;
5629
5630 if (op == BTRFS_MAP_WRITE) {
5631 int index_where_to_add;
5632
5633 /*
5634 * duplicate the write operations while the dev replace
5635 * procedure is running. Since the copying of the old disk to
5636 * the new disk takes place at run time while the filesystem is
5637 * mounted writable, the regular write operations to the old
5638 * disk have to be duplicated to go to the new disk as well.
5639 *
5640 * Note that device->missing is handled by the caller, and that
5641 * the write to the old disk is already set up in the stripes
5642 * array.
5643 */
5644 index_where_to_add = num_stripes;
5645 for (i = 0; i < num_stripes; i++) {
5646 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5647 /* write to new disk, too */
5648 struct btrfs_bio_stripe *new =
5649 bbio->stripes + index_where_to_add;
5650 struct btrfs_bio_stripe *old =
5651 bbio->stripes + i;
5652
5653 new->physical = old->physical;
5654 new->length = old->length;
5655 new->dev = dev_replace->tgtdev;
5656 bbio->tgtdev_map[i] = index_where_to_add;
5657 index_where_to_add++;
5658 max_errors++;
5659 tgtdev_indexes++;
5660 }
5661 }
5662 num_stripes = index_where_to_add;
5663 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5664 int index_srcdev = 0;
5665 int found = 0;
5666 u64 physical_of_found = 0;
5667
5668 /*
5669 * During the dev-replace procedure, the target drive can also
5670 * be used to read data in case it is needed to repair a corrupt
5671 * block elsewhere. This is possible if the requested area is
5672 * left of the left cursor. In this area, the target drive is a
5673 * full copy of the source drive.
5674 */
5675 for (i = 0; i < num_stripes; i++) {
5676 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5677 /*
5678 * In case of DUP, in order to keep it simple,
5679 * only add the mirror with the lowest physical
5680 * address
5681 */
5682 if (found &&
5683 physical_of_found <=
5684 bbio->stripes[i].physical)
5685 continue;
5686 index_srcdev = i;
5687 found = 1;
5688 physical_of_found = bbio->stripes[i].physical;
5689 }
5690 }
5691 if (found) {
5692 struct btrfs_bio_stripe *tgtdev_stripe =
5693 bbio->stripes + num_stripes;
5694
5695 tgtdev_stripe->physical = physical_of_found;
5696 tgtdev_stripe->length =
5697 bbio->stripes[index_srcdev].length;
5698 tgtdev_stripe->dev = dev_replace->tgtdev;
5699 bbio->tgtdev_map[index_srcdev] = num_stripes;
5700
5701 tgtdev_indexes++;
5702 num_stripes++;
5703 }
5704 }
5705
5706 *num_stripes_ret = num_stripes;
5707 *max_errors_ret = max_errors;
5708 bbio->num_tgtdevs = tgtdev_indexes;
5709 *bbio_ret = bbio;
5710}
5711
Liu Bo2b19a1f2017-03-14 13:34:00 -07005712static bool need_full_stripe(enum btrfs_map_op op)
5713{
5714 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5715}
5716
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005717static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5718 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005719 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005720 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005721 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005722{
5723 struct extent_map *em;
5724 struct map_lookup *map;
Chris Mason0b86a832008-03-24 15:01:56 -04005725 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005726 u64 stripe_offset;
5727 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005728 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005729 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005730 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005731 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005732 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005733 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005734 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005735 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005736 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5737 int dev_replace_is_ongoing = 0;
5738 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005739 int patch_the_first_stripe_for_dev_replace = 0;
5740 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005741 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005742
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005743 if (op == BTRFS_MAP_DISCARD)
5744 return __btrfs_map_block_for_discard(fs_info, logical,
5745 *length, bbio_ret);
5746
Liu Bo592d92e2017-03-14 13:33:55 -07005747 em = get_chunk_map(fs_info, logical, *length);
5748 if (IS_ERR(em))
5749 return PTR_ERR(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005750
Jeff Mahoney95617d62015-06-03 10:55:48 -04005751 map = em->map_lookup;
Chris Mason0b86a832008-03-24 15:01:56 -04005752 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005753
David Woodhouse53b381b2013-01-29 18:40:14 -05005754 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005755 stripe_nr = offset;
5756 /*
5757 * stripe_nr counts the total number of stripes we have to stride
5758 * to get to this block
5759 */
David Sterba47c57132015-02-20 18:43:47 +01005760 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005761
David Woodhouse53b381b2013-01-29 18:40:14 -05005762 stripe_offset = stripe_nr * stripe_len;
Josef Bacike042d1e2016-04-12 12:54:40 -04005763 if (offset < stripe_offset) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005764 btrfs_crit(fs_info,
5765 "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
Josef Bacike042d1e2016-04-12 12:54:40 -04005766 stripe_offset, offset, em->start, logical,
5767 stripe_len);
5768 free_extent_map(em);
5769 return -EINVAL;
5770 }
Chris Mason593060d2008-03-25 16:50:33 -04005771
5772 /* stripe_offset is the offset of this block in its stripe*/
5773 stripe_offset = offset - stripe_offset;
5774
David Woodhouse53b381b2013-01-29 18:40:14 -05005775 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005776 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005777 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5778 raid56_full_stripe_start = offset;
5779
5780 /* allow a write of a full stripe, but make sure we don't
5781 * allow straddling of stripes
5782 */
David Sterba47c57132015-02-20 18:43:47 +01005783 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5784 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005785 raid56_full_stripe_start *= full_stripe_len;
5786 }
5787
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005788 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005789 u64 max_len;
5790 /* For writes to RAID[56], allow a full stripeset across all disks.
5791 For other RAID types and for RAID[56] reads, just allow a single
5792 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005793 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005794 (op == BTRFS_MAP_WRITE)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005795 max_len = stripe_len * nr_data_stripes(map) -
5796 (offset - raid56_full_stripe_start);
5797 } else {
5798 /* we limit the length of each bio to what fits in a stripe */
5799 max_len = stripe_len - stripe_offset;
5800 }
5801 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005802 } else {
5803 *length = em->len - offset;
5804 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005805
David Woodhouse53b381b2013-01-29 18:40:14 -05005806 /* This is for when we're called from btrfs_merge_bio_hook() and all
5807 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005808 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005809 goto out;
5810
Liu Bo73beece2015-07-17 16:49:19 +08005811 btrfs_dev_replace_lock(dev_replace, 0);
Stefan Behrens472262f2012-11-06 14:43:46 +01005812 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5813 if (!dev_replace_is_ongoing)
Liu Bo73beece2015-07-17 16:49:19 +08005814 btrfs_dev_replace_unlock(dev_replace, 0);
5815 else
5816 btrfs_dev_replace_set_lock_blocking(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005817
Stefan Behrensad6d6202012-11-06 15:06:47 +01005818 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005819 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07005820 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
5821 dev_replace->srcdev->devid,
5822 &mirror_num,
5823 &physical_to_patch_in_first_stripe);
5824 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005825 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07005826 else
5827 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005828 } else if (mirror_num > map->num_stripes) {
5829 mirror_num = 0;
5830 }
5831
Chris Masonf2d8d742008-04-21 10:03:05 -04005832 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005833 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005834 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01005835 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5836 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005837 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08005838 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005839 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Anand Jainde483732017-10-12 16:43:00 +08005840 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005841 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005842 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005843 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005844 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005845 stripe_index = find_live_mirror(fs_info, map, 0,
Stefan Behrens30d98612012-11-06 14:52:18 +01005846 current->pid % map->num_stripes,
5847 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005848 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005849 }
Chris Mason2fff7342008-04-29 14:12:09 -04005850
Chris Mason611f0e02008-04-03 16:29:03 -04005851 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Anand Jainde483732017-10-12 16:43:00 +08005852 if (need_full_stripe(op)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005853 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005854 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005855 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005856 } else {
5857 mirror_num = 1;
5858 }
Chris Mason2fff7342008-04-29 14:12:09 -04005859
Chris Mason321aecc2008-04-16 10:49:51 -04005860 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005861 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005862
David Sterba47c57132015-02-20 18:43:47 +01005863 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005864 stripe_index *= map->sub_stripes;
5865
Anand Jainde483732017-10-12 16:43:00 +08005866 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005867 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005868 else if (mirror_num)
5869 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005870 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005871 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005872 stripe_index = find_live_mirror(fs_info, map,
5873 stripe_index,
Anand Jain99f92a72018-03-14 16:29:12 +08005874 stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005875 current->pid % map->sub_stripes,
5876 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005877 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005878 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005879
Zhao Leiffe2d202015-01-20 15:11:44 +08005880 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08005881 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005882 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07005883 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbab8b93ad2015-01-16 17:26:13 +01005884 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005885
5886 /* RAID[56] write or recovery. Return all stripes */
5887 num_stripes = map->num_stripes;
5888 max_errors = nr_parity_stripes(map);
5889
David Woodhouse53b381b2013-01-29 18:40:14 -05005890 *length = map->stripe_len;
5891 stripe_index = 0;
5892 stripe_offset = 0;
5893 } else {
5894 /*
5895 * Mirror #0 or #1 means the original data block.
5896 * Mirror #2 is RAID5 parity block.
5897 * Mirror #3 is RAID6 Q block.
5898 */
David Sterba47c57132015-02-20 18:43:47 +01005899 stripe_nr = div_u64_rem(stripe_nr,
5900 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005901 if (mirror_num > 1)
5902 stripe_index = nr_data_stripes(map) +
5903 mirror_num - 2;
5904
5905 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005906 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5907 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005908 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08005909 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005910 }
Chris Mason8790d502008-04-03 16:29:03 -04005911 } else {
5912 /*
David Sterba47c57132015-02-20 18:43:47 +01005913 * after this, stripe_nr is the number of stripes on this
5914 * device we have to walk to find the data, and stripe_index is
5915 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005916 */
David Sterba47c57132015-02-20 18:43:47 +01005917 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5918 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005919 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005920 }
Josef Bacike042d1e2016-04-12 12:54:40 -04005921 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005922 btrfs_crit(fs_info,
5923 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04005924 stripe_index, map->num_stripes);
5925 ret = -EINVAL;
5926 goto out;
5927 }
Chris Mason593060d2008-03-25 16:50:33 -04005928
Stefan Behrens472262f2012-11-06 14:43:46 +01005929 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07005930 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005931 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005932 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005933 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005934 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005935 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005936 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005937
Zhao Lei6e9606d2015-01-20 15:11:34 +08005938 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005939 if (!bbio) {
5940 ret = -ENOMEM;
5941 goto out;
5942 }
Liu Bo6fad8232017-03-14 13:33:59 -07005943 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
Miao Xie2c8cdd62014-11-14 16:06:25 +08005944 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005945
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005946 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07005947 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
5948 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005949 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005950 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005951
5952 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5953 sizeof(struct btrfs_bio_stripe) *
5954 num_alloc_stripes +
5955 sizeof(int) * tgtdev_indexes);
5956
5957 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005958 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005959
5960 /* Fill in the logical address of each stripe */
5961 tmp = stripe_nr * nr_data_stripes(map);
5962 for (i = 0; i < nr_data_stripes(map); i++)
5963 bbio->raid_map[(i+rot) % num_stripes] =
5964 em->start + (tmp + i) * map->stripe_len;
5965
5966 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5967 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5968 bbio->raid_map[(i+rot+1) % num_stripes] =
5969 RAID6_Q_STRIPE;
5970 }
5971
Li Zefanec9ef7a2011-12-01 14:06:42 +08005972
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005973 for (i = 0; i < num_stripes; i++) {
5974 bbio->stripes[i].physical =
5975 map->stripes[stripe_index].physical +
5976 stripe_offset +
5977 stripe_nr * map->stripe_len;
5978 bbio->stripes[i].dev =
5979 map->stripes[stripe_index].dev;
5980 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04005981 }
Li Zefande11cc12011-12-01 12:55:47 +08005982
Liu Bo2b19a1f2017-03-14 13:34:00 -07005983 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08005984 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005985
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005986 if (bbio->raid_map)
5987 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005988
Liu Bo73c0f222017-03-14 13:33:58 -07005989 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005990 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07005991 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
5992 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01005993 }
5994
Li Zefande11cc12011-12-01 12:55:47 +08005995 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005996 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005997 bbio->num_stripes = num_stripes;
5998 bbio->max_errors = max_errors;
5999 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006000
6001 /*
6002 * this is the case that REQ_READ && dev_replace_is_ongoing &&
6003 * mirror_num == num_stripes + 1 && dev_replace target drive is
6004 * available as a mirror
6005 */
6006 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
6007 WARN_ON(num_stripes > 1);
6008 bbio->stripes[0].dev = dev_replace->tgtdev;
6009 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
6010 bbio->mirror_num = map->num_stripes + 1;
6011 }
Chris Masoncea9e442008-04-09 16:28:12 -04006012out:
Liu Bo73beece2015-07-17 16:49:19 +08006013 if (dev_replace_is_ongoing) {
6014 btrfs_dev_replace_clear_lock_blocking(dev_replace);
6015 btrfs_dev_replace_unlock(dev_replace, 0);
6016 }
Chris Mason0b86a832008-03-24 15:01:56 -04006017 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08006018 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006019}
6020
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006021int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04006022 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02006023 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04006024{
Mike Christieb3d3fa52016-06-05 14:31:53 -05006025 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006026 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04006027}
6028
Miao Xieaf8e2d12014-10-23 14:42:50 +08006029/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006030int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08006031 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02006032 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08006033{
David Sterba825ad4c2017-03-28 14:45:22 +02006034 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08006035}
6036
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006037int btrfs_rmap_block(struct btrfs_fs_info *fs_info,
Yan Zhenga512bbf2008-12-08 16:46:26 -05006038 u64 chunk_start, u64 physical, u64 devid,
6039 u64 **logical, int *naddrs, int *stripe_len)
6040{
Yan Zhenga512bbf2008-12-08 16:46:26 -05006041 struct extent_map *em;
6042 struct map_lookup *map;
6043 u64 *buf;
6044 u64 bytenr;
6045 u64 length;
6046 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006047 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006048 int i, j, nr = 0;
6049
Liu Bo592d92e2017-03-14 13:33:55 -07006050 em = get_chunk_map(fs_info, chunk_start, 1);
6051 if (IS_ERR(em))
Josef Bacik835d9742013-03-19 12:13:25 -04006052 return -EIO;
Josef Bacik835d9742013-03-19 12:13:25 -04006053
Jeff Mahoney95617d62015-06-03 10:55:48 -04006054 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006055 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05006056 rmap_len = map->stripe_len;
6057
Yan Zhenga512bbf2008-12-08 16:46:26 -05006058 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01006059 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006060 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01006061 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08006062 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01006063 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05006064 rmap_len = map->stripe_len * nr_data_stripes(map);
6065 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006066
David Sterba31e818f2015-02-20 18:00:26 +01006067 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006068 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05006069
6070 for (i = 0; i < map->num_stripes; i++) {
6071 if (devid && map->stripes[i].dev->devid != devid)
6072 continue;
6073 if (map->stripes[i].physical > physical ||
6074 map->stripes[i].physical + length <= physical)
6075 continue;
6076
6077 stripe_nr = physical - map->stripes[i].physical;
Liu Bo42c61ab2017-04-03 13:45:24 -07006078 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006079
6080 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
6081 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01006082 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006083 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
6084 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05006085 } /* else if RAID[56], multiply by nr_data_stripes().
6086 * Alternatively, just use rmap_len below instead of
6087 * map->stripe_len */
6088
6089 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05006090 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006091 for (j = 0; j < nr; j++) {
6092 if (buf[j] == bytenr)
6093 break;
6094 }
Chris Mason934d3752008-12-08 16:43:10 -05006095 if (j == nr) {
6096 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006097 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05006098 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006099 }
6100
Yan Zhenga512bbf2008-12-08 16:46:26 -05006101 *logical = buf;
6102 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006103 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006104
6105 free_extent_map(em);
6106 return 0;
6107}
6108
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006109static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08006110{
Mike Snitzer326e1db2015-05-22 09:14:03 -04006111 bio->bi_private = bbio->private;
6112 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006113 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04006114
Zhao Lei6e9606d2015-01-20 15:11:34 +08006115 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08006116}
6117
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006118static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04006119{
Chris Mason9be33952013-05-17 18:30:14 -04006120 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006121 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04006122
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006123 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02006124 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006125 if (bio->bi_status == BLK_STS_IOERR ||
6126 bio->bi_status == BLK_STS_TARGET) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02006127 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04006128 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08006129 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006130
6131 BUG_ON(stripe_index >= bbio->num_stripes);
6132 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02006133 if (dev->bdev) {
Mike Christie37226b22016-06-05 14:31:52 -05006134 if (bio_op(bio) == REQ_OP_WRITE)
Anand Jain1cb34c82017-10-21 01:45:33 +08006135 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006136 BTRFS_DEV_STAT_WRITE_ERRS);
6137 else
Anand Jain1cb34c82017-10-21 01:45:33 +08006138 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006139 BTRFS_DEV_STAT_READ_ERRS);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06006140 if (bio->bi_opf & REQ_PREFLUSH)
Anand Jain1cb34c82017-10-21 01:45:33 +08006141 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006142 BTRFS_DEV_STAT_FLUSH_ERRS);
Stefan Behrens597a60f2012-06-14 16:42:31 +02006143 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02006144 }
6145 }
Chris Mason8790d502008-04-03 16:29:03 -04006146
Jan Schmidta1d3c472011-08-04 17:15:33 +02006147 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006148 is_orig_bio = 1;
6149
Miao Xiec404e0d2014-01-30 16:46:55 +08006150 btrfs_bio_counter_dec(bbio->fs_info);
6151
Jan Schmidta1d3c472011-08-04 17:15:33 +02006152 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006153 if (!is_orig_bio) {
6154 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006155 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006156 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006157
Chris Mason9be33952013-05-17 18:30:14 -04006158 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006159 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006160 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006161 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006162 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006163 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006164 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006165 /*
6166 * this bio is actually up to date, we didn't
6167 * go over the max number of errors
6168 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006169 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006170 }
Miao Xiec55f1392014-06-19 10:42:54 +08006171
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006172 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006173 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006174 bio_put(bio);
6175 }
Chris Mason8790d502008-04-03 16:29:03 -04006176}
6177
Chris Mason8b712842008-06-11 16:50:36 -04006178/*
6179 * see run_scheduled_bios for a description of why bios are collected for
6180 * async submit.
6181 *
6182 * This will add one bio to the pending list for a device and make sure
6183 * the work struct is scheduled.
6184 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006185static noinline void btrfs_schedule_bio(struct btrfs_device *device,
Mike Christie4e49ea42016-06-05 14:31:41 -05006186 struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04006187{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006188 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -04006189 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04006190 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006191
Anand Jaine6e674b2017-12-04 12:54:54 +08006192 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state) ||
6193 !device->bdev) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006194 bio_io_error(bio);
David Woodhouse53b381b2013-01-29 18:40:14 -05006195 return;
6196 }
6197
Chris Mason8b712842008-06-11 16:50:36 -04006198 /* don't bother with additional async steps for reads, right now */
Mike Christie37226b22016-06-05 14:31:52 -05006199 if (bio_op(bio) == REQ_OP_READ) {
Mike Christie4e49ea42016-06-05 14:31:41 -05006200 btrfsic_submit_bio(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01006201 return;
Chris Mason8b712842008-06-11 16:50:36 -04006202 }
6203
Chris Mason492bb6de2008-07-31 16:29:02 -04006204 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04006205 bio->bi_next = NULL;
Chris Mason8b712842008-06-11 16:50:36 -04006206
6207 spin_lock(&device->io_lock);
Christoph Hellwig67f055c2016-11-01 07:40:06 -06006208 if (op_is_sync(bio->bi_opf))
Chris Masonffbd5172009-04-20 15:50:09 -04006209 pending_bios = &device->pending_sync_bios;
6210 else
6211 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006212
Chris Masonffbd5172009-04-20 15:50:09 -04006213 if (pending_bios->tail)
6214 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006215
Chris Masonffbd5172009-04-20 15:50:09 -04006216 pending_bios->tail = bio;
6217 if (!pending_bios->head)
6218 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006219 if (device->running_pending)
6220 should_queue = 0;
6221
6222 spin_unlock(&device->io_lock);
6223
6224 if (should_queue)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006225 btrfs_queue_work(fs_info->submit_workers, &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04006226}
6227
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006228static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6229 u64 physical, int dev_nr, int async)
Josef Bacikde1ee922012-10-19 16:50:56 -04006230{
6231 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006232 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006233
6234 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006235 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006236 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006237 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04006238#ifdef DEBUG
6239 {
6240 struct rcu_string *name;
6241
6242 rcu_read_lock();
6243 name = rcu_dereference(dev->name);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006244 btrfs_debug(fs_info,
6245 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6246 bio_op(bio), bio->bi_opf,
6247 (u64)bio->bi_iter.bi_sector,
6248 (u_long)dev->bdev->bd_dev, name->str, dev->devid,
6249 bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04006250 rcu_read_unlock();
6251 }
6252#endif
Christoph Hellwig74d46992017-08-23 19:10:32 +02006253 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006254
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006255 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006256
Josef Bacikde1ee922012-10-19 16:50:56 -04006257 if (async)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006258 btrfs_schedule_bio(dev, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006259 else
Mike Christie4e49ea42016-06-05 14:31:41 -05006260 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006261}
6262
Josef Bacikde1ee922012-10-19 16:50:56 -04006263static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6264{
6265 atomic_inc(&bbio->error);
6266 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006267 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006268 WARN_ON(bio != bbio->orig_bio);
6269
Chris Mason9be33952013-05-17 18:30:14 -04006270 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006271 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006272 if (atomic_read(&bbio->error) > bbio->max_errors)
6273 bio->bi_status = BLK_STS_IOERR;
6274 else
6275 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006276 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006277 }
6278}
6279
Omar Sandoval58efbc92017-08-22 23:45:59 -07006280blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6281 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006282{
Chris Mason0b86a832008-03-24 15:01:56 -04006283 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006284 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006285 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006286 u64 length = 0;
6287 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006288 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006289 int dev_nr;
6290 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006291 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006292
Kent Overstreet4f024f32013-10-11 15:44:27 -07006293 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006294 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006295
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006296 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006297 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006298 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006299 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006300 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006301 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006302 }
Chris Masoncea9e442008-04-09 16:28:12 -04006303
Jan Schmidta1d3c472011-08-04 17:15:33 +02006304 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006305 bbio->orig_bio = first_bio;
6306 bbio->private = first_bio->bi_private;
6307 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006308 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006309 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6310
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006311 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006312 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006313 /* In this case, map_length has been set to the length of
6314 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006315 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006316 ret = raid56_parity_write(fs_info, bio, bbio,
6317 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006318 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006319 ret = raid56_parity_recover(fs_info, bio, bbio,
6320 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006321 }
Miao Xie42452152014-11-25 16:39:28 +08006322
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006323 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006324 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006325 }
6326
Chris Masoncea9e442008-04-09 16:28:12 -04006327 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006328 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006329 "mapping failed logical %llu bio len %llu len %llu",
6330 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006331 BUG();
6332 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006333
Zhao Lei08da7572015-02-12 15:42:16 +08006334 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006335 dev = bbio->stripes[dev_nr].dev;
Mike Christie37226b22016-06-05 14:31:52 -05006336 if (!dev || !dev->bdev ||
Anand Jainebbede42017-12-04 12:54:52 +08006337 (bio_op(first_bio) == REQ_OP_WRITE &&
6338 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006339 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006340 continue;
6341 }
6342
David Sterba3aa8e072017-06-02 17:38:30 +02006343 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006344 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006345 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006346 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006347
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006348 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6349 dev_nr, async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006350 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006351 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006352 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006353}
6354
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006355struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006356 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006357{
Yan Zheng2b820322008-11-17 21:11:30 -05006358 struct btrfs_device *device;
6359 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006360
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006361 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006362 while (cur_devices) {
6363 if (!fsid ||
Anand Jain44880fd2017-07-29 17:50:09 +08006364 !memcmp(cur_devices->fsid, fsid, BTRFS_FSID_SIZE)) {
David Sterba35c70102017-06-15 19:51:51 +02006365 device = find_device(cur_devices, devid, uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006366 if (device)
6367 return device;
6368 }
6369 cur_devices = cur_devices->seed;
6370 }
6371 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006372}
6373
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006374static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006375 u64 devid, u8 *dev_uuid)
6376{
6377 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006378
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006379 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6380 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006381 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006382
6383 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006384 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006385 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006386
Anand Jaine6e674b2017-12-04 12:54:54 +08006387 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05006388 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006389
Chris Masondfe25022008-05-13 13:46:40 -04006390 return device;
6391}
6392
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006393/**
6394 * btrfs_alloc_device - allocate struct btrfs_device
6395 * @fs_info: used only for generating a new devid, can be NULL if
6396 * devid is provided (i.e. @devid != NULL).
6397 * @devid: a pointer to devid for this device. If NULL a new devid
6398 * is generated.
6399 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6400 * is generated.
6401 *
6402 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006403 * on error. Returned struct is not linked onto any lists and must be
6404 * destroyed with free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006405 */
6406struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6407 const u64 *devid,
6408 const u8 *uuid)
6409{
6410 struct btrfs_device *dev;
6411 u64 tmp;
6412
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306413 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006414 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006415
6416 dev = __alloc_device();
6417 if (IS_ERR(dev))
6418 return dev;
6419
6420 if (devid)
6421 tmp = *devid;
6422 else {
6423 int ret;
6424
6425 ret = find_next_devid(fs_info, &tmp);
6426 if (ret) {
David Sterba55de4802017-10-30 18:55:47 +01006427 free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006428 return ERR_PTR(ret);
6429 }
6430 }
6431 dev->devid = tmp;
6432
6433 if (uuid)
6434 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6435 else
6436 generate_random_uuid(dev->uuid);
6437
Liu Bo9e0af232014-08-15 23:36:53 +08006438 btrfs_init_work(&dev->work, btrfs_submit_helper,
6439 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006440
6441 return dev;
6442}
6443
Liu Boe06cd3d2016-06-03 12:05:15 -07006444/* Return -EIO if any error, otherwise return 0. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006445static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006446 struct extent_buffer *leaf,
6447 struct btrfs_chunk *chunk, u64 logical)
6448{
6449 u64 length;
6450 u64 stripe_len;
6451 u16 num_stripes;
6452 u16 sub_stripes;
6453 u64 type;
6454
6455 length = btrfs_chunk_length(leaf, chunk);
6456 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6457 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6458 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6459 type = btrfs_chunk_type(leaf, chunk);
6460
6461 if (!num_stripes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006462 btrfs_err(fs_info, "invalid chunk num_stripes: %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006463 num_stripes);
6464 return -EIO;
6465 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006466 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
6467 btrfs_err(fs_info, "invalid chunk logical %llu", logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006468 return -EIO;
6469 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006470 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
6471 btrfs_err(fs_info, "invalid chunk sectorsize %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006472 btrfs_chunk_sector_size(leaf, chunk));
6473 return -EIO;
6474 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006475 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
6476 btrfs_err(fs_info, "invalid chunk length %llu", length);
Liu Boe06cd3d2016-06-03 12:05:15 -07006477 return -EIO;
6478 }
6479 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006480 btrfs_err(fs_info, "invalid chunk stripe length: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006481 stripe_len);
6482 return -EIO;
6483 }
6484 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6485 type) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006486 btrfs_err(fs_info, "unrecognized chunk type: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006487 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6488 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6489 btrfs_chunk_type(leaf, chunk));
6490 return -EIO;
6491 }
6492 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
6493 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
6494 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
6495 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
6496 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
6497 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
6498 num_stripes != 1)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006499 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006500 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
6501 num_stripes, sub_stripes,
6502 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
6503 return -EIO;
6504 }
6505
6506 return 0;
6507}
6508
Anand Jain5a2b8e62017-10-09 11:07:45 +08006509static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006510 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006511{
Anand Jain2b902df2017-10-09 11:07:46 +08006512 if (error)
6513 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6514 devid, uuid);
6515 else
6516 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6517 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006518}
6519
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006520static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
Chris Mason0b86a832008-03-24 15:01:56 -04006521 struct extent_buffer *leaf,
6522 struct btrfs_chunk *chunk)
6523{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006524 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006525 struct map_lookup *map;
6526 struct extent_map *em;
6527 u64 logical;
6528 u64 length;
6529 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006530 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006531 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006532 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006533 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006534
Chris Masone17cade2008-04-15 15:41:47 -04006535 logical = key->offset;
6536 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006537 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006538
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006539 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006540 if (ret)
6541 return ret;
Chris Masona061fc82008-05-07 11:43:44 -04006542
Chris Mason890871b2009-09-02 16:24:52 -04006543 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006544 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006545 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006546
6547 /* already mapped? */
6548 if (em && em->start <= logical && em->start + em->len > logical) {
6549 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006550 return 0;
6551 } else if (em) {
6552 free_extent_map(em);
6553 }
Chris Mason0b86a832008-03-24 15:01:56 -04006554
David Sterba172ddd62011-04-21 00:48:27 +02006555 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006556 if (!em)
6557 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006558 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006559 if (!map) {
6560 free_extent_map(em);
6561 return -ENOMEM;
6562 }
6563
Wang Shilong298a8f92014-06-19 10:42:52 +08006564 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006565 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006566 em->start = logical;
6567 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006568 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006569 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006570 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006571
Chris Mason593060d2008-03-25 16:50:33 -04006572 map->num_stripes = num_stripes;
6573 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6574 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006575 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6576 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006577 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006578 for (i = 0; i < num_stripes; i++) {
6579 map->stripes[i].physical =
6580 btrfs_stripe_offset_nr(leaf, chunk, i);
6581 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006582 read_extent_buffer(leaf, uuid, (unsigned long)
6583 btrfs_stripe_dev_uuid_nr(chunk, i),
6584 BTRFS_UUID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006585 map->stripes[i].dev = btrfs_find_device(fs_info, devid,
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006586 uuid, NULL);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006587 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006588 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006589 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006590 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006591 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006592 }
Chris Masondfe25022008-05-13 13:46:40 -04006593 if (!map->stripes[i].dev) {
6594 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006595 add_missing_dev(fs_info->fs_devices, devid,
6596 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006597 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006598 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006599 btrfs_err(fs_info,
6600 "failed to init missing dev %llu: %ld",
6601 devid, PTR_ERR(map->stripes[i].dev));
6602 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006603 }
Anand Jain2b902df2017-10-09 11:07:46 +08006604 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006605 }
Anand Jaine12c9622017-12-04 12:54:53 +08006606 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6607 &(map->stripes[i].dev->dev_state));
6608
Chris Mason0b86a832008-03-24 15:01:56 -04006609 }
6610
Chris Mason890871b2009-09-02 16:24:52 -04006611 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006612 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006613 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006614 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006615 free_extent_map(em);
6616
6617 return 0;
6618}
6619
Jeff Mahoney143bede2012-03-01 14:56:26 +01006620static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006621 struct btrfs_dev_item *dev_item,
6622 struct btrfs_device *device)
6623{
6624 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006625
6626 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006627 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6628 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006629 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006630 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006631 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006632 device->type = btrfs_device_type(leaf, dev_item);
6633 device->io_align = btrfs_device_io_align(leaf, dev_item);
6634 device->io_width = btrfs_device_io_width(leaf, dev_item);
6635 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006636 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Anand Jain401e29c2017-12-04 12:54:55 +08006637 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Chris Mason0b86a832008-03-24 15:01:56 -04006638
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006639 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006640 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006641}
6642
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006643static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006644 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006645{
6646 struct btrfs_fs_devices *fs_devices;
6647 int ret;
6648
Li Zefanb367e472011-12-07 11:38:24 +08006649 BUG_ON(!mutex_is_locked(&uuid_mutex));
David Sterba2dfeca92017-06-14 02:48:07 +02006650 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006651
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006652 fs_devices = fs_info->fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006653 while (fs_devices) {
Anand Jain44880fd2017-07-29 17:50:09 +08006654 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006655 return fs_devices;
6656
Yan Zheng2b820322008-11-17 21:11:30 -05006657 fs_devices = fs_devices->seed;
6658 }
6659
6660 fs_devices = find_fsid(fsid);
6661 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006662 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006663 return ERR_PTR(-ENOENT);
6664
6665 fs_devices = alloc_fs_devices(fsid);
6666 if (IS_ERR(fs_devices))
6667 return fs_devices;
6668
6669 fs_devices->seeding = 1;
6670 fs_devices->opened = 1;
6671 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006672 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006673
6674 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006675 if (IS_ERR(fs_devices))
6676 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006677
Christoph Hellwig97288f22008-12-02 06:36:09 -05006678 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006679 fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006680 if (ret) {
6681 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006682 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006683 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006684 }
Yan Zheng2b820322008-11-17 21:11:30 -05006685
6686 if (!fs_devices->seeding) {
6687 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006688 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006689 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006690 goto out;
6691 }
6692
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006693 fs_devices->seed = fs_info->fs_devices->seed;
6694 fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006695out:
Miao Xie5f375832014-09-03 21:35:46 +08006696 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006697}
6698
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006699static int read_one_dev(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04006700 struct extent_buffer *leaf,
6701 struct btrfs_dev_item *dev_item)
6702{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006703 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006704 struct btrfs_device *device;
6705 u64 devid;
6706 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08006707 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006708 u8 dev_uuid[BTRFS_UUID_SIZE];
6709
Chris Mason0b86a832008-03-24 15:01:56 -04006710 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006711 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006712 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006713 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08006714 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05006715
Anand Jain44880fd2017-07-29 17:50:09 +08006716 if (memcmp(fs_uuid, fs_info->fsid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006717 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006718 if (IS_ERR(fs_devices))
6719 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006720 }
6721
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006722 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006723 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08006724 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08006725 btrfs_report_missing_device(fs_info, devid,
6726 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006727 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08006728 }
Yan Zheng2b820322008-11-17 21:11:30 -05006729
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006730 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006731 if (IS_ERR(device)) {
6732 btrfs_err(fs_info,
6733 "failed to add missing dev %llu: %ld",
6734 devid, PTR_ERR(device));
6735 return PTR_ERR(device);
6736 }
Anand Jain2b902df2017-10-09 11:07:46 +08006737 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08006738 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08006739 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08006740 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6741 btrfs_report_missing_device(fs_info,
6742 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006743 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08006744 }
6745 btrfs_report_missing_device(fs_info, devid,
6746 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08006747 }
Miao Xie5f375832014-09-03 21:35:46 +08006748
Anand Jaine6e674b2017-12-04 12:54:54 +08006749 if (!device->bdev &&
6750 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006751 /*
6752 * this happens when a device that was properly setup
6753 * in the device info lists suddenly goes bad.
6754 * device->bdev is NULL, and so we have to set
6755 * device->missing to one here
6756 */
Miao Xie5f375832014-09-03 21:35:46 +08006757 device->fs_devices->missing_devices++;
Anand Jaine6e674b2017-12-04 12:54:54 +08006758 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05006759 }
Miao Xie5f375832014-09-03 21:35:46 +08006760
6761 /* Move the device to its own fs_devices */
6762 if (device->fs_devices != fs_devices) {
Anand Jaine6e674b2017-12-04 12:54:54 +08006763 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
6764 &device->dev_state));
Miao Xie5f375832014-09-03 21:35:46 +08006765
6766 list_move(&device->dev_list, &fs_devices->devices);
6767 device->fs_devices->num_devices--;
6768 fs_devices->num_devices++;
6769
6770 device->fs_devices->missing_devices--;
6771 fs_devices->missing_devices++;
6772
6773 device->fs_devices = fs_devices;
6774 }
Yan Zheng2b820322008-11-17 21:11:30 -05006775 }
6776
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006777 if (device->fs_devices != fs_info->fs_devices) {
Anand Jainebbede42017-12-04 12:54:52 +08006778 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
Yan Zheng2b820322008-11-17 21:11:30 -05006779 if (device->generation !=
6780 btrfs_device_generation(leaf, dev_item))
6781 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006782 }
Chris Mason0b86a832008-03-24 15:01:56 -04006783
6784 fill_device_from_item(leaf, dev_item, device);
Anand Jaine12c9622017-12-04 12:54:53 +08006785 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jainebbede42017-12-04 12:54:52 +08006786 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jain401e29c2017-12-04 12:54:55 +08006787 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05006788 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03006789 atomic64_add(device->total_bytes - device->bytes_used,
6790 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04006791 }
Chris Mason0b86a832008-03-24 15:01:56 -04006792 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006793 return ret;
6794}
6795
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006796int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006797{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006798 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006799 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006800 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006801 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006802 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006803 u8 *array_ptr;
6804 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006805 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006806 u32 num_stripes;
6807 u32 array_size;
6808 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006809 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07006810 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04006811 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006812
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006813 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02006814 /*
6815 * This will create extent buffer of nodesize, superblock size is
6816 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6817 * overallocate but we can keep it as-is, only the first page is used.
6818 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006819 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07006820 if (IS_ERR(sb))
6821 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01006822 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006823 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006824 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006825 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01006826 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02006827 * pages up-to-date when the page is larger: extent does not cover the
6828 * whole page and consequently check_page_uptodate does not find all
6829 * the page's extents up-to-date (the hole beyond sb),
6830 * write_extent_buffer then triggers a WARN_ON.
6831 *
6832 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6833 * but sb spans only this function. Add an explicit SetPageUptodate call
6834 * to silence the warning eg. on PowerPC 64.
6835 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006836 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006837 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006838
Chris Masona061fc82008-05-07 11:43:44 -04006839 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006840 array_size = btrfs_super_sys_array_size(super_copy);
6841
David Sterba1ffb22c2014-10-31 19:02:42 +01006842 array_ptr = super_copy->sys_chunk_array;
6843 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6844 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006845
David Sterba1ffb22c2014-10-31 19:02:42 +01006846 while (cur_offset < array_size) {
6847 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006848 len = sizeof(*disk_key);
6849 if (cur_offset + len > array_size)
6850 goto out_short_read;
6851
Chris Mason0b86a832008-03-24 15:01:56 -04006852 btrfs_disk_key_to_cpu(&key, disk_key);
6853
David Sterba1ffb22c2014-10-31 19:02:42 +01006854 array_ptr += len;
6855 sb_array_offset += len;
6856 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006857
Chris Mason0d81ba52008-03-24 15:02:07 -04006858 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006859 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006860 /*
6861 * At least one btrfs_chunk with one stripe must be
6862 * present, exact stripe count check comes afterwards
6863 */
6864 len = btrfs_chunk_item_size(1);
6865 if (cur_offset + len > array_size)
6866 goto out_short_read;
6867
6868 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01006869 if (!num_stripes) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006870 btrfs_err(fs_info,
6871 "invalid number of stripes %u in sys_array at offset %u",
David Sterbaf5cdedd2015-11-30 17:27:06 +01006872 num_stripes, cur_offset);
6873 ret = -EIO;
6874 break;
6875 }
6876
Liu Boe06cd3d2016-06-03 12:05:15 -07006877 type = btrfs_chunk_type(sb, chunk);
6878 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006879 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006880 "invalid chunk type %llu in sys_array at offset %u",
6881 type, cur_offset);
6882 ret = -EIO;
6883 break;
6884 }
6885
David Sterbae3540ea2014-11-05 15:24:51 +01006886 len = btrfs_chunk_item_size(num_stripes);
6887 if (cur_offset + len > array_size)
6888 goto out_short_read;
6889
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006890 ret = read_one_chunk(fs_info, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006891 if (ret)
6892 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006893 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006894 btrfs_err(fs_info,
6895 "unexpected item type %u in sys_array at offset %u",
6896 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04006897 ret = -EIO;
6898 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006899 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006900 array_ptr += len;
6901 sb_array_offset += len;
6902 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006903 }
Liu Bod8651772016-06-03 17:41:42 -07006904 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006905 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006906 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006907
6908out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006909 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01006910 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07006911 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006912 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01006913 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006914}
6915
Qu Wenruo21634a12017-03-09 09:34:36 +08006916/*
6917 * Check if all chunks in the fs are OK for read-write degraded mount
6918 *
Anand Jain6528b992017-12-18 17:08:59 +08006919 * If the @failing_dev is specified, it's accounted as missing.
6920 *
Qu Wenruo21634a12017-03-09 09:34:36 +08006921 * Return true if all chunks meet the minimal RW mount requirements.
6922 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6923 */
Anand Jain6528b992017-12-18 17:08:59 +08006924bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
6925 struct btrfs_device *failing_dev)
Qu Wenruo21634a12017-03-09 09:34:36 +08006926{
6927 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
6928 struct extent_map *em;
6929 u64 next_start = 0;
6930 bool ret = true;
6931
6932 read_lock(&map_tree->map_tree.lock);
6933 em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1);
6934 read_unlock(&map_tree->map_tree.lock);
6935 /* No chunk at all? Return false anyway */
6936 if (!em) {
6937 ret = false;
6938 goto out;
6939 }
6940 while (em) {
6941 struct map_lookup *map;
6942 int missing = 0;
6943 int max_tolerated;
6944 int i;
6945
6946 map = em->map_lookup;
6947 max_tolerated =
6948 btrfs_get_num_tolerated_disk_barrier_failures(
6949 map->type);
6950 for (i = 0; i < map->num_stripes; i++) {
6951 struct btrfs_device *dev = map->stripes[i].dev;
6952
Anand Jaine6e674b2017-12-04 12:54:54 +08006953 if (!dev || !dev->bdev ||
6954 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
Qu Wenruo21634a12017-03-09 09:34:36 +08006955 dev->last_flush_error)
6956 missing++;
Anand Jain6528b992017-12-18 17:08:59 +08006957 else if (failing_dev && failing_dev == dev)
6958 missing++;
Qu Wenruo21634a12017-03-09 09:34:36 +08006959 }
6960 if (missing > max_tolerated) {
Anand Jain6528b992017-12-18 17:08:59 +08006961 if (!failing_dev)
6962 btrfs_warn(fs_info,
Qu Wenruo21634a12017-03-09 09:34:36 +08006963 "chunk %llu missing %d devices, max tolerance is %d for writeable mount",
6964 em->start, missing, max_tolerated);
6965 free_extent_map(em);
6966 ret = false;
6967 goto out;
6968 }
6969 next_start = extent_map_end(em);
6970 free_extent_map(em);
6971
6972 read_lock(&map_tree->map_tree.lock);
6973 em = lookup_extent_mapping(&map_tree->map_tree, next_start,
6974 (u64)(-1) - next_start);
6975 read_unlock(&map_tree->map_tree.lock);
6976 }
6977out:
6978 return ret;
6979}
6980
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006981int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006982{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006983 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04006984 struct btrfs_path *path;
6985 struct extent_buffer *leaf;
6986 struct btrfs_key key;
6987 struct btrfs_key found_key;
6988 int ret;
6989 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006990 u64 total_dev = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006991
Chris Mason0b86a832008-03-24 15:01:56 -04006992 path = btrfs_alloc_path();
6993 if (!path)
6994 return -ENOMEM;
6995
Li Zefanb367e472011-12-07 11:38:24 +08006996 mutex_lock(&uuid_mutex);
David Sterba34441362016-10-04 19:34:27 +02006997 mutex_lock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08006998
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006999 /*
7000 * Read all device items, and then all the chunk items. All
7001 * device items are found before any chunk item (their object id
7002 * is smaller than the lowest possible object id for a chunk
7003 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04007004 */
7005 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
7006 key.offset = 0;
7007 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007008 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00007009 if (ret < 0)
7010 goto error;
Chris Masond3977122009-01-05 21:25:51 -05007011 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007012 leaf = path->nodes[0];
7013 slot = path->slots[0];
7014 if (slot >= btrfs_header_nritems(leaf)) {
7015 ret = btrfs_next_leaf(root, path);
7016 if (ret == 0)
7017 continue;
7018 if (ret < 0)
7019 goto error;
7020 break;
7021 }
7022 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007023 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
7024 struct btrfs_dev_item *dev_item;
7025 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04007026 struct btrfs_dev_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007027 ret = read_one_dev(fs_info, leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007028 if (ret)
7029 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007030 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04007031 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
7032 struct btrfs_chunk *chunk;
7033 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007034 ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05007035 if (ret)
7036 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04007037 }
7038 path->slots[0]++;
7039 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07007040
7041 /*
7042 * After loading chunk tree, we've got all device information,
7043 * do another round of validation checks.
7044 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007045 if (total_dev != fs_info->fs_devices->total_devices) {
7046 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007047 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007048 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07007049 total_dev);
7050 ret = -EINVAL;
7051 goto error;
7052 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007053 if (btrfs_super_total_bytes(fs_info->super_copy) <
7054 fs_info->fs_devices->total_rw_bytes) {
7055 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007056 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007057 btrfs_super_total_bytes(fs_info->super_copy),
7058 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07007059 ret = -EINVAL;
7060 goto error;
7061 }
Chris Mason0b86a832008-03-24 15:01:56 -04007062 ret = 0;
7063error:
David Sterba34441362016-10-04 19:34:27 +02007064 mutex_unlock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007065 mutex_unlock(&uuid_mutex);
7066
Yan Zheng2b820322008-11-17 21:11:30 -05007067 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007068 return ret;
7069}
Stefan Behrens442a4f62012-05-25 16:06:08 +02007070
Miao Xiecb517ea2013-05-15 07:48:19 +00007071void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7072{
7073 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7074 struct btrfs_device *device;
7075
Liu Bo29cc83f2014-05-11 23:14:59 +08007076 while (fs_devices) {
7077 mutex_lock(&fs_devices->device_list_mutex);
7078 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007079 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007080 mutex_unlock(&fs_devices->device_list_mutex);
7081
7082 fs_devices = fs_devices->seed;
7083 }
Miao Xiecb517ea2013-05-15 07:48:19 +00007084}
7085
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007086static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
7087{
7088 int i;
7089
7090 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7091 btrfs_dev_stat_reset(dev, i);
7092}
7093
7094int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7095{
7096 struct btrfs_key key;
7097 struct btrfs_key found_key;
7098 struct btrfs_root *dev_root = fs_info->dev_root;
7099 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7100 struct extent_buffer *eb;
7101 int slot;
7102 int ret = 0;
7103 struct btrfs_device *device;
7104 struct btrfs_path *path = NULL;
7105 int i;
7106
7107 path = btrfs_alloc_path();
7108 if (!path) {
7109 ret = -ENOMEM;
7110 goto out;
7111 }
7112
7113 mutex_lock(&fs_devices->device_list_mutex);
7114 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7115 int item_size;
7116 struct btrfs_dev_stats_item *ptr;
7117
David Sterba242e2952016-01-25 17:51:31 +01007118 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7119 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007120 key.offset = device->devid;
7121 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7122 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007123 __btrfs_reset_dev_stats(device);
7124 device->dev_stats_valid = 1;
7125 btrfs_release_path(path);
7126 continue;
7127 }
7128 slot = path->slots[0];
7129 eb = path->nodes[0];
7130 btrfs_item_key_to_cpu(eb, &found_key, slot);
7131 item_size = btrfs_item_size_nr(eb, slot);
7132
7133 ptr = btrfs_item_ptr(eb, slot,
7134 struct btrfs_dev_stats_item);
7135
7136 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7137 if (item_size >= (1 + i) * sizeof(__le64))
7138 btrfs_dev_stat_set(device, i,
7139 btrfs_dev_stats_value(eb, ptr, i));
7140 else
7141 btrfs_dev_stat_reset(device, i);
7142 }
7143
7144 device->dev_stats_valid = 1;
7145 btrfs_dev_stat_print_on_load(device);
7146 btrfs_release_path(path);
7147 }
7148 mutex_unlock(&fs_devices->device_list_mutex);
7149
7150out:
7151 btrfs_free_path(path);
7152 return ret < 0 ? ret : 0;
7153}
7154
7155static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007156 struct btrfs_fs_info *fs_info,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007157 struct btrfs_device *device)
7158{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007159 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007160 struct btrfs_path *path;
7161 struct btrfs_key key;
7162 struct extent_buffer *eb;
7163 struct btrfs_dev_stats_item *ptr;
7164 int ret;
7165 int i;
7166
David Sterba242e2952016-01-25 17:51:31 +01007167 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7168 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007169 key.offset = device->devid;
7170
7171 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007172 if (!path)
7173 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007174 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7175 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007176 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007177 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007178 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007179 goto out;
7180 }
7181
7182 if (ret == 0 &&
7183 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7184 /* need to delete old one and insert a new one */
7185 ret = btrfs_del_item(trans, dev_root, path);
7186 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007187 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007188 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007189 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007190 goto out;
7191 }
7192 ret = 1;
7193 }
7194
7195 if (ret == 1) {
7196 /* need to insert a new item */
7197 btrfs_release_path(path);
7198 ret = btrfs_insert_empty_item(trans, dev_root, path,
7199 &key, sizeof(*ptr));
7200 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007201 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007202 "insert dev_stats item for device %s failed %d",
7203 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007204 goto out;
7205 }
7206 }
7207
7208 eb = path->nodes[0];
7209 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7210 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7211 btrfs_set_dev_stats_value(eb, ptr, i,
7212 btrfs_dev_stat_read(device, i));
7213 btrfs_mark_buffer_dirty(eb);
7214
7215out:
7216 btrfs_free_path(path);
7217 return ret;
7218}
7219
7220/*
7221 * called from commit_transaction. Writes all changed device stats to disk.
7222 */
7223int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
7224 struct btrfs_fs_info *fs_info)
7225{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007226 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7227 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007228 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007229 int ret = 0;
7230
7231 mutex_lock(&fs_devices->device_list_mutex);
7232 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007233 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7234 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007235 continue;
7236
Nikolay Borisov9deae962017-10-24 13:47:37 +03007237
7238 /*
7239 * There is a LOAD-LOAD control dependency between the value of
7240 * dev_stats_ccnt and updating the on-disk values which requires
7241 * reading the in-memory counters. Such control dependencies
7242 * require explicit read memory barriers.
7243 *
7244 * This memory barriers pairs with smp_mb__before_atomic in
7245 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7246 * barrier implied by atomic_xchg in
7247 * btrfs_dev_stats_read_and_reset
7248 */
7249 smp_rmb();
7250
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007251 ret = update_dev_stat_item(trans, fs_info, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007252 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007253 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007254 }
7255 mutex_unlock(&fs_devices->device_list_mutex);
7256
7257 return ret;
7258}
7259
Stefan Behrens442a4f62012-05-25 16:06:08 +02007260void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7261{
7262 btrfs_dev_stat_inc(dev, index);
7263 btrfs_dev_stat_print_on_error(dev);
7264}
7265
Eric Sandeen48a3b632013-04-25 20:41:01 +00007266static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007267{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007268 if (!dev->dev_stats_valid)
7269 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007270 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007271 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007272 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007273 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7274 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7275 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007276 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7277 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007278}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007279
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007280static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7281{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007282 int i;
7283
7284 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7285 if (btrfs_dev_stat_read(dev, i) != 0)
7286 break;
7287 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7288 return; /* all values == 0, suppress message */
7289
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007290 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007291 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007292 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007293 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7294 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7295 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7296 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7297 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7298}
7299
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007300int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007301 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007302{
7303 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007304 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007305 int i;
7306
7307 mutex_lock(&fs_devices->device_list_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007308 dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007309 mutex_unlock(&fs_devices->device_list_mutex);
7310
7311 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007312 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007313 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007314 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007315 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007316 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007317 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007318 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7319 if (stats->nr_items > i)
7320 stats->values[i] =
7321 btrfs_dev_stat_read_and_reset(dev, i);
7322 else
7323 btrfs_dev_stat_reset(dev, i);
7324 }
7325 } else {
7326 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7327 if (stats->nr_items > i)
7328 stats->values[i] = btrfs_dev_stat_read(dev, i);
7329 }
7330 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7331 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7332 return 0;
7333}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007334
David Sterbada353f62017-02-14 17:55:53 +01007335void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007336{
7337 struct buffer_head *bh;
7338 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08007339 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007340
Anand Jain12b1c262015-08-14 18:32:59 +08007341 if (!bdev)
7342 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007343
Anand Jain12b1c262015-08-14 18:32:59 +08007344 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7345 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007346
Anand Jain12b1c262015-08-14 18:32:59 +08007347 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7348 continue;
7349
7350 disk_super = (struct btrfs_super_block *)bh->b_data;
7351
7352 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7353 set_buffer_dirty(bh);
7354 sync_dirty_buffer(bh);
7355 brelse(bh);
7356 }
7357
7358 /* Notify udev that device has changed */
7359 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7360
7361 /* Update ctime/mtime for device path for libblkid */
7362 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007363}
Miao Xie935e5cc2014-09-03 21:35:33 +08007364
7365/*
7366 * Update the size of all devices, which is used for writing out the
7367 * super blocks.
7368 */
7369void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
7370{
7371 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7372 struct btrfs_device *curr, *next;
7373
7374 if (list_empty(&fs_devices->resized_devices))
7375 return;
7376
7377 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02007378 mutex_lock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007379 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
7380 resized_list) {
7381 list_del_init(&curr->resized_list);
7382 curr->commit_total_bytes = curr->disk_total_bytes;
7383 }
David Sterba34441362016-10-04 19:34:27 +02007384 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007385 mutex_unlock(&fs_devices->device_list_mutex);
7386}
Miao Xiece7213c2014-09-03 21:35:34 +08007387
7388/* Must be invoked during the transaction commit */
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007389void btrfs_update_commit_device_bytes_used(struct btrfs_transaction *trans)
Miao Xiece7213c2014-09-03 21:35:34 +08007390{
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007391 struct btrfs_fs_info *fs_info = trans->fs_info;
Miao Xiece7213c2014-09-03 21:35:34 +08007392 struct extent_map *em;
7393 struct map_lookup *map;
7394 struct btrfs_device *dev;
7395 int i;
7396
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007397 if (list_empty(&trans->pending_chunks))
Miao Xiece7213c2014-09-03 21:35:34 +08007398 return;
7399
7400 /* In order to kick the device replace finish process */
David Sterba34441362016-10-04 19:34:27 +02007401 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007402 list_for_each_entry(em, &trans->pending_chunks, list) {
Jeff Mahoney95617d62015-06-03 10:55:48 -04007403 map = em->map_lookup;
Miao Xiece7213c2014-09-03 21:35:34 +08007404
7405 for (i = 0; i < map->num_stripes; i++) {
7406 dev = map->stripes[i].dev;
7407 dev->commit_bytes_used = dev->bytes_used;
7408 }
7409 }
David Sterba34441362016-10-04 19:34:27 +02007410 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007411}
Anand Jain5a13f432015-03-10 06:38:31 +08007412
7413void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7414{
7415 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7416 while (fs_devices) {
7417 fs_devices->fs_info = fs_info;
7418 fs_devices = fs_devices->seed;
7419 }
7420}
7421
7422void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7423{
7424 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7425 while (fs_devices) {
7426 fs_devices->fs_info = NULL;
7427 fs_devices = fs_devices->seed;
7428 }
7429}