blob: 13bb44194c564459e2114032e524ed606af2d6d5 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason0b86a832008-03-24 15:01:56 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason0b86a832008-03-24 15:01:56 -04004 */
David Sterbac1d7c512018-04-03 19:23:33 +02005
Chris Mason0b86a832008-03-24 15:01:56 -04006#include <linux/sched.h>
7#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -04009#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040010#include <linux/blkdev.h>
Stefan Behrens442a4f62012-05-25 16:06:08 +020011#include <linux/ratelimit.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020012#include <linux/kthread.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050013#include <linux/raid/pq.h>
Stefan Behrens803b2f52013-08-15 17:11:21 +020014#include <linux/semaphore.h>
Andy Shevchenko8da4b8c2016-05-20 17:01:00 -070015#include <linux/uuid.h>
Anand Jainf8e10cd2018-01-22 14:49:36 -080016#include <linux/list_sort.h>
Chris Mason0b86a832008-03-24 15:01:56 -040017#include "ctree.h"
18#include "extent_map.h"
19#include "disk-io.h"
20#include "transaction.h"
21#include "print-tree.h"
22#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050023#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040024#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010025#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040026#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060027#include "math.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010028#include "dev-replace.h"
Anand Jain99994cd2014-06-03 11:36:00 +080029#include "sysfs.h"
Chris Mason0b86a832008-03-24 15:01:56 -040030
Zhao Leiaf902042015-09-15 21:08:06 +080031const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
32 [BTRFS_RAID_RAID10] = {
33 .sub_stripes = 2,
34 .dev_stripes = 1,
35 .devs_max = 0, /* 0 == as many as possible */
36 .devs_min = 4,
Zhao Lei8789f4f2015-09-15 21:08:07 +080037 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080038 .devs_increment = 2,
39 .ncopies = 2,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020040 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080041 .raid_name = "raid10",
Anand Jain41a6e892018-04-25 19:01:43 +080042 .bg_flag = BTRFS_BLOCK_GROUP_RAID10,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080043 .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +080044 },
45 [BTRFS_RAID_RAID1] = {
46 .sub_stripes = 1,
47 .dev_stripes = 1,
48 .devs_max = 2,
49 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080050 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080051 .devs_increment = 2,
52 .ncopies = 2,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020053 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080054 .raid_name = "raid1",
Anand Jain41a6e892018-04-25 19:01:43 +080055 .bg_flag = BTRFS_BLOCK_GROUP_RAID1,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080056 .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +080057 },
58 [BTRFS_RAID_DUP] = {
59 .sub_stripes = 1,
60 .dev_stripes = 2,
61 .devs_max = 1,
62 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080063 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080064 .devs_increment = 1,
65 .ncopies = 2,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020066 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080067 .raid_name = "dup",
Anand Jain41a6e892018-04-25 19:01:43 +080068 .bg_flag = BTRFS_BLOCK_GROUP_DUP,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080069 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080070 },
71 [BTRFS_RAID_RAID0] = {
72 .sub_stripes = 1,
73 .dev_stripes = 1,
74 .devs_max = 0,
75 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080076 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080077 .devs_increment = 1,
78 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020079 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080080 .raid_name = "raid0",
Anand Jain41a6e892018-04-25 19:01:43 +080081 .bg_flag = BTRFS_BLOCK_GROUP_RAID0,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080082 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080083 },
84 [BTRFS_RAID_SINGLE] = {
85 .sub_stripes = 1,
86 .dev_stripes = 1,
87 .devs_max = 1,
88 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080089 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080090 .devs_increment = 1,
91 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020092 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080093 .raid_name = "single",
Anand Jain41a6e892018-04-25 19:01:43 +080094 .bg_flag = 0,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080095 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080096 },
97 [BTRFS_RAID_RAID5] = {
98 .sub_stripes = 1,
99 .dev_stripes = 1,
100 .devs_max = 0,
101 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800102 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +0800103 .devs_increment = 1,
Hans van Kranenburgda612e32018-10-04 23:24:41 +0200104 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +0200105 .nparity = 1,
Anand Jained234672018-04-25 19:01:42 +0800106 .raid_name = "raid5",
Anand Jain41a6e892018-04-25 19:01:43 +0800107 .bg_flag = BTRFS_BLOCK_GROUP_RAID5,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800108 .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +0800109 },
110 [BTRFS_RAID_RAID6] = {
111 .sub_stripes = 1,
112 .dev_stripes = 1,
113 .devs_max = 0,
114 .devs_min = 3,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800115 .tolerated_failures = 2,
Zhao Leiaf902042015-09-15 21:08:06 +0800116 .devs_increment = 1,
Hans van Kranenburgda612e32018-10-04 23:24:41 +0200117 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +0200118 .nparity = 2,
Anand Jained234672018-04-25 19:01:42 +0800119 .raid_name = "raid6",
Anand Jain41a6e892018-04-25 19:01:43 +0800120 .bg_flag = BTRFS_BLOCK_GROUP_RAID6,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800121 .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +0800122 },
123};
124
Anand Jained234672018-04-25 19:01:42 +0800125const char *get_raid_name(enum btrfs_raid_types type)
126{
127 if (type >= BTRFS_NR_RAID_TYPES)
128 return NULL;
129
130 return btrfs_raid_array[type].raid_name;
131}
132
Yan Zheng2b820322008-11-17 21:11:30 -0500133static int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +0100134 struct btrfs_fs_info *fs_info);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400135static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200136static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000137static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200138static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Liu Bo5ab56092017-03-14 13:33:57 -0700139static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
140 enum btrfs_map_op op,
141 u64 logical, u64 *length,
142 struct btrfs_bio **bbio_ret,
143 int mirror_num, int need_raid_map);
Yan Zheng2b820322008-11-17 21:11:30 -0500144
David Sterba9c6b1c42017-06-16 22:30:00 +0200145/*
146 * Device locking
147 * ==============
148 *
149 * There are several mutexes that protect manipulation of devices and low-level
150 * structures like chunks but not block groups, extents or files
151 *
152 * uuid_mutex (global lock)
153 * ------------------------
154 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
155 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
156 * device) or requested by the device= mount option
157 *
158 * the mutex can be very coarse and can cover long-running operations
159 *
160 * protects: updates to fs_devices counters like missing devices, rw devices,
161 * seeding, structure cloning, openning/closing devices at mount/umount time
162 *
163 * global::fs_devs - add, remove, updates to the global list
164 *
165 * does not protect: manipulation of the fs_devices::devices list!
166 *
167 * btrfs_device::name - renames (write side), read is RCU
168 *
169 * fs_devices::device_list_mutex (per-fs, with RCU)
170 * ------------------------------------------------
171 * protects updates to fs_devices::devices, ie. adding and deleting
172 *
173 * simple list traversal with read-only actions can be done with RCU protection
174 *
175 * may be used to exclude some operations from running concurrently without any
176 * modifications to the list (see write_all_supers)
177 *
David Sterba9c6b1c42017-06-16 22:30:00 +0200178 * balance_mutex
179 * -------------
180 * protects balance structures (status, state) and context accessed from
181 * several places (internally, ioctl)
182 *
183 * chunk_mutex
184 * -----------
185 * protects chunks, adding or removing during allocation, trim or when a new
186 * device is added/removed
187 *
188 * cleaner_mutex
189 * -------------
190 * a big lock that is held by the cleaner thread and prevents running subvolume
191 * cleaning together with relocation or delayed iputs
192 *
193 *
194 * Lock nesting
195 * ============
196 *
197 * uuid_mutex
198 * volume_mutex
199 * device_list_mutex
200 * chunk_mutex
201 * balance_mutex
Anand Jain89595e82018-04-18 14:59:25 +0800202 *
203 *
204 * Exclusive operations, BTRFS_FS_EXCL_OP
205 * ======================================
206 *
207 * Maintains the exclusivity of the following operations that apply to the
208 * whole filesystem and cannot run in parallel.
209 *
210 * - Balance (*)
211 * - Device add
212 * - Device remove
213 * - Device replace (*)
214 * - Resize
215 *
216 * The device operations (as above) can be in one of the following states:
217 *
218 * - Running state
219 * - Paused state
220 * - Completed state
221 *
222 * Only device operations marked with (*) can go into the Paused state for the
223 * following reasons:
224 *
225 * - ioctl (only Balance can be Paused through ioctl)
226 * - filesystem remounted as read-only
227 * - filesystem unmounted and mounted as read-only
228 * - system power-cycle and filesystem mounted as read-only
229 * - filesystem or device errors leading to forced read-only
230 *
231 * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations.
232 * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set.
233 * A device operation in Paused or Running state can be canceled or resumed
234 * either by ioctl (Balance only) or when remounted as read-write.
235 * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or
236 * completed.
David Sterba9c6b1c42017-06-16 22:30:00 +0200237 */
238
Miao Xie67a2c452014-09-03 21:35:43 +0800239DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400240static LIST_HEAD(fs_uuids);
Anand Jainc73eccf2015-03-10 06:38:30 +0800241struct list_head *btrfs_get_fs_uuids(void)
242{
243 return &fs_uuids;
244}
Chris Mason8a4b83c2008-03-24 15:02:07 -0400245
David Sterba2dfeca92017-06-14 02:48:07 +0200246/*
247 * alloc_fs_devices - allocate struct btrfs_fs_devices
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200248 * @fsid: if not NULL, copy the UUID to fs_devices::fsid
249 * @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid
David Sterba2dfeca92017-06-14 02:48:07 +0200250 *
251 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
252 * The returned struct is not linked onto any lists and can be destroyed with
253 * kfree() right away.
254 */
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200255static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid,
256 const u8 *metadata_fsid)
Ilya Dryomov2208a372013-08-12 14:33:03 +0300257{
258 struct btrfs_fs_devices *fs_devs;
259
David Sterba78f2c9e2016-02-11 14:25:38 +0100260 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300261 if (!fs_devs)
262 return ERR_PTR(-ENOMEM);
263
264 mutex_init(&fs_devs->device_list_mutex);
265
266 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +0800267 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300268 INIT_LIST_HEAD(&fs_devs->alloc_list);
Anand Jainc4babc52018-04-12 10:29:25 +0800269 INIT_LIST_HEAD(&fs_devs->fs_list);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300270 if (fsid)
271 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300272
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200273 if (metadata_fsid)
274 memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE);
275 else if (fsid)
276 memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE);
277
Ilya Dryomov2208a372013-08-12 14:33:03 +0300278 return fs_devs;
279}
280
David Sterbaa425f9d2018-03-20 15:47:33 +0100281void btrfs_free_device(struct btrfs_device *device)
David Sterba48dae9c2017-10-30 18:10:25 +0100282{
283 rcu_string_free(device->name);
284 bio_put(device->flush_bio);
285 kfree(device);
286}
287
Yan Zhenge4404d62008-12-12 10:03:26 -0500288static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
289{
290 struct btrfs_device *device;
291 WARN_ON(fs_devices->opened);
292 while (!list_empty(&fs_devices->devices)) {
293 device = list_entry(fs_devices->devices.next,
294 struct btrfs_device, dev_list);
295 list_del(&device->dev_list);
David Sterbaa425f9d2018-03-20 15:47:33 +0100296 btrfs_free_device(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500297 }
298 kfree(fs_devices);
299}
300
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000301static void btrfs_kobject_uevent(struct block_device *bdev,
302 enum kobject_action action)
303{
304 int ret;
305
306 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
307 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500308 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000309 action,
310 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
311 &disk_to_dev(bdev->bd_disk)->kobj);
312}
313
David Sterbaffc5a372018-02-19 17:24:15 +0100314void __exit btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400315{
316 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400317
Yan Zheng2b820322008-11-17 21:11:30 -0500318 while (!list_empty(&fs_uuids)) {
319 fs_devices = list_entry(fs_uuids.next,
Anand Jainc4babc52018-04-12 10:29:25 +0800320 struct btrfs_fs_devices, fs_list);
321 list_del(&fs_devices->fs_list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500322 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400323 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400324}
325
David Sterba48dae9c2017-10-30 18:10:25 +0100326/*
327 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
328 * Returned struct is not linked onto any lists and must be destroyed using
David Sterbaa425f9d2018-03-20 15:47:33 +0100329 * btrfs_free_device.
David Sterba48dae9c2017-10-30 18:10:25 +0100330 */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300331static struct btrfs_device *__alloc_device(void)
332{
333 struct btrfs_device *dev;
334
David Sterba78f2c9e2016-02-11 14:25:38 +0100335 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300336 if (!dev)
337 return ERR_PTR(-ENOMEM);
338
David Sterbae0ae9992017-06-06 17:06:06 +0200339 /*
340 * Preallocate a bio that's always going to be used for flushing device
341 * barriers and matches the device lifespan
342 */
343 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
344 if (!dev->flush_bio) {
345 kfree(dev);
346 return ERR_PTR(-ENOMEM);
347 }
David Sterbae0ae9992017-06-06 17:06:06 +0200348
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300349 INIT_LIST_HEAD(&dev->dev_list);
350 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800351 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300352
353 spin_lock_init(&dev->io_lock);
354
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300355 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800356 atomic_set(&dev->dev_stats_ccnt, 0);
Sebastian Andrzej Siewior546bed62016-01-15 14:37:15 +0100357 btrfs_device_data_ordered_init(dev);
Chris Mason9bcaaea2017-05-04 16:08:08 -0700358 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Mel Gormand0164ad2015-11-06 16:28:21 -0800359 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300360
361 return dev;
362}
363
David Sterba35c70102017-06-15 19:51:51 +0200364/*
365 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
366 * return NULL.
367 *
368 * If devid and uuid are both specified, the match must be exact, otherwise
369 * only devid is used.
370 */
371static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
372 u64 devid, const u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400373{
374 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400375
Anand Jain636d2c92018-04-12 10:29:29 +0800376 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400377 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400378 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400379 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400380 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400381 }
382 return NULL;
383}
384
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200385static noinline struct btrfs_fs_devices *find_fsid(
386 const u8 *fsid, const u8 *metadata_fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400387{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400388 struct btrfs_fs_devices *fs_devices;
389
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200390 ASSERT(fsid);
391
Anand Jainc4babc52018-04-12 10:29:25 +0800392 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200393 if (metadata_fsid) {
394 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0
395 && memcmp(metadata_fsid, fs_devices->metadata_uuid,
396 BTRFS_FSID_SIZE) == 0)
397 return fs_devices;
398 } else {
399 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
400 return fs_devices;
401 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400402 }
403 return NULL;
404}
405
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100406static int
407btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
408 int flush, struct block_device **bdev,
409 struct buffer_head **bh)
410{
411 int ret;
412
413 *bdev = blkdev_get_by_path(device_path, flags, holder);
414
415 if (IS_ERR(*bdev)) {
416 ret = PTR_ERR(*bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100417 goto error;
418 }
419
420 if (flush)
421 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
David Sterba9f6d2512017-06-16 01:48:05 +0200422 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100423 if (ret) {
424 blkdev_put(*bdev, flags);
425 goto error;
426 }
427 invalidate_bdev(*bdev);
428 *bh = btrfs_read_dev_super(*bdev);
Anand Jain92fc03f2015-08-14 18:32:51 +0800429 if (IS_ERR(*bh)) {
430 ret = PTR_ERR(*bh);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100431 blkdev_put(*bdev, flags);
432 goto error;
433 }
434
435 return 0;
436
437error:
438 *bdev = NULL;
439 *bh = NULL;
440 return ret;
441}
442
Chris Masonffbd5172009-04-20 15:50:09 -0400443static void requeue_list(struct btrfs_pending_bios *pending_bios,
444 struct bio *head, struct bio *tail)
445{
446
447 struct bio *old_head;
448
449 old_head = pending_bios->head;
450 pending_bios->head = head;
451 if (pending_bios->tail)
452 tail->bi_next = old_head;
453 else
454 pending_bios->tail = tail;
455}
456
Chris Mason8b712842008-06-11 16:50:36 -0400457/*
458 * we try to collect pending bios for a device so we don't get a large
459 * number of procs sending bios down to the same device. This greatly
460 * improves the schedulers ability to collect and merge the bios.
461 *
462 * But, it also turns into a long list of bios to process and that is sure
463 * to eventually make the worker thread block. The solution here is to
464 * make some progress and then put this work struct back at the end of
465 * the list if the block device is congested. This way, multiple devices
466 * can make progress from a single worker thread.
467 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100468static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400469{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400470 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -0400471 struct bio *pending;
472 struct backing_dev_info *bdi;
Chris Masonffbd5172009-04-20 15:50:09 -0400473 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400474 struct bio *tail;
475 struct bio *cur;
476 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400477 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400478 unsigned long batch_run = 0;
Chris Masonb765ead2009-04-03 10:27:10 -0400479 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400480 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000481 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400482 struct blk_plug plug;
483
484 /*
485 * this function runs all the bios we've collected for
486 * a particular device. We don't want to wander off to
487 * another device without first sending all of these down.
488 * So, setup a plug here and finish it off before we return
489 */
490 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400491
Jan Karaefa7c9f2017-02-02 15:56:53 +0100492 bdi = device->bdev->bd_bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400493
Chris Mason8b712842008-06-11 16:50:36 -0400494loop:
495 spin_lock(&device->io_lock);
496
Chris Masona6837052009-02-04 09:19:41 -0500497loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400498 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400499
Chris Mason8b712842008-06-11 16:50:36 -0400500 /* take all the bios off the list at once and process them
501 * later on (without the lock held). But, remember the
502 * tail and other pointers so the bios can be properly reinserted
503 * into the list if we hit congestion
504 */
Chris Masond84275c2009-06-09 15:39:08 -0400505 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400506 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400507 force_reg = 1;
508 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400509 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400510 force_reg = 0;
511 }
Chris Masonffbd5172009-04-20 15:50:09 -0400512
513 pending = pending_bios->head;
514 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400515 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400516
517 /*
518 * if pending was null this time around, no bios need processing
519 * at all and we can stop. Otherwise it'll loop back up again
520 * and do an additional check so no bios are missed.
521 *
522 * device->running_pending is used to synchronize with the
523 * schedule_bio code.
524 */
Chris Masonffbd5172009-04-20 15:50:09 -0400525 if (device->pending_sync_bios.head == NULL &&
526 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400527 again = 0;
528 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400529 } else {
530 again = 1;
531 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400532 }
Chris Masonffbd5172009-04-20 15:50:09 -0400533
534 pending_bios->head = NULL;
535 pending_bios->tail = NULL;
536
Chris Mason8b712842008-06-11 16:50:36 -0400537 spin_unlock(&device->io_lock);
538
Chris Masond3977122009-01-05 21:25:51 -0500539 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400540
541 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400542 /* we want to work on both lists, but do more bios on the
543 * sync list than the regular list
544 */
545 if ((num_run > 32 &&
546 pending_bios != &device->pending_sync_bios &&
547 device->pending_sync_bios.head) ||
548 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
549 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400550 spin_lock(&device->io_lock);
551 requeue_list(pending_bios, pending, tail);
552 goto loop_lock;
553 }
554
Chris Mason8b712842008-06-11 16:50:36 -0400555 cur = pending;
556 pending = pending->bi_next;
557 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400558
Jens Axboedac56212015-04-17 16:23:59 -0600559 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400560
Chris Mason2ab1ba62011-08-04 14:28:36 -0400561 /*
562 * if we're doing the sync list, record that our
563 * plug has some sync requests on it
564 *
565 * If we're doing the regular list and there are
566 * sync requests sitting around, unplug before
567 * we add more
568 */
569 if (pending_bios == &device->pending_sync_bios) {
570 sync_pending = 1;
571 } else if (sync_pending) {
572 blk_finish_plug(&plug);
573 blk_start_plug(&plug);
574 sync_pending = 0;
575 }
576
Mike Christie4e49ea42016-06-05 14:31:41 -0500577 btrfsic_submit_bio(cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400578 num_run++;
579 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100580
581 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400582
583 /*
584 * we made progress, there is more work to do and the bdi
585 * is now congested. Back off and let other work structs
586 * run instead
587 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400588 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500589 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400590 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400591
Chris Masonb765ead2009-04-03 10:27:10 -0400592 ioc = current->io_context;
593
594 /*
595 * the main goal here is that we don't want to
596 * block if we're going to be able to submit
597 * more requests without blocking.
598 *
599 * This code does two great things, it pokes into
600 * the elevator code from a filesystem _and_
601 * it makes assumptions about how batching works.
602 */
603 if (ioc && ioc->nr_batch_requests > 0 &&
604 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
605 (last_waited == 0 ||
606 ioc->last_waited == last_waited)) {
607 /*
608 * we want to go through our batch of
609 * requests and stop. So, we copy out
610 * the ioc->last_waited time and test
611 * against it before looping
612 */
613 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100614 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400615 continue;
616 }
Chris Mason8b712842008-06-11 16:50:36 -0400617 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400618 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500619 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400620
621 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800622 btrfs_queue_work(fs_info->submit_workers,
623 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400624 goto done;
625 }
626 }
Chris Masonffbd5172009-04-20 15:50:09 -0400627
Chris Mason51684082010-03-10 15:33:32 -0500628 cond_resched();
629 if (again)
630 goto loop;
631
632 spin_lock(&device->io_lock);
633 if (device->pending_bios.head || device->pending_sync_bios.head)
634 goto loop_lock;
635 spin_unlock(&device->io_lock);
636
Chris Mason8b712842008-06-11 16:50:36 -0400637done:
Chris Mason211588a2011-04-19 20:12:40 -0400638 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400639}
640
Christoph Hellwigb2950862008-12-02 09:54:17 -0500641static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400642{
643 struct btrfs_device *device;
644
645 device = container_of(work, struct btrfs_device, work);
646 run_scheduled_bios(device);
647}
648
Anand Jaind8367db2018-01-18 22:00:37 +0800649/*
650 * Search and remove all stale (devices which are not mounted) devices.
651 * When both inputs are NULL, it will search and release all stale devices.
652 * path: Optional. When provided will it release all unmounted devices
653 * matching this path only.
654 * skip_dev: Optional. Will skip this device when searching for the stale
655 * devices.
656 */
657static void btrfs_free_stale_devices(const char *path,
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800658 struct btrfs_device *skip_device)
Anand Jain4fde46f2015-06-17 21:10:48 +0800659{
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800660 struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
661 struct btrfs_device *device, *tmp_device;
Anand Jain4fde46f2015-06-17 21:10:48 +0800662
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800663 list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
Anand Jain7bcb8162018-05-29 17:23:20 +0800664 mutex_lock(&fs_devices->device_list_mutex);
665 if (fs_devices->opened) {
666 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain4fde46f2015-06-17 21:10:48 +0800667 continue;
Anand Jain7bcb8162018-05-29 17:23:20 +0800668 }
Anand Jain4fde46f2015-06-17 21:10:48 +0800669
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800670 list_for_each_entry_safe(device, tmp_device,
671 &fs_devices->devices, dev_list) {
Anand Jain522f1b42018-01-18 22:00:35 +0800672 int not_found = 0;
Anand Jain4fde46f2015-06-17 21:10:48 +0800673
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800674 if (skip_device && skip_device == device)
Anand Jaind8367db2018-01-18 22:00:37 +0800675 continue;
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800676 if (path && !device->name)
Anand Jain4fde46f2015-06-17 21:10:48 +0800677 continue;
678
Anand Jain4fde46f2015-06-17 21:10:48 +0800679 rcu_read_lock();
Anand Jaind8367db2018-01-18 22:00:37 +0800680 if (path)
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800681 not_found = strcmp(rcu_str_deref(device->name),
Anand Jaind8367db2018-01-18 22:00:37 +0800682 path);
Anand Jain4fde46f2015-06-17 21:10:48 +0800683 rcu_read_unlock();
Anand Jain38cf6652018-01-18 22:00:34 +0800684 if (not_found)
685 continue;
Anand Jain4fde46f2015-06-17 21:10:48 +0800686
Anand Jain4fde46f2015-06-17 21:10:48 +0800687 /* delete the stale device */
Anand Jain7bcb8162018-05-29 17:23:20 +0800688 fs_devices->num_devices--;
689 list_del(&device->dev_list);
690 btrfs_free_device(device);
691
692 if (fs_devices->num_devices == 0)
Nikolay Borisovfd649f12018-01-30 16:07:37 +0200693 break;
Anand Jain7bcb8162018-05-29 17:23:20 +0800694 }
695 mutex_unlock(&fs_devices->device_list_mutex);
696 if (fs_devices->num_devices == 0) {
697 btrfs_sysfs_remove_fsid(fs_devices);
698 list_del(&fs_devices->fs_list);
699 free_fs_devices(fs_devices);
Anand Jain4fde46f2015-06-17 21:10:48 +0800700 }
701 }
702}
703
Anand Jain0fb08bc2017-11-09 23:45:24 +0800704static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
705 struct btrfs_device *device, fmode_t flags,
706 void *holder)
707{
708 struct request_queue *q;
709 struct block_device *bdev;
710 struct buffer_head *bh;
711 struct btrfs_super_block *disk_super;
712 u64 devid;
713 int ret;
714
715 if (device->bdev)
716 return -EINVAL;
717 if (!device->name)
718 return -EINVAL;
719
720 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
721 &bdev, &bh);
722 if (ret)
723 return ret;
724
725 disk_super = (struct btrfs_super_block *)bh->b_data;
726 devid = btrfs_stack_device_id(&disk_super->dev_item);
727 if (devid != device->devid)
728 goto error_brelse;
729
730 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
731 goto error_brelse;
732
733 device->generation = btrfs_super_generation(disk_super);
734
735 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200736 if (btrfs_super_incompat_flags(disk_super) &
737 BTRFS_FEATURE_INCOMPAT_METADATA_UUID) {
738 pr_err(
739 "BTRFS: Invalid seeding and uuid-changed device detected\n");
740 goto error_brelse;
741 }
742
Anand Jainebbede42017-12-04 12:54:52 +0800743 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800744 fs_devices->seeding = 1;
745 } else {
Anand Jainebbede42017-12-04 12:54:52 +0800746 if (bdev_read_only(bdev))
747 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
748 else
749 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800750 }
751
752 q = bdev_get_queue(bdev);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800753 if (!blk_queue_nonrot(q))
754 fs_devices->rotating = 1;
755
756 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +0800757 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800758 device->mode = flags;
759
760 fs_devices->open_devices++;
Anand Jainebbede42017-12-04 12:54:52 +0800761 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
762 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Anand Jain0fb08bc2017-11-09 23:45:24 +0800763 fs_devices->rw_devices++;
Anand Jainb1b8e382018-01-22 14:49:37 -0800764 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800765 }
766 brelse(bh);
767
768 return 0;
769
770error_brelse:
771 brelse(bh);
772 blkdev_put(bdev, flags);
773
774 return -EINVAL;
775}
776
David Sterba60999ca2014-03-26 18:26:36 +0100777/*
778 * Add new device to list of registered devices
779 *
780 * Returns:
Anand Jaine124ece2018-01-18 22:02:35 +0800781 * device pointer which was just added or updated when successful
782 * error pointer when failed
David Sterba60999ca2014-03-26 18:26:36 +0100783 */
Anand Jaine124ece2018-01-18 22:02:35 +0800784static noinline struct btrfs_device *device_list_add(const char *path,
Anand Jain4306a972018-05-29 12:28:37 +0800785 struct btrfs_super_block *disk_super,
786 bool *new_device_added)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400787{
788 struct btrfs_device *device;
789 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400790 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400791 u64 found_transid = btrfs_super_generation(disk_super);
Anand Jain3acbcbf2018-01-18 22:02:36 +0800792 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200793 bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
794 BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200795 bool fsid_change_in_progress = (btrfs_super_flags(disk_super) &
796 BTRFS_SUPER_FLAG_CHANGING_FSID_V2);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400797
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200798 if (has_metadata_uuid)
799 fs_devices = find_fsid(disk_super->fsid, disk_super->metadata_uuid);
800 else
801 fs_devices = find_fsid(disk_super->fsid, NULL);
802
Chris Mason8a4b83c2008-03-24 15:02:07 -0400803 if (!fs_devices) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200804 if (has_metadata_uuid)
805 fs_devices = alloc_fs_devices(disk_super->fsid,
806 disk_super->metadata_uuid);
807 else
808 fs_devices = alloc_fs_devices(disk_super->fsid, NULL);
809
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200810 fs_devices->fsid_change = fsid_change_in_progress;
811
Ilya Dryomov2208a372013-08-12 14:33:03 +0300812 if (IS_ERR(fs_devices))
Anand Jaine124ece2018-01-18 22:02:35 +0800813 return ERR_CAST(fs_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300814
Anand Jain9c6d1732018-05-29 14:10:20 +0800815 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainc4babc52018-04-12 10:29:25 +0800816 list_add(&fs_devices->fs_list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300817
Chris Mason8a4b83c2008-03-24 15:02:07 -0400818 device = NULL;
819 } else {
Anand Jain9c6d1732018-05-29 14:10:20 +0800820 mutex_lock(&fs_devices->device_list_mutex);
David Sterba35c70102017-06-15 19:51:51 +0200821 device = find_device(fs_devices, devid,
822 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400823 }
Miao Xie443f24f2014-07-24 11:37:15 +0800824
Chris Mason8a4b83c2008-03-24 15:02:07 -0400825 if (!device) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800826 if (fs_devices->opened) {
827 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800828 return ERR_PTR(-EBUSY);
Anand Jain9c6d1732018-05-29 14:10:20 +0800829 }
Yan Zheng2b820322008-11-17 21:11:30 -0500830
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300831 device = btrfs_alloc_device(NULL, &devid,
832 disk_super->dev_item.uuid);
833 if (IS_ERR(device)) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800834 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400835 /* we can safely leave the fs_devices entry around */
Anand Jaine124ece2018-01-18 22:02:35 +0800836 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400837 }
Josef Bacik606686e2012-06-04 14:03:51 -0400838
839 name = rcu_string_strdup(path, GFP_NOFS);
840 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +0100841 btrfs_free_device(device);
Anand Jain9c6d1732018-05-29 14:10:20 +0800842 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800843 return ERR_PTR(-ENOMEM);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400844 }
Josef Bacik606686e2012-06-04 14:03:51 -0400845 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200846
Xiao Guangrong1f781602011-04-20 10:09:16 +0000847 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100848 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400849
Yan Zheng2b820322008-11-17 21:11:30 -0500850 device->fs_devices = fs_devices;
Anand Jain4306a972018-05-29 12:28:37 +0800851 *new_device_added = true;
Anand Jain327f18c2018-01-18 22:02:33 +0800852
853 if (disk_super->label[0])
854 pr_info("BTRFS: device label %s devid %llu transid %llu %s\n",
855 disk_super->label, devid, found_transid, path);
856 else
857 pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n",
858 disk_super->fsid, devid, found_transid, path);
859
Josef Bacik606686e2012-06-04 14:03:51 -0400860 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800861 /*
862 * When FS is already mounted.
863 * 1. If you are here and if the device->name is NULL that
864 * means this device was missing at time of FS mount.
865 * 2. If you are here and if the device->name is different
866 * from 'path' that means either
867 * a. The same device disappeared and reappeared with
868 * different name. or
869 * b. The missing-disk-which-was-replaced, has
870 * reappeared now.
871 *
872 * We must allow 1 and 2a above. But 2b would be a spurious
873 * and unintentional.
874 *
875 * Further in case of 1 and 2a above, the disk at 'path'
876 * would have missed some transaction when it was away and
877 * in case of 2a the stale bdev has to be updated as well.
878 * 2b must not be allowed at all time.
879 */
880
881 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700882 * For now, we do allow update to btrfs_fs_device through the
883 * btrfs dev scan cli after FS has been mounted. We're still
884 * tracking a problem where systems fail mount by subvolume id
885 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800886 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700887 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800888 /*
889 * That is if the FS is _not_ mounted and if you
890 * are here, that means there is more than one
891 * disk with same uuid and devid.We keep the one
892 * with larger generation number or the last-in if
893 * generation are equal.
894 */
Anand Jain9c6d1732018-05-29 14:10:20 +0800895 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800896 return ERR_PTR(-EEXIST);
Anand Jain77bdae42014-07-03 18:22:06 +0800897 }
Anand Jainb96de002014-07-03 18:22:05 +0800898
Anand Jaina9261d42018-10-15 10:45:17 +0800899 /*
900 * We are going to replace the device path for a given devid,
901 * make sure it's the same device if the device is mounted
902 */
903 if (device->bdev) {
904 struct block_device *path_bdev;
905
906 path_bdev = lookup_bdev(path);
907 if (IS_ERR(path_bdev)) {
908 mutex_unlock(&fs_devices->device_list_mutex);
909 return ERR_CAST(path_bdev);
910 }
911
912 if (device->bdev != path_bdev) {
913 bdput(path_bdev);
914 mutex_unlock(&fs_devices->device_list_mutex);
915 btrfs_warn_in_rcu(device->fs_info,
916 "duplicate device fsid:devid for %pU:%llu old:%s new:%s",
917 disk_super->fsid, devid,
918 rcu_str_deref(device->name), path);
919 return ERR_PTR(-EEXIST);
920 }
921 bdput(path_bdev);
922 btrfs_info_in_rcu(device->fs_info,
923 "device fsid %pU devid %llu moved old:%s new:%s",
924 disk_super->fsid, devid,
925 rcu_str_deref(device->name), path);
926 }
927
Josef Bacik606686e2012-06-04 14:03:51 -0400928 name = rcu_string_strdup(path, GFP_NOFS);
Anand Jain9c6d1732018-05-29 14:10:20 +0800929 if (!name) {
930 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800931 return ERR_PTR(-ENOMEM);
Anand Jain9c6d1732018-05-29 14:10:20 +0800932 }
Josef Bacik606686e2012-06-04 14:03:51 -0400933 rcu_string_free(device->name);
934 rcu_assign_pointer(device->name, name);
Anand Jaine6e674b2017-12-04 12:54:54 +0800935 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -0500936 fs_devices->missing_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +0800937 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -0500938 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400939 }
940
Anand Jain77bdae42014-07-03 18:22:06 +0800941 /*
942 * Unmount does not free the btrfs_device struct but would zero
943 * generation along with most of the other members. So just update
944 * it back. We need it to pick the disk with largest generation
945 * (as above).
946 */
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200947 if (!fs_devices->opened) {
Anand Jain77bdae42014-07-03 18:22:06 +0800948 device->generation = found_transid;
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200949 fs_devices->latest_generation = max_t(u64, found_transid,
950 fs_devices->latest_generation);
951 }
Anand Jain77bdae42014-07-03 18:22:06 +0800952
Anand Jainf2788d22018-01-18 22:02:34 +0800953 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
954
Anand Jain9c6d1732018-05-29 14:10:20 +0800955 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800956 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400957}
958
Yan Zhenge4404d62008-12-12 10:03:26 -0500959static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
960{
961 struct btrfs_fs_devices *fs_devices;
962 struct btrfs_device *device;
963 struct btrfs_device *orig_dev;
964
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200965 fs_devices = alloc_fs_devices(orig->fsid, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300966 if (IS_ERR(fs_devices))
967 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500968
Miao Xieadbbb862014-09-03 21:35:42 +0800969 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400970 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500971
Xiao Guangrong46224702011-04-20 10:08:47 +0000972 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500973 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400974 struct rcu_string *name;
975
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300976 device = btrfs_alloc_device(NULL, &orig_dev->devid,
977 orig_dev->uuid);
978 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500979 goto error;
980
Josef Bacik606686e2012-06-04 14:03:51 -0400981 /*
982 * This is ok to do without rcu read locked because we hold the
983 * uuid mutex so nothing we touch in here is going to disappear.
984 */
Anand Jaine755f782014-06-30 17:12:47 +0800985 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +0100986 name = rcu_string_strdup(orig_dev->name->str,
987 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +0800988 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +0100989 btrfs_free_device(device);
Anand Jaine755f782014-06-30 17:12:47 +0800990 goto error;
991 }
992 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400993 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500994
Yan Zhenge4404d62008-12-12 10:03:26 -0500995 list_add(&device->dev_list, &fs_devices->devices);
996 device->fs_devices = fs_devices;
997 fs_devices->num_devices++;
998 }
Miao Xieadbbb862014-09-03 21:35:42 +0800999 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001000 return fs_devices;
1001error:
Miao Xieadbbb862014-09-03 21:35:42 +08001002 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001003 free_fs_devices(fs_devices);
1004 return ERR_PTR(-ENOMEM);
1005}
1006
Anand Jain9b99b112018-02-27 12:41:59 +08001007/*
1008 * After we have read the system tree and know devids belonging to
1009 * this filesystem, remove the device which does not belong there.
1010 */
1011void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -04001012{
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001013 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +08001014 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001015
Chris Masondfe25022008-05-13 13:46:40 -04001016 mutex_lock(&uuid_mutex);
1017again:
Xiao Guangrong46224702011-04-20 10:08:47 +00001018 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001019 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +08001020 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1021 &device->dev_state)) {
Anand Jain401e29c2017-12-04 12:54:55 +08001022 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1023 &device->dev_state) &&
1024 (!latest_dev ||
1025 device->generation > latest_dev->generation)) {
Miao Xie443f24f2014-07-24 11:37:15 +08001026 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001027 }
Yan Zheng2b820322008-11-17 21:11:30 -05001028 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001029 }
Yan Zheng2b820322008-11-17 21:11:30 -05001030
Stefan Behrens8dabb742012-11-06 13:15:27 +01001031 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
1032 /*
1033 * In the first step, keep the device which has
1034 * the correct fsid and the devid that is used
1035 * for the dev_replace procedure.
1036 * In the second step, the dev_replace state is
1037 * read from the device tree and it is known
1038 * whether the procedure is really active or
1039 * not, which means whether this device is
1040 * used or whether it should be removed.
1041 */
Anand Jain401e29c2017-12-04 12:54:55 +08001042 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1043 &device->dev_state)) {
Stefan Behrens8dabb742012-11-06 13:15:27 +01001044 continue;
1045 }
1046 }
Yan Zheng2b820322008-11-17 21:11:30 -05001047 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +01001048 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -05001049 device->bdev = NULL;
1050 fs_devices->open_devices--;
1051 }
Anand Jainebbede42017-12-04 12:54:52 +08001052 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001053 list_del_init(&device->dev_alloc_list);
Anand Jainebbede42017-12-04 12:54:52 +08001054 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08001055 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1056 &device->dev_state))
Stefan Behrens8dabb742012-11-06 13:15:27 +01001057 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001058 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001059 list_del_init(&device->dev_list);
1060 fs_devices->num_devices--;
David Sterbaa425f9d2018-03-20 15:47:33 +01001061 btrfs_free_device(device);
Chris Masondfe25022008-05-13 13:46:40 -04001062 }
Yan Zheng2b820322008-11-17 21:11:30 -05001063
1064 if (fs_devices->seed) {
1065 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001066 goto again;
1067 }
1068
Miao Xie443f24f2014-07-24 11:37:15 +08001069 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001070
Chris Masondfe25022008-05-13 13:46:40 -04001071 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001072}
Chris Masona0af4692008-05-13 16:03:06 -04001073
David Sterbaf06c5962017-06-06 17:08:23 +02001074static void free_device_rcu(struct rcu_head *head)
Xiao Guangrong1f781602011-04-20 10:09:16 +00001075{
1076 struct btrfs_device *device;
1077
1078 device = container_of(head, struct btrfs_device, rcu);
David Sterbaa425f9d2018-03-20 15:47:33 +01001079 btrfs_free_device(device);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001080}
1081
Anand Jain14238812016-07-22 06:04:53 +08001082static void btrfs_close_bdev(struct btrfs_device *device)
1083{
David Sterba08ffcae2017-06-19 16:55:35 +02001084 if (!device->bdev)
1085 return;
1086
Anand Jainebbede42017-12-04 12:54:52 +08001087 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Anand Jain14238812016-07-22 06:04:53 +08001088 sync_blockdev(device->bdev);
1089 invalidate_bdev(device->bdev);
1090 }
1091
David Sterba08ffcae2017-06-19 16:55:35 +02001092 blkdev_put(device->bdev, device->mode);
Anand Jain14238812016-07-22 06:04:53 +08001093}
1094
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001095static void btrfs_close_one_device(struct btrfs_device *device)
Anand Jainf448341a2016-06-14 18:55:25 +08001096{
1097 struct btrfs_fs_devices *fs_devices = device->fs_devices;
1098 struct btrfs_device *new_device;
1099 struct rcu_string *name;
1100
1101 if (device->bdev)
1102 fs_devices->open_devices--;
1103
Anand Jainebbede42017-12-04 12:54:52 +08001104 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jainf448341a2016-06-14 18:55:25 +08001105 device->devid != BTRFS_DEV_REPLACE_DEVID) {
1106 list_del_init(&device->dev_alloc_list);
1107 fs_devices->rw_devices--;
1108 }
1109
Anand Jaine6e674b2017-12-04 12:54:54 +08001110 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jainf448341a2016-06-14 18:55:25 +08001111 fs_devices->missing_devices--;
1112
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001113 btrfs_close_bdev(device);
1114
Anand Jainf448341a2016-06-14 18:55:25 +08001115 new_device = btrfs_alloc_device(NULL, &device->devid,
1116 device->uuid);
1117 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
1118
1119 /* Safe because we are under uuid_mutex */
1120 if (device->name) {
1121 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1122 BUG_ON(!name); /* -ENOMEM */
1123 rcu_assign_pointer(new_device->name, name);
1124 }
1125
1126 list_replace_rcu(&device->dev_list, &new_device->dev_list);
1127 new_device->fs_devices = device->fs_devices;
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001128
1129 call_rcu(&device->rcu, free_device_rcu);
Anand Jainf448341a2016-06-14 18:55:25 +08001130}
1131
Anand Jain0226e0e2018-04-12 10:29:27 +08001132static int close_fs_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001133{
Sasha Levin2037a092015-05-12 19:31:37 -04001134 struct btrfs_device *device, *tmp;
Yan Zhenge4404d62008-12-12 10:03:26 -05001135
Yan Zheng2b820322008-11-17 21:11:30 -05001136 if (--fs_devices->opened > 0)
1137 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001138
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001139 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -04001140 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001141 btrfs_close_one_device(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001142 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001143 mutex_unlock(&fs_devices->device_list_mutex);
1144
Yan Zhenge4404d62008-12-12 10:03:26 -05001145 WARN_ON(fs_devices->open_devices);
1146 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001147 fs_devices->opened = 0;
1148 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001149
Chris Mason8a4b83c2008-03-24 15:02:07 -04001150 return 0;
1151}
1152
Yan Zheng2b820322008-11-17 21:11:30 -05001153int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1154{
Yan Zhenge4404d62008-12-12 10:03:26 -05001155 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001156 int ret;
1157
1158 mutex_lock(&uuid_mutex);
Anand Jain0226e0e2018-04-12 10:29:27 +08001159 ret = close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001160 if (!fs_devices->opened) {
1161 seed_devices = fs_devices->seed;
1162 fs_devices->seed = NULL;
1163 }
Yan Zheng2b820322008-11-17 21:11:30 -05001164 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001165
1166 while (seed_devices) {
1167 fs_devices = seed_devices;
1168 seed_devices = fs_devices->seed;
Anand Jain0226e0e2018-04-12 10:29:27 +08001169 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001170 free_fs_devices(fs_devices);
1171 }
Yan Zheng2b820322008-11-17 21:11:30 -05001172 return ret;
1173}
1174
Anand Jain897fb572018-04-12 10:29:28 +08001175static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
Yan Zhenge4404d62008-12-12 10:03:26 -05001176 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001177{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001178 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +08001179 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -04001180 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001181
Tejun Heod4d77622010-11-13 11:55:18 +01001182 flags |= FMODE_EXCL;
1183
Anand Jainf117e292018-04-12 10:29:26 +08001184 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Eric Sandeenf63e0cc2013-04-04 20:45:08 +00001185 /* Just open everything we can; ignore failures here */
Anand Jain0fb08bc2017-11-09 23:45:24 +08001186 if (btrfs_open_one_device(fs_devices, device, flags, holder))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001187 continue;
Chris Masona0af4692008-05-13 16:03:06 -04001188
Anand Jain9f050db2017-11-09 23:45:25 +08001189 if (!latest_dev ||
1190 device->generation > latest_dev->generation)
1191 latest_dev = device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001192 }
Chris Masona0af4692008-05-13 16:03:06 -04001193 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +03001194 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -04001195 goto out;
1196 }
Yan Zheng2b820322008-11-17 21:11:30 -05001197 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +08001198 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -05001199 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -04001200out:
Yan Zheng2b820322008-11-17 21:11:30 -05001201 return ret;
1202}
1203
Anand Jainf8e10cd2018-01-22 14:49:36 -08001204static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1205{
1206 struct btrfs_device *dev1, *dev2;
1207
1208 dev1 = list_entry(a, struct btrfs_device, dev_list);
1209 dev2 = list_entry(b, struct btrfs_device, dev_list);
1210
1211 if (dev1->devid < dev2->devid)
1212 return -1;
1213 else if (dev1->devid > dev2->devid)
1214 return 1;
1215 return 0;
1216}
1217
Yan Zheng2b820322008-11-17 21:11:30 -05001218int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001219 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001220{
1221 int ret;
1222
David Sterbaf5194e32018-06-19 17:09:47 +02001223 lockdep_assert_held(&uuid_mutex);
1224
Anand Jain542c5902018-04-12 10:29:34 +08001225 mutex_lock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001226 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001227 fs_devices->opened++;
1228 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001229 } else {
Anand Jainf8e10cd2018-01-22 14:49:36 -08001230 list_sort(NULL, &fs_devices->devices, devid_cmp);
Anand Jain897fb572018-04-12 10:29:28 +08001231 ret = open_fs_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001232 }
Anand Jain542c5902018-04-12 10:29:34 +08001233 mutex_unlock(&fs_devices->device_list_mutex);
1234
Chris Mason8a4b83c2008-03-24 15:02:07 -04001235 return ret;
1236}
1237
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001238static void btrfs_release_disk_super(struct page *page)
Anand Jain6cf86a002016-02-13 10:01:29 +08001239{
1240 kunmap(page);
1241 put_page(page);
1242}
1243
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001244static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1245 struct page **page,
1246 struct btrfs_super_block **disk_super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001247{
1248 void *p;
1249 pgoff_t index;
1250
1251 /* make sure our super fits in the device */
1252 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1253 return 1;
1254
1255 /* make sure our super fits in the page */
1256 if (sizeof(**disk_super) > PAGE_SIZE)
1257 return 1;
1258
1259 /* make sure our super doesn't straddle pages on disk */
1260 index = bytenr >> PAGE_SHIFT;
1261 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1262 return 1;
1263
1264 /* pull in the page with our super */
1265 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1266 index, GFP_KERNEL);
1267
1268 if (IS_ERR_OR_NULL(*page))
1269 return 1;
1270
1271 p = kmap(*page);
1272
1273 /* align our pointer to the offset of the super block */
1274 *disk_super = p + (bytenr & ~PAGE_MASK);
1275
1276 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1277 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1278 btrfs_release_disk_super(*page);
1279 return 1;
1280 }
1281
1282 if ((*disk_super)->label[0] &&
1283 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1284 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1285
1286 return 0;
1287}
1288
David Sterba6f60cbd2013-02-15 11:31:02 -07001289/*
1290 * Look for a btrfs signature on a device. This may be called out of the mount path
1291 * and we are not allowed to call set_blocksize during the scan. The superblock
1292 * is read via pagecache
1293 */
Gu Jinxiang36350e92018-07-12 14:23:16 +08001294struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,
1295 void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001296{
1297 struct btrfs_super_block *disk_super;
Anand Jain4306a972018-05-29 12:28:37 +08001298 bool new_device_added = false;
Gu Jinxiang36350e92018-07-12 14:23:16 +08001299 struct btrfs_device *device = NULL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001300 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001301 struct page *page;
David Sterba6f60cbd2013-02-15 11:31:02 -07001302 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001303
David Sterba899f9302018-06-19 16:37:36 +02001304 lockdep_assert_held(&uuid_mutex);
1305
David Sterba6f60cbd2013-02-15 11:31:02 -07001306 /*
1307 * we would like to check all the supers, but that would make
1308 * a btrfs mount succeed after a mkfs from a different FS.
1309 * So, we need to add a special mount option to scan for
1310 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1311 */
1312 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001313 flags |= FMODE_EXCL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001314
1315 bdev = blkdev_get_by_path(path, flags, holder);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001316 if (IS_ERR(bdev))
Gu Jinxiang36350e92018-07-12 14:23:16 +08001317 return ERR_CAST(bdev);
David Sterba6f60cbd2013-02-15 11:31:02 -07001318
Anand Jain05a5c552017-12-15 15:40:16 +08001319 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
Gu Jinxiang36350e92018-07-12 14:23:16 +08001320 device = ERR_PTR(-EINVAL);
David Sterba6f60cbd2013-02-15 11:31:02 -07001321 goto error_bdev_put;
Anand Jain05a5c552017-12-15 15:40:16 +08001322 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001323
Anand Jain4306a972018-05-29 12:28:37 +08001324 device = device_list_add(path, disk_super, &new_device_added);
Gu Jinxiang36350e92018-07-12 14:23:16 +08001325 if (!IS_ERR(device)) {
Anand Jain4306a972018-05-29 12:28:37 +08001326 if (new_device_added)
1327 btrfs_free_stale_devices(path, device);
1328 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001329
Anand Jain6cf86a002016-02-13 10:01:29 +08001330 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001331
1332error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001333 blkdev_put(bdev, flags);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001334
Gu Jinxiang36350e92018-07-12 14:23:16 +08001335 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001336}
Chris Mason0b86a832008-03-24 15:01:56 -04001337
Jeff Mahoney499f3772015-06-15 09:41:17 -04001338static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001339 struct btrfs_device *device,
1340 u64 *start, u64 len)
1341{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001342 struct btrfs_fs_info *fs_info = device->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001343 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001344 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001345 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001346 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001347
Jeff Mahoney499f3772015-06-15 09:41:17 -04001348 if (transaction)
1349 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001350again:
1351 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001352 struct map_lookup *map;
1353 int i;
1354
Jeff Mahoney95617d62015-06-03 10:55:48 -04001355 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04001356 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001357 u64 end;
1358
Josef Bacik6df9a952013-06-27 13:22:46 -04001359 if (map->stripes[i].dev != device)
1360 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001361 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001362 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001363 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001364 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001365 /*
1366 * Make sure that while processing the pinned list we do
1367 * not override our *start with a lower value, because
1368 * we can have pinned chunks that fall within this
1369 * device hole and that have lower physical addresses
1370 * than the pending chunks we processed before. If we
1371 * do not take this special care we can end up getting
1372 * 2 pending chunks that start at the same physical
1373 * device offsets because the end offset of a pinned
1374 * chunk can be equal to the start offset of some
1375 * pending chunk.
1376 */
1377 end = map->stripes[i].physical + em->orig_block_len;
1378 if (end > *start) {
1379 *start = end;
1380 ret = 1;
1381 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001382 }
1383 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001384 if (search_list != &fs_info->pinned_chunks) {
1385 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001386 goto again;
1387 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001388
1389 return ret;
1390}
1391
1392
Chris Mason0b86a832008-03-24 15:01:56 -04001393/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001394 * find_free_dev_extent_start - find free space in the specified device
1395 * @device: the device which we search the free space in
1396 * @num_bytes: the size of the free space that we need
1397 * @search_start: the position from which to begin the search
1398 * @start: store the start of the free space.
1399 * @len: the size of the free space. that we find, or the size
1400 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001401 *
Chris Mason0b86a832008-03-24 15:01:56 -04001402 * this uses a pretty simple search, the expectation is that it is
1403 * called very infrequently and that a given device has a small number
1404 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001405 *
1406 * @start is used to store the start of the free space if we find. But if we
1407 * don't find suitable free space, it will be used to store the start position
1408 * of the max free space.
1409 *
1410 * @len is used to store the size of the free space that we find.
1411 * But if we don't find suitable free space, it is used to store the size of
1412 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001413 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001414int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1415 struct btrfs_device *device, u64 num_bytes,
1416 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001417{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001418 struct btrfs_fs_info *fs_info = device->fs_info;
1419 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001420 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001421 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001422 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001423 u64 hole_size;
1424 u64 max_hole_start;
1425 u64 max_hole_size;
1426 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001427 u64 search_end = device->total_bytes;
1428 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001429 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001430 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001431
1432 /*
1433 * We don't want to overwrite the superblock on the drive nor any area
1434 * used by the boot loader (grub for example), so we make sure to start
1435 * at an offset of at least 1MB.
1436 */
David Sterba0d0c71b2017-06-15 01:30:06 +02001437 search_start = max_t(u64, search_start, SZ_1M);
Chris Mason0b86a832008-03-24 15:01:56 -04001438
Josef Bacik6df9a952013-06-27 13:22:46 -04001439 path = btrfs_alloc_path();
1440 if (!path)
1441 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001442
Miao Xie7bfc8372011-01-05 10:07:26 +00001443 max_hole_start = search_start;
1444 max_hole_size = 0;
1445
Zhao Leif2ab7612015-02-16 18:52:17 +08001446again:
Anand Jain401e29c2017-12-04 12:54:55 +08001447 if (search_start >= search_end ||
1448 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001449 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001450 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001451 }
1452
David Sterbae4058b52015-11-27 16:31:35 +01001453 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001454 path->search_commit_root = 1;
1455 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001456
Chris Mason0b86a832008-03-24 15:01:56 -04001457 key.objectid = device->devid;
1458 key.offset = search_start;
1459 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001460
Li Zefan125ccb02011-12-08 15:07:24 +08001461 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001462 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001463 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001464 if (ret > 0) {
1465 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1466 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001467 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001468 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001469
Chris Mason0b86a832008-03-24 15:01:56 -04001470 while (1) {
1471 l = path->nodes[0];
1472 slot = path->slots[0];
1473 if (slot >= btrfs_header_nritems(l)) {
1474 ret = btrfs_next_leaf(root, path);
1475 if (ret == 0)
1476 continue;
1477 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001478 goto out;
1479
1480 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001481 }
1482 btrfs_item_key_to_cpu(l, &key, slot);
1483
1484 if (key.objectid < device->devid)
1485 goto next;
1486
1487 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001488 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001489
David Sterba962a2982014-06-04 18:41:45 +02001490 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001491 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001492
Miao Xie7bfc8372011-01-05 10:07:26 +00001493 if (key.offset > search_start) {
1494 hole_size = key.offset - search_start;
1495
Josef Bacik6df9a952013-06-27 13:22:46 -04001496 /*
1497 * Have to check before we set max_hole_start, otherwise
1498 * we could end up sending back this offset anyway.
1499 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001500 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001501 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001502 hole_size)) {
1503 if (key.offset >= search_start) {
1504 hole_size = key.offset - search_start;
1505 } else {
1506 WARN_ON_ONCE(1);
1507 hole_size = 0;
1508 }
1509 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001510
Miao Xie7bfc8372011-01-05 10:07:26 +00001511 if (hole_size > max_hole_size) {
1512 max_hole_start = search_start;
1513 max_hole_size = hole_size;
1514 }
1515
1516 /*
1517 * If this free space is greater than which we need,
1518 * it must be the max free space that we have found
1519 * until now, so max_hole_start must point to the start
1520 * of this free space and the length of this free space
1521 * is stored in max_hole_size. Thus, we return
1522 * max_hole_start and max_hole_size and go back to the
1523 * caller.
1524 */
1525 if (hole_size >= num_bytes) {
1526 ret = 0;
1527 goto out;
1528 }
1529 }
1530
Chris Mason0b86a832008-03-24 15:01:56 -04001531 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001532 extent_end = key.offset + btrfs_dev_extent_length(l,
1533 dev_extent);
1534 if (extent_end > search_start)
1535 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001536next:
1537 path->slots[0]++;
1538 cond_resched();
1539 }
Chris Mason0b86a832008-03-24 15:01:56 -04001540
liubo38c01b92011-08-02 02:39:03 +00001541 /*
1542 * At this point, search_start should be the end of
1543 * allocated dev extents, and when shrinking the device,
1544 * search_end may be smaller than search_start.
1545 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001546 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001547 hole_size = search_end - search_start;
1548
Jeff Mahoney499f3772015-06-15 09:41:17 -04001549 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001550 hole_size)) {
1551 btrfs_release_path(path);
1552 goto again;
1553 }
Chris Mason0b86a832008-03-24 15:01:56 -04001554
Zhao Leif2ab7612015-02-16 18:52:17 +08001555 if (hole_size > max_hole_size) {
1556 max_hole_start = search_start;
1557 max_hole_size = hole_size;
1558 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001559 }
1560
Miao Xie7bfc8372011-01-05 10:07:26 +00001561 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001562 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001563 ret = -ENOSPC;
1564 else
1565 ret = 0;
1566
1567out:
Yan Zheng2b820322008-11-17 21:11:30 -05001568 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001569 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001570 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001571 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001572 return ret;
1573}
1574
Jeff Mahoney499f3772015-06-15 09:41:17 -04001575int find_free_dev_extent(struct btrfs_trans_handle *trans,
1576 struct btrfs_device *device, u64 num_bytes,
1577 u64 *start, u64 *len)
1578{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001579 /* FIXME use last free of some kind */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001580 return find_free_dev_extent_start(trans->transaction, device,
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001581 num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001582}
1583
Christoph Hellwigb2950862008-12-02 09:54:17 -05001584static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001585 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001586 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001587{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001588 struct btrfs_fs_info *fs_info = device->fs_info;
1589 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001590 int ret;
1591 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001592 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001593 struct btrfs_key found_key;
1594 struct extent_buffer *leaf = NULL;
1595 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001596
1597 path = btrfs_alloc_path();
1598 if (!path)
1599 return -ENOMEM;
1600
1601 key.objectid = device->devid;
1602 key.offset = start;
1603 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001604again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001605 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001606 if (ret > 0) {
1607 ret = btrfs_previous_item(root, path, key.objectid,
1608 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001609 if (ret)
1610 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001611 leaf = path->nodes[0];
1612 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1613 extent = btrfs_item_ptr(leaf, path->slots[0],
1614 struct btrfs_dev_extent);
1615 BUG_ON(found_key.offset > start || found_key.offset +
1616 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001617 key = found_key;
1618 btrfs_release_path(path);
1619 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001620 } else if (ret == 0) {
1621 leaf = path->nodes[0];
1622 extent = btrfs_item_ptr(leaf, path->slots[0],
1623 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001624 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001625 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001626 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001627 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001628
Miao Xie2196d6e2014-09-03 21:35:41 +08001629 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1630
Chris Mason8f18cf12008-04-25 16:53:30 -04001631 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001632 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001633 btrfs_handle_fs_error(fs_info, ret,
1634 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001635 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001636 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001637 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001638out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001639 btrfs_free_path(path);
1640 return ret;
1641}
1642
Eric Sandeen48a3b632013-04-25 20:41:01 +00001643static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1644 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001645 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001646{
1647 int ret;
1648 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001649 struct btrfs_fs_info *fs_info = device->fs_info;
1650 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001651 struct btrfs_dev_extent *extent;
1652 struct extent_buffer *leaf;
1653 struct btrfs_key key;
1654
Anand Jaine12c9622017-12-04 12:54:53 +08001655 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
Anand Jain401e29c2017-12-04 12:54:55 +08001656 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
Chris Mason0b86a832008-03-24 15:01:56 -04001657 path = btrfs_alloc_path();
1658 if (!path)
1659 return -ENOMEM;
1660
Chris Mason0b86a832008-03-24 15:01:56 -04001661 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001662 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001663 key.type = BTRFS_DEV_EXTENT_KEY;
1664 ret = btrfs_insert_empty_item(trans, root, path, &key,
1665 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001666 if (ret)
1667 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001668
1669 leaf = path->nodes[0];
1670 extent = btrfs_item_ptr(leaf, path->slots[0],
1671 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001672 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1673 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001674 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1675 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001676 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1677
Chris Mason0b86a832008-03-24 15:01:56 -04001678 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1679 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001680out:
Chris Mason0b86a832008-03-24 15:01:56 -04001681 btrfs_free_path(path);
1682 return ret;
1683}
1684
Josef Bacik6df9a952013-06-27 13:22:46 -04001685static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001686{
Josef Bacik6df9a952013-06-27 13:22:46 -04001687 struct extent_map_tree *em_tree;
1688 struct extent_map *em;
1689 struct rb_node *n;
1690 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001691
Josef Bacik6df9a952013-06-27 13:22:46 -04001692 em_tree = &fs_info->mapping_tree.map_tree;
1693 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08001694 n = rb_last(&em_tree->map.rb_root);
Josef Bacik6df9a952013-06-27 13:22:46 -04001695 if (n) {
1696 em = rb_entry(n, struct extent_map, rb_node);
1697 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001698 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001699 read_unlock(&em_tree->lock);
1700
Chris Mason0b86a832008-03-24 15:01:56 -04001701 return ret;
1702}
1703
Ilya Dryomov53f10652013-08-12 14:33:01 +03001704static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1705 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001706{
1707 int ret;
1708 struct btrfs_key key;
1709 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001710 struct btrfs_path *path;
1711
Yan Zheng2b820322008-11-17 21:11:30 -05001712 path = btrfs_alloc_path();
1713 if (!path)
1714 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001715
1716 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1717 key.type = BTRFS_DEV_ITEM_KEY;
1718 key.offset = (u64)-1;
1719
Ilya Dryomov53f10652013-08-12 14:33:01 +03001720 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001721 if (ret < 0)
1722 goto error;
1723
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001724 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001725
Ilya Dryomov53f10652013-08-12 14:33:01 +03001726 ret = btrfs_previous_item(fs_info->chunk_root, path,
1727 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001728 BTRFS_DEV_ITEM_KEY);
1729 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001730 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001731 } else {
1732 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1733 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001734 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001735 }
1736 ret = 0;
1737error:
Yan Zheng2b820322008-11-17 21:11:30 -05001738 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001739 return ret;
1740}
1741
1742/*
1743 * the device information is stored in the chunk root
1744 * the btrfs_device struct should be fully filled in
1745 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001746static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001747 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001748{
1749 int ret;
1750 struct btrfs_path *path;
1751 struct btrfs_dev_item *dev_item;
1752 struct extent_buffer *leaf;
1753 struct btrfs_key key;
1754 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001755
Chris Mason0b86a832008-03-24 15:01:56 -04001756 path = btrfs_alloc_path();
1757 if (!path)
1758 return -ENOMEM;
1759
Chris Mason0b86a832008-03-24 15:01:56 -04001760 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1761 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001762 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001763
Nikolay Borisov8e87e852018-07-20 19:37:47 +03001764 ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path,
1765 &key, sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001766 if (ret)
1767 goto out;
1768
1769 leaf = path->nodes[0];
1770 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1771
1772 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001773 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001774 btrfs_set_device_type(leaf, dev_item, device->type);
1775 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1776 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1777 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001778 btrfs_set_device_total_bytes(leaf, dev_item,
1779 btrfs_device_get_disk_total_bytes(device));
1780 btrfs_set_device_bytes_used(leaf, dev_item,
1781 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001782 btrfs_set_device_group(leaf, dev_item, 0);
1783 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1784 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001785 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001786
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001787 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001788 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001789 ptr = btrfs_device_fsid(dev_item);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02001790 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid,
1791 ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001792 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001793
Yan Zheng2b820322008-11-17 21:11:30 -05001794 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001795out:
1796 btrfs_free_path(path);
1797 return ret;
1798}
Chris Mason8f18cf12008-04-25 16:53:30 -04001799
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001800/*
1801 * Function to update ctime/mtime for a given device path.
1802 * Mainly used for ctime/mtime based probe like libblkid.
1803 */
David Sterbada353f62017-02-14 17:55:53 +01001804static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001805{
1806 struct file *filp;
1807
1808 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001809 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001810 return;
1811 file_update_time(filp);
1812 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001813}
1814
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001815static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
Chris Masona061fc82008-05-07 11:43:44 -04001816 struct btrfs_device *device)
1817{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001818 struct btrfs_root *root = fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001819 int ret;
1820 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001821 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001822 struct btrfs_trans_handle *trans;
1823
Chris Masona061fc82008-05-07 11:43:44 -04001824 path = btrfs_alloc_path();
1825 if (!path)
1826 return -ENOMEM;
1827
Yan, Zhenga22285a2010-05-16 10:48:46 -04001828 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001829 if (IS_ERR(trans)) {
1830 btrfs_free_path(path);
1831 return PTR_ERR(trans);
1832 }
Chris Masona061fc82008-05-07 11:43:44 -04001833 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1834 key.type = BTRFS_DEV_ITEM_KEY;
1835 key.offset = device->devid;
1836
1837 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001838 if (ret) {
1839 if (ret > 0)
1840 ret = -ENOENT;
1841 btrfs_abort_transaction(trans, ret);
1842 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001843 goto out;
1844 }
1845
1846 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001847 if (ret) {
1848 btrfs_abort_transaction(trans, ret);
1849 btrfs_end_transaction(trans);
1850 }
1851
Chris Masona061fc82008-05-07 11:43:44 -04001852out:
1853 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001854 if (!ret)
1855 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001856 return ret;
1857}
1858
David Sterba3cc31a02016-02-15 16:00:26 +01001859/*
1860 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1861 * filesystem. It's up to the caller to adjust that number regarding eg. device
1862 * replace.
1863 */
1864static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1865 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001866{
Chris Masona061fc82008-05-07 11:43:44 -04001867 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00001868 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01001869 int i;
Chris Masona061fc82008-05-07 11:43:44 -04001870
Miao Xiede98ced2013-01-29 10:13:12 +00001871 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001872 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00001873
Anand Jainbd45ffb2016-02-13 10:01:34 +08001874 all_avail = fs_info->avail_data_alloc_bits |
1875 fs_info->avail_system_alloc_bits |
1876 fs_info->avail_metadata_alloc_bits;
1877 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08001878
David Sterba418775a2016-02-15 16:28:14 +01001879 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
Anand Jain41a6e892018-04-25 19:01:43 +08001880 if (!(all_avail & btrfs_raid_array[i].bg_flag))
David Sterba418775a2016-02-15 16:28:14 +01001881 continue;
Chris Masona061fc82008-05-07 11:43:44 -04001882
David Sterba418775a2016-02-15 16:28:14 +01001883 if (num_devices < btrfs_raid_array[i].devs_min) {
Anand Jainf9fbcaa2018-04-25 19:01:44 +08001884 int ret = btrfs_raid_array[i].mindev_error;
Chris Masona061fc82008-05-07 11:43:44 -04001885
David Sterba418775a2016-02-15 16:28:14 +01001886 if (ret)
1887 return ret;
1888 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08001889 }
1890
1891 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001892}
1893
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001894static struct btrfs_device * btrfs_find_next_active_device(
1895 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08001896{
1897 struct btrfs_device *next_device;
1898
1899 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1900 if (next_device != device &&
Anand Jaine6e674b2017-12-04 12:54:54 +08001901 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
1902 && next_device->bdev)
Anand Jain88acff62016-05-03 17:44:43 +08001903 return next_device;
1904 }
1905
1906 return NULL;
1907}
1908
1909/*
1910 * Helper function to check if the given device is part of s_bdev / latest_bdev
1911 * and replace it with the provided or the next active device, in the context
1912 * where this function called, there should be always be another device (or
1913 * this_dev) which is active.
1914 */
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03001915void btrfs_assign_next_active_device(struct btrfs_device *device,
1916 struct btrfs_device *this_dev)
Anand Jain88acff62016-05-03 17:44:43 +08001917{
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03001918 struct btrfs_fs_info *fs_info = device->fs_info;
Anand Jain88acff62016-05-03 17:44:43 +08001919 struct btrfs_device *next_device;
1920
1921 if (this_dev)
1922 next_device = this_dev;
1923 else
1924 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1925 device);
1926 ASSERT(next_device);
1927
1928 if (fs_info->sb->s_bdev &&
1929 (fs_info->sb->s_bdev == device->bdev))
1930 fs_info->sb->s_bdev = next_device->bdev;
1931
1932 if (fs_info->fs_devices->latest_bdev == device->bdev)
1933 fs_info->fs_devices->latest_bdev = next_device->bdev;
1934}
1935
Anand Jain1da73962018-08-10 13:53:21 +08001936/*
1937 * Return btrfs_fs_devices::num_devices excluding the device that's being
1938 * currently replaced.
1939 */
1940static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info)
1941{
1942 u64 num_devices = fs_info->fs_devices->num_devices;
1943
1944 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
1945 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
1946 ASSERT(num_devices > 1);
1947 num_devices--;
1948 }
1949 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
1950
1951 return num_devices;
1952}
1953
David Sterbada353f62017-02-14 17:55:53 +01001954int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
1955 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08001956{
1957 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001958 struct btrfs_fs_devices *cur_devices;
Anand Jainb5185192018-04-12 10:29:30 +08001959 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001960 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001961 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001962
1963 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001964
Anand Jain1da73962018-08-10 13:53:21 +08001965 num_devices = btrfs_num_devices(fs_info);
Chris Masona061fc82008-05-07 11:43:44 -04001966
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001967 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08001968 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001969 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001970
Nikolay Borisova27a94c2018-09-03 12:46:14 +03001971 device = btrfs_find_device_by_devspec(fs_info, devid, device_path);
1972
1973 if (IS_ERR(device)) {
1974 if (PTR_ERR(device) == -ENOENT &&
1975 strcmp(device_path, "missing") == 0)
1976 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
1977 else
1978 ret = PTR_ERR(device);
Chris Masona061fc82008-05-07 11:43:44 -04001979 goto out;
Nikolay Borisova27a94c2018-09-03 12:46:14 +03001980 }
Yan Zheng2b820322008-11-17 21:11:30 -05001981
Omar Sandovaleede2bf2016-11-03 10:28:12 -07001982 if (btrfs_pinned_by_swapfile(fs_info, device)) {
1983 btrfs_warn_in_rcu(fs_info,
1984 "cannot remove device %s (devid %llu) due to active swapfile",
1985 rcu_str_deref(device->name), device->devid);
1986 ret = -ETXTBSY;
1987 goto out;
1988 }
1989
Anand Jain401e29c2017-12-04 12:54:55 +08001990 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Anand Jain183860f2013-05-17 10:52:45 +00001991 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08001992 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001993 }
1994
Anand Jainebbede42017-12-04 12:54:52 +08001995 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1996 fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001997 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08001998 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001999 }
2000
Anand Jainebbede42017-12-04 12:54:52 +08002001 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002002 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002003 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002004 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02002005 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002006 }
Chris Masona061fc82008-05-07 11:43:44 -04002007
Carey Underwoodd7901552013-03-04 16:37:06 -06002008 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002009 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06002010 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002011 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002012 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002013
Stefan Behrens63a212a2012-11-05 18:29:28 +01002014 /*
2015 * TODO: the superblock still includes this device in its num_devices
2016 * counter although write_all_supers() is not locked out. This
2017 * could give a filesystem state which requires a degraded mount.
2018 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002019 ret = btrfs_rm_dev_item(fs_info, device);
Chris Masona061fc82008-05-07 11:43:44 -04002020 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002021 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002022
Anand Jaine12c9622017-12-04 12:54:53 +08002023 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002024 btrfs_scrub_cancel_dev(fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04002025
2026 /*
2027 * the device list mutex makes sure that we don't change
2028 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01002029 * the device supers. Whoever is writing all supers, should
2030 * lock the device list mutex before getting the number of
2031 * devices in the super block (super_copy). Conversely,
2032 * whoever updates the number of devices in the super block
2033 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04002034 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002035
Anand Jain41a52a02018-04-12 10:29:31 +08002036 /*
2037 * In normal cases the cur_devices == fs_devices. But in case
2038 * of deleting a seed device, the cur_devices should point to
2039 * its own fs_devices listed under the fs_devices->seed.
2040 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002041 cur_devices = device->fs_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002042 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002043 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002044
Anand Jain41a52a02018-04-12 10:29:31 +08002045 cur_devices->num_devices--;
2046 cur_devices->total_devices--;
Anand Jainb4993e62018-07-03 17:07:23 +08002047 /* Update total_devices of the parent fs_devices if it's seed */
2048 if (cur_devices != fs_devices)
2049 fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05002050
Anand Jaine6e674b2017-12-04 12:54:54 +08002051 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jain41a52a02018-04-12 10:29:31 +08002052 cur_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05002053
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002054 btrfs_assign_next_active_device(device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05002055
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002056 if (device->bdev) {
Anand Jain41a52a02018-04-12 10:29:31 +08002057 cur_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002058 /* remove sysfs entry */
Anand Jainb5185192018-04-12 10:29:30 +08002059 btrfs_sysfs_rm_device_link(fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002060 }
Anand Jain99994cd2014-06-03 11:36:00 +08002061
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002062 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2063 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
Anand Jainb5185192018-04-12 10:29:30 +08002064 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002065
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002066 /*
2067 * at this point, the device is zero sized and detached from
2068 * the devices list. All that's left is to zero out the old
2069 * supers and free the device.
2070 */
Anand Jainebbede42017-12-04 12:54:52 +08002071 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002072 btrfs_scratch_superblocks(device->bdev, device->name->str);
2073
2074 btrfs_close_bdev(device);
David Sterbaf06c5962017-06-06 17:08:23 +02002075 call_rcu(&device->rcu, free_device_rcu);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002076
Xiao Guangrong1f781602011-04-20 10:09:16 +00002077 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002078 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002079 if (fs_devices->seed == cur_devices) {
2080 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05002081 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002082 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002083 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05002084 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00002085 cur_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002086 close_fs_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002087 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002088 }
2089
Chris Masona061fc82008-05-07 11:43:44 -04002090out:
2091 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002092 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08002093
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002094error_undo:
Anand Jainebbede42017-12-04 12:54:52 +08002095 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002096 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002097 list_add(&device->dev_alloc_list,
Anand Jainb5185192018-04-12 10:29:30 +08002098 &fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002099 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002100 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002101 }
Anand Jain24fc5722016-02-13 10:01:36 +08002102 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002103}
2104
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002105void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002106{
Anand Jaind51908c2014-08-13 14:24:19 +08002107 struct btrfs_fs_devices *fs_devices;
2108
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002109 lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex);
Ilya Dryomov13572722013-10-02 20:41:01 +03002110
Anand Jain25e8e912014-08-20 10:56:56 +08002111 /*
2112 * in case of fs with no seed, srcdev->fs_devices will point
2113 * to fs_devices of fs_info. However when the dev being replaced is
2114 * a seed dev it will point to the seed's local fs_devices. In short
2115 * srcdev will have its correct fs_devices in both the cases.
2116 */
2117 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002118
Stefan Behrense93c89c2012-11-05 17:33:06 +01002119 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002120 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002121 fs_devices->num_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08002122 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
Anand Jaind51908c2014-08-13 14:24:19 +08002123 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002124
Anand Jainebbede42017-12-04 12:54:52 +08002125 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
Miao Xie82372bc2014-09-03 21:35:44 +08002126 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002127
Miao Xie82372bc2014-09-03 21:35:44 +08002128 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002129 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002130}
2131
2132void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2133 struct btrfs_device *srcdev)
2134{
2135 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002136
Anand Jainebbede42017-12-04 12:54:52 +08002137 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
Anand Jain48b3b9d2016-04-12 21:36:16 +08002138 /* zero out the old super if it is writable */
2139 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2140 }
Anand Jain14238812016-07-22 06:04:53 +08002141
2142 btrfs_close_bdev(srcdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002143 call_rcu(&srcdev->rcu, free_device_rcu);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002144
Anand Jain94d5f0c2014-08-13 14:24:22 +08002145 /* if this is no devs we rather delete the fs_devices */
2146 if (!fs_devices->num_devices) {
2147 struct btrfs_fs_devices *tmp_fs_devices;
2148
Anand Jain6dd38f82017-10-17 06:53:50 +08002149 /*
2150 * On a mounted FS, num_devices can't be zero unless it's a
2151 * seed. In case of a seed device being replaced, the replace
2152 * target added to the sprout FS, so there will be no more
2153 * device left under the seed FS.
2154 */
2155 ASSERT(fs_devices->seeding);
2156
Anand Jain94d5f0c2014-08-13 14:24:22 +08002157 tmp_fs_devices = fs_info->fs_devices;
2158 while (tmp_fs_devices) {
2159 if (tmp_fs_devices->seed == fs_devices) {
2160 tmp_fs_devices->seed = fs_devices->seed;
2161 break;
2162 }
2163 tmp_fs_devices = tmp_fs_devices->seed;
2164 }
2165 fs_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002166 close_fs_devices(fs_devices);
Anand Jain8bef8402014-08-13 14:24:23 +08002167 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002168 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002169}
2170
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002171void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002172{
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002173 struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices;
Anand Jaind2ff1b22015-03-10 06:38:42 +08002174
Anand Jaind9a071f2018-04-12 10:29:38 +08002175 WARN_ON(!tgtdev);
2176 mutex_lock(&fs_devices->device_list_mutex);
2177
2178 btrfs_sysfs_rm_device_link(fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002179
Anand Jain779bf3fe2016-04-18 16:51:23 +08002180 if (tgtdev->bdev)
Anand Jaind9a071f2018-04-12 10:29:38 +08002181 fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002182
Anand Jaind9a071f2018-04-12 10:29:38 +08002183 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002184
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002185 btrfs_assign_next_active_device(tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002186
Stefan Behrense93c89c2012-11-05 17:33:06 +01002187 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002188
Anand Jaind9a071f2018-04-12 10:29:38 +08002189 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002190
2191 /*
2192 * The update_dev_time() with in btrfs_scratch_superblocks()
2193 * may lead to a call to btrfs_show_devname() which will try
2194 * to hold device_list_mutex. And here this device
2195 * is already out of device list, so we don't have to hold
2196 * the device_list_mutex lock.
2197 */
2198 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002199
2200 btrfs_close_bdev(tgtdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002201 call_rcu(&tgtdev->rcu, free_device_rcu);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002202}
2203
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002204static struct btrfs_device *btrfs_find_device_by_path(
2205 struct btrfs_fs_info *fs_info, const char *device_path)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002206{
2207 int ret = 0;
2208 struct btrfs_super_block *disk_super;
2209 u64 devid;
2210 u8 *dev_uuid;
2211 struct block_device *bdev;
2212 struct buffer_head *bh;
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002213 struct btrfs_device *device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002214
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002215 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002216 fs_info->bdev_holder, 0, &bdev, &bh);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002217 if (ret)
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002218 return ERR_PTR(ret);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002219 disk_super = (struct btrfs_super_block *)bh->b_data;
2220 devid = btrfs_stack_device_id(&disk_super->dev_item);
2221 dev_uuid = disk_super->dev_item.uuid;
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002222 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
2223 device = btrfs_find_device(fs_info, devid, dev_uuid,
2224 disk_super->metadata_uuid);
2225 else
2226 device = btrfs_find_device(fs_info, devid,
2227 dev_uuid, disk_super->fsid);
2228
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002229 brelse(bh);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002230 if (!device)
2231 device = ERR_PTR(-ENOENT);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002232 blkdev_put(bdev, FMODE_READ);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002233 return device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002234}
2235
Nikolay Borisov6c050402018-09-03 12:46:13 +03002236static struct btrfs_device *btrfs_find_device_missing_or_by_path(
2237 struct btrfs_fs_info *fs_info, const char *device_path)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002238{
Nikolay Borisov6c050402018-09-03 12:46:13 +03002239 struct btrfs_device *device = NULL;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002240 if (strcmp(device_path, "missing") == 0) {
2241 struct list_head *devices;
2242 struct btrfs_device *tmp;
2243
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002244 devices = &fs_info->fs_devices->devices;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002245 list_for_each_entry(tmp, devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +08002246 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2247 &tmp->dev_state) && !tmp->bdev) {
Nikolay Borisov6c050402018-09-03 12:46:13 +03002248 device = tmp;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002249 break;
2250 }
2251 }
2252
Nikolay Borisov6c050402018-09-03 12:46:13 +03002253 if (!device)
2254 return ERR_PTR(-ENOENT);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002255 } else {
Nikolay Borisov6c050402018-09-03 12:46:13 +03002256 device = btrfs_find_device_by_path(fs_info, device_path);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002257 }
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002258
Nikolay Borisov6c050402018-09-03 12:46:13 +03002259 return device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002260}
2261
Yan Zheng2b820322008-11-17 21:11:30 -05002262/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002263 * Lookup a device given by device id, or the path if the id is 0.
2264 */
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002265struct btrfs_device *btrfs_find_device_by_devspec(
2266 struct btrfs_fs_info *fs_info, u64 devid, const char *devpath)
Anand Jain24e04742016-02-13 10:01:35 +08002267{
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002268 struct btrfs_device *device;
Anand Jain24e04742016-02-13 10:01:35 +08002269
David Sterba5c5c0df2016-02-15 16:39:55 +01002270 if (devid) {
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002271 device = btrfs_find_device(fs_info, devid, NULL, NULL);
2272 if (!device)
2273 return ERR_PTR(-ENOENT);
Anand Jain24e04742016-02-13 10:01:35 +08002274 } else {
David Sterba5c5c0df2016-02-15 16:39:55 +01002275 if (!devpath || !devpath[0])
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002276 return ERR_PTR(-EINVAL);
2277 device = btrfs_find_device_missing_or_by_path(fs_info, devpath);
Anand Jain24e04742016-02-13 10:01:35 +08002278 }
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002279 return device;
Anand Jain24e04742016-02-13 10:01:35 +08002280}
2281
Yan Zheng2b820322008-11-17 21:11:30 -05002282/*
2283 * does all the dirty work required for changing file system's UUID.
2284 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002285static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002286{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002287 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002288 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002289 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002290 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002291 struct btrfs_device *device;
2292 u64 super_flags;
2293
David Sterbaa32bf9a2018-03-16 02:21:22 +01002294 lockdep_assert_held(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002295 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002296 return -EINVAL;
2297
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002298 seed_devices = alloc_fs_devices(NULL, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002299 if (IS_ERR(seed_devices))
2300 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002301
Yan Zhenge4404d62008-12-12 10:03:26 -05002302 old_devices = clone_fs_devices(fs_devices);
2303 if (IS_ERR(old_devices)) {
2304 kfree(seed_devices);
2305 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002306 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002307
Anand Jainc4babc52018-04-12 10:29:25 +08002308 list_add(&old_devices->fs_list, &fs_uuids);
Yan Zheng2b820322008-11-17 21:11:30 -05002309
Yan Zhenge4404d62008-12-12 10:03:26 -05002310 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2311 seed_devices->opened = 1;
2312 INIT_LIST_HEAD(&seed_devices->devices);
2313 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002314 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002315
Anand Jain321a4bf2018-07-16 22:58:09 +08002316 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002317 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2318 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002319 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002320 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002321
David Sterba34441362016-10-04 19:34:27 +02002322 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002323 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002324 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002325
Yan Zheng2b820322008-11-17 21:11:30 -05002326 fs_devices->seeding = 0;
2327 fs_devices->num_devices = 0;
2328 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002329 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002330 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002331 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002332
2333 generate_random_uuid(fs_devices->fsid);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002334 memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002335 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Anand Jain321a4bf2018-07-16 22:58:09 +08002336 mutex_unlock(&fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002337
Yan Zheng2b820322008-11-17 21:11:30 -05002338 super_flags = btrfs_super_flags(disk_super) &
2339 ~BTRFS_SUPER_FLAG_SEEDING;
2340 btrfs_set_super_flags(disk_super, super_flags);
2341
2342 return 0;
2343}
2344
2345/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002346 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002347 */
2348static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002349 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002350{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002351 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002352 struct btrfs_path *path;
2353 struct extent_buffer *leaf;
2354 struct btrfs_dev_item *dev_item;
2355 struct btrfs_device *device;
2356 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002357 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002358 u8 dev_uuid[BTRFS_UUID_SIZE];
2359 u64 devid;
2360 int ret;
2361
2362 path = btrfs_alloc_path();
2363 if (!path)
2364 return -ENOMEM;
2365
Yan Zheng2b820322008-11-17 21:11:30 -05002366 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2367 key.offset = 0;
2368 key.type = BTRFS_DEV_ITEM_KEY;
2369
2370 while (1) {
2371 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2372 if (ret < 0)
2373 goto error;
2374
2375 leaf = path->nodes[0];
2376next_slot:
2377 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2378 ret = btrfs_next_leaf(root, path);
2379 if (ret > 0)
2380 break;
2381 if (ret < 0)
2382 goto error;
2383 leaf = path->nodes[0];
2384 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002385 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002386 continue;
2387 }
2388
2389 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2390 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2391 key.type != BTRFS_DEV_ITEM_KEY)
2392 break;
2393
2394 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2395 struct btrfs_dev_item);
2396 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002397 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002398 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002399 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002400 BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002401 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002402 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002403
2404 if (device->fs_devices->seeding) {
2405 btrfs_set_device_generation(leaf, dev_item,
2406 device->generation);
2407 btrfs_mark_buffer_dirty(leaf);
2408 }
2409
2410 path->slots[0]++;
2411 goto next_slot;
2412 }
2413 ret = 0;
2414error:
2415 btrfs_free_path(path);
2416 return ret;
2417}
2418
David Sterbada353f62017-02-14 17:55:53 +01002419int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002420{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002421 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002422 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002423 struct btrfs_trans_handle *trans;
2424 struct btrfs_device *device;
2425 struct block_device *bdev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002426 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002427 struct rcu_string *name;
Anand Jain5da54bc2018-07-03 13:14:50 +08002428 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Naohiro Aota39379fa2018-07-27 09:04:55 +09002429 u64 orig_super_total_bytes;
2430 u64 orig_super_num_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002431 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002432 int ret = 0;
Anand Jain7132a262017-09-28 14:51:11 +08002433 bool unlocked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002434
Anand Jain5da54bc2018-07-03 13:14:50 +08002435 if (sb_rdonly(sb) && !fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002436 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002437
Li Zefana5d163332011-12-07 20:08:40 -05002438 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002439 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002440 if (IS_ERR(bdev))
2441 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002442
Anand Jain5da54bc2018-07-03 13:14:50 +08002443 if (fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002444 seeding_dev = 1;
2445 down_write(&sb->s_umount);
2446 mutex_lock(&uuid_mutex);
2447 }
2448
Chris Mason8c8bee12008-09-29 11:19:10 -04002449 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002450
Anand Jain5da54bc2018-07-03 13:14:50 +08002451 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain694c51f2018-07-03 13:14:51 +08002452 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002453 if (device->bdev == bdev) {
2454 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002455 mutex_unlock(
Anand Jain5da54bc2018-07-03 13:14:50 +08002456 &fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002457 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002458 }
2459 }
Anand Jain5da54bc2018-07-03 13:14:50 +08002460 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002461
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002462 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002463 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002464 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002465 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002466 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002467 }
2468
David Sterba78f2c9e2016-02-11 14:25:38 +01002469 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002470 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002471 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002472 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002473 }
Josef Bacik606686e2012-06-04 14:03:51 -04002474 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002475
Yan, Zhenga22285a2010-05-16 10:48:46 -04002476 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002477 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002478 ret = PTR_ERR(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002479 goto error_free_device;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002480 }
2481
Josef Bacikd5e20032011-08-04 14:52:27 +00002482 q = bdev_get_queue(bdev);
Anand Jainebbede42017-12-04 12:54:52 +08002483 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05002484 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002485 device->io_width = fs_info->sectorsize;
2486 device->io_align = fs_info->sectorsize;
2487 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002488 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2489 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002490 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002491 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002492 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002493 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002494 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002495 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002496 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002497 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002498 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002499
Yan Zheng2b820322008-11-17 21:11:30 -05002500 if (seeding_dev) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002501 sb->s_flags &= ~SB_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002502 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002503 if (ret) {
2504 btrfs_abort_transaction(trans, ret);
2505 goto error_trans;
2506 }
Yan Zheng2b820322008-11-17 21:11:30 -05002507 }
2508
Anand Jain5da54bc2018-07-03 13:14:50 +08002509 device->fs_devices = fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002510
Anand Jain5da54bc2018-07-03 13:14:50 +08002511 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002512 mutex_lock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002513 list_add_rcu(&device->dev_list, &fs_devices->devices);
2514 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
2515 fs_devices->num_devices++;
2516 fs_devices->open_devices++;
2517 fs_devices->rw_devices++;
2518 fs_devices->total_devices++;
2519 fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002520
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002521 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002522
Anand Jaine884f4f2017-04-04 18:40:19 +08002523 if (!blk_queue_nonrot(q))
Anand Jain5da54bc2018-07-03 13:14:50 +08002524 fs_devices->rotating = 1;
Chris Masonc2898112009-06-10 09:51:32 -04002525
Naohiro Aota39379fa2018-07-27 09:04:55 +09002526 orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002527 btrfs_set_super_total_bytes(fs_info->super_copy,
Naohiro Aota39379fa2018-07-27 09:04:55 +09002528 round_down(orig_super_total_bytes + device->total_bytes,
2529 fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002530
Naohiro Aota39379fa2018-07-27 09:04:55 +09002531 orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy);
2532 btrfs_set_super_num_devices(fs_info->super_copy,
2533 orig_super_num_devices + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002534
2535 /* add sysfs device entry */
Anand Jain5da54bc2018-07-03 13:14:50 +08002536 btrfs_sysfs_add_device_link(fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002537
Miao Xie2196d6e2014-09-03 21:35:41 +08002538 /*
2539 * we've got more storage, clear any full flags on the space
2540 * infos
2541 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002542 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002543
David Sterba34441362016-10-04 19:34:27 +02002544 mutex_unlock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002545 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002546
Yan Zheng2b820322008-11-17 21:11:30 -05002547 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002548 mutex_lock(&fs_info->chunk_mutex);
David Sterbae4a4dce2017-02-10 19:49:01 +01002549 ret = init_first_rw_device(trans, fs_info);
David Sterba34441362016-10-04 19:34:27 +02002550 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002551 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002552 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002553 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002554 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002555 }
2556
Nikolay Borisov8e87e852018-07-20 19:37:47 +03002557 ret = btrfs_add_dev_item(trans, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002558 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002559 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002560 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002561 }
2562
2563 if (seeding_dev) {
2564 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2565
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002566 ret = btrfs_finish_sprout(trans, fs_info);
David Sterba005d6422012-09-18 07:52:32 -06002567 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002568 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002569 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002570 }
Anand Jainb2373f22014-06-03 11:36:03 +08002571
2572 /* Sprouting would change fsid of the mounted root,
2573 * so rename the fsid on the sysfs
2574 */
2575 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
Nikolay Borisovde37aa52018-10-30 16:43:24 +02002576 fs_info->fs_devices->fsid);
Anand Jain5da54bc2018-07-03 13:14:50 +08002577 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002578 btrfs_warn(fs_info,
2579 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002580 }
2581
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002582 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002583
2584 if (seeding_dev) {
2585 mutex_unlock(&uuid_mutex);
2586 up_write(&sb->s_umount);
Anand Jain7132a262017-09-28 14:51:11 +08002587 unlocked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002588
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002589 if (ret) /* transaction commit */
2590 return ret;
2591
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002592 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002593 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002594 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002595 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002596 trans = btrfs_attach_transaction(root);
2597 if (IS_ERR(trans)) {
2598 if (PTR_ERR(trans) == -ENOENT)
2599 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002600 ret = PTR_ERR(trans);
2601 trans = NULL;
2602 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002603 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002604 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002605 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002606
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002607 /* Update ctime/mtime for libblkid */
2608 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002609 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002610
Anand Jaind31c32f2017-09-28 14:51:10 +08002611error_sysfs:
Anand Jain5da54bc2018-07-03 13:14:50 +08002612 btrfs_sysfs_rm_device_link(fs_devices, device);
Naohiro Aota39379fa2018-07-27 09:04:55 +09002613 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2614 mutex_lock(&fs_info->chunk_mutex);
2615 list_del_rcu(&device->dev_list);
2616 list_del(&device->dev_alloc_list);
2617 fs_info->fs_devices->num_devices--;
2618 fs_info->fs_devices->open_devices--;
2619 fs_info->fs_devices->rw_devices--;
2620 fs_info->fs_devices->total_devices--;
2621 fs_info->fs_devices->total_rw_bytes -= device->total_bytes;
2622 atomic64_sub(device->total_bytes, &fs_info->free_chunk_space);
2623 btrfs_set_super_total_bytes(fs_info->super_copy,
2624 orig_super_total_bytes);
2625 btrfs_set_super_num_devices(fs_info->super_copy,
2626 orig_super_num_devices);
2627 mutex_unlock(&fs_info->chunk_mutex);
2628 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002629error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002630 if (seeding_dev)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002631 sb->s_flags |= SB_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002632 if (trans)
2633 btrfs_end_transaction(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002634error_free_device:
David Sterbaa425f9d2018-03-20 15:47:33 +01002635 btrfs_free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002636error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002637 blkdev_put(bdev, FMODE_EXCL);
Anand Jain7132a262017-09-28 14:51:11 +08002638 if (seeding_dev && !unlocked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002639 mutex_unlock(&uuid_mutex);
2640 up_write(&sb->s_umount);
2641 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002642 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002643}
2644
Chris Masond3977122009-01-05 21:25:51 -05002645static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2646 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002647{
2648 int ret;
2649 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002650 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002651 struct btrfs_dev_item *dev_item;
2652 struct extent_buffer *leaf;
2653 struct btrfs_key key;
2654
Chris Mason0b86a832008-03-24 15:01:56 -04002655 path = btrfs_alloc_path();
2656 if (!path)
2657 return -ENOMEM;
2658
2659 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2660 key.type = BTRFS_DEV_ITEM_KEY;
2661 key.offset = device->devid;
2662
2663 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2664 if (ret < 0)
2665 goto out;
2666
2667 if (ret > 0) {
2668 ret = -ENOENT;
2669 goto out;
2670 }
2671
2672 leaf = path->nodes[0];
2673 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2674
2675 btrfs_set_device_id(leaf, dev_item, device->devid);
2676 btrfs_set_device_type(leaf, dev_item, device->type);
2677 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2678 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2679 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002680 btrfs_set_device_total_bytes(leaf, dev_item,
2681 btrfs_device_get_disk_total_bytes(device));
2682 btrfs_set_device_bytes_used(leaf, dev_item,
2683 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002684 btrfs_mark_buffer_dirty(leaf);
2685
2686out:
2687 btrfs_free_path(path);
2688 return ret;
2689}
2690
Miao Xie2196d6e2014-09-03 21:35:41 +08002691int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002692 struct btrfs_device *device, u64 new_size)
2693{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002694 struct btrfs_fs_info *fs_info = device->fs_info;
2695 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002696 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002697 u64 old_total;
2698 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002699
Anand Jainebbede42017-12-04 12:54:52 +08002700 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Yan Zheng2b820322008-11-17 21:11:30 -05002701 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002702
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002703 new_size = round_down(new_size, fs_info->sectorsize);
2704
David Sterba34441362016-10-04 19:34:27 +02002705 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002706 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002707 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002708
Stefan Behrens63a212a2012-11-05 18:29:28 +01002709 if (new_size <= device->total_bytes ||
Anand Jain401e29c2017-12-04 12:54:55 +08002710 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002711 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002712 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002713 }
Yan Zheng2b820322008-11-17 21:11:30 -05002714
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002715 fs_devices = fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002716
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002717 btrfs_set_super_total_bytes(super_copy,
2718 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002719 device->fs_devices->total_rw_bytes += diff;
2720
Miao Xie7cc8e582014-09-03 21:35:38 +08002721 btrfs_device_set_total_bytes(device, new_size);
2722 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002723 btrfs_clear_space_info_full(device->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002724 if (list_empty(&device->resized_list))
2725 list_add_tail(&device->resized_list,
2726 &fs_devices->resized_devices);
David Sterba34441362016-10-04 19:34:27 +02002727 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002728
Chris Mason8f18cf12008-04-25 16:53:30 -04002729 return btrfs_update_device(trans, device);
2730}
2731
Nikolay Borisovf4208792018-07-20 19:37:52 +03002732static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002733{
Nikolay Borisovf4208792018-07-20 19:37:52 +03002734 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002735 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002736 int ret;
2737 struct btrfs_path *path;
2738 struct btrfs_key key;
2739
Chris Mason8f18cf12008-04-25 16:53:30 -04002740 path = btrfs_alloc_path();
2741 if (!path)
2742 return -ENOMEM;
2743
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002744 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002745 key.offset = chunk_offset;
2746 key.type = BTRFS_CHUNK_ITEM_KEY;
2747
2748 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002749 if (ret < 0)
2750 goto out;
2751 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002752 btrfs_handle_fs_error(fs_info, -ENOENT,
2753 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002754 ret = -ENOENT;
2755 goto out;
2756 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002757
2758 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002759 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002760 btrfs_handle_fs_error(fs_info, ret,
2761 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002762out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002763 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002764 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002765}
2766
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002767static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002768{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002769 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002770 struct btrfs_disk_key *disk_key;
2771 struct btrfs_chunk *chunk;
2772 u8 *ptr;
2773 int ret = 0;
2774 u32 num_stripes;
2775 u32 array_size;
2776 u32 len = 0;
2777 u32 cur;
2778 struct btrfs_key key;
2779
David Sterba34441362016-10-04 19:34:27 +02002780 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002781 array_size = btrfs_super_sys_array_size(super_copy);
2782
2783 ptr = super_copy->sys_chunk_array;
2784 cur = 0;
2785
2786 while (cur < array_size) {
2787 disk_key = (struct btrfs_disk_key *)ptr;
2788 btrfs_disk_key_to_cpu(&key, disk_key);
2789
2790 len = sizeof(*disk_key);
2791
2792 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2793 chunk = (struct btrfs_chunk *)(ptr + len);
2794 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2795 len += btrfs_chunk_item_size(num_stripes);
2796 } else {
2797 ret = -EIO;
2798 break;
2799 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002800 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002801 key.offset == chunk_offset) {
2802 memmove(ptr, ptr + len, array_size - (cur + len));
2803 array_size -= len;
2804 btrfs_set_super_sys_array_size(super_copy, array_size);
2805 } else {
2806 ptr += len;
2807 cur += len;
2808 }
2809 }
David Sterba34441362016-10-04 19:34:27 +02002810 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002811 return ret;
2812}
2813
Omar Sandoval60ca8422018-05-16 16:34:31 -07002814/*
2815 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2816 * @logical: Logical block offset in bytes.
2817 * @length: Length of extent in bytes.
2818 *
2819 * Return: Chunk mapping or ERR_PTR.
2820 */
2821struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
2822 u64 logical, u64 length)
Liu Bo592d92e2017-03-14 13:33:55 -07002823{
2824 struct extent_map_tree *em_tree;
2825 struct extent_map *em;
2826
2827 em_tree = &fs_info->mapping_tree.map_tree;
2828 read_lock(&em_tree->lock);
2829 em = lookup_extent_mapping(em_tree, logical, length);
2830 read_unlock(&em_tree->lock);
2831
2832 if (!em) {
2833 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2834 logical, length);
2835 return ERR_PTR(-EINVAL);
2836 }
2837
2838 if (em->start > logical || em->start + em->len < logical) {
2839 btrfs_crit(fs_info,
2840 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2841 logical, length, em->start, em->start + em->len);
2842 free_extent_map(em);
2843 return ERR_PTR(-EINVAL);
2844 }
2845
2846 /* callers are responsible for dropping em's ref. */
2847 return em;
2848}
2849
Nikolay Borisov97aff912018-07-20 19:37:53 +03002850int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04002851{
Nikolay Borisov97aff912018-07-20 19:37:53 +03002852 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002853 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002854 struct map_lookup *map;
2855 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002856 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002857 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002858
Omar Sandoval60ca8422018-05-16 16:34:31 -07002859 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07002860 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04002861 /*
2862 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002863 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002864 * do anything we still error out.
2865 */
2866 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07002867 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002868 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002869 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02002870 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisov451a2c12018-06-20 15:49:07 +03002871 check_system_chunk(trans, map->type);
David Sterba34441362016-10-04 19:34:27 +02002872 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002873
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002874 /*
2875 * Take the device list mutex to prevent races with the final phase of
2876 * a device replace operation that replaces the device object associated
2877 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2878 */
2879 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002880 for (i = 0; i < map->num_stripes; i++) {
2881 struct btrfs_device *device = map->stripes[i].dev;
2882 ret = btrfs_free_dev_extent(trans, device,
2883 map->stripes[i].physical,
2884 &dev_extent_len);
2885 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002886 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002887 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002888 goto out;
2889 }
2890
2891 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02002892 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002893 btrfs_device_set_bytes_used(device,
2894 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002895 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002896 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02002897 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002898 }
2899
Nikolay Borisov64bc6c22018-10-26 14:43:19 +03002900 ret = btrfs_update_device(trans, device);
2901 if (ret) {
2902 mutex_unlock(&fs_devices->device_list_mutex);
2903 btrfs_abort_transaction(trans, ret);
2904 goto out;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002905 }
2906 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002907 mutex_unlock(&fs_devices->device_list_mutex);
2908
Nikolay Borisovf4208792018-07-20 19:37:52 +03002909 ret = btrfs_free_chunk(trans, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002910 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002911 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002912 goto out;
2913 }
2914
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002915 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002916
2917 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002918 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002919 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002920 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002921 goto out;
2922 }
2923 }
2924
Nikolay Borisov5a98ec02018-06-20 15:48:56 +03002925 ret = btrfs_remove_block_group(trans, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002926 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002927 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002928 goto out;
2929 }
2930
Josef Bacik47ab2a62014-09-18 11:20:02 -04002931out:
2932 /* once for us */
2933 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002934 return ret;
2935}
2936
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002937static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002938{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002939 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07002940 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002941 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002942
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002943 /*
2944 * Prevent races with automatic removal of unused block groups.
2945 * After we relocate and before we remove the chunk with offset
2946 * chunk_offset, automatic removal of the block group can kick in,
2947 * resulting in a failure when calling btrfs_remove_chunk() below.
2948 *
2949 * Make sure to acquire this mutex before doing a tree search (dev
2950 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2951 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2952 * we release the path used to search the chunk/dev tree and before
2953 * the current task acquires this mutex and calls us.
2954 */
David Sterbaa32bf9a2018-03-16 02:21:22 +01002955 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002956
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002957 ret = btrfs_can_relocate(fs_info, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002958 if (ret)
2959 return -ENOSPC;
2960
Chris Mason8f18cf12008-04-25 16:53:30 -04002961 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002962 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002963 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002964 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002965 if (ret)
2966 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002967
Jeff Mahoney75cb3792018-03-20 15:25:26 -04002968 /*
2969 * We add the kobjects here (and after forcing data chunk creation)
2970 * since relocation is the only place we'll create chunks of a new
2971 * type at runtime. The only place where we'll remove the last
2972 * chunk of a type is the call immediately below this one. Even
2973 * so, we're protected against races with the cleaner thread since
2974 * we're covered by the delete_unused_bgs_mutex.
2975 */
2976 btrfs_add_raid_kobjects(fs_info);
2977
Chris Mason19c4d2f2016-10-10 13:43:31 -07002978 trans = btrfs_start_trans_remove_block_group(root->fs_info,
2979 chunk_offset);
2980 if (IS_ERR(trans)) {
2981 ret = PTR_ERR(trans);
2982 btrfs_handle_fs_error(root->fs_info, ret, NULL);
2983 return ret;
2984 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09002985
Chris Mason19c4d2f2016-10-10 13:43:31 -07002986 /*
2987 * step two, delete the device extents and the
2988 * chunk tree entries
2989 */
Nikolay Borisov97aff912018-07-20 19:37:53 +03002990 ret = btrfs_remove_chunk(trans, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002991 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07002992 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002993}
2994
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002995static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002996{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002997 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002998 struct btrfs_path *path;
2999 struct extent_buffer *leaf;
3000 struct btrfs_chunk *chunk;
3001 struct btrfs_key key;
3002 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05003003 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04003004 bool retried = false;
3005 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05003006 int ret;
3007
3008 path = btrfs_alloc_path();
3009 if (!path)
3010 return -ENOMEM;
3011
Josef Bacikba1bf482009-09-11 16:11:19 -04003012again:
Yan Zheng2b820322008-11-17 21:11:30 -05003013 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3014 key.offset = (u64)-1;
3015 key.type = BTRFS_CHUNK_ITEM_KEY;
3016
3017 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003018 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003019 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003020 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003021 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003022 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003023 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003024 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05003025
3026 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3027 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003028 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003029 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003030 if (ret < 0)
3031 goto error;
3032 if (ret > 0)
3033 break;
3034
3035 leaf = path->nodes[0];
3036 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3037
3038 chunk = btrfs_item_ptr(leaf, path->slots[0],
3039 struct btrfs_chunk);
3040 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003041 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003042
3043 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003044 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003045 if (ret == -ENOSPC)
3046 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303047 else
3048 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003049 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003050 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003051
3052 if (found_key.offset == 0)
3053 break;
3054 key.offset = found_key.offset - 1;
3055 }
3056 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003057 if (failed && !retried) {
3058 failed = 0;
3059 retried = true;
3060 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303061 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003062 ret = -ENOSPC;
3063 }
Yan Zheng2b820322008-11-17 21:11:30 -05003064error:
3065 btrfs_free_path(path);
3066 return ret;
3067}
3068
Liu Boa6f93c72017-11-15 16:28:11 -07003069/*
3070 * return 1 : allocate a data chunk successfully,
3071 * return <0: errors during allocating a data chunk,
3072 * return 0 : no need to allocate a data chunk.
3073 */
3074static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3075 u64 chunk_offset)
3076{
3077 struct btrfs_block_group_cache *cache;
3078 u64 bytes_used;
3079 u64 chunk_type;
3080
3081 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3082 ASSERT(cache);
3083 chunk_type = cache->flags;
3084 btrfs_put_block_group(cache);
3085
3086 if (chunk_type & BTRFS_BLOCK_GROUP_DATA) {
3087 spin_lock(&fs_info->data_sinfo->lock);
3088 bytes_used = fs_info->data_sinfo->bytes_used;
3089 spin_unlock(&fs_info->data_sinfo->lock);
3090
3091 if (!bytes_used) {
3092 struct btrfs_trans_handle *trans;
3093 int ret;
3094
3095 trans = btrfs_join_transaction(fs_info->tree_root);
3096 if (IS_ERR(trans))
3097 return PTR_ERR(trans);
3098
Nikolay Borisov43a7e992018-06-20 15:49:15 +03003099 ret = btrfs_force_chunk_alloc(trans,
Liu Boa6f93c72017-11-15 16:28:11 -07003100 BTRFS_BLOCK_GROUP_DATA);
3101 btrfs_end_transaction(trans);
3102 if (ret < 0)
3103 return ret;
3104
Jeff Mahoney75cb3792018-03-20 15:25:26 -04003105 btrfs_add_raid_kobjects(fs_info);
3106
Liu Boa6f93c72017-11-15 16:28:11 -07003107 return 1;
3108 }
3109 }
3110 return 0;
3111}
3112
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003113static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003114 struct btrfs_balance_control *bctl)
3115{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003116 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003117 struct btrfs_trans_handle *trans;
3118 struct btrfs_balance_item *item;
3119 struct btrfs_disk_balance_args disk_bargs;
3120 struct btrfs_path *path;
3121 struct extent_buffer *leaf;
3122 struct btrfs_key key;
3123 int ret, err;
3124
3125 path = btrfs_alloc_path();
3126 if (!path)
3127 return -ENOMEM;
3128
3129 trans = btrfs_start_transaction(root, 0);
3130 if (IS_ERR(trans)) {
3131 btrfs_free_path(path);
3132 return PTR_ERR(trans);
3133 }
3134
3135 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003136 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003137 key.offset = 0;
3138
3139 ret = btrfs_insert_empty_item(trans, root, path, &key,
3140 sizeof(*item));
3141 if (ret)
3142 goto out;
3143
3144 leaf = path->nodes[0];
3145 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3146
David Sterbab159fa22016-11-08 18:09:03 +01003147 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003148
3149 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3150 btrfs_set_balance_data(leaf, item, &disk_bargs);
3151 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3152 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3153 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3154 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3155
3156 btrfs_set_balance_flags(leaf, item, bctl->flags);
3157
3158 btrfs_mark_buffer_dirty(leaf);
3159out:
3160 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003161 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003162 if (err && !ret)
3163 ret = err;
3164 return ret;
3165}
3166
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003167static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003168{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003169 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003170 struct btrfs_trans_handle *trans;
3171 struct btrfs_path *path;
3172 struct btrfs_key key;
3173 int ret, err;
3174
3175 path = btrfs_alloc_path();
3176 if (!path)
3177 return -ENOMEM;
3178
3179 trans = btrfs_start_transaction(root, 0);
3180 if (IS_ERR(trans)) {
3181 btrfs_free_path(path);
3182 return PTR_ERR(trans);
3183 }
3184
3185 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003186 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003187 key.offset = 0;
3188
3189 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3190 if (ret < 0)
3191 goto out;
3192 if (ret > 0) {
3193 ret = -ENOENT;
3194 goto out;
3195 }
3196
3197 ret = btrfs_del_item(trans, root, path);
3198out:
3199 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003200 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003201 if (err && !ret)
3202 ret = err;
3203 return ret;
3204}
3205
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003206/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003207 * This is a heuristic used to reduce the number of chunks balanced on
3208 * resume after balance was interrupted.
3209 */
3210static void update_balance_args(struct btrfs_balance_control *bctl)
3211{
3212 /*
3213 * Turn on soft mode for chunk types that were being converted.
3214 */
3215 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3216 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3217 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3218 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3219 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3220 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3221
3222 /*
3223 * Turn on usage filter if is not already used. The idea is
3224 * that chunks that we have already balanced should be
3225 * reasonably full. Don't do it for chunks that are being
3226 * converted - that will keep us from relocating unconverted
3227 * (albeit full) chunks.
3228 */
3229 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003230 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003231 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3232 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3233 bctl->data.usage = 90;
3234 }
3235 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003236 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003237 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3238 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3239 bctl->sys.usage = 90;
3240 }
3241 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003242 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003243 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3244 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3245 bctl->meta.usage = 90;
3246 }
3247}
3248
3249/*
David Sterba149196a2018-03-20 20:23:09 +01003250 * Clear the balance status in fs_info and delete the balance item from disk.
3251 */
3252static void reset_balance_state(struct btrfs_fs_info *fs_info)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003253{
3254 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
David Sterba149196a2018-03-20 20:23:09 +01003255 int ret;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003256
3257 BUG_ON(!fs_info->balance_ctl);
3258
3259 spin_lock(&fs_info->balance_lock);
3260 fs_info->balance_ctl = NULL;
3261 spin_unlock(&fs_info->balance_lock);
3262
3263 kfree(bctl);
David Sterba149196a2018-03-20 20:23:09 +01003264 ret = del_balance_item(fs_info);
3265 if (ret)
3266 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003267}
3268
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003269/*
3270 * Balance filters. Return 1 if chunk should be filtered out
3271 * (should not be balanced).
3272 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003273static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003274 struct btrfs_balance_args *bargs)
3275{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003276 chunk_type = chunk_to_extended(chunk_type) &
3277 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003278
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003279 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003280 return 0;
3281
3282 return 1;
3283}
3284
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003285static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003286 struct btrfs_balance_args *bargs)
3287{
3288 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003289 u64 chunk_used;
3290 u64 user_thresh_min;
3291 u64 user_thresh_max;
3292 int ret = 1;
3293
3294 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3295 chunk_used = btrfs_block_group_used(&cache->item);
3296
3297 if (bargs->usage_min == 0)
3298 user_thresh_min = 0;
3299 else
3300 user_thresh_min = div_factor_fine(cache->key.offset,
3301 bargs->usage_min);
3302
3303 if (bargs->usage_max == 0)
3304 user_thresh_max = 1;
3305 else if (bargs->usage_max > 100)
3306 user_thresh_max = cache->key.offset;
3307 else
3308 user_thresh_max = div_factor_fine(cache->key.offset,
3309 bargs->usage_max);
3310
3311 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3312 ret = 0;
3313
3314 btrfs_put_block_group(cache);
3315 return ret;
3316}
3317
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003318static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003319 u64 chunk_offset, struct btrfs_balance_args *bargs)
3320{
3321 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003322 u64 chunk_used, user_thresh;
3323 int ret = 1;
3324
3325 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3326 chunk_used = btrfs_block_group_used(&cache->item);
3327
David Sterbabc309462015-10-20 18:22:13 +02003328 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003329 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003330 else if (bargs->usage > 100)
3331 user_thresh = cache->key.offset;
3332 else
3333 user_thresh = div_factor_fine(cache->key.offset,
3334 bargs->usage);
3335
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003336 if (chunk_used < user_thresh)
3337 ret = 0;
3338
3339 btrfs_put_block_group(cache);
3340 return ret;
3341}
3342
Ilya Dryomov409d4042012-01-16 22:04:47 +02003343static int chunk_devid_filter(struct extent_buffer *leaf,
3344 struct btrfs_chunk *chunk,
3345 struct btrfs_balance_args *bargs)
3346{
3347 struct btrfs_stripe *stripe;
3348 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3349 int i;
3350
3351 for (i = 0; i < num_stripes; i++) {
3352 stripe = btrfs_stripe_nr(chunk, i);
3353 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3354 return 0;
3355 }
3356
3357 return 1;
3358}
3359
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003360/* [pstart, pend) */
3361static int chunk_drange_filter(struct extent_buffer *leaf,
3362 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003363 struct btrfs_balance_args *bargs)
3364{
3365 struct btrfs_stripe *stripe;
3366 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3367 u64 stripe_offset;
3368 u64 stripe_length;
3369 int factor;
3370 int i;
3371
3372 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3373 return 0;
3374
3375 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003376 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3377 factor = num_stripes / 2;
3378 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3379 factor = num_stripes - 1;
3380 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3381 factor = num_stripes - 2;
3382 } else {
3383 factor = num_stripes;
3384 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003385
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 continue;
3390
3391 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3392 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003393 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003394
3395 if (stripe_offset < bargs->pend &&
3396 stripe_offset + stripe_length > bargs->pstart)
3397 return 0;
3398 }
3399
3400 return 1;
3401}
3402
Ilya Dryomovea671762012-01-16 22:04:48 +02003403/* [vstart, vend) */
3404static int chunk_vrange_filter(struct extent_buffer *leaf,
3405 struct btrfs_chunk *chunk,
3406 u64 chunk_offset,
3407 struct btrfs_balance_args *bargs)
3408{
3409 if (chunk_offset < bargs->vend &&
3410 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3411 /* at least part of the chunk is inside this vrange */
3412 return 0;
3413
3414 return 1;
3415}
3416
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003417static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3418 struct btrfs_chunk *chunk,
3419 struct btrfs_balance_args *bargs)
3420{
3421 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3422
3423 if (bargs->stripes_min <= num_stripes
3424 && num_stripes <= bargs->stripes_max)
3425 return 0;
3426
3427 return 1;
3428}
3429
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003430static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003431 struct btrfs_balance_args *bargs)
3432{
3433 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3434 return 0;
3435
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003436 chunk_type = chunk_to_extended(chunk_type) &
3437 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003438
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003439 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003440 return 1;
3441
3442 return 0;
3443}
3444
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003445static int should_balance_chunk(struct btrfs_fs_info *fs_info,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003446 struct extent_buffer *leaf,
3447 struct btrfs_chunk *chunk, u64 chunk_offset)
3448{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003449 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003450 struct btrfs_balance_args *bargs = NULL;
3451 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3452
3453 /* type filter */
3454 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3455 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3456 return 0;
3457 }
3458
3459 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3460 bargs = &bctl->data;
3461 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3462 bargs = &bctl->sys;
3463 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3464 bargs = &bctl->meta;
3465
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003466 /* profiles filter */
3467 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3468 chunk_profiles_filter(chunk_type, bargs)) {
3469 return 0;
3470 }
3471
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003472 /* usage filter */
3473 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003474 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003475 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003476 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003477 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003478 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003479 }
3480
Ilya Dryomov409d4042012-01-16 22:04:47 +02003481 /* devid filter */
3482 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3483 chunk_devid_filter(leaf, chunk, bargs)) {
3484 return 0;
3485 }
3486
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003487 /* drange filter, makes sense only with devid filter */
3488 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003489 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003490 return 0;
3491 }
3492
Ilya Dryomovea671762012-01-16 22:04:48 +02003493 /* vrange filter */
3494 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3495 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3496 return 0;
3497 }
3498
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003499 /* stripes filter */
3500 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3501 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3502 return 0;
3503 }
3504
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003505 /* soft profile changing mode */
3506 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3507 chunk_soft_convert_filter(chunk_type, bargs)) {
3508 return 0;
3509 }
3510
David Sterba7d824b62014-05-07 17:37:51 +02003511 /*
3512 * limited by count, must be the last filter
3513 */
3514 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3515 if (bargs->limit == 0)
3516 return 0;
3517 else
3518 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003519 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3520 /*
3521 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003522 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003523 * about the count of all chunks that satisfy the filters.
3524 */
3525 if (bargs->limit_max == 0)
3526 return 0;
3527 else
3528 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003529 }
3530
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003531 return 1;
3532}
3533
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003534static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003535{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003536 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003537 struct btrfs_root *chunk_root = fs_info->chunk_root;
3538 struct btrfs_root *dev_root = fs_info->dev_root;
3539 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003540 struct btrfs_device *device;
3541 u64 old_size;
3542 u64 size_to_free;
David Sterba12907fc2015-10-10 17:16:50 +02003543 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003544 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003545 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003546 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003547 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003548 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003549 struct extent_buffer *leaf;
3550 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003551 int ret;
3552 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003553 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003554 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003555 u64 limit_data = bctl->data.limit;
3556 u64 limit_meta = bctl->meta.limit;
3557 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003558 u32 count_data = 0;
3559 u32 count_meta = 0;
3560 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003561 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003562
Chris Masonec44a352008-04-28 15:29:52 -04003563 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003564 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003565 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003566 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003567 size_to_free = div_factor(old_size, 1);
Byongho Leeee221842015-12-15 01:42:10 +09003568 size_to_free = min_t(u64, size_to_free, SZ_1M);
Anand Jainebbede42017-12-04 12:54:52 +08003569 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003570 btrfs_device_get_total_bytes(device) -
3571 btrfs_device_get_bytes_used(device) > size_to_free ||
Anand Jain401e29c2017-12-04 12:54:55 +08003572 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Chris Masonec44a352008-04-28 15:29:52 -04003573 continue;
3574
3575 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003576 if (ret == -ENOSPC)
3577 break;
Liu Bo5a488b92016-07-12 11:24:21 -07003578 if (ret) {
3579 /* btrfs_shrink_device never returns ret > 0 */
3580 WARN_ON(ret > 0);
3581 goto error;
3582 }
Chris Masonec44a352008-04-28 15:29:52 -04003583
Yan, Zhenga22285a2010-05-16 10:48:46 -04003584 trans = btrfs_start_transaction(dev_root, 0);
Liu Bo5a488b92016-07-12 11:24:21 -07003585 if (IS_ERR(trans)) {
3586 ret = PTR_ERR(trans);
3587 btrfs_info_in_rcu(fs_info,
3588 "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu",
3589 rcu_str_deref(device->name), ret,
3590 old_size, old_size - size_to_free);
3591 goto error;
3592 }
Chris Masonec44a352008-04-28 15:29:52 -04003593
3594 ret = btrfs_grow_device(trans, device, old_size);
Liu Bo5a488b92016-07-12 11:24:21 -07003595 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003596 btrfs_end_transaction(trans);
Liu Bo5a488b92016-07-12 11:24:21 -07003597 /* btrfs_grow_device never returns ret > 0 */
3598 WARN_ON(ret > 0);
3599 btrfs_info_in_rcu(fs_info,
3600 "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu",
3601 rcu_str_deref(device->name), ret,
3602 old_size, old_size - size_to_free);
3603 goto error;
3604 }
Chris Masonec44a352008-04-28 15:29:52 -04003605
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003606 btrfs_end_transaction(trans);
Chris Masonec44a352008-04-28 15:29:52 -04003607 }
3608
3609 /* step two, relocate all the chunks */
3610 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003611 if (!path) {
3612 ret = -ENOMEM;
3613 goto error;
3614 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003615
3616 /* zero out stat counters */
3617 spin_lock(&fs_info->balance_lock);
3618 memset(&bctl->stat, 0, sizeof(bctl->stat));
3619 spin_unlock(&fs_info->balance_lock);
3620again:
David Sterba7d824b62014-05-07 17:37:51 +02003621 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003622 /*
3623 * The single value limit and min/max limits use the same bytes
3624 * in the
3625 */
David Sterba7d824b62014-05-07 17:37:51 +02003626 bctl->data.limit = limit_data;
3627 bctl->meta.limit = limit_meta;
3628 bctl->sys.limit = limit_sys;
3629 }
Chris Masonec44a352008-04-28 15:29:52 -04003630 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3631 key.offset = (u64)-1;
3632 key.type = BTRFS_CHUNK_ITEM_KEY;
3633
Chris Masond3977122009-01-05 21:25:51 -05003634 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003635 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003636 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003637 ret = -ECANCELED;
3638 goto error;
3639 }
3640
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003641 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003642 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003643 if (ret < 0) {
3644 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003645 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003646 }
Chris Masonec44a352008-04-28 15:29:52 -04003647
3648 /*
3649 * this shouldn't happen, it means the last relocate
3650 * failed
3651 */
3652 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003653 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003654
3655 ret = btrfs_previous_item(chunk_root, path, 0,
3656 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003657 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003658 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003659 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003660 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003661 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003662
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003663 leaf = path->nodes[0];
3664 slot = path->slots[0];
3665 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3666
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003667 if (found_key.objectid != key.objectid) {
3668 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003669 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003670 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003671
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003672 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003673 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003674
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003675 if (!counting) {
3676 spin_lock(&fs_info->balance_lock);
3677 bctl->stat.considered++;
3678 spin_unlock(&fs_info->balance_lock);
3679 }
3680
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003681 ret = should_balance_chunk(fs_info, leaf, chunk,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003682 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003683
David Sterbab3b4aa72011-04-21 01:20:15 +02003684 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003685 if (!ret) {
3686 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003687 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003688 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003689
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003690 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003691 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003692 spin_lock(&fs_info->balance_lock);
3693 bctl->stat.expected++;
3694 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003695
3696 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3697 count_data++;
3698 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3699 count_sys++;
3700 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3701 count_meta++;
3702
3703 goto loop;
3704 }
3705
3706 /*
3707 * Apply limit_min filter, no need to check if the LIMITS
3708 * filter is used, limit_min is 0 by default
3709 */
3710 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3711 count_data < bctl->data.limit_min)
3712 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3713 count_meta < bctl->meta.limit_min)
3714 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3715 count_sys < bctl->sys.limit_min)) {
3716 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003717 goto loop;
3718 }
3719
Liu Boa6f93c72017-11-15 16:28:11 -07003720 if (!chunk_reserved) {
3721 /*
3722 * We may be relocating the only data chunk we have,
3723 * which could potentially end up with losing data's
3724 * raid profile, so lets allocate an empty one in
3725 * advance.
3726 */
3727 ret = btrfs_may_alloc_data_chunk(fs_info,
3728 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003729 if (ret < 0) {
3730 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3731 goto error;
Liu Boa6f93c72017-11-15 16:28:11 -07003732 } else if (ret == 1) {
3733 chunk_reserved = 1;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003734 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003735 }
3736
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003737 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003738 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003739 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003740 enospc_errors++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07003741 } else if (ret == -ETXTBSY) {
3742 btrfs_info(fs_info,
3743 "skipping relocation of block group %llu due to active swapfile",
3744 found_key.offset);
3745 ret = 0;
3746 } else if (ret) {
3747 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003748 } else {
3749 spin_lock(&fs_info->balance_lock);
3750 bctl->stat.completed++;
3751 spin_unlock(&fs_info->balance_lock);
3752 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003753loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003754 if (found_key.offset == 0)
3755 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003756 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003757 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003758
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003759 if (counting) {
3760 btrfs_release_path(path);
3761 counting = false;
3762 goto again;
3763 }
Chris Masonec44a352008-04-28 15:29:52 -04003764error:
3765 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003766 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003767 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003768 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003769 if (!ret)
3770 ret = -ENOSPC;
3771 }
3772
Chris Masonec44a352008-04-28 15:29:52 -04003773 return ret;
3774}
3775
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003776/**
3777 * alloc_profile_is_valid - see if a given profile is valid and reduced
3778 * @flags: profile to validate
3779 * @extended: if true @flags is treated as an extended profile
3780 */
3781static int alloc_profile_is_valid(u64 flags, int extended)
3782{
3783 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3784 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3785
3786 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3787
3788 /* 1) check that all other bits are zeroed */
3789 if (flags & ~mask)
3790 return 0;
3791
3792 /* 2) see if profile is reduced */
3793 if (flags == 0)
3794 return !extended; /* "0" is valid for usual profiles */
3795
3796 /* true if exactly one bit set */
David Sterba818255f2018-09-21 14:26:34 +02003797 return is_power_of_2(flags);
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003798}
3799
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003800static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3801{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003802 /* cancel requested || normal exit path */
3803 return atomic_read(&fs_info->balance_cancel_req) ||
3804 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3805 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003806}
3807
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003808/* Non-zero return value signifies invalidity */
3809static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3810 u64 allowed)
3811{
3812 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3813 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3814 (bctl_arg->target & ~allowed)));
3815}
3816
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003817/*
David Sterbadccdb072018-03-21 00:20:05 +01003818 * Should be called with balance mutexe held
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003819 */
David Sterba6fcf6e22018-05-07 17:44:03 +02003820int btrfs_balance(struct btrfs_fs_info *fs_info,
3821 struct btrfs_balance_control *bctl,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003822 struct btrfs_ioctl_balance_args *bargs)
3823{
Adam Borowski14506122017-03-07 23:34:44 +01003824 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003825 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003826 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003827 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003828 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003829 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003830
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003831 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003832 atomic_read(&fs_info->balance_pause_req) ||
3833 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003834 ret = -EINVAL;
3835 goto out;
3836 }
3837
Ilya Dryomove4837f82012-03-27 17:09:17 +03003838 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3839 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3840 mixed = 1;
3841
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003842 /*
3843 * In case of mixed groups both data and meta should be picked,
3844 * and identical options should be given for both of them.
3845 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003846 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3847 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003848 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3849 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3850 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003851 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003852 "balance: mixed groups data and metadata options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003853 ret = -EINVAL;
3854 goto out;
3855 }
3856 }
3857
Anand Jain1da73962018-08-10 13:53:21 +08003858 num_devices = btrfs_num_devices(fs_info);
3859
Austin S. Hemmelgarn88be1592016-03-23 14:22:59 -04003860 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
3861 if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003862 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003863 if (num_devices > 2)
3864 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3865 if (num_devices > 3)
3866 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3867 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003868 if (validate_convert_profile(&bctl->data, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08003869 int index = btrfs_bg_flags_to_raid_index(bctl->data.target);
3870
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003871 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003872 "balance: invalid convert data profile %s",
3873 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003874 ret = -EINVAL;
3875 goto out;
3876 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003877 if (validate_convert_profile(&bctl->meta, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08003878 int index = btrfs_bg_flags_to_raid_index(bctl->meta.target);
3879
Frank Holtonefe120a2013-12-20 11:37:06 -05003880 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003881 "balance: invalid convert metadata profile %s",
3882 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003883 ret = -EINVAL;
3884 goto out;
3885 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003886 if (validate_convert_profile(&bctl->sys, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08003887 int index = btrfs_bg_flags_to_raid_index(bctl->sys.target);
3888
Frank Holtonefe120a2013-12-20 11:37:06 -05003889 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003890 "balance: invalid convert system profile %s",
3891 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003892 ret = -EINVAL;
3893 goto out;
3894 }
3895
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003896 /* allow to reduce meta or sys integrity only if force set */
3897 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003898 BTRFS_BLOCK_GROUP_RAID10 |
3899 BTRFS_BLOCK_GROUP_RAID5 |
3900 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003901 do {
3902 seq = read_seqbegin(&fs_info->profiles_lock);
3903
3904 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3905 (fs_info->avail_system_alloc_bits & allowed) &&
3906 !(bctl->sys.target & allowed)) ||
3907 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3908 (fs_info->avail_metadata_alloc_bits & allowed) &&
3909 !(bctl->meta.target & allowed))) {
3910 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003911 btrfs_info(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003912 "balance: force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003913 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003914 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003915 "balance: reduces metadata integrity, use --force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003916 ret = -EINVAL;
3917 goto out;
3918 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003919 }
Miao Xiede98ced2013-01-29 10:13:12 +00003920 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003921
Adam Borowski14506122017-03-07 23:34:44 +01003922 /* if we're not converting, the target field is uninitialized */
3923 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3924 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
3925 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3926 bctl->data.target : fs_info->avail_data_alloc_bits;
3927 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
3928 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08003929 int meta_index = btrfs_bg_flags_to_raid_index(meta_target);
3930 int data_index = btrfs_bg_flags_to_raid_index(data_target);
3931
Sam Tygieree592d02016-01-06 08:46:12 +00003932 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003933 "balance: metadata profile %s has lower redundancy than data profile %s",
3934 get_raid_name(meta_index), get_raid_name(data_index));
Sam Tygieree592d02016-01-06 08:46:12 +00003935 }
3936
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003937 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003938 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003939 goto out;
3940
Ilya Dryomov59641012012-01-16 22:04:48 +02003941 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3942 BUG_ON(ret == -EEXIST);
David Sterba833aae12018-03-21 02:41:30 +01003943 BUG_ON(fs_info->balance_ctl);
3944 spin_lock(&fs_info->balance_lock);
3945 fs_info->balance_ctl = bctl;
3946 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov59641012012-01-16 22:04:48 +02003947 } else {
3948 BUG_ON(ret != -EEXIST);
3949 spin_lock(&fs_info->balance_lock);
3950 update_balance_args(bctl);
3951 spin_unlock(&fs_info->balance_lock);
3952 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003953
David Sterba3009a622018-03-21 01:31:04 +01003954 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
3955 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003956 mutex_unlock(&fs_info->balance_mutex);
3957
3958 ret = __btrfs_balance(fs_info);
3959
3960 mutex_lock(&fs_info->balance_mutex);
David Sterba3009a622018-03-21 01:31:04 +01003961 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003962
3963 if (bargs) {
3964 memset(bargs, 0, sizeof(*bargs));
David Sterba008ef092018-03-21 02:05:27 +01003965 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003966 }
3967
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003968 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3969 balance_need_close(fs_info)) {
David Sterba149196a2018-03-20 20:23:09 +01003970 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01003971 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003972 }
3973
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003974 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003975
3976 return ret;
3977out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003978 if (bctl->flags & BTRFS_BALANCE_RESUME)
David Sterba149196a2018-03-20 20:23:09 +01003979 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01003980 else
Ilya Dryomov59641012012-01-16 22:04:48 +02003981 kfree(bctl);
David Sterbaa17c95d2018-03-20 17:28:05 +01003982 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
3983
Ilya Dryomov59641012012-01-16 22:04:48 +02003984 return ret;
3985}
3986
3987static int balance_kthread(void *data)
3988{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003989 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003990 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003991
Ilya Dryomov59641012012-01-16 22:04:48 +02003992 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003993 if (fs_info->balance_ctl) {
Anand Jain6dac13f2018-05-16 10:51:26 +08003994 btrfs_info(fs_info, "balance: resuming");
David Sterba6fcf6e22018-05-07 17:44:03 +02003995 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003996 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003997 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003998
Ilya Dryomov59641012012-01-16 22:04:48 +02003999 return ret;
4000}
4001
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004002int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
4003{
4004 struct task_struct *tsk;
4005
David Sterba1354e1a2018-03-21 02:29:13 +01004006 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004007 if (!fs_info->balance_ctl) {
David Sterba1354e1a2018-03-21 02:29:13 +01004008 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004009 return 0;
4010 }
David Sterba1354e1a2018-03-21 02:29:13 +01004011 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004012
Jeff Mahoney3cdde222016-06-09 21:38:35 -04004013 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004014 btrfs_info(fs_info, "balance: resume skipped");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004015 return 0;
4016 }
4017
Anand Jain02ee6542018-05-17 15:16:51 +08004018 /*
4019 * A ro->rw remount sequence should continue with the paused balance
4020 * regardless of who pauses it, system or the user as of now, so set
4021 * the resume flag.
4022 */
4023 spin_lock(&fs_info->balance_lock);
4024 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
4025 spin_unlock(&fs_info->balance_lock);
4026
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004027 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05304028 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004029}
4030
Ilya Dryomov68310a52012-06-22 12:24:12 -06004031int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02004032{
Ilya Dryomov59641012012-01-16 22:04:48 +02004033 struct btrfs_balance_control *bctl;
4034 struct btrfs_balance_item *item;
4035 struct btrfs_disk_balance_args disk_bargs;
4036 struct btrfs_path *path;
4037 struct extent_buffer *leaf;
4038 struct btrfs_key key;
4039 int ret;
4040
4041 path = btrfs_alloc_path();
4042 if (!path)
4043 return -ENOMEM;
4044
Ilya Dryomov68310a52012-06-22 12:24:12 -06004045 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01004046 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06004047 key.offset = 0;
4048
4049 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4050 if (ret < 0)
4051 goto out;
4052 if (ret > 0) { /* ret = -ENOENT; */
4053 ret = 0;
4054 goto out;
4055 }
4056
Ilya Dryomov59641012012-01-16 22:04:48 +02004057 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4058 if (!bctl) {
4059 ret = -ENOMEM;
4060 goto out;
4061 }
4062
Ilya Dryomov59641012012-01-16 22:04:48 +02004063 leaf = path->nodes[0];
4064 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4065
Ilya Dryomov68310a52012-06-22 12:24:12 -06004066 bctl->flags = btrfs_balance_flags(leaf, item);
4067 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004068
4069 btrfs_balance_data(leaf, item, &disk_bargs);
4070 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4071 btrfs_balance_meta(leaf, item, &disk_bargs);
4072 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4073 btrfs_balance_sys(leaf, item, &disk_bargs);
4074 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4075
David Sterbaeee95e32018-03-20 20:07:58 +01004076 /*
4077 * This should never happen, as the paused balance state is recovered
4078 * during mount without any chance of other exclusive ops to collide.
4079 *
4080 * This gives the exclusive op status to balance and keeps in paused
4081 * state until user intervention (cancel or umount). If the ownership
4082 * cannot be assigned, show a message but do not fail. The balance
4083 * is in a paused state and must have fs_info::balance_ctl properly
4084 * set up.
4085 */
4086 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
4087 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004088 "balance: cannot set exclusive op status, resume manually");
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004089
Ilya Dryomov68310a52012-06-22 12:24:12 -06004090 mutex_lock(&fs_info->balance_mutex);
David Sterba833aae12018-03-21 02:41:30 +01004091 BUG_ON(fs_info->balance_ctl);
4092 spin_lock(&fs_info->balance_lock);
4093 fs_info->balance_ctl = bctl;
4094 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov68310a52012-06-22 12:24:12 -06004095 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004096out:
4097 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004098 return ret;
4099}
4100
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004101int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4102{
4103 int ret = 0;
4104
4105 mutex_lock(&fs_info->balance_mutex);
4106 if (!fs_info->balance_ctl) {
4107 mutex_unlock(&fs_info->balance_mutex);
4108 return -ENOTCONN;
4109 }
4110
David Sterba3009a622018-03-21 01:31:04 +01004111 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004112 atomic_inc(&fs_info->balance_pause_req);
4113 mutex_unlock(&fs_info->balance_mutex);
4114
4115 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004116 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004117
4118 mutex_lock(&fs_info->balance_mutex);
4119 /* we are good with balance_ctl ripped off from under us */
David Sterba3009a622018-03-21 01:31:04 +01004120 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004121 atomic_dec(&fs_info->balance_pause_req);
4122 } else {
4123 ret = -ENOTCONN;
4124 }
4125
4126 mutex_unlock(&fs_info->balance_mutex);
4127 return ret;
4128}
4129
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004130int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4131{
4132 mutex_lock(&fs_info->balance_mutex);
4133 if (!fs_info->balance_ctl) {
4134 mutex_unlock(&fs_info->balance_mutex);
4135 return -ENOTCONN;
4136 }
4137
David Sterbacf7d20f2018-03-21 01:45:32 +01004138 /*
4139 * A paused balance with the item stored on disk can be resumed at
4140 * mount time if the mount is read-write. Otherwise it's still paused
4141 * and we must not allow cancelling as it deletes the item.
4142 */
4143 if (sb_rdonly(fs_info->sb)) {
4144 mutex_unlock(&fs_info->balance_mutex);
4145 return -EROFS;
4146 }
4147
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004148 atomic_inc(&fs_info->balance_cancel_req);
4149 /*
4150 * if we are running just wait and return, balance item is
4151 * deleted in btrfs_balance in this case
4152 */
David Sterba3009a622018-03-21 01:31:04 +01004153 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004154 mutex_unlock(&fs_info->balance_mutex);
4155 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004156 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004157 mutex_lock(&fs_info->balance_mutex);
4158 } else {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004159 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004160 /*
4161 * Lock released to allow other waiters to continue, we'll
4162 * reexamine the status again.
4163 */
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004164 mutex_lock(&fs_info->balance_mutex);
4165
David Sterbaa17c95d2018-03-20 17:28:05 +01004166 if (fs_info->balance_ctl) {
David Sterba149196a2018-03-20 20:23:09 +01004167 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004168 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain6dac13f2018-05-16 10:51:26 +08004169 btrfs_info(fs_info, "balance: canceled");
David Sterbaa17c95d2018-03-20 17:28:05 +01004170 }
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004171 }
4172
David Sterba3009a622018-03-21 01:31:04 +01004173 BUG_ON(fs_info->balance_ctl ||
4174 test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004175 atomic_dec(&fs_info->balance_cancel_req);
4176 mutex_unlock(&fs_info->balance_mutex);
4177 return 0;
4178}
4179
Stefan Behrens803b2f52013-08-15 17:11:21 +02004180static int btrfs_uuid_scan_kthread(void *data)
4181{
4182 struct btrfs_fs_info *fs_info = data;
4183 struct btrfs_root *root = fs_info->tree_root;
4184 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004185 struct btrfs_path *path = NULL;
4186 int ret = 0;
4187 struct extent_buffer *eb;
4188 int slot;
4189 struct btrfs_root_item root_item;
4190 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004191 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004192
4193 path = btrfs_alloc_path();
4194 if (!path) {
4195 ret = -ENOMEM;
4196 goto out;
4197 }
4198
4199 key.objectid = 0;
4200 key.type = BTRFS_ROOT_ITEM_KEY;
4201 key.offset = 0;
4202
Stefan Behrens803b2f52013-08-15 17:11:21 +02004203 while (1) {
Anand Jain7c829b72018-03-07 17:29:18 +08004204 ret = btrfs_search_forward(root, &key, path,
4205 BTRFS_OLDEST_GENERATION);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004206 if (ret) {
4207 if (ret > 0)
4208 ret = 0;
4209 break;
4210 }
4211
4212 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4213 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4214 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4215 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4216 goto skip;
4217
4218 eb = path->nodes[0];
4219 slot = path->slots[0];
4220 item_size = btrfs_item_size_nr(eb, slot);
4221 if (item_size < sizeof(root_item))
4222 goto skip;
4223
Stefan Behrens803b2f52013-08-15 17:11:21 +02004224 read_extent_buffer(eb, &root_item,
4225 btrfs_item_ptr_offset(eb, slot),
4226 (int)sizeof(root_item));
4227 if (btrfs_root_refs(&root_item) == 0)
4228 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004229
4230 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4231 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4232 if (trans)
4233 goto update_tree;
4234
4235 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004236 /*
4237 * 1 - subvol uuid item
4238 * 1 - received_subvol uuid item
4239 */
4240 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4241 if (IS_ERR(trans)) {
4242 ret = PTR_ERR(trans);
4243 break;
4244 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004245 continue;
4246 } else {
4247 goto skip;
4248 }
4249update_tree:
4250 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004251 ret = btrfs_uuid_tree_add(trans, root_item.uuid,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004252 BTRFS_UUID_KEY_SUBVOL,
4253 key.objectid);
4254 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004255 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004256 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004257 break;
4258 }
4259 }
4260
4261 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004262 ret = btrfs_uuid_tree_add(trans,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004263 root_item.received_uuid,
4264 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4265 key.objectid);
4266 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004267 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004268 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004269 break;
4270 }
4271 }
4272
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004273skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004274 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004275 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004276 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004277 if (ret)
4278 break;
4279 }
4280
Stefan Behrens803b2f52013-08-15 17:11:21 +02004281 btrfs_release_path(path);
4282 if (key.offset < (u64)-1) {
4283 key.offset++;
4284 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4285 key.offset = 0;
4286 key.type = BTRFS_ROOT_ITEM_KEY;
4287 } else if (key.objectid < (u64)-1) {
4288 key.offset = 0;
4289 key.type = BTRFS_ROOT_ITEM_KEY;
4290 key.objectid++;
4291 } else {
4292 break;
4293 }
4294 cond_resched();
4295 }
4296
4297out:
4298 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004299 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004300 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004301 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004302 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004303 else
Josef Bacikafcdd122016-09-02 15:40:02 -04004304 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004305 up(&fs_info->uuid_tree_rescan_sem);
4306 return 0;
4307}
4308
Stefan Behrens70f80172013-08-15 17:11:23 +02004309/*
4310 * Callback for btrfs_uuid_tree_iterate().
4311 * returns:
4312 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004313 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004314 * > 0 if the check failed, which means the caller shall remove the entry.
4315 */
4316static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4317 u8 *uuid, u8 type, u64 subid)
4318{
4319 struct btrfs_key key;
4320 int ret = 0;
4321 struct btrfs_root *subvol_root;
4322
4323 if (type != BTRFS_UUID_KEY_SUBVOL &&
4324 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4325 goto out;
4326
4327 key.objectid = subid;
4328 key.type = BTRFS_ROOT_ITEM_KEY;
4329 key.offset = (u64)-1;
4330 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4331 if (IS_ERR(subvol_root)) {
4332 ret = PTR_ERR(subvol_root);
4333 if (ret == -ENOENT)
4334 ret = 1;
4335 goto out;
4336 }
4337
4338 switch (type) {
4339 case BTRFS_UUID_KEY_SUBVOL:
4340 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4341 ret = 1;
4342 break;
4343 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4344 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4345 BTRFS_UUID_SIZE))
4346 ret = 1;
4347 break;
4348 }
4349
4350out:
4351 return ret;
4352}
4353
4354static int btrfs_uuid_rescan_kthread(void *data)
4355{
4356 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4357 int ret;
4358
4359 /*
4360 * 1st step is to iterate through the existing UUID tree and
4361 * to delete all entries that contain outdated data.
4362 * 2nd step is to add all missing entries to the UUID tree.
4363 */
4364 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4365 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004366 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004367 up(&fs_info->uuid_tree_rescan_sem);
4368 return ret;
4369 }
4370 return btrfs_uuid_scan_kthread(data);
4371}
4372
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004373int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4374{
4375 struct btrfs_trans_handle *trans;
4376 struct btrfs_root *tree_root = fs_info->tree_root;
4377 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004378 struct task_struct *task;
4379 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004380
4381 /*
4382 * 1 - root node
4383 * 1 - root item
4384 */
4385 trans = btrfs_start_transaction(tree_root, 2);
4386 if (IS_ERR(trans))
4387 return PTR_ERR(trans);
4388
4389 uuid_root = btrfs_create_tree(trans, fs_info,
4390 BTRFS_UUID_TREE_OBJECTID);
4391 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004392 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004393 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004394 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004395 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004396 }
4397
4398 fs_info->uuid_root = uuid_root;
4399
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004400 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004401 if (ret)
4402 return ret;
4403
4404 down(&fs_info->uuid_tree_rescan_sem);
4405 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4406 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004407 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004408 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004409 up(&fs_info->uuid_tree_rescan_sem);
4410 return PTR_ERR(task);
4411 }
4412
4413 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004414}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004415
Stefan Behrens70f80172013-08-15 17:11:23 +02004416int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4417{
4418 struct task_struct *task;
4419
4420 down(&fs_info->uuid_tree_rescan_sem);
4421 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4422 if (IS_ERR(task)) {
4423 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004424 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004425 up(&fs_info->uuid_tree_rescan_sem);
4426 return PTR_ERR(task);
4427 }
4428
4429 return 0;
4430}
4431
Chris Mason8f18cf12008-04-25 16:53:30 -04004432/*
4433 * shrinking a device means finding all of the device extents past
4434 * the new size, and then following the back refs to the chunks.
4435 * The chunk relocation code actually frees the device extent
4436 */
4437int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4438{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004439 struct btrfs_fs_info *fs_info = device->fs_info;
4440 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004441 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004442 struct btrfs_dev_extent *dev_extent = NULL;
4443 struct btrfs_path *path;
4444 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004445 u64 chunk_offset;
4446 int ret;
4447 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004448 int failed = 0;
4449 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004450 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004451 struct extent_buffer *l;
4452 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004453 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004454 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004455 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004456 u64 diff;
4457
4458 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004459 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004460
Anand Jain401e29c2017-12-04 12:54:55 +08004461 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Stefan Behrens63a212a2012-11-05 18:29:28 +01004462 return -EINVAL;
4463
Chris Mason8f18cf12008-04-25 16:53:30 -04004464 path = btrfs_alloc_path();
4465 if (!path)
4466 return -ENOMEM;
4467
Gu Jinxiang0338dff2018-04-27 16:22:07 +08004468 path->reada = READA_BACK;
Chris Mason8f18cf12008-04-25 16:53:30 -04004469
David Sterba34441362016-10-04 19:34:27 +02004470 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004471
Miao Xie7cc8e582014-09-03 21:35:38 +08004472 btrfs_device_set_total_bytes(device, new_size);
Anand Jainebbede42017-12-04 12:54:52 +08004473 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05004474 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004475 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004476 }
David Sterba34441362016-10-04 19:34:27 +02004477 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004478
Josef Bacikba1bf482009-09-11 16:11:19 -04004479again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004480 key.objectid = device->devid;
4481 key.offset = (u64)-1;
4482 key.type = BTRFS_DEV_EXTENT_KEY;
4483
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004484 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004485 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004486 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004487 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004488 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004489 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004490 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004491
4492 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004493 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004494 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004495 if (ret < 0)
4496 goto done;
4497 if (ret) {
4498 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004499 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004500 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004501 }
4502
4503 l = path->nodes[0];
4504 slot = path->slots[0];
4505 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4506
Josef Bacikba1bf482009-09-11 16:11:19 -04004507 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004508 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004509 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004510 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004511 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004512
4513 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4514 length = btrfs_dev_extent_length(l, dev_extent);
4515
Josef Bacikba1bf482009-09-11 16:11:19 -04004516 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004517 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004518 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004519 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004520 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004521
Chris Mason8f18cf12008-04-25 16:53:30 -04004522 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004523 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004524
Liu Boa6f93c72017-11-15 16:28:11 -07004525 /*
4526 * We may be relocating the only data chunk we have,
4527 * which could potentially end up with losing data's
4528 * raid profile, so lets allocate an empty one in
4529 * advance.
4530 */
4531 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4532 if (ret < 0) {
4533 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4534 goto done;
4535 }
4536
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004537 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4538 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004539 if (ret == -ENOSPC) {
Josef Bacikba1bf482009-09-11 16:11:19 -04004540 failed++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004541 } else if (ret) {
4542 if (ret == -ETXTBSY) {
4543 btrfs_warn(fs_info,
4544 "could not shrink block group %llu due to active swapfile",
4545 chunk_offset);
4546 }
4547 goto done;
4548 }
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004549 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004550
4551 if (failed && !retried) {
4552 failed = 0;
4553 retried = true;
4554 goto again;
4555 } else if (failed && retried) {
4556 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004557 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004558 }
4559
Chris Balld6397ba2009-04-27 07:29:03 -04004560 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004561 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004562 if (IS_ERR(trans)) {
4563 ret = PTR_ERR(trans);
4564 goto done;
4565 }
4566
David Sterba34441362016-10-04 19:34:27 +02004567 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004568
4569 /*
4570 * We checked in the above loop all device extents that were already in
4571 * the device tree. However before we have updated the device's
4572 * total_bytes to the new size, we might have had chunk allocations that
4573 * have not complete yet (new block groups attached to transaction
4574 * handles), and therefore their device extents were not yet in the
4575 * device tree and we missed them in the loop above. So if we have any
4576 * pending chunk using a device extent that overlaps the device range
4577 * that we can not use anymore, commit the current transaction and
4578 * repeat the search on the device tree - this way we guarantee we will
4579 * not have chunks using device extents that end beyond 'new_size'.
4580 */
4581 if (!checked_pending_chunks) {
4582 u64 start = new_size;
4583 u64 len = old_size - new_size;
4584
Jeff Mahoney499f3772015-06-15 09:41:17 -04004585 if (contains_pending_extent(trans->transaction, device,
4586 &start, len)) {
David Sterba34441362016-10-04 19:34:27 +02004587 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004588 checked_pending_chunks = true;
4589 failed = 0;
4590 retried = false;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004591 ret = btrfs_commit_transaction(trans);
Filipe Manana53e489b2015-06-02 14:43:21 +01004592 if (ret)
4593 goto done;
4594 goto again;
4595 }
4596 }
4597
Miao Xie7cc8e582014-09-03 21:35:38 +08004598 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004599 if (list_empty(&device->resized_list))
4600 list_add_tail(&device->resized_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004601 &fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004602
Chris Balld6397ba2009-04-27 07:29:03 -04004603 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004604 btrfs_set_super_total_bytes(super_copy,
4605 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004606 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004607
4608 /* Now btrfs_update_device() will change the on-disk size. */
4609 ret = btrfs_update_device(trans, device);
Anand Jain801660b2018-08-06 18:12:37 +08004610 if (ret < 0) {
4611 btrfs_abort_transaction(trans, ret);
4612 btrfs_end_transaction(trans);
4613 } else {
4614 ret = btrfs_commit_transaction(trans);
4615 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004616done:
4617 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004618 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004619 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004620 btrfs_device_set_total_bytes(device, old_size);
Anand Jainebbede42017-12-04 12:54:52 +08004621 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Filipe Manana53e489b2015-06-02 14:43:21 +01004622 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004623 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004624 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004625 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004626 return ret;
4627}
4628
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004629static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004630 struct btrfs_key *key,
4631 struct btrfs_chunk *chunk, int item_size)
4632{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004633 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004634 struct btrfs_disk_key disk_key;
4635 u32 array_size;
4636 u8 *ptr;
4637
David Sterba34441362016-10-04 19:34:27 +02004638 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004639 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004640 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004641 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004642 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004643 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004644 }
Chris Mason0b86a832008-03-24 15:01:56 -04004645
4646 ptr = super_copy->sys_chunk_array + array_size;
4647 btrfs_cpu_key_to_disk(&disk_key, key);
4648 memcpy(ptr, &disk_key, sizeof(disk_key));
4649 ptr += sizeof(disk_key);
4650 memcpy(ptr, chunk, item_size);
4651 item_size += sizeof(disk_key);
4652 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004653 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004654
Chris Mason0b86a832008-03-24 15:01:56 -04004655 return 0;
4656}
4657
Miao Xieb2117a32011-01-05 10:07:28 +00004658/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004659 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004660 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004661static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004662{
Arne Jansen73c5de02011-04-12 12:07:57 +02004663 const struct btrfs_device_info *di_a = a;
4664 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004665
Arne Jansen73c5de02011-04-12 12:07:57 +02004666 if (di_a->max_avail > di_b->max_avail)
4667 return -1;
4668 if (di_a->max_avail < di_b->max_avail)
4669 return 1;
4670 if (di_a->total_avail > di_b->total_avail)
4671 return -1;
4672 if (di_a->total_avail < di_b->total_avail)
4673 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004674 return 0;
4675}
4676
David Woodhouse53b381b2013-01-29 18:40:14 -05004677static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4678{
Zhao Leiffe2d202015-01-20 15:11:44 +08004679 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004680 return;
4681
Miao Xieceda0862013-04-11 10:30:16 +00004682 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004683}
4684
Qu Wenruo062d4d12018-01-30 18:20:46 +08004685#define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004686 - sizeof(struct btrfs_chunk)) \
4687 / sizeof(struct btrfs_stripe) + 1)
4688
4689#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4690 - 2 * sizeof(struct btrfs_disk_key) \
4691 - 2 * sizeof(struct btrfs_chunk)) \
4692 / sizeof(struct btrfs_stripe) + 1)
4693
Miao Xieb2117a32011-01-05 10:07:28 +00004694static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
David Sterba72b468c2017-02-10 19:46:27 +01004695 u64 start, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004696{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004697 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004698 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004699 struct btrfs_device *device;
Arne Jansen73c5de02011-04-12 12:07:57 +02004700 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004701 struct extent_map_tree *em_tree;
4702 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004703 struct btrfs_device_info *devices_info = NULL;
4704 u64 total_avail;
4705 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004706 int data_stripes; /* number of stripes that count for
4707 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004708 int sub_stripes; /* sub_stripes info for map */
4709 int dev_stripes; /* stripes per dev */
4710 int devs_max; /* max devs to use */
4711 int devs_min; /* min devs needed */
4712 int devs_increment; /* ndevs has to be a multiple of this */
4713 int ncopies; /* how many copies to data has */
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02004714 int nparity; /* number of stripes worth of bytes to
4715 store parity information */
Miao Xieb2117a32011-01-05 10:07:28 +00004716 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004717 u64 max_stripe_size;
4718 u64 max_chunk_size;
4719 u64 stripe_size;
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02004720 u64 chunk_size;
Arne Jansen73c5de02011-04-12 12:07:57 +02004721 int ndevs;
4722 int i;
4723 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004724 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004725
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004726 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004727
Qu Wenruo4117f202018-01-22 13:50:54 +08004728 if (list_empty(&fs_devices->alloc_list)) {
4729 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4730 btrfs_debug(info, "%s: no writable device", __func__);
Miao Xieb2117a32011-01-05 10:07:28 +00004731 return -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08004732 }
Miao Xieb2117a32011-01-05 10:07:28 +00004733
Qu Wenruo3e72ee82018-01-30 18:20:45 +08004734 index = btrfs_bg_flags_to_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004735
Liu Bo31e50222012-11-21 14:18:10 +00004736 sub_stripes = btrfs_raid_array[index].sub_stripes;
4737 dev_stripes = btrfs_raid_array[index].dev_stripes;
4738 devs_max = btrfs_raid_array[index].devs_max;
4739 devs_min = btrfs_raid_array[index].devs_min;
4740 devs_increment = btrfs_raid_array[index].devs_increment;
4741 ncopies = btrfs_raid_array[index].ncopies;
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02004742 nparity = btrfs_raid_array[index].nparity;
Arne Jansen73c5de02011-04-12 12:07:57 +02004743
4744 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09004745 max_stripe_size = SZ_1G;
Qu Wenruofce466e2018-07-03 17:10:05 +08004746 max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004747 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08004748 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02004749 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004750 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09004751 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4752 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05004753 else
Byongho Leeee221842015-12-15 01:42:10 +09004754 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004755 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004756 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08004757 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02004758 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09004759 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004760 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004761 if (!devs_max)
4762 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004763 } else {
David Sterba351fd352014-05-15 16:48:20 +02004764 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004765 type);
4766 BUG_ON(1);
4767 }
4768
4769 /* we don't want a chunk larger than 10% of writeable space */
4770 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4771 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004772
David Sterba31e818f2015-02-20 18:00:26 +01004773 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004774 GFP_NOFS);
4775 if (!devices_info)
4776 return -ENOMEM;
4777
Arne Jansen73c5de02011-04-12 12:07:57 +02004778 /*
4779 * in the first pass through the devices list, we gather information
4780 * about the available holes on each device.
4781 */
4782 ndevs = 0;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004783 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004784 u64 max_avail;
4785 u64 dev_offset;
4786
Anand Jainebbede42017-12-04 12:54:52 +08004787 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004788 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004789 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004790 continue;
4791 }
4792
Anand Jaine12c9622017-12-04 12:54:53 +08004793 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
4794 &device->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08004795 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Arne Jansen73c5de02011-04-12 12:07:57 +02004796 continue;
4797
4798 if (device->total_bytes > device->bytes_used)
4799 total_avail = device->total_bytes - device->bytes_used;
4800 else
4801 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004802
4803 /* If there is no space on this device, skip it. */
4804 if (total_avail == 0)
4805 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004806
Josef Bacik6df9a952013-06-27 13:22:46 -04004807 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004808 max_stripe_size * dev_stripes,
4809 &dev_offset, &max_avail);
4810 if (ret && ret != -ENOSPC)
4811 goto error;
4812
4813 if (ret == 0)
4814 max_avail = max_stripe_size * dev_stripes;
4815
Qu Wenruo4117f202018-01-22 13:50:54 +08004816 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) {
4817 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4818 btrfs_debug(info,
4819 "%s: devid %llu has no free space, have=%llu want=%u",
4820 __func__, device->devid, max_avail,
4821 BTRFS_STRIPE_LEN * dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02004822 continue;
Qu Wenruo4117f202018-01-22 13:50:54 +08004823 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004824
Eric Sandeen063d0062013-01-31 00:55:01 +00004825 if (ndevs == fs_devices->rw_devices) {
4826 WARN(1, "%s: found more than %llu devices\n",
4827 __func__, fs_devices->rw_devices);
4828 break;
4829 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004830 devices_info[ndevs].dev_offset = dev_offset;
4831 devices_info[ndevs].max_avail = max_avail;
4832 devices_info[ndevs].total_avail = total_avail;
4833 devices_info[ndevs].dev = device;
4834 ++ndevs;
4835 }
4836
4837 /*
4838 * now sort the devices by hole size / available space
4839 */
4840 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4841 btrfs_cmp_device_info, NULL);
4842
4843 /* round down to number of usable stripes */
Nikolay Borisove5600fd2017-06-27 10:02:25 +03004844 ndevs = round_down(ndevs, devs_increment);
Arne Jansen73c5de02011-04-12 12:07:57 +02004845
Qu Wenruoba89b802018-01-31 13:56:15 +08004846 if (ndevs < devs_min) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004847 ret = -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08004848 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
4849 btrfs_debug(info,
4850 "%s: not enough devices with free space: have=%d minimum required=%d",
Qu Wenruoba89b802018-01-31 13:56:15 +08004851 __func__, ndevs, devs_min);
Qu Wenruo4117f202018-01-22 13:50:54 +08004852 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004853 goto error;
4854 }
4855
Nikolay Borisovf148ef42017-06-27 10:02:26 +03004856 ndevs = min(ndevs, devs_max);
4857
Arne Jansen73c5de02011-04-12 12:07:57 +02004858 /*
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01004859 * The primary goal is to maximize the number of stripes, so use as
4860 * many devices as possible, even if the stripes are not maximum sized.
4861 *
4862 * The DUP profile stores more than one stripe per device, the
4863 * max_avail is the total size so we have to adjust.
Arne Jansen73c5de02011-04-12 12:07:57 +02004864 */
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01004865 stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02004866 num_stripes = ndevs * dev_stripes;
4867
David Woodhouse53b381b2013-01-29 18:40:14 -05004868 /*
4869 * this will have to be fixed for RAID1 and RAID10 over
4870 * more drives
4871 */
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02004872 data_stripes = (num_stripes - nparity) / ncopies;
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,
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02004877 * and compare that answer with the max chunk size. If it's higher,
4878 * we try to reduce stripe_size.
Chris Mason86db2572013-02-20 16:23:40 -05004879 */
4880 if (stripe_size * data_stripes > max_chunk_size) {
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004881 /*
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02004882 * Reduce stripe_size, round it up to a 16MB boundary again and
4883 * then use it, unless it ends up being even bigger than the
4884 * previous value we had already.
Chris Mason86db2572013-02-20 16:23:40 -05004885 */
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02004886 stripe_size = min(round_up(div_u64(max_chunk_size,
4887 data_stripes), SZ_16M),
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004888 stripe_size);
Chris Mason86db2572013-02-20 16:23:40 -05004889 }
4890
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004891 /* align to BTRFS_STRIPE_LEN */
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004892 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
Arne Jansen73c5de02011-04-12 12:07:57 +02004893
Miao Xieb2117a32011-01-05 10:07:28 +00004894 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4895 if (!map) {
4896 ret = -ENOMEM;
4897 goto error;
4898 }
4899 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004900
Arne Jansen73c5de02011-04-12 12:07:57 +02004901 for (i = 0; i < ndevs; ++i) {
4902 for (j = 0; j < dev_stripes; ++j) {
4903 int s = i * dev_stripes + j;
4904 map->stripes[s].dev = devices_info[i].dev;
4905 map->stripes[s].physical = devices_info[i].dev_offset +
4906 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004907 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004908 }
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004909 map->stripe_len = BTRFS_STRIPE_LEN;
4910 map->io_align = BTRFS_STRIPE_LEN;
4911 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04004912 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004913 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004914
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02004915 chunk_size = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004916
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02004917 trace_btrfs_chunk_alloc(info, map, start, chunk_size);
liubo1abe9b82011-03-24 11:18:59 +00004918
David Sterba172ddd62011-04-21 00:48:27 +02004919 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004920 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004921 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004922 ret = -ENOMEM;
4923 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004924 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004925 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04004926 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05004927 em->start = start;
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02004928 em->len = chunk_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004929 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004930 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004931 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004932
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004933 em_tree = &info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004934 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004935 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004936 if (ret) {
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03004937 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004938 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004939 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004940 }
Yan Zheng2b820322008-11-17 21:11:30 -05004941
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03004942 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4943 refcount_inc(&em->refs);
4944 write_unlock(&em_tree->lock);
4945
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02004946 ret = btrfs_make_block_group(trans, 0, type, start, chunk_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04004947 if (ret)
4948 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004949
Hans van Kranenburg2f29df42018-10-04 23:24:38 +02004950 for (i = 0; i < map->num_stripes; i++)
4951 btrfs_device_set_bytes_used(map->stripes[i].dev,
4952 map->stripes[i].dev->bytes_used + stripe_size);
Miao Xie43530c42014-09-03 21:35:36 +08004953
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004954 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
Miao Xie1c116182014-09-03 21:35:37 +08004955
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004956 free_extent_map(em);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004957 check_raid56_incompat_flag(info, type);
David Woodhouse53b381b2013-01-29 18:40:14 -05004958
Miao Xieb2117a32011-01-05 10:07:28 +00004959 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004960 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004961
Josef Bacik6df9a952013-06-27 13:22:46 -04004962error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004963 write_lock(&em_tree->lock);
4964 remove_extent_mapping(em_tree, em);
4965 write_unlock(&em_tree->lock);
4966
4967 /* One for our allocation */
4968 free_extent_map(em);
4969 /* One for the tree reference */
4970 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004971 /* One for the pending_chunks list reference */
4972 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004973error:
Miao Xieb2117a32011-01-05 10:07:28 +00004974 kfree(devices_info);
4975 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004976}
4977
Josef Bacik6df9a952013-06-27 13:22:46 -04004978int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Nikolay Borisov97aff912018-07-20 19:37:53 +03004979 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004980{
Nikolay Borisov97aff912018-07-20 19:37:53 +03004981 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004982 struct btrfs_root *extent_root = fs_info->extent_root;
4983 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05004984 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05004985 struct btrfs_device *device;
4986 struct btrfs_chunk *chunk;
4987 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004988 struct extent_map *em;
4989 struct map_lookup *map;
4990 size_t item_size;
4991 u64 dev_offset;
4992 u64 stripe_size;
4993 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08004994 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004995
Omar Sandoval60ca8422018-05-16 16:34:31 -07004996 em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
Liu Bo592d92e2017-03-14 13:33:55 -07004997 if (IS_ERR(em))
4998 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04004999
Jeff Mahoney95617d62015-06-03 10:55:48 -04005000 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04005001 item_size = btrfs_chunk_item_size(map->num_stripes);
5002 stripe_size = em->orig_block_len;
5003
5004 chunk = kzalloc(item_size, GFP_NOFS);
5005 if (!chunk) {
5006 ret = -ENOMEM;
5007 goto out;
5008 }
5009
Filipe Manana50460e32015-11-20 10:42:47 +00005010 /*
5011 * Take the device list mutex to prevent races with the final phase of
5012 * a device replace operation that replaces the device object associated
5013 * with the map's stripes, because the device object's id can change
5014 * at any time during that final phase of the device replace operation
5015 * (dev-replace.c:btrfs_dev_replace_finishing()).
5016 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005017 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04005018 for (i = 0; i < map->num_stripes; i++) {
5019 device = map->stripes[i].dev;
5020 dev_offset = map->stripes[i].physical;
5021
Yan Zheng2b820322008-11-17 21:11:30 -05005022 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07005023 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005024 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03005025 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
5026 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005027 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005028 break;
5029 }
5030 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005031 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00005032 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05005033 }
5034
Yan Zheng2b820322008-11-17 21:11:30 -05005035 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005036 for (i = 0; i < map->num_stripes; i++) {
5037 device = map->stripes[i].dev;
5038 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05005039
5040 btrfs_set_stack_stripe_devid(stripe, device->devid);
5041 btrfs_set_stack_stripe_offset(stripe, dev_offset);
5042 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
5043 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05005044 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005045 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05005046
5047 btrfs_set_stack_chunk_length(chunk, chunk_size);
5048 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
5049 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
5050 btrfs_set_stack_chunk_type(chunk, map->type);
5051 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
5052 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
5053 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005054 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05005055 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
5056
5057 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5058 key.type = BTRFS_CHUNK_ITEM_KEY;
5059 key.offset = chunk_offset;
5060
5061 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005062 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5063 /*
5064 * TODO: Cleanup of inserted chunk root in case of
5065 * failure.
5066 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005067 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05005068 }
liubo1abe9b82011-03-24 11:18:59 +00005069
Josef Bacik6df9a952013-06-27 13:22:46 -04005070out:
Yan Zheng2b820322008-11-17 21:11:30 -05005071 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04005072 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005073 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005074}
5075
5076/*
5077 * Chunk allocation falls into two parts. The first part does works
5078 * that make the new allocated chunk useable, but not do any operation
5079 * that modifies the chunk tree. The second part does the works that
5080 * require modifying the chunk tree. This division is important for the
5081 * bootstrap process of adding storage to a seed btrfs.
5082 */
Nikolay Borisovc216b202018-06-20 15:49:06 +03005083int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type)
Yan Zheng2b820322008-11-17 21:11:30 -05005084{
5085 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005086
Nikolay Borisovc216b202018-06-20 15:49:06 +03005087 lockdep_assert_held(&trans->fs_info->chunk_mutex);
5088 chunk_offset = find_next_chunk(trans->fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005089 return __btrfs_alloc_chunk(trans, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05005090}
5091
Chris Masond3977122009-01-05 21:25:51 -05005092static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +01005093 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05005094{
5095 u64 chunk_offset;
5096 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005097 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05005098 int ret;
5099
Josef Bacik6df9a952013-06-27 13:22:46 -04005100 chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005101 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005102 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005103 if (ret)
5104 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005105
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005106 sys_chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005107 alloc_profile = btrfs_system_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005108 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005109 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005110}
5111
Miao Xied20983b2014-07-03 18:22:13 +08005112static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5113{
5114 int max_errors;
5115
5116 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5117 BTRFS_BLOCK_GROUP_RAID10 |
5118 BTRFS_BLOCK_GROUP_RAID5 |
5119 BTRFS_BLOCK_GROUP_DUP)) {
5120 max_errors = 1;
5121 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5122 max_errors = 2;
5123 } else {
5124 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005125 }
5126
Miao Xied20983b2014-07-03 18:22:13 +08005127 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05005128}
5129
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005130int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005131{
5132 struct extent_map *em;
5133 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005134 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005135 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005136 int i;
5137
Omar Sandoval60ca8422018-05-16 16:34:31 -07005138 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07005139 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005140 return 1;
5141
Jeff Mahoney95617d62015-06-03 10:55:48 -04005142 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005143 for (i = 0; i < map->num_stripes; i++) {
Anand Jaine6e674b2017-12-04 12:54:54 +08005144 if (test_bit(BTRFS_DEV_STATE_MISSING,
5145 &map->stripes[i].dev->dev_state)) {
Miao Xied20983b2014-07-03 18:22:13 +08005146 miss_ndevs++;
5147 continue;
5148 }
Anand Jainebbede42017-12-04 12:54:52 +08005149 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5150 &map->stripes[i].dev->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05005151 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005152 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005153 }
5154 }
Miao Xied20983b2014-07-03 18:22:13 +08005155
5156 /*
5157 * If the number of missing devices is larger than max errors,
5158 * we can not write the data into that chunk successfully, so
5159 * set it readonly.
5160 */
5161 if (miss_ndevs > btrfs_chunk_max_errors(map))
5162 readonly = 1;
5163end:
Yan Zheng2b820322008-11-17 21:11:30 -05005164 free_extent_map(em);
5165 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005166}
5167
5168void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
5169{
David Sterbaa8067e02011-04-21 00:34:43 +02005170 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04005171}
5172
5173void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
5174{
5175 struct extent_map *em;
5176
Chris Masond3977122009-01-05 21:25:51 -05005177 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04005178 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005179 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
5180 if (em)
5181 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005182 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005183 if (!em)
5184 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005185 /* once for us */
5186 free_extent_map(em);
5187 /* once for the tree */
5188 free_extent_map(em);
5189 }
5190}
5191
Stefan Behrens5d964052012-11-05 14:59:07 +01005192int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005193{
5194 struct extent_map *em;
5195 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005196 int ret;
5197
Omar Sandoval60ca8422018-05-16 16:34:31 -07005198 em = btrfs_get_chunk_map(fs_info, logical, len);
Liu Bo592d92e2017-03-14 13:33:55 -07005199 if (IS_ERR(em))
5200 /*
5201 * We could return errors for these cases, but that could get
5202 * ugly and we'd probably do the same thing which is just not do
5203 * anything else and exit, so return 1 so the callers don't try
5204 * to use other copies.
5205 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005206 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005207
Jeff Mahoney95617d62015-06-03 10:55:48 -04005208 map = em->map_lookup;
Chris Masonf1885912008-04-09 16:28:12 -04005209 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5210 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005211 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5212 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005213 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5214 ret = 2;
5215 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
Liu Bo8810f752018-01-02 13:36:41 -07005216 /*
5217 * There could be two corrupted data stripes, we need
5218 * to loop retry in order to rebuild the correct data.
Nikolay Borisove7e02092018-06-20 15:48:55 +03005219 *
Liu Bo8810f752018-01-02 13:36:41 -07005220 * Fail a stripe at a time on every retry except the
5221 * stripe under reconstruction.
5222 */
5223 ret = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04005224 else
5225 ret = 1;
5226 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005227
David Sterba7e79cb82018-03-24 02:11:38 +01005228 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
Liu Bo6fad8232017-03-14 13:33:59 -07005229 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5230 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005231 ret++;
David Sterba7e79cb82018-03-24 02:11:38 +01005232 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005233
Chris Masonf1885912008-04-09 16:28:12 -04005234 return ret;
5235}
5236
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005237unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005238 u64 logical)
5239{
5240 struct extent_map *em;
5241 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005242 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005243
Omar Sandoval60ca8422018-05-16 16:34:31 -07005244 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005245
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005246 if (!WARN_ON(IS_ERR(em))) {
5247 map = em->map_lookup;
5248 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5249 len = map->stripe_len * nr_data_stripes(map);
5250 free_extent_map(em);
5251 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005252 return len;
5253}
5254
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005255int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005256{
5257 struct extent_map *em;
5258 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005259 int ret = 0;
5260
Omar Sandoval60ca8422018-05-16 16:34:31 -07005261 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005262
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005263 if(!WARN_ON(IS_ERR(em))) {
5264 map = em->map_lookup;
5265 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5266 ret = 1;
5267 free_extent_map(em);
5268 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005269 return ret;
5270}
5271
Stefan Behrens30d98612012-11-06 14:52:18 +01005272static int find_live_mirror(struct btrfs_fs_info *fs_info,
Anand Jain99f92a72018-03-14 16:29:12 +08005273 struct map_lookup *map, int first,
Anand Jain8ba0ae72018-03-14 16:29:13 +08005274 int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005275{
5276 int i;
Anand Jain99f92a72018-03-14 16:29:12 +08005277 int num_stripes;
Anand Jain8ba0ae72018-03-14 16:29:13 +08005278 int preferred_mirror;
Stefan Behrens30d98612012-11-06 14:52:18 +01005279 int tolerance;
5280 struct btrfs_device *srcdev;
5281
Anand Jain99f92a72018-03-14 16:29:12 +08005282 ASSERT((map->type &
5283 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)));
5284
5285 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5286 num_stripes = map->sub_stripes;
5287 else
5288 num_stripes = map->num_stripes;
5289
Anand Jain8ba0ae72018-03-14 16:29:13 +08005290 preferred_mirror = first + current->pid % num_stripes;
5291
Stefan Behrens30d98612012-11-06 14:52:18 +01005292 if (dev_replace_is_ongoing &&
5293 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5294 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5295 srcdev = fs_info->dev_replace.srcdev;
5296 else
5297 srcdev = NULL;
5298
5299 /*
5300 * try to avoid the drive that is the source drive for a
5301 * dev-replace procedure, only choose it if no other non-missing
5302 * mirror is available
5303 */
5304 for (tolerance = 0; tolerance < 2; tolerance++) {
Anand Jain8ba0ae72018-03-14 16:29:13 +08005305 if (map->stripes[preferred_mirror].dev->bdev &&
5306 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5307 return preferred_mirror;
Anand Jain99f92a72018-03-14 16:29:12 +08005308 for (i = first; i < first + num_stripes; i++) {
Stefan Behrens30d98612012-11-06 14:52:18 +01005309 if (map->stripes[i].dev->bdev &&
5310 (tolerance || map->stripes[i].dev != srcdev))
5311 return i;
5312 }
Chris Masondfe25022008-05-13 13:46:40 -04005313 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005314
Chris Masondfe25022008-05-13 13:46:40 -04005315 /* we couldn't find one that doesn't fail. Just return something
5316 * and the io error handling code will clean up eventually
5317 */
Anand Jain8ba0ae72018-03-14 16:29:13 +08005318 return preferred_mirror;
Chris Masondfe25022008-05-13 13:46:40 -04005319}
5320
David Woodhouse53b381b2013-01-29 18:40:14 -05005321static inline int parity_smaller(u64 a, u64 b)
5322{
5323 return a > b;
5324}
5325
5326/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005327static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005328{
5329 struct btrfs_bio_stripe s;
5330 int i;
5331 u64 l;
5332 int again = 1;
5333
5334 while (again) {
5335 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005336 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005337 if (parity_smaller(bbio->raid_map[i],
5338 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005339 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005340 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005341 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005342 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005343 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005344 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005345
David Woodhouse53b381b2013-01-29 18:40:14 -05005346 again = 1;
5347 }
5348 }
5349 }
5350}
5351
Zhao Lei6e9606d2015-01-20 15:11:34 +08005352static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5353{
5354 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005355 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005356 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005357 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005358 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005359 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005360 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005361 /*
5362 * plus the raid_map, which includes both the tgt dev
5363 * and the stripes
5364 */
5365 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005366 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005367
5368 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005369 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005370
5371 return bbio;
5372}
5373
5374void btrfs_get_bbio(struct btrfs_bio *bbio)
5375{
Elena Reshetova140475a2017-03-03 10:55:10 +02005376 WARN_ON(!refcount_read(&bbio->refs));
5377 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005378}
5379
5380void btrfs_put_bbio(struct btrfs_bio *bbio)
5381{
5382 if (!bbio)
5383 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005384 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005385 kfree(bbio);
5386}
5387
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005388/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5389/*
5390 * Please note that, discard won't be sent to target device of device
5391 * replace.
5392 */
5393static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5394 u64 logical, u64 length,
5395 struct btrfs_bio **bbio_ret)
5396{
5397 struct extent_map *em;
5398 struct map_lookup *map;
5399 struct btrfs_bio *bbio;
5400 u64 offset;
5401 u64 stripe_nr;
5402 u64 stripe_nr_end;
5403 u64 stripe_end_offset;
5404 u64 stripe_cnt;
5405 u64 stripe_len;
5406 u64 stripe_offset;
5407 u64 num_stripes;
5408 u32 stripe_index;
5409 u32 factor = 0;
5410 u32 sub_stripes = 0;
5411 u64 stripes_per_dev = 0;
5412 u32 remaining_stripes = 0;
5413 u32 last_stripe = 0;
5414 int ret = 0;
5415 int i;
5416
5417 /* discard always return a bbio */
5418 ASSERT(bbio_ret);
5419
Omar Sandoval60ca8422018-05-16 16:34:31 -07005420 em = btrfs_get_chunk_map(fs_info, logical, length);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005421 if (IS_ERR(em))
5422 return PTR_ERR(em);
5423
5424 map = em->map_lookup;
5425 /* we don't discard raid56 yet */
5426 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5427 ret = -EOPNOTSUPP;
5428 goto out;
5429 }
5430
5431 offset = logical - em->start;
5432 length = min_t(u64, em->len - offset, length);
5433
5434 stripe_len = map->stripe_len;
5435 /*
5436 * stripe_nr counts the total number of stripes we have to stride
5437 * to get to this block
5438 */
5439 stripe_nr = div64_u64(offset, stripe_len);
5440
5441 /* stripe_offset is the offset of this block in its stripe */
5442 stripe_offset = offset - stripe_nr * stripe_len;
5443
5444 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005445 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005446 stripe_cnt = stripe_nr_end - stripe_nr;
5447 stripe_end_offset = stripe_nr_end * map->stripe_len -
5448 (offset + length);
5449 /*
5450 * after this, stripe_nr is the number of stripes on this
5451 * device we have to walk to find the data, and stripe_index is
5452 * the number of our device in the stripe array
5453 */
5454 num_stripes = 1;
5455 stripe_index = 0;
5456 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5457 BTRFS_BLOCK_GROUP_RAID10)) {
5458 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5459 sub_stripes = 1;
5460 else
5461 sub_stripes = map->sub_stripes;
5462
5463 factor = map->num_stripes / sub_stripes;
5464 num_stripes = min_t(u64, map->num_stripes,
5465 sub_stripes * stripe_cnt);
5466 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5467 stripe_index *= sub_stripes;
5468 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5469 &remaining_stripes);
5470 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5471 last_stripe *= sub_stripes;
5472 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5473 BTRFS_BLOCK_GROUP_DUP)) {
5474 num_stripes = map->num_stripes;
5475 } else {
5476 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5477 &stripe_index);
5478 }
5479
5480 bbio = alloc_btrfs_bio(num_stripes, 0);
5481 if (!bbio) {
5482 ret = -ENOMEM;
5483 goto out;
5484 }
5485
5486 for (i = 0; i < num_stripes; i++) {
5487 bbio->stripes[i].physical =
5488 map->stripes[stripe_index].physical +
5489 stripe_offset + stripe_nr * map->stripe_len;
5490 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5491
5492 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5493 BTRFS_BLOCK_GROUP_RAID10)) {
5494 bbio->stripes[i].length = stripes_per_dev *
5495 map->stripe_len;
5496
5497 if (i / sub_stripes < remaining_stripes)
5498 bbio->stripes[i].length +=
5499 map->stripe_len;
5500
5501 /*
5502 * Special for the first stripe and
5503 * the last stripe:
5504 *
5505 * |-------|...|-------|
5506 * |----------|
5507 * off end_off
5508 */
5509 if (i < sub_stripes)
5510 bbio->stripes[i].length -=
5511 stripe_offset;
5512
5513 if (stripe_index >= last_stripe &&
5514 stripe_index <= (last_stripe +
5515 sub_stripes - 1))
5516 bbio->stripes[i].length -=
5517 stripe_end_offset;
5518
5519 if (i == sub_stripes - 1)
5520 stripe_offset = 0;
5521 } else {
5522 bbio->stripes[i].length = length;
5523 }
5524
5525 stripe_index++;
5526 if (stripe_index == map->num_stripes) {
5527 stripe_index = 0;
5528 stripe_nr++;
5529 }
5530 }
5531
5532 *bbio_ret = bbio;
5533 bbio->map_type = map->type;
5534 bbio->num_stripes = num_stripes;
5535out:
5536 free_extent_map(em);
5537 return ret;
5538}
5539
Liu Bo5ab56092017-03-14 13:33:57 -07005540/*
5541 * In dev-replace case, for repair case (that's the only case where the mirror
5542 * is selected explicitly when calling btrfs_map_block), blocks left of the
5543 * left cursor can also be read from the target drive.
5544 *
5545 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5546 * array of stripes.
5547 * For READ, it also needs to be supported using the same mirror number.
5548 *
5549 * If the requested block is not left of the left cursor, EIO is returned. This
5550 * can happen because btrfs_num_copies() returns one more in the dev-replace
5551 * case.
5552 */
5553static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5554 u64 logical, u64 length,
5555 u64 srcdev_devid, int *mirror_num,
5556 u64 *physical)
5557{
5558 struct btrfs_bio *bbio = NULL;
5559 int num_stripes;
5560 int index_srcdev = 0;
5561 int found = 0;
5562 u64 physical_of_found = 0;
5563 int i;
5564 int ret = 0;
5565
5566 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5567 logical, &length, &bbio, 0, 0);
5568 if (ret) {
5569 ASSERT(bbio == NULL);
5570 return ret;
5571 }
5572
5573 num_stripes = bbio->num_stripes;
5574 if (*mirror_num > num_stripes) {
5575 /*
5576 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5577 * that means that the requested area is not left of the left
5578 * cursor
5579 */
5580 btrfs_put_bbio(bbio);
5581 return -EIO;
5582 }
5583
5584 /*
5585 * process the rest of the function using the mirror_num of the source
5586 * drive. Therefore look it up first. At the end, patch the device
5587 * pointer to the one of the target drive.
5588 */
5589 for (i = 0; i < num_stripes; i++) {
5590 if (bbio->stripes[i].dev->devid != srcdev_devid)
5591 continue;
5592
5593 /*
5594 * In case of DUP, in order to keep it simple, only add the
5595 * mirror with the lowest physical address
5596 */
5597 if (found &&
5598 physical_of_found <= bbio->stripes[i].physical)
5599 continue;
5600
5601 index_srcdev = i;
5602 found = 1;
5603 physical_of_found = bbio->stripes[i].physical;
5604 }
5605
5606 btrfs_put_bbio(bbio);
5607
5608 ASSERT(found);
5609 if (!found)
5610 return -EIO;
5611
5612 *mirror_num = index_srcdev + 1;
5613 *physical = physical_of_found;
5614 return ret;
5615}
5616
Liu Bo73c0f222017-03-14 13:33:58 -07005617static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5618 struct btrfs_bio **bbio_ret,
5619 struct btrfs_dev_replace *dev_replace,
5620 int *num_stripes_ret, int *max_errors_ret)
5621{
5622 struct btrfs_bio *bbio = *bbio_ret;
5623 u64 srcdev_devid = dev_replace->srcdev->devid;
5624 int tgtdev_indexes = 0;
5625 int num_stripes = *num_stripes_ret;
5626 int max_errors = *max_errors_ret;
5627 int i;
5628
5629 if (op == BTRFS_MAP_WRITE) {
5630 int index_where_to_add;
5631
5632 /*
5633 * duplicate the write operations while the dev replace
5634 * procedure is running. Since the copying of the old disk to
5635 * the new disk takes place at run time while the filesystem is
5636 * mounted writable, the regular write operations to the old
5637 * disk have to be duplicated to go to the new disk as well.
5638 *
5639 * Note that device->missing is handled by the caller, and that
5640 * the write to the old disk is already set up in the stripes
5641 * array.
5642 */
5643 index_where_to_add = num_stripes;
5644 for (i = 0; i < num_stripes; i++) {
5645 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5646 /* write to new disk, too */
5647 struct btrfs_bio_stripe *new =
5648 bbio->stripes + index_where_to_add;
5649 struct btrfs_bio_stripe *old =
5650 bbio->stripes + i;
5651
5652 new->physical = old->physical;
5653 new->length = old->length;
5654 new->dev = dev_replace->tgtdev;
5655 bbio->tgtdev_map[i] = index_where_to_add;
5656 index_where_to_add++;
5657 max_errors++;
5658 tgtdev_indexes++;
5659 }
5660 }
5661 num_stripes = index_where_to_add;
5662 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5663 int index_srcdev = 0;
5664 int found = 0;
5665 u64 physical_of_found = 0;
5666
5667 /*
5668 * During the dev-replace procedure, the target drive can also
5669 * be used to read data in case it is needed to repair a corrupt
5670 * block elsewhere. This is possible if the requested area is
5671 * left of the left cursor. In this area, the target drive is a
5672 * full copy of the source drive.
5673 */
5674 for (i = 0; i < num_stripes; i++) {
5675 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5676 /*
5677 * In case of DUP, in order to keep it simple,
5678 * only add the mirror with the lowest physical
5679 * address
5680 */
5681 if (found &&
5682 physical_of_found <=
5683 bbio->stripes[i].physical)
5684 continue;
5685 index_srcdev = i;
5686 found = 1;
5687 physical_of_found = bbio->stripes[i].physical;
5688 }
5689 }
5690 if (found) {
5691 struct btrfs_bio_stripe *tgtdev_stripe =
5692 bbio->stripes + num_stripes;
5693
5694 tgtdev_stripe->physical = physical_of_found;
5695 tgtdev_stripe->length =
5696 bbio->stripes[index_srcdev].length;
5697 tgtdev_stripe->dev = dev_replace->tgtdev;
5698 bbio->tgtdev_map[index_srcdev] = num_stripes;
5699
5700 tgtdev_indexes++;
5701 num_stripes++;
5702 }
5703 }
5704
5705 *num_stripes_ret = num_stripes;
5706 *max_errors_ret = max_errors;
5707 bbio->num_tgtdevs = tgtdev_indexes;
5708 *bbio_ret = bbio;
5709}
5710
Liu Bo2b19a1f2017-03-14 13:34:00 -07005711static bool need_full_stripe(enum btrfs_map_op op)
5712{
5713 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5714}
5715
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005716static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5717 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005718 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005719 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005720 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005721{
5722 struct extent_map *em;
5723 struct map_lookup *map;
Chris Mason0b86a832008-03-24 15:01:56 -04005724 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005725 u64 stripe_offset;
5726 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005727 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005728 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005729 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005730 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005731 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005732 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005733 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005734 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005735 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5736 int dev_replace_is_ongoing = 0;
5737 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005738 int patch_the_first_stripe_for_dev_replace = 0;
5739 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005740 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005741
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005742 if (op == BTRFS_MAP_DISCARD)
5743 return __btrfs_map_block_for_discard(fs_info, logical,
5744 *length, bbio_ret);
5745
Omar Sandoval60ca8422018-05-16 16:34:31 -07005746 em = btrfs_get_chunk_map(fs_info, logical, *length);
Liu Bo592d92e2017-03-14 13:33:55 -07005747 if (IS_ERR(em))
5748 return PTR_ERR(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005749
Jeff Mahoney95617d62015-06-03 10:55:48 -04005750 map = em->map_lookup;
Chris Mason0b86a832008-03-24 15:01:56 -04005751 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005752
David Woodhouse53b381b2013-01-29 18:40:14 -05005753 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005754 stripe_nr = offset;
5755 /*
5756 * stripe_nr counts the total number of stripes we have to stride
5757 * to get to this block
5758 */
David Sterba47c57132015-02-20 18:43:47 +01005759 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005760
David Woodhouse53b381b2013-01-29 18:40:14 -05005761 stripe_offset = stripe_nr * stripe_len;
Josef Bacike042d1e2016-04-12 12:54:40 -04005762 if (offset < stripe_offset) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005763 btrfs_crit(fs_info,
5764 "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
Josef Bacike042d1e2016-04-12 12:54:40 -04005765 stripe_offset, offset, em->start, logical,
5766 stripe_len);
5767 free_extent_map(em);
5768 return -EINVAL;
5769 }
Chris Mason593060d2008-03-25 16:50:33 -04005770
5771 /* stripe_offset is the offset of this block in its stripe*/
5772 stripe_offset = offset - stripe_offset;
5773
David Woodhouse53b381b2013-01-29 18:40:14 -05005774 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005775 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005776 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5777 raid56_full_stripe_start = offset;
5778
5779 /* allow a write of a full stripe, but make sure we don't
5780 * allow straddling of stripes
5781 */
David Sterba47c57132015-02-20 18:43:47 +01005782 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5783 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005784 raid56_full_stripe_start *= full_stripe_len;
5785 }
5786
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005787 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005788 u64 max_len;
5789 /* For writes to RAID[56], allow a full stripeset across all disks.
5790 For other RAID types and for RAID[56] reads, just allow a single
5791 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005792 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005793 (op == BTRFS_MAP_WRITE)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005794 max_len = stripe_len * nr_data_stripes(map) -
5795 (offset - raid56_full_stripe_start);
5796 } else {
5797 /* we limit the length of each bio to what fits in a stripe */
5798 max_len = stripe_len - stripe_offset;
5799 }
5800 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005801 } else {
5802 *length = em->len - offset;
5803 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005804
David Woodhouse53b381b2013-01-29 18:40:14 -05005805 /* This is for when we're called from btrfs_merge_bio_hook() and all
5806 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005807 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005808 goto out;
5809
David Sterba7e79cb82018-03-24 02:11:38 +01005810 btrfs_dev_replace_read_lock(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005811 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5812 if (!dev_replace_is_ongoing)
David Sterba7e79cb82018-03-24 02:11:38 +01005813 btrfs_dev_replace_read_unlock(dev_replace);
Liu Bo73beece2015-07-17 16:49:19 +08005814 else
5815 btrfs_dev_replace_set_lock_blocking(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005816
Stefan Behrensad6d6202012-11-06 15:06:47 +01005817 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005818 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07005819 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
5820 dev_replace->srcdev->devid,
5821 &mirror_num,
5822 &physical_to_patch_in_first_stripe);
5823 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005824 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07005825 else
5826 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005827 } else if (mirror_num > map->num_stripes) {
5828 mirror_num = 0;
5829 }
5830
Chris Masonf2d8d742008-04-21 10:03:05 -04005831 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005832 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005833 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01005834 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5835 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005836 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08005837 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005838 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Anand Jainde483732017-10-12 16:43:00 +08005839 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005840 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005841 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005842 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005843 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005844 stripe_index = find_live_mirror(fs_info, map, 0,
Stefan Behrens30d98612012-11-06 14:52:18 +01005845 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005846 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005847 }
Chris Mason2fff7342008-04-29 14:12:09 -04005848
Chris Mason611f0e02008-04-03 16:29:03 -04005849 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Anand Jainde483732017-10-12 16:43:00 +08005850 if (need_full_stripe(op)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005851 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005852 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005853 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005854 } else {
5855 mirror_num = 1;
5856 }
Chris Mason2fff7342008-04-29 14:12:09 -04005857
Chris Mason321aecc2008-04-16 10:49:51 -04005858 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005859 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005860
David Sterba47c57132015-02-20 18:43:47 +01005861 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005862 stripe_index *= map->sub_stripes;
5863
Anand Jainde483732017-10-12 16:43:00 +08005864 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005865 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005866 else if (mirror_num)
5867 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005868 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005869 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005870 stripe_index = find_live_mirror(fs_info, map,
5871 stripe_index,
Stefan Behrens30d98612012-11-06 14:52:18 +01005872 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005873 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005874 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005875
Zhao Leiffe2d202015-01-20 15:11:44 +08005876 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08005877 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005878 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07005879 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbab8b93ad2015-01-16 17:26:13 +01005880 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005881
5882 /* RAID[56] write or recovery. Return all stripes */
5883 num_stripes = map->num_stripes;
5884 max_errors = nr_parity_stripes(map);
5885
David Woodhouse53b381b2013-01-29 18:40:14 -05005886 *length = map->stripe_len;
5887 stripe_index = 0;
5888 stripe_offset = 0;
5889 } else {
5890 /*
5891 * Mirror #0 or #1 means the original data block.
5892 * Mirror #2 is RAID5 parity block.
5893 * Mirror #3 is RAID6 Q block.
5894 */
David Sterba47c57132015-02-20 18:43:47 +01005895 stripe_nr = div_u64_rem(stripe_nr,
5896 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005897 if (mirror_num > 1)
5898 stripe_index = nr_data_stripes(map) +
5899 mirror_num - 2;
5900
5901 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005902 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5903 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005904 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08005905 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005906 }
Chris Mason8790d502008-04-03 16:29:03 -04005907 } else {
5908 /*
David Sterba47c57132015-02-20 18:43:47 +01005909 * after this, stripe_nr is the number of stripes on this
5910 * device we have to walk to find the data, and stripe_index is
5911 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005912 */
David Sterba47c57132015-02-20 18:43:47 +01005913 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5914 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005915 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005916 }
Josef Bacike042d1e2016-04-12 12:54:40 -04005917 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005918 btrfs_crit(fs_info,
5919 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04005920 stripe_index, map->num_stripes);
5921 ret = -EINVAL;
5922 goto out;
5923 }
Chris Mason593060d2008-03-25 16:50:33 -04005924
Stefan Behrens472262f2012-11-06 14:43:46 +01005925 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07005926 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005927 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005928 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005929 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005930 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005931 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005932 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005933
Zhao Lei6e9606d2015-01-20 15:11:34 +08005934 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005935 if (!bbio) {
5936 ret = -ENOMEM;
5937 goto out;
5938 }
Liu Bo6fad8232017-03-14 13:33:59 -07005939 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
Miao Xie2c8cdd62014-11-14 16:06:25 +08005940 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005941
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005942 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07005943 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
5944 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005945 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005946 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005947
5948 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5949 sizeof(struct btrfs_bio_stripe) *
5950 num_alloc_stripes +
5951 sizeof(int) * tgtdev_indexes);
5952
5953 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005954 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005955
5956 /* Fill in the logical address of each stripe */
5957 tmp = stripe_nr * nr_data_stripes(map);
5958 for (i = 0; i < nr_data_stripes(map); i++)
5959 bbio->raid_map[(i+rot) % num_stripes] =
5960 em->start + (tmp + i) * map->stripe_len;
5961
5962 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5963 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5964 bbio->raid_map[(i+rot+1) % num_stripes] =
5965 RAID6_Q_STRIPE;
5966 }
5967
Li Zefanec9ef7a2011-12-01 14:06:42 +08005968
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005969 for (i = 0; i < num_stripes; i++) {
5970 bbio->stripes[i].physical =
5971 map->stripes[stripe_index].physical +
5972 stripe_offset +
5973 stripe_nr * map->stripe_len;
5974 bbio->stripes[i].dev =
5975 map->stripes[stripe_index].dev;
5976 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04005977 }
Li Zefande11cc12011-12-01 12:55:47 +08005978
Liu Bo2b19a1f2017-03-14 13:34:00 -07005979 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08005980 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005981
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005982 if (bbio->raid_map)
5983 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005984
Liu Bo73c0f222017-03-14 13:33:58 -07005985 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005986 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07005987 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
5988 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01005989 }
5990
Li Zefande11cc12011-12-01 12:55:47 +08005991 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005992 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005993 bbio->num_stripes = num_stripes;
5994 bbio->max_errors = max_errors;
5995 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005996
5997 /*
5998 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5999 * mirror_num == num_stripes + 1 && dev_replace target drive is
6000 * available as a mirror
6001 */
6002 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
6003 WARN_ON(num_stripes > 1);
6004 bbio->stripes[0].dev = dev_replace->tgtdev;
6005 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
6006 bbio->mirror_num = map->num_stripes + 1;
6007 }
Chris Masoncea9e442008-04-09 16:28:12 -04006008out:
Liu Bo73beece2015-07-17 16:49:19 +08006009 if (dev_replace_is_ongoing) {
David Sterba7fb2ece2018-08-24 17:33:58 +02006010 ASSERT(atomic_read(&dev_replace->blocking_readers) > 0);
6011 btrfs_dev_replace_read_lock(dev_replace);
6012 /* Barrier implied by atomic_dec_and_test */
6013 if (atomic_dec_and_test(&dev_replace->blocking_readers))
6014 cond_wake_up_nomb(&dev_replace->read_lock_wq);
David Sterba7e79cb82018-03-24 02:11:38 +01006015 btrfs_dev_replace_read_unlock(dev_replace);
Liu Bo73beece2015-07-17 16:49:19 +08006016 }
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
Nikolay Borisov63a9c7b2018-05-04 10:53:05 +03006037int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
6038 u64 physical, u64 **logical, int *naddrs, int *stripe_len)
Yan Zhenga512bbf2008-12-08 16:46:26 -05006039{
Yan Zhenga512bbf2008-12-08 16:46:26 -05006040 struct extent_map *em;
6041 struct map_lookup *map;
6042 u64 *buf;
6043 u64 bytenr;
6044 u64 length;
6045 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006046 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006047 int i, j, nr = 0;
6048
Omar Sandoval60ca8422018-05-16 16:34:31 -07006049 em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07006050 if (IS_ERR(em))
Josef Bacik835d9742013-03-19 12:13:25 -04006051 return -EIO;
Josef Bacik835d9742013-03-19 12:13:25 -04006052
Jeff Mahoney95617d62015-06-03 10:55:48 -04006053 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006054 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05006055 rmap_len = map->stripe_len;
6056
Yan Zhenga512bbf2008-12-08 16:46:26 -05006057 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01006058 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006059 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01006060 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08006061 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01006062 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05006063 rmap_len = map->stripe_len * nr_data_stripes(map);
6064 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006065
David Sterba31e818f2015-02-20 18:00:26 +01006066 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006067 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05006068
6069 for (i = 0; i < map->num_stripes; i++) {
Yan Zhenga512bbf2008-12-08 16:46:26 -05006070 if (map->stripes[i].physical > physical ||
6071 map->stripes[i].physical + length <= physical)
6072 continue;
6073
6074 stripe_nr = physical - map->stripes[i].physical;
Liu Bo42c61ab2017-04-03 13:45:24 -07006075 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006076
6077 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
6078 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01006079 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006080 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
6081 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05006082 } /* else if RAID[56], multiply by nr_data_stripes().
6083 * Alternatively, just use rmap_len below instead of
6084 * map->stripe_len */
6085
6086 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05006087 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006088 for (j = 0; j < nr; j++) {
6089 if (buf[j] == bytenr)
6090 break;
6091 }
Chris Mason934d3752008-12-08 16:43:10 -05006092 if (j == nr) {
6093 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006094 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05006095 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006096 }
6097
Yan Zhenga512bbf2008-12-08 16:46:26 -05006098 *logical = buf;
6099 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006100 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006101
6102 free_extent_map(em);
6103 return 0;
6104}
6105
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006106static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08006107{
Mike Snitzer326e1db2015-05-22 09:14:03 -04006108 bio->bi_private = bbio->private;
6109 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006110 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04006111
Zhao Lei6e9606d2015-01-20 15:11:34 +08006112 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08006113}
6114
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006115static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04006116{
Chris Mason9be33952013-05-17 18:30:14 -04006117 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006118 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04006119
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006120 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02006121 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006122 if (bio->bi_status == BLK_STS_IOERR ||
6123 bio->bi_status == BLK_STS_TARGET) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02006124 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04006125 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08006126 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006127
6128 BUG_ON(stripe_index >= bbio->num_stripes);
6129 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02006130 if (dev->bdev) {
Mike Christie37226b22016-06-05 14:31:52 -05006131 if (bio_op(bio) == REQ_OP_WRITE)
Anand Jain1cb34c82017-10-21 01:45:33 +08006132 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006133 BTRFS_DEV_STAT_WRITE_ERRS);
6134 else
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_READ_ERRS);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06006137 if (bio->bi_opf & REQ_PREFLUSH)
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_FLUSH_ERRS);
Stefan Behrens597a60f2012-06-14 16:42:31 +02006140 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02006141 }
6142 }
Chris Mason8790d502008-04-03 16:29:03 -04006143
Jan Schmidta1d3c472011-08-04 17:15:33 +02006144 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006145 is_orig_bio = 1;
6146
Miao Xiec404e0d2014-01-30 16:46:55 +08006147 btrfs_bio_counter_dec(bbio->fs_info);
6148
Jan Schmidta1d3c472011-08-04 17:15:33 +02006149 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006150 if (!is_orig_bio) {
6151 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006152 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006153 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006154
Chris Mason9be33952013-05-17 18:30:14 -04006155 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006156 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006157 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006158 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006159 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006160 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006161 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006162 /*
6163 * this bio is actually up to date, we didn't
6164 * go over the max number of errors
6165 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006166 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006167 }
Miao Xiec55f1392014-06-19 10:42:54 +08006168
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006169 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006170 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006171 bio_put(bio);
6172 }
Chris Mason8790d502008-04-03 16:29:03 -04006173}
6174
Chris Mason8b712842008-06-11 16:50:36 -04006175/*
6176 * see run_scheduled_bios for a description of why bios are collected for
6177 * async submit.
6178 *
6179 * This will add one bio to the pending list for a device and make sure
6180 * the work struct is scheduled.
6181 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006182static noinline void btrfs_schedule_bio(struct btrfs_device *device,
Mike Christie4e49ea42016-06-05 14:31:41 -05006183 struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04006184{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006185 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -04006186 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04006187 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006188
6189 /* don't bother with additional async steps for reads, right now */
Mike Christie37226b22016-06-05 14:31:52 -05006190 if (bio_op(bio) == REQ_OP_READ) {
Mike Christie4e49ea42016-06-05 14:31:41 -05006191 btrfsic_submit_bio(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01006192 return;
Chris Mason8b712842008-06-11 16:50:36 -04006193 }
6194
Chris Mason492bb6de2008-07-31 16:29:02 -04006195 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04006196 bio->bi_next = NULL;
Chris Mason8b712842008-06-11 16:50:36 -04006197
6198 spin_lock(&device->io_lock);
Christoph Hellwig67f055c2016-11-01 07:40:06 -06006199 if (op_is_sync(bio->bi_opf))
Chris Masonffbd5172009-04-20 15:50:09 -04006200 pending_bios = &device->pending_sync_bios;
6201 else
6202 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006203
Chris Masonffbd5172009-04-20 15:50:09 -04006204 if (pending_bios->tail)
6205 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006206
Chris Masonffbd5172009-04-20 15:50:09 -04006207 pending_bios->tail = bio;
6208 if (!pending_bios->head)
6209 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006210 if (device->running_pending)
6211 should_queue = 0;
6212
6213 spin_unlock(&device->io_lock);
6214
6215 if (should_queue)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006216 btrfs_queue_work(fs_info->submit_workers, &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04006217}
6218
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006219static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6220 u64 physical, int dev_nr, int async)
Josef Bacikde1ee922012-10-19 16:50:56 -04006221{
6222 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006223 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006224
6225 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006226 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006227 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006228 bio->bi_iter.bi_sector = physical >> 9;
Misono Tomohiro672d5992018-08-02 16:19:07 +09006229 btrfs_debug_in_rcu(fs_info,
6230 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6231 bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector,
6232 (u_long)dev->bdev->bd_dev, rcu_str_deref(dev->name), dev->devid,
6233 bio->bi_iter.bi_size);
Christoph Hellwig74d46992017-08-23 19:10:32 +02006234 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006235
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006236 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006237
Josef Bacikde1ee922012-10-19 16:50:56 -04006238 if (async)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006239 btrfs_schedule_bio(dev, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006240 else
Mike Christie4e49ea42016-06-05 14:31:41 -05006241 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006242}
6243
Josef Bacikde1ee922012-10-19 16:50:56 -04006244static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6245{
6246 atomic_inc(&bbio->error);
6247 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006248 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006249 WARN_ON(bio != bbio->orig_bio);
6250
Chris Mason9be33952013-05-17 18:30:14 -04006251 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006252 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006253 if (atomic_read(&bbio->error) > bbio->max_errors)
6254 bio->bi_status = BLK_STS_IOERR;
6255 else
6256 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006257 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006258 }
6259}
6260
Omar Sandoval58efbc92017-08-22 23:45:59 -07006261blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6262 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006263{
Chris Mason0b86a832008-03-24 15:01:56 -04006264 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006265 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006266 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006267 u64 length = 0;
6268 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006269 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006270 int dev_nr;
6271 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006272 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006273
Kent Overstreet4f024f32013-10-11 15:44:27 -07006274 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006275 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006276
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006277 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006278 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006279 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006280 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006281 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006282 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006283 }
Chris Masoncea9e442008-04-09 16:28:12 -04006284
Jan Schmidta1d3c472011-08-04 17:15:33 +02006285 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006286 bbio->orig_bio = first_bio;
6287 bbio->private = first_bio->bi_private;
6288 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006289 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006290 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6291
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006292 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006293 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006294 /* In this case, map_length has been set to the length of
6295 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006296 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006297 ret = raid56_parity_write(fs_info, bio, bbio,
6298 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006299 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006300 ret = raid56_parity_recover(fs_info, bio, bbio,
6301 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006302 }
Miao Xie42452152014-11-25 16:39:28 +08006303
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006304 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006305 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006306 }
6307
Chris Masoncea9e442008-04-09 16:28:12 -04006308 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006309 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006310 "mapping failed logical %llu bio len %llu len %llu",
6311 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006312 BUG();
6313 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006314
Zhao Lei08da7572015-02-12 15:42:16 +08006315 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006316 dev = bbio->stripes[dev_nr].dev;
Nikolay Borisovfc8a1682018-11-08 16:16:38 +02006317 if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING,
6318 &dev->dev_state) ||
Anand Jainebbede42017-12-04 12:54:52 +08006319 (bio_op(first_bio) == REQ_OP_WRITE &&
6320 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006321 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006322 continue;
6323 }
6324
David Sterba3aa8e072017-06-02 17:38:30 +02006325 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006326 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006327 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006328 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006329
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006330 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6331 dev_nr, async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006332 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006333 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006334 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006335}
6336
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006337struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006338 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006339{
Yan Zheng2b820322008-11-17 21:11:30 -05006340 struct btrfs_device *device;
6341 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006342
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006343 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006344 while (cur_devices) {
6345 if (!fsid ||
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006346 !memcmp(cur_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
David Sterba35c70102017-06-15 19:51:51 +02006347 device = find_device(cur_devices, devid, uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006348 if (device)
6349 return device;
6350 }
6351 cur_devices = cur_devices->seed;
6352 }
6353 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006354}
6355
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006356static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006357 u64 devid, u8 *dev_uuid)
6358{
6359 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006360
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006361 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6362 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006363 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006364
6365 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006366 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006367 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006368
Anand Jaine6e674b2017-12-04 12:54:54 +08006369 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05006370 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006371
Chris Masondfe25022008-05-13 13:46:40 -04006372 return device;
6373}
6374
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006375/**
6376 * btrfs_alloc_device - allocate struct btrfs_device
6377 * @fs_info: used only for generating a new devid, can be NULL if
6378 * devid is provided (i.e. @devid != NULL).
6379 * @devid: a pointer to devid for this device. If NULL a new devid
6380 * is generated.
6381 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6382 * is generated.
6383 *
6384 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006385 * on error. Returned struct is not linked onto any lists and must be
David Sterbaa425f9d2018-03-20 15:47:33 +01006386 * destroyed with btrfs_free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006387 */
6388struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6389 const u64 *devid,
6390 const u8 *uuid)
6391{
6392 struct btrfs_device *dev;
6393 u64 tmp;
6394
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306395 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006396 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006397
6398 dev = __alloc_device();
6399 if (IS_ERR(dev))
6400 return dev;
6401
6402 if (devid)
6403 tmp = *devid;
6404 else {
6405 int ret;
6406
6407 ret = find_next_devid(fs_info, &tmp);
6408 if (ret) {
David Sterbaa425f9d2018-03-20 15:47:33 +01006409 btrfs_free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006410 return ERR_PTR(ret);
6411 }
6412 }
6413 dev->devid = tmp;
6414
6415 if (uuid)
6416 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6417 else
6418 generate_random_uuid(dev->uuid);
6419
Liu Bo9e0af232014-08-15 23:36:53 +08006420 btrfs_init_work(&dev->work, btrfs_submit_helper,
6421 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006422
6423 return dev;
6424}
6425
Liu Boe06cd3d2016-06-03 12:05:15 -07006426/* Return -EIO if any error, otherwise return 0. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006427static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006428 struct extent_buffer *leaf,
6429 struct btrfs_chunk *chunk, u64 logical)
6430{
6431 u64 length;
6432 u64 stripe_len;
6433 u16 num_stripes;
6434 u16 sub_stripes;
6435 u64 type;
Gu Jinxiang315409b2018-07-04 18:16:39 +08006436 u64 features;
6437 bool mixed = false;
Liu Boe06cd3d2016-06-03 12:05:15 -07006438
6439 length = btrfs_chunk_length(leaf, chunk);
6440 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6441 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6442 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6443 type = btrfs_chunk_type(leaf, chunk);
6444
6445 if (!num_stripes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006446 btrfs_err(fs_info, "invalid chunk num_stripes: %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006447 num_stripes);
6448 return -EIO;
6449 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006450 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
6451 btrfs_err(fs_info, "invalid chunk logical %llu", logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006452 return -EIO;
6453 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006454 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
6455 btrfs_err(fs_info, "invalid chunk sectorsize %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006456 btrfs_chunk_sector_size(leaf, chunk));
6457 return -EIO;
6458 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006459 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
6460 btrfs_err(fs_info, "invalid chunk length %llu", length);
Liu Boe06cd3d2016-06-03 12:05:15 -07006461 return -EIO;
6462 }
6463 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006464 btrfs_err(fs_info, "invalid chunk stripe length: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006465 stripe_len);
6466 return -EIO;
6467 }
6468 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6469 type) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006470 btrfs_err(fs_info, "unrecognized chunk type: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006471 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6472 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6473 btrfs_chunk_type(leaf, chunk));
6474 return -EIO;
6475 }
Gu Jinxiang315409b2018-07-04 18:16:39 +08006476
6477 if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0) {
6478 btrfs_err(fs_info, "missing chunk type flag: 0x%llx", type);
6479 return -EIO;
6480 }
6481
6482 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) &&
6483 (type & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA))) {
6484 btrfs_err(fs_info,
6485 "system chunk with data or metadata type: 0x%llx", type);
6486 return -EIO;
6487 }
6488
6489 features = btrfs_super_incompat_flags(fs_info->super_copy);
6490 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
6491 mixed = true;
6492
6493 if (!mixed) {
6494 if ((type & BTRFS_BLOCK_GROUP_METADATA) &&
6495 (type & BTRFS_BLOCK_GROUP_DATA)) {
6496 btrfs_err(fs_info,
6497 "mixed chunk type in non-mixed mode: 0x%llx", type);
6498 return -EIO;
6499 }
6500 }
6501
Liu Boe06cd3d2016-06-03 12:05:15 -07006502 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
6503 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
6504 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
6505 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
6506 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
6507 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
6508 num_stripes != 1)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006509 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006510 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
6511 num_stripes, sub_stripes,
6512 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
6513 return -EIO;
6514 }
6515
6516 return 0;
6517}
6518
Anand Jain5a2b8e62017-10-09 11:07:45 +08006519static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006520 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006521{
Anand Jain2b902df2017-10-09 11:07:46 +08006522 if (error)
6523 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6524 devid, uuid);
6525 else
6526 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6527 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006528}
6529
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006530static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
Chris Mason0b86a832008-03-24 15:01:56 -04006531 struct extent_buffer *leaf,
6532 struct btrfs_chunk *chunk)
6533{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006534 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006535 struct map_lookup *map;
6536 struct extent_map *em;
6537 u64 logical;
6538 u64 length;
6539 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006540 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006541 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006542 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006543 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006544
Chris Masone17cade2008-04-15 15:41:47 -04006545 logical = key->offset;
6546 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006547 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006548
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006549 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006550 if (ret)
6551 return ret;
Chris Masona061fc82008-05-07 11:43:44 -04006552
Chris Mason890871b2009-09-02 16:24:52 -04006553 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006554 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006555 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006556
6557 /* already mapped? */
6558 if (em && em->start <= logical && em->start + em->len > logical) {
6559 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006560 return 0;
6561 } else if (em) {
6562 free_extent_map(em);
6563 }
Chris Mason0b86a832008-03-24 15:01:56 -04006564
David Sterba172ddd62011-04-21 00:48:27 +02006565 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006566 if (!em)
6567 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006568 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006569 if (!map) {
6570 free_extent_map(em);
6571 return -ENOMEM;
6572 }
6573
Wang Shilong298a8f92014-06-19 10:42:52 +08006574 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006575 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006576 em->start = logical;
6577 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006578 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006579 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006580 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006581
Chris Mason593060d2008-03-25 16:50:33 -04006582 map->num_stripes = num_stripes;
6583 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6584 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006585 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6586 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006587 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Qu Wenruocf90d882018-08-01 10:37:19 +08006588 map->verified_stripes = 0;
Chris Mason593060d2008-03-25 16:50:33 -04006589 for (i = 0; i < num_stripes; i++) {
6590 map->stripes[i].physical =
6591 btrfs_stripe_offset_nr(leaf, chunk, i);
6592 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006593 read_extent_buffer(leaf, uuid, (unsigned long)
6594 btrfs_stripe_dev_uuid_nr(chunk, i),
6595 BTRFS_UUID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006596 map->stripes[i].dev = btrfs_find_device(fs_info, devid,
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006597 uuid, NULL);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006598 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006599 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006600 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006601 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006602 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006603 }
Chris Masondfe25022008-05-13 13:46:40 -04006604 if (!map->stripes[i].dev) {
6605 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006606 add_missing_dev(fs_info->fs_devices, devid,
6607 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006608 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006609 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006610 btrfs_err(fs_info,
6611 "failed to init missing dev %llu: %ld",
6612 devid, PTR_ERR(map->stripes[i].dev));
6613 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006614 }
Anand Jain2b902df2017-10-09 11:07:46 +08006615 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006616 }
Anand Jaine12c9622017-12-04 12:54:53 +08006617 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6618 &(map->stripes[i].dev->dev_state));
6619
Chris Mason0b86a832008-03-24 15:01:56 -04006620 }
6621
Chris Mason890871b2009-09-02 16:24:52 -04006622 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006623 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006624 write_unlock(&map_tree->map_tree.lock);
Qu Wenruo64f64f42018-08-01 10:37:20 +08006625 if (ret < 0) {
6626 btrfs_err(fs_info,
6627 "failed to add chunk map, start=%llu len=%llu: %d",
6628 em->start, em->len, ret);
6629 }
Chris Mason0b86a832008-03-24 15:01:56 -04006630 free_extent_map(em);
6631
Qu Wenruo64f64f42018-08-01 10:37:20 +08006632 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006633}
6634
Jeff Mahoney143bede2012-03-01 14:56:26 +01006635static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006636 struct btrfs_dev_item *dev_item,
6637 struct btrfs_device *device)
6638{
6639 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006640
6641 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006642 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6643 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006644 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006645 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006646 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006647 device->type = btrfs_device_type(leaf, dev_item);
6648 device->io_align = btrfs_device_io_align(leaf, dev_item);
6649 device->io_width = btrfs_device_io_width(leaf, dev_item);
6650 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006651 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Anand Jain401e29c2017-12-04 12:54:55 +08006652 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Chris Mason0b86a832008-03-24 15:01:56 -04006653
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006654 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006655 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006656}
6657
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006658static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006659 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006660{
6661 struct btrfs_fs_devices *fs_devices;
6662 int ret;
6663
David Sterbaa32bf9a2018-03-16 02:21:22 +01006664 lockdep_assert_held(&uuid_mutex);
David Sterba2dfeca92017-06-14 02:48:07 +02006665 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006666
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006667 fs_devices = fs_info->fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006668 while (fs_devices) {
Anand Jain44880fd2017-07-29 17:50:09 +08006669 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006670 return fs_devices;
6671
Yan Zheng2b820322008-11-17 21:11:30 -05006672 fs_devices = fs_devices->seed;
6673 }
6674
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006675 fs_devices = find_fsid(fsid, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05006676 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006677 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006678 return ERR_PTR(-ENOENT);
6679
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006680 fs_devices = alloc_fs_devices(fsid, NULL);
Miao Xie5f375832014-09-03 21:35:46 +08006681 if (IS_ERR(fs_devices))
6682 return fs_devices;
6683
6684 fs_devices->seeding = 1;
6685 fs_devices->opened = 1;
6686 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006687 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006688
6689 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006690 if (IS_ERR(fs_devices))
6691 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006692
Anand Jain897fb572018-04-12 10:29:28 +08006693 ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006694 if (ret) {
6695 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006696 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006697 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006698 }
Yan Zheng2b820322008-11-17 21:11:30 -05006699
6700 if (!fs_devices->seeding) {
Anand Jain0226e0e2018-04-12 10:29:27 +08006701 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006702 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006703 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006704 goto out;
6705 }
6706
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006707 fs_devices->seed = fs_info->fs_devices->seed;
6708 fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006709out:
Miao Xie5f375832014-09-03 21:35:46 +08006710 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006711}
6712
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006713static int read_one_dev(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04006714 struct extent_buffer *leaf,
6715 struct btrfs_dev_item *dev_item)
6716{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006717 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006718 struct btrfs_device *device;
6719 u64 devid;
6720 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08006721 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006722 u8 dev_uuid[BTRFS_UUID_SIZE];
6723
Chris Mason0b86a832008-03-24 15:01:56 -04006724 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006725 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006726 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006727 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08006728 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05006729
Nikolay Borisovde37aa52018-10-30 16:43:24 +02006730 if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006731 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006732 if (IS_ERR(fs_devices))
6733 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006734 }
6735
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006736 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006737 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08006738 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08006739 btrfs_report_missing_device(fs_info, devid,
6740 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006741 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08006742 }
Yan Zheng2b820322008-11-17 21:11:30 -05006743
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006744 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006745 if (IS_ERR(device)) {
6746 btrfs_err(fs_info,
6747 "failed to add missing dev %llu: %ld",
6748 devid, PTR_ERR(device));
6749 return PTR_ERR(device);
6750 }
Anand Jain2b902df2017-10-09 11:07:46 +08006751 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08006752 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08006753 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08006754 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6755 btrfs_report_missing_device(fs_info,
6756 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006757 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08006758 }
6759 btrfs_report_missing_device(fs_info, devid,
6760 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08006761 }
Miao Xie5f375832014-09-03 21:35:46 +08006762
Anand Jaine6e674b2017-12-04 12:54:54 +08006763 if (!device->bdev &&
6764 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006765 /*
6766 * this happens when a device that was properly setup
6767 * in the device info lists suddenly goes bad.
6768 * device->bdev is NULL, and so we have to set
6769 * device->missing to one here
6770 */
Miao Xie5f375832014-09-03 21:35:46 +08006771 device->fs_devices->missing_devices++;
Anand Jaine6e674b2017-12-04 12:54:54 +08006772 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05006773 }
Miao Xie5f375832014-09-03 21:35:46 +08006774
6775 /* Move the device to its own fs_devices */
6776 if (device->fs_devices != fs_devices) {
Anand Jaine6e674b2017-12-04 12:54:54 +08006777 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
6778 &device->dev_state));
Miao Xie5f375832014-09-03 21:35:46 +08006779
6780 list_move(&device->dev_list, &fs_devices->devices);
6781 device->fs_devices->num_devices--;
6782 fs_devices->num_devices++;
6783
6784 device->fs_devices->missing_devices--;
6785 fs_devices->missing_devices++;
6786
6787 device->fs_devices = fs_devices;
6788 }
Yan Zheng2b820322008-11-17 21:11:30 -05006789 }
6790
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006791 if (device->fs_devices != fs_info->fs_devices) {
Anand Jainebbede42017-12-04 12:54:52 +08006792 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
Yan Zheng2b820322008-11-17 21:11:30 -05006793 if (device->generation !=
6794 btrfs_device_generation(leaf, dev_item))
6795 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006796 }
Chris Mason0b86a832008-03-24 15:01:56 -04006797
6798 fill_device_from_item(leaf, dev_item, device);
Anand Jaine12c9622017-12-04 12:54:53 +08006799 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jainebbede42017-12-04 12:54:52 +08006800 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jain401e29c2017-12-04 12:54:55 +08006801 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05006802 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03006803 atomic64_add(device->total_bytes - device->bytes_used,
6804 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04006805 }
Chris Mason0b86a832008-03-24 15:01:56 -04006806 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006807 return ret;
6808}
6809
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006810int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006811{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006812 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006813 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006814 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006815 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006816 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006817 u8 *array_ptr;
6818 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006819 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006820 u32 num_stripes;
6821 u32 array_size;
6822 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006823 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07006824 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04006825 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006826
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006827 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02006828 /*
6829 * This will create extent buffer of nodesize, superblock size is
6830 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6831 * overallocate but we can keep it as-is, only the first page is used.
6832 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006833 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07006834 if (IS_ERR(sb))
6835 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01006836 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006837 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006838 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006839 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01006840 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02006841 * pages up-to-date when the page is larger: extent does not cover the
6842 * whole page and consequently check_page_uptodate does not find all
6843 * the page's extents up-to-date (the hole beyond sb),
6844 * write_extent_buffer then triggers a WARN_ON.
6845 *
6846 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6847 * but sb spans only this function. Add an explicit SetPageUptodate call
6848 * to silence the warning eg. on PowerPC 64.
6849 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006850 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006851 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006852
Chris Masona061fc82008-05-07 11:43:44 -04006853 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006854 array_size = btrfs_super_sys_array_size(super_copy);
6855
David Sterba1ffb22c2014-10-31 19:02:42 +01006856 array_ptr = super_copy->sys_chunk_array;
6857 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6858 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006859
David Sterba1ffb22c2014-10-31 19:02:42 +01006860 while (cur_offset < array_size) {
6861 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006862 len = sizeof(*disk_key);
6863 if (cur_offset + len > array_size)
6864 goto out_short_read;
6865
Chris Mason0b86a832008-03-24 15:01:56 -04006866 btrfs_disk_key_to_cpu(&key, disk_key);
6867
David Sterba1ffb22c2014-10-31 19:02:42 +01006868 array_ptr += len;
6869 sb_array_offset += len;
6870 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006871
Chris Mason0d81ba52008-03-24 15:02:07 -04006872 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006873 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006874 /*
6875 * At least one btrfs_chunk with one stripe must be
6876 * present, exact stripe count check comes afterwards
6877 */
6878 len = btrfs_chunk_item_size(1);
6879 if (cur_offset + len > array_size)
6880 goto out_short_read;
6881
6882 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01006883 if (!num_stripes) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006884 btrfs_err(fs_info,
6885 "invalid number of stripes %u in sys_array at offset %u",
David Sterbaf5cdedd2015-11-30 17:27:06 +01006886 num_stripes, cur_offset);
6887 ret = -EIO;
6888 break;
6889 }
6890
Liu Boe06cd3d2016-06-03 12:05:15 -07006891 type = btrfs_chunk_type(sb, chunk);
6892 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006893 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006894 "invalid chunk type %llu in sys_array at offset %u",
6895 type, cur_offset);
6896 ret = -EIO;
6897 break;
6898 }
6899
David Sterbae3540ea2014-11-05 15:24:51 +01006900 len = btrfs_chunk_item_size(num_stripes);
6901 if (cur_offset + len > array_size)
6902 goto out_short_read;
6903
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006904 ret = read_one_chunk(fs_info, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006905 if (ret)
6906 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006907 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006908 btrfs_err(fs_info,
6909 "unexpected item type %u in sys_array at offset %u",
6910 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04006911 ret = -EIO;
6912 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006913 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006914 array_ptr += len;
6915 sb_array_offset += len;
6916 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006917 }
Liu Bod8651772016-06-03 17:41:42 -07006918 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006919 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006920 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006921
6922out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006923 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01006924 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07006925 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006926 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01006927 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006928}
6929
Qu Wenruo21634a12017-03-09 09:34:36 +08006930/*
6931 * Check if all chunks in the fs are OK for read-write degraded mount
6932 *
Anand Jain6528b992017-12-18 17:08:59 +08006933 * If the @failing_dev is specified, it's accounted as missing.
6934 *
Qu Wenruo21634a12017-03-09 09:34:36 +08006935 * Return true if all chunks meet the minimal RW mount requirements.
6936 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6937 */
Anand Jain6528b992017-12-18 17:08:59 +08006938bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
6939 struct btrfs_device *failing_dev)
Qu Wenruo21634a12017-03-09 09:34:36 +08006940{
6941 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
6942 struct extent_map *em;
6943 u64 next_start = 0;
6944 bool ret = true;
6945
6946 read_lock(&map_tree->map_tree.lock);
6947 em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1);
6948 read_unlock(&map_tree->map_tree.lock);
6949 /* No chunk at all? Return false anyway */
6950 if (!em) {
6951 ret = false;
6952 goto out;
6953 }
6954 while (em) {
6955 struct map_lookup *map;
6956 int missing = 0;
6957 int max_tolerated;
6958 int i;
6959
6960 map = em->map_lookup;
6961 max_tolerated =
6962 btrfs_get_num_tolerated_disk_barrier_failures(
6963 map->type);
6964 for (i = 0; i < map->num_stripes; i++) {
6965 struct btrfs_device *dev = map->stripes[i].dev;
6966
Anand Jaine6e674b2017-12-04 12:54:54 +08006967 if (!dev || !dev->bdev ||
6968 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
Qu Wenruo21634a12017-03-09 09:34:36 +08006969 dev->last_flush_error)
6970 missing++;
Anand Jain6528b992017-12-18 17:08:59 +08006971 else if (failing_dev && failing_dev == dev)
6972 missing++;
Qu Wenruo21634a12017-03-09 09:34:36 +08006973 }
6974 if (missing > max_tolerated) {
Anand Jain6528b992017-12-18 17:08:59 +08006975 if (!failing_dev)
6976 btrfs_warn(fs_info,
Qu Wenruo21634a12017-03-09 09:34:36 +08006977 "chunk %llu missing %d devices, max tolerance is %d for writeable mount",
6978 em->start, missing, max_tolerated);
6979 free_extent_map(em);
6980 ret = false;
6981 goto out;
6982 }
6983 next_start = extent_map_end(em);
6984 free_extent_map(em);
6985
6986 read_lock(&map_tree->map_tree.lock);
6987 em = lookup_extent_mapping(&map_tree->map_tree, next_start,
6988 (u64)(-1) - next_start);
6989 read_unlock(&map_tree->map_tree.lock);
6990 }
6991out:
6992 return ret;
6993}
6994
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006995int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006996{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006997 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04006998 struct btrfs_path *path;
6999 struct extent_buffer *leaf;
7000 struct btrfs_key key;
7001 struct btrfs_key found_key;
7002 int ret;
7003 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007004 u64 total_dev = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007005
Chris Mason0b86a832008-03-24 15:01:56 -04007006 path = btrfs_alloc_path();
7007 if (!path)
7008 return -ENOMEM;
7009
Anand Jain3dd0f7a2018-04-12 10:29:32 +08007010 /*
7011 * uuid_mutex is needed only if we are mounting a sprout FS
7012 * otherwise we don't need it.
7013 */
Li Zefanb367e472011-12-07 11:38:24 +08007014 mutex_lock(&uuid_mutex);
David Sterba34441362016-10-04 19:34:27 +02007015 mutex_lock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007016
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007017 /*
7018 * Read all device items, and then all the chunk items. All
7019 * device items are found before any chunk item (their object id
7020 * is smaller than the lowest possible object id for a chunk
7021 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04007022 */
7023 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
7024 key.offset = 0;
7025 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007026 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00007027 if (ret < 0)
7028 goto error;
Chris Masond3977122009-01-05 21:25:51 -05007029 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007030 leaf = path->nodes[0];
7031 slot = path->slots[0];
7032 if (slot >= btrfs_header_nritems(leaf)) {
7033 ret = btrfs_next_leaf(root, path);
7034 if (ret == 0)
7035 continue;
7036 if (ret < 0)
7037 goto error;
7038 break;
7039 }
7040 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007041 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
7042 struct btrfs_dev_item *dev_item;
7043 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04007044 struct btrfs_dev_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007045 ret = read_one_dev(fs_info, leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007046 if (ret)
7047 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007048 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04007049 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
7050 struct btrfs_chunk *chunk;
7051 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007052 ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05007053 if (ret)
7054 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04007055 }
7056 path->slots[0]++;
7057 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07007058
7059 /*
7060 * After loading chunk tree, we've got all device information,
7061 * do another round of validation checks.
7062 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007063 if (total_dev != fs_info->fs_devices->total_devices) {
7064 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007065 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007066 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07007067 total_dev);
7068 ret = -EINVAL;
7069 goto error;
7070 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007071 if (btrfs_super_total_bytes(fs_info->super_copy) <
7072 fs_info->fs_devices->total_rw_bytes) {
7073 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007074 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007075 btrfs_super_total_bytes(fs_info->super_copy),
7076 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07007077 ret = -EINVAL;
7078 goto error;
7079 }
Chris Mason0b86a832008-03-24 15:01:56 -04007080 ret = 0;
7081error:
David Sterba34441362016-10-04 19:34:27 +02007082 mutex_unlock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007083 mutex_unlock(&uuid_mutex);
7084
Yan Zheng2b820322008-11-17 21:11:30 -05007085 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007086 return ret;
7087}
Stefan Behrens442a4f62012-05-25 16:06:08 +02007088
Miao Xiecb517ea2013-05-15 07:48:19 +00007089void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7090{
7091 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7092 struct btrfs_device *device;
7093
Liu Bo29cc83f2014-05-11 23:14:59 +08007094 while (fs_devices) {
7095 mutex_lock(&fs_devices->device_list_mutex);
7096 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007097 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007098 mutex_unlock(&fs_devices->device_list_mutex);
7099
7100 fs_devices = fs_devices->seed;
7101 }
Miao Xiecb517ea2013-05-15 07:48:19 +00007102}
7103
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007104static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
7105{
7106 int i;
7107
7108 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7109 btrfs_dev_stat_reset(dev, i);
7110}
7111
7112int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7113{
7114 struct btrfs_key key;
7115 struct btrfs_key found_key;
7116 struct btrfs_root *dev_root = fs_info->dev_root;
7117 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7118 struct extent_buffer *eb;
7119 int slot;
7120 int ret = 0;
7121 struct btrfs_device *device;
7122 struct btrfs_path *path = NULL;
7123 int i;
7124
7125 path = btrfs_alloc_path();
7126 if (!path) {
7127 ret = -ENOMEM;
7128 goto out;
7129 }
7130
7131 mutex_lock(&fs_devices->device_list_mutex);
7132 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7133 int item_size;
7134 struct btrfs_dev_stats_item *ptr;
7135
David Sterba242e2952016-01-25 17:51:31 +01007136 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7137 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007138 key.offset = device->devid;
7139 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7140 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007141 __btrfs_reset_dev_stats(device);
7142 device->dev_stats_valid = 1;
7143 btrfs_release_path(path);
7144 continue;
7145 }
7146 slot = path->slots[0];
7147 eb = path->nodes[0];
7148 btrfs_item_key_to_cpu(eb, &found_key, slot);
7149 item_size = btrfs_item_size_nr(eb, slot);
7150
7151 ptr = btrfs_item_ptr(eb, slot,
7152 struct btrfs_dev_stats_item);
7153
7154 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7155 if (item_size >= (1 + i) * sizeof(__le64))
7156 btrfs_dev_stat_set(device, i,
7157 btrfs_dev_stats_value(eb, ptr, i));
7158 else
7159 btrfs_dev_stat_reset(device, i);
7160 }
7161
7162 device->dev_stats_valid = 1;
7163 btrfs_dev_stat_print_on_load(device);
7164 btrfs_release_path(path);
7165 }
7166 mutex_unlock(&fs_devices->device_list_mutex);
7167
7168out:
7169 btrfs_free_path(path);
7170 return ret < 0 ? ret : 0;
7171}
7172
7173static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007174 struct btrfs_device *device)
7175{
Nikolay Borisov5495f192018-07-20 19:37:49 +03007176 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007177 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007178 struct btrfs_path *path;
7179 struct btrfs_key key;
7180 struct extent_buffer *eb;
7181 struct btrfs_dev_stats_item *ptr;
7182 int ret;
7183 int i;
7184
David Sterba242e2952016-01-25 17:51:31 +01007185 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7186 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007187 key.offset = device->devid;
7188
7189 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007190 if (!path)
7191 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007192 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7193 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007194 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007195 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007196 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007197 goto out;
7198 }
7199
7200 if (ret == 0 &&
7201 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7202 /* need to delete old one and insert a new one */
7203 ret = btrfs_del_item(trans, dev_root, path);
7204 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007205 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007206 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007207 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007208 goto out;
7209 }
7210 ret = 1;
7211 }
7212
7213 if (ret == 1) {
7214 /* need to insert a new item */
7215 btrfs_release_path(path);
7216 ret = btrfs_insert_empty_item(trans, dev_root, path,
7217 &key, sizeof(*ptr));
7218 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007219 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007220 "insert dev_stats item for device %s failed %d",
7221 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007222 goto out;
7223 }
7224 }
7225
7226 eb = path->nodes[0];
7227 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7228 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7229 btrfs_set_dev_stats_value(eb, ptr, i,
7230 btrfs_dev_stat_read(device, i));
7231 btrfs_mark_buffer_dirty(eb);
7232
7233out:
7234 btrfs_free_path(path);
7235 return ret;
7236}
7237
7238/*
7239 * called from commit_transaction. Writes all changed device stats to disk.
7240 */
7241int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
7242 struct btrfs_fs_info *fs_info)
7243{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007244 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7245 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007246 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007247 int ret = 0;
7248
7249 mutex_lock(&fs_devices->device_list_mutex);
7250 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007251 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7252 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007253 continue;
7254
Nikolay Borisov9deae962017-10-24 13:47:37 +03007255
7256 /*
7257 * There is a LOAD-LOAD control dependency between the value of
7258 * dev_stats_ccnt and updating the on-disk values which requires
7259 * reading the in-memory counters. Such control dependencies
7260 * require explicit read memory barriers.
7261 *
7262 * This memory barriers pairs with smp_mb__before_atomic in
7263 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7264 * barrier implied by atomic_xchg in
7265 * btrfs_dev_stats_read_and_reset
7266 */
7267 smp_rmb();
7268
Nikolay Borisov5495f192018-07-20 19:37:49 +03007269 ret = update_dev_stat_item(trans, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007270 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007271 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007272 }
7273 mutex_unlock(&fs_devices->device_list_mutex);
7274
7275 return ret;
7276}
7277
Stefan Behrens442a4f62012-05-25 16:06:08 +02007278void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7279{
7280 btrfs_dev_stat_inc(dev, index);
7281 btrfs_dev_stat_print_on_error(dev);
7282}
7283
Eric Sandeen48a3b632013-04-25 20:41:01 +00007284static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007285{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007286 if (!dev->dev_stats_valid)
7287 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007288 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007289 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007290 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007291 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7292 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7293 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007294 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7295 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007296}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007297
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007298static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7299{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007300 int i;
7301
7302 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7303 if (btrfs_dev_stat_read(dev, i) != 0)
7304 break;
7305 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7306 return; /* all values == 0, suppress message */
7307
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007308 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007309 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007310 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007311 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7312 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7313 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7314 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7315 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7316}
7317
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007318int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007319 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007320{
7321 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007322 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007323 int i;
7324
7325 mutex_lock(&fs_devices->device_list_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007326 dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007327 mutex_unlock(&fs_devices->device_list_mutex);
7328
7329 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007330 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007331 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007332 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007333 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007334 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007335 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007336 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7337 if (stats->nr_items > i)
7338 stats->values[i] =
7339 btrfs_dev_stat_read_and_reset(dev, i);
7340 else
7341 btrfs_dev_stat_reset(dev, i);
7342 }
7343 } else {
7344 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7345 if (stats->nr_items > i)
7346 stats->values[i] = btrfs_dev_stat_read(dev, i);
7347 }
7348 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7349 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7350 return 0;
7351}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007352
David Sterbada353f62017-02-14 17:55:53 +01007353void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007354{
7355 struct buffer_head *bh;
7356 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08007357 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007358
Anand Jain12b1c262015-08-14 18:32:59 +08007359 if (!bdev)
7360 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007361
Anand Jain12b1c262015-08-14 18:32:59 +08007362 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7363 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007364
Anand Jain12b1c262015-08-14 18:32:59 +08007365 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7366 continue;
7367
7368 disk_super = (struct btrfs_super_block *)bh->b_data;
7369
7370 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7371 set_buffer_dirty(bh);
7372 sync_dirty_buffer(bh);
7373 brelse(bh);
7374 }
7375
7376 /* Notify udev that device has changed */
7377 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7378
7379 /* Update ctime/mtime for device path for libblkid */
7380 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007381}
Miao Xie935e5cc2014-09-03 21:35:33 +08007382
7383/*
7384 * Update the size of all devices, which is used for writing out the
7385 * super blocks.
7386 */
7387void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
7388{
7389 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7390 struct btrfs_device *curr, *next;
7391
7392 if (list_empty(&fs_devices->resized_devices))
7393 return;
7394
7395 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02007396 mutex_lock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007397 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
7398 resized_list) {
7399 list_del_init(&curr->resized_list);
7400 curr->commit_total_bytes = curr->disk_total_bytes;
7401 }
David Sterba34441362016-10-04 19:34:27 +02007402 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007403 mutex_unlock(&fs_devices->device_list_mutex);
7404}
Miao Xiece7213c2014-09-03 21:35:34 +08007405
7406/* Must be invoked during the transaction commit */
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007407void btrfs_update_commit_device_bytes_used(struct btrfs_transaction *trans)
Miao Xiece7213c2014-09-03 21:35:34 +08007408{
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007409 struct btrfs_fs_info *fs_info = trans->fs_info;
Miao Xiece7213c2014-09-03 21:35:34 +08007410 struct extent_map *em;
7411 struct map_lookup *map;
7412 struct btrfs_device *dev;
7413 int i;
7414
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007415 if (list_empty(&trans->pending_chunks))
Miao Xiece7213c2014-09-03 21:35:34 +08007416 return;
7417
7418 /* In order to kick the device replace finish process */
David Sterba34441362016-10-04 19:34:27 +02007419 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007420 list_for_each_entry(em, &trans->pending_chunks, list) {
Jeff Mahoney95617d62015-06-03 10:55:48 -04007421 map = em->map_lookup;
Miao Xiece7213c2014-09-03 21:35:34 +08007422
7423 for (i = 0; i < map->num_stripes; i++) {
7424 dev = map->stripes[i].dev;
7425 dev->commit_bytes_used = dev->bytes_used;
7426 }
7427 }
David Sterba34441362016-10-04 19:34:27 +02007428 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007429}
Anand Jain5a13f432015-03-10 06:38:31 +08007430
7431void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7432{
7433 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7434 while (fs_devices) {
7435 fs_devices->fs_info = fs_info;
7436 fs_devices = fs_devices->seed;
7437 }
7438}
7439
7440void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7441{
7442 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7443 while (fs_devices) {
7444 fs_devices->fs_info = NULL;
7445 fs_devices = fs_devices->seed;
7446 }
7447}
David Sterba46df06b2018-07-13 20:46:30 +02007448
7449/*
7450 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7451 */
7452int btrfs_bg_type_to_factor(u64 flags)
7453{
7454 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
7455 BTRFS_BLOCK_GROUP_RAID10))
7456 return 2;
7457 return 1;
7458}
Qu Wenruocf90d882018-08-01 10:37:19 +08007459
7460
7461static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes)
7462{
7463 int index = btrfs_bg_flags_to_raid_index(type);
7464 int ncopies = btrfs_raid_array[index].ncopies;
7465 int data_stripes;
7466
7467 switch (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
7468 case BTRFS_BLOCK_GROUP_RAID5:
7469 data_stripes = num_stripes - 1;
7470 break;
7471 case BTRFS_BLOCK_GROUP_RAID6:
7472 data_stripes = num_stripes - 2;
7473 break;
7474 default:
7475 data_stripes = num_stripes / ncopies;
7476 break;
7477 }
7478 return div_u64(chunk_len, data_stripes);
7479}
7480
7481static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
7482 u64 chunk_offset, u64 devid,
7483 u64 physical_offset, u64 physical_len)
7484{
7485 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
7486 struct extent_map *em;
7487 struct map_lookup *map;
Qu Wenruo05a37c42018-10-05 17:45:55 +08007488 struct btrfs_device *dev;
Qu Wenruocf90d882018-08-01 10:37:19 +08007489 u64 stripe_len;
7490 bool found = false;
7491 int ret = 0;
7492 int i;
7493
7494 read_lock(&em_tree->lock);
7495 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
7496 read_unlock(&em_tree->lock);
7497
7498 if (!em) {
7499 btrfs_err(fs_info,
7500"dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7501 physical_offset, devid);
7502 ret = -EUCLEAN;
7503 goto out;
7504 }
7505
7506 map = em->map_lookup;
7507 stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes);
7508 if (physical_len != stripe_len) {
7509 btrfs_err(fs_info,
7510"dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7511 physical_offset, devid, em->start, physical_len,
7512 stripe_len);
7513 ret = -EUCLEAN;
7514 goto out;
7515 }
7516
7517 for (i = 0; i < map->num_stripes; i++) {
7518 if (map->stripes[i].dev->devid == devid &&
7519 map->stripes[i].physical == physical_offset) {
7520 found = true;
7521 if (map->verified_stripes >= map->num_stripes) {
7522 btrfs_err(fs_info,
7523 "too many dev extents for chunk %llu found",
7524 em->start);
7525 ret = -EUCLEAN;
7526 goto out;
7527 }
7528 map->verified_stripes++;
7529 break;
7530 }
7531 }
7532 if (!found) {
7533 btrfs_err(fs_info,
7534 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7535 physical_offset, devid);
7536 ret = -EUCLEAN;
7537 }
Qu Wenruo05a37c42018-10-05 17:45:55 +08007538
7539 /* Make sure no dev extent is beyond device bondary */
7540 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
7541 if (!dev) {
7542 btrfs_err(fs_info, "failed to find devid %llu", devid);
7543 ret = -EUCLEAN;
7544 goto out;
7545 }
7546 if (physical_offset + physical_len > dev->disk_total_bytes) {
7547 btrfs_err(fs_info,
7548"dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7549 devid, physical_offset, physical_len,
7550 dev->disk_total_bytes);
7551 ret = -EUCLEAN;
7552 goto out;
7553 }
Qu Wenruocf90d882018-08-01 10:37:19 +08007554out:
7555 free_extent_map(em);
7556 return ret;
7557}
7558
7559static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)
7560{
7561 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
7562 struct extent_map *em;
7563 struct rb_node *node;
7564 int ret = 0;
7565
7566 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08007567 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
Qu Wenruocf90d882018-08-01 10:37:19 +08007568 em = rb_entry(node, struct extent_map, rb_node);
7569 if (em->map_lookup->num_stripes !=
7570 em->map_lookup->verified_stripes) {
7571 btrfs_err(fs_info,
7572 "chunk %llu has missing dev extent, have %d expect %d",
7573 em->start, em->map_lookup->verified_stripes,
7574 em->map_lookup->num_stripes);
7575 ret = -EUCLEAN;
7576 goto out;
7577 }
7578 }
7579out:
7580 read_unlock(&em_tree->lock);
7581 return ret;
7582}
7583
7584/*
7585 * Ensure that all dev extents are mapped to correct chunk, otherwise
7586 * later chunk allocation/free would cause unexpected behavior.
7587 *
7588 * NOTE: This will iterate through the whole device tree, which should be of
7589 * the same size level as the chunk tree. This slightly increases mount time.
7590 */
7591int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info)
7592{
7593 struct btrfs_path *path;
7594 struct btrfs_root *root = fs_info->dev_root;
7595 struct btrfs_key key;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007596 u64 prev_devid = 0;
7597 u64 prev_dev_ext_end = 0;
Qu Wenruocf90d882018-08-01 10:37:19 +08007598 int ret = 0;
7599
7600 key.objectid = 1;
7601 key.type = BTRFS_DEV_EXTENT_KEY;
7602 key.offset = 0;
7603
7604 path = btrfs_alloc_path();
7605 if (!path)
7606 return -ENOMEM;
7607
7608 path->reada = READA_FORWARD;
7609 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7610 if (ret < 0)
7611 goto out;
7612
7613 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7614 ret = btrfs_next_item(root, path);
7615 if (ret < 0)
7616 goto out;
7617 /* No dev extents at all? Not good */
7618 if (ret > 0) {
7619 ret = -EUCLEAN;
7620 goto out;
7621 }
7622 }
7623 while (1) {
7624 struct extent_buffer *leaf = path->nodes[0];
7625 struct btrfs_dev_extent *dext;
7626 int slot = path->slots[0];
7627 u64 chunk_offset;
7628 u64 physical_offset;
7629 u64 physical_len;
7630 u64 devid;
7631
7632 btrfs_item_key_to_cpu(leaf, &key, slot);
7633 if (key.type != BTRFS_DEV_EXTENT_KEY)
7634 break;
7635 devid = key.objectid;
7636 physical_offset = key.offset;
7637
7638 dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
7639 chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext);
7640 physical_len = btrfs_dev_extent_length(leaf, dext);
7641
Qu Wenruo5eb19382018-10-05 17:45:54 +08007642 /* Check if this dev extent overlaps with the previous one */
7643 if (devid == prev_devid && physical_offset < prev_dev_ext_end) {
7644 btrfs_err(fs_info,
7645"dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
7646 devid, physical_offset, prev_dev_ext_end);
7647 ret = -EUCLEAN;
7648 goto out;
7649 }
7650
Qu Wenruocf90d882018-08-01 10:37:19 +08007651 ret = verify_one_dev_extent(fs_info, chunk_offset, devid,
7652 physical_offset, physical_len);
7653 if (ret < 0)
7654 goto out;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007655 prev_devid = devid;
7656 prev_dev_ext_end = physical_offset + physical_len;
7657
Qu Wenruocf90d882018-08-01 10:37:19 +08007658 ret = btrfs_next_item(root, path);
7659 if (ret < 0)
7660 goto out;
7661 if (ret > 0) {
7662 ret = 0;
7663 break;
7664 }
7665 }
7666
7667 /* Ensure all chunks have corresponding dev extents */
7668 ret = verify_chunk_dev_extent_mapping(fs_info);
7669out:
7670 btrfs_free_path(path);
7671 return ret;
7672}
Omar Sandovaleede2bf2016-11-03 10:28:12 -07007673
7674/*
7675 * Check whether the given block group or device is pinned by any inode being
7676 * used as a swapfile.
7677 */
7678bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr)
7679{
7680 struct btrfs_swapfile_pin *sp;
7681 struct rb_node *node;
7682
7683 spin_lock(&fs_info->swapfile_pins_lock);
7684 node = fs_info->swapfile_pins.rb_node;
7685 while (node) {
7686 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
7687 if (ptr < sp->ptr)
7688 node = node->rb_left;
7689 else if (ptr > sp->ptr)
7690 node = node->rb_right;
7691 else
7692 break;
7693 }
7694 spin_unlock(&fs_info->swapfile_pins_lock);
7695 return node != NULL;
7696}