blob: 74bbfed6428b375e60c9de8be65acabbe1648d0d [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
Anand Jainf89e09c2018-11-20 16:12:55 +0800133/*
134 * Fill @buf with textual description of @bg_flags, no more than @size_buf
135 * bytes including terminating null byte.
136 */
137void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf)
138{
139 int i;
140 int ret;
141 char *bp = buf;
142 u64 flags = bg_flags;
143 u32 size_bp = size_buf;
144
145 if (!flags) {
146 strcpy(bp, "NONE");
147 return;
148 }
149
150#define DESCRIBE_FLAG(flag, desc) \
151 do { \
152 if (flags & (flag)) { \
153 ret = snprintf(bp, size_bp, "%s|", (desc)); \
154 if (ret < 0 || ret >= size_bp) \
155 goto out_overflow; \
156 size_bp -= ret; \
157 bp += ret; \
158 flags &= ~(flag); \
159 } \
160 } while (0)
161
162 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data");
163 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system");
164 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata");
165
166 DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single");
167 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
168 DESCRIBE_FLAG(btrfs_raid_array[i].bg_flag,
169 btrfs_raid_array[i].raid_name);
170#undef DESCRIBE_FLAG
171
172 if (flags) {
173 ret = snprintf(bp, size_bp, "0x%llx|", flags);
174 size_bp -= ret;
175 }
176
177 if (size_bp < size_buf)
178 buf[size_buf - size_bp - 1] = '\0'; /* remove last | */
179
180 /*
181 * The text is trimmed, it's up to the caller to provide sufficiently
182 * large buffer
183 */
184out_overflow:;
185}
186
Yan Zheng2b820322008-11-17 21:11:30 -0500187static int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +0100188 struct btrfs_fs_info *fs_info);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400189static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200190static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000191static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200192static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Liu Bo5ab56092017-03-14 13:33:57 -0700193static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
194 enum btrfs_map_op op,
195 u64 logical, u64 *length,
196 struct btrfs_bio **bbio_ret,
197 int mirror_num, int need_raid_map);
Yan Zheng2b820322008-11-17 21:11:30 -0500198
David Sterba9c6b1c42017-06-16 22:30:00 +0200199/*
200 * Device locking
201 * ==============
202 *
203 * There are several mutexes that protect manipulation of devices and low-level
204 * structures like chunks but not block groups, extents or files
205 *
206 * uuid_mutex (global lock)
207 * ------------------------
208 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
209 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
210 * device) or requested by the device= mount option
211 *
212 * the mutex can be very coarse and can cover long-running operations
213 *
214 * protects: updates to fs_devices counters like missing devices, rw devices,
Andrea Gelmini52042d82018-11-28 12:05:13 +0100215 * seeding, structure cloning, opening/closing devices at mount/umount time
David Sterba9c6b1c42017-06-16 22:30:00 +0200216 *
217 * global::fs_devs - add, remove, updates to the global list
218 *
219 * does not protect: manipulation of the fs_devices::devices list!
220 *
221 * btrfs_device::name - renames (write side), read is RCU
222 *
223 * fs_devices::device_list_mutex (per-fs, with RCU)
224 * ------------------------------------------------
225 * protects updates to fs_devices::devices, ie. adding and deleting
226 *
227 * simple list traversal with read-only actions can be done with RCU protection
228 *
229 * may be used to exclude some operations from running concurrently without any
230 * modifications to the list (see write_all_supers)
231 *
David Sterba9c6b1c42017-06-16 22:30:00 +0200232 * balance_mutex
233 * -------------
234 * protects balance structures (status, state) and context accessed from
235 * several places (internally, ioctl)
236 *
237 * chunk_mutex
238 * -----------
239 * protects chunks, adding or removing during allocation, trim or when a new
240 * device is added/removed
241 *
242 * cleaner_mutex
243 * -------------
244 * a big lock that is held by the cleaner thread and prevents running subvolume
245 * cleaning together with relocation or delayed iputs
246 *
247 *
248 * Lock nesting
249 * ============
250 *
251 * uuid_mutex
252 * volume_mutex
253 * device_list_mutex
254 * chunk_mutex
255 * balance_mutex
Anand Jain89595e82018-04-18 14:59:25 +0800256 *
257 *
258 * Exclusive operations, BTRFS_FS_EXCL_OP
259 * ======================================
260 *
261 * Maintains the exclusivity of the following operations that apply to the
262 * whole filesystem and cannot run in parallel.
263 *
264 * - Balance (*)
265 * - Device add
266 * - Device remove
267 * - Device replace (*)
268 * - Resize
269 *
270 * The device operations (as above) can be in one of the following states:
271 *
272 * - Running state
273 * - Paused state
274 * - Completed state
275 *
276 * Only device operations marked with (*) can go into the Paused state for the
277 * following reasons:
278 *
279 * - ioctl (only Balance can be Paused through ioctl)
280 * - filesystem remounted as read-only
281 * - filesystem unmounted and mounted as read-only
282 * - system power-cycle and filesystem mounted as read-only
283 * - filesystem or device errors leading to forced read-only
284 *
285 * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations.
286 * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set.
287 * A device operation in Paused or Running state can be canceled or resumed
288 * either by ioctl (Balance only) or when remounted as read-write.
289 * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or
290 * completed.
David Sterba9c6b1c42017-06-16 22:30:00 +0200291 */
292
Miao Xie67a2c452014-09-03 21:35:43 +0800293DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400294static LIST_HEAD(fs_uuids);
Anand Jainc73eccf2015-03-10 06:38:30 +0800295struct list_head *btrfs_get_fs_uuids(void)
296{
297 return &fs_uuids;
298}
Chris Mason8a4b83c2008-03-24 15:02:07 -0400299
David Sterba2dfeca92017-06-14 02:48:07 +0200300/*
301 * alloc_fs_devices - allocate struct btrfs_fs_devices
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200302 * @fsid: if not NULL, copy the UUID to fs_devices::fsid
303 * @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid
David Sterba2dfeca92017-06-14 02:48:07 +0200304 *
305 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
306 * The returned struct is not linked onto any lists and can be destroyed with
307 * kfree() right away.
308 */
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200309static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid,
310 const u8 *metadata_fsid)
Ilya Dryomov2208a372013-08-12 14:33:03 +0300311{
312 struct btrfs_fs_devices *fs_devs;
313
David Sterba78f2c9e2016-02-11 14:25:38 +0100314 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300315 if (!fs_devs)
316 return ERR_PTR(-ENOMEM);
317
318 mutex_init(&fs_devs->device_list_mutex);
319
320 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +0800321 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300322 INIT_LIST_HEAD(&fs_devs->alloc_list);
Anand Jainc4babc52018-04-12 10:29:25 +0800323 INIT_LIST_HEAD(&fs_devs->fs_list);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300324 if (fsid)
325 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300326
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200327 if (metadata_fsid)
328 memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE);
329 else if (fsid)
330 memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE);
331
Ilya Dryomov2208a372013-08-12 14:33:03 +0300332 return fs_devs;
333}
334
David Sterbaa425f9d2018-03-20 15:47:33 +0100335void btrfs_free_device(struct btrfs_device *device)
David Sterba48dae9c2017-10-30 18:10:25 +0100336{
337 rcu_string_free(device->name);
338 bio_put(device->flush_bio);
339 kfree(device);
340}
341
Yan Zhenge4404d62008-12-12 10:03:26 -0500342static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
343{
344 struct btrfs_device *device;
345 WARN_ON(fs_devices->opened);
346 while (!list_empty(&fs_devices->devices)) {
347 device = list_entry(fs_devices->devices.next,
348 struct btrfs_device, dev_list);
349 list_del(&device->dev_list);
David Sterbaa425f9d2018-03-20 15:47:33 +0100350 btrfs_free_device(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500351 }
352 kfree(fs_devices);
353}
354
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000355static void btrfs_kobject_uevent(struct block_device *bdev,
356 enum kobject_action action)
357{
358 int ret;
359
360 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
361 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500362 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000363 action,
364 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
365 &disk_to_dev(bdev->bd_disk)->kobj);
366}
367
David Sterbaffc5a372018-02-19 17:24:15 +0100368void __exit btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400369{
370 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400371
Yan Zheng2b820322008-11-17 21:11:30 -0500372 while (!list_empty(&fs_uuids)) {
373 fs_devices = list_entry(fs_uuids.next,
Anand Jainc4babc52018-04-12 10:29:25 +0800374 struct btrfs_fs_devices, fs_list);
375 list_del(&fs_devices->fs_list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500376 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400377 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400378}
379
David Sterba48dae9c2017-10-30 18:10:25 +0100380/*
381 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
382 * Returned struct is not linked onto any lists and must be destroyed using
David Sterbaa425f9d2018-03-20 15:47:33 +0100383 * btrfs_free_device.
David Sterba48dae9c2017-10-30 18:10:25 +0100384 */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300385static struct btrfs_device *__alloc_device(void)
386{
387 struct btrfs_device *dev;
388
David Sterba78f2c9e2016-02-11 14:25:38 +0100389 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300390 if (!dev)
391 return ERR_PTR(-ENOMEM);
392
David Sterbae0ae9992017-06-06 17:06:06 +0200393 /*
394 * Preallocate a bio that's always going to be used for flushing device
395 * barriers and matches the device lifespan
396 */
397 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
398 if (!dev->flush_bio) {
399 kfree(dev);
400 return ERR_PTR(-ENOMEM);
401 }
David Sterbae0ae9992017-06-06 17:06:06 +0200402
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300403 INIT_LIST_HEAD(&dev->dev_list);
404 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800405 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300406
407 spin_lock_init(&dev->io_lock);
408
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300409 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800410 atomic_set(&dev->dev_stats_ccnt, 0);
Sebastian Andrzej Siewior546bed62016-01-15 14:37:15 +0100411 btrfs_device_data_ordered_init(dev);
Chris Mason9bcaaea2017-05-04 16:08:08 -0700412 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Mel Gormand0164ad2015-11-06 16:28:21 -0800413 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300414
415 return dev;
416}
417
David Sterba35c70102017-06-15 19:51:51 +0200418/*
419 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
420 * return NULL.
421 *
422 * If devid and uuid are both specified, the match must be exact, otherwise
423 * only devid is used.
424 */
425static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
426 u64 devid, const u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400427{
428 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400429
Anand Jain636d2c92018-04-12 10:29:29 +0800430 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400431 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400432 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400433 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400434 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400435 }
436 return NULL;
437}
438
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200439static noinline struct btrfs_fs_devices *find_fsid(
440 const u8 *fsid, const u8 *metadata_fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400441{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400442 struct btrfs_fs_devices *fs_devices;
443
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200444 ASSERT(fsid);
445
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200446 if (metadata_fsid) {
447 /*
448 * Handle scanned device having completed its fsid change but
449 * belonging to a fs_devices that was created by first scanning
450 * a device which didn't have its fsid/metadata_uuid changed
451 * at all and the CHANGING_FSID_V2 flag set.
452 */
453 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
454 if (fs_devices->fsid_change &&
455 memcmp(metadata_fsid, fs_devices->fsid,
456 BTRFS_FSID_SIZE) == 0 &&
457 memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
458 BTRFS_FSID_SIZE) == 0) {
459 return fs_devices;
460 }
461 }
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200462 /*
463 * Handle scanned device having completed its fsid change but
464 * belonging to a fs_devices that was created by a device that
465 * has an outdated pair of fsid/metadata_uuid and
466 * CHANGING_FSID_V2 flag set.
467 */
468 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
469 if (fs_devices->fsid_change &&
470 memcmp(fs_devices->metadata_uuid,
471 fs_devices->fsid, BTRFS_FSID_SIZE) != 0 &&
472 memcmp(metadata_fsid, fs_devices->metadata_uuid,
473 BTRFS_FSID_SIZE) == 0) {
474 return fs_devices;
475 }
476 }
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200477 }
478
479 /* Handle non-split brain cases */
Anand Jainc4babc52018-04-12 10:29:25 +0800480 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200481 if (metadata_fsid) {
482 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0
483 && memcmp(metadata_fsid, fs_devices->metadata_uuid,
484 BTRFS_FSID_SIZE) == 0)
485 return fs_devices;
486 } else {
487 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
488 return fs_devices;
489 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400490 }
491 return NULL;
492}
493
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100494static int
495btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
496 int flush, struct block_device **bdev,
497 struct buffer_head **bh)
498{
499 int ret;
500
501 *bdev = blkdev_get_by_path(device_path, flags, holder);
502
503 if (IS_ERR(*bdev)) {
504 ret = PTR_ERR(*bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100505 goto error;
506 }
507
508 if (flush)
509 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
David Sterba9f6d2512017-06-16 01:48:05 +0200510 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100511 if (ret) {
512 blkdev_put(*bdev, flags);
513 goto error;
514 }
515 invalidate_bdev(*bdev);
516 *bh = btrfs_read_dev_super(*bdev);
Anand Jain92fc03f2015-08-14 18:32:51 +0800517 if (IS_ERR(*bh)) {
518 ret = PTR_ERR(*bh);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100519 blkdev_put(*bdev, flags);
520 goto error;
521 }
522
523 return 0;
524
525error:
526 *bdev = NULL;
527 *bh = NULL;
528 return ret;
529}
530
Chris Masonffbd5172009-04-20 15:50:09 -0400531static void requeue_list(struct btrfs_pending_bios *pending_bios,
532 struct bio *head, struct bio *tail)
533{
534
535 struct bio *old_head;
536
537 old_head = pending_bios->head;
538 pending_bios->head = head;
539 if (pending_bios->tail)
540 tail->bi_next = old_head;
541 else
542 pending_bios->tail = tail;
543}
544
Chris Mason8b712842008-06-11 16:50:36 -0400545/*
546 * we try to collect pending bios for a device so we don't get a large
547 * number of procs sending bios down to the same device. This greatly
548 * improves the schedulers ability to collect and merge the bios.
549 *
550 * But, it also turns into a long list of bios to process and that is sure
551 * to eventually make the worker thread block. The solution here is to
552 * make some progress and then put this work struct back at the end of
553 * the list if the block device is congested. This way, multiple devices
554 * can make progress from a single worker thread.
555 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100556static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400557{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400558 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -0400559 struct bio *pending;
560 struct backing_dev_info *bdi;
Chris Masonffbd5172009-04-20 15:50:09 -0400561 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400562 struct bio *tail;
563 struct bio *cur;
564 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400565 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400566 unsigned long batch_run = 0;
Chris Masonb765ead2009-04-03 10:27:10 -0400567 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400568 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000569 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400570 struct blk_plug plug;
571
572 /*
573 * this function runs all the bios we've collected for
574 * a particular device. We don't want to wander off to
575 * another device without first sending all of these down.
576 * So, setup a plug here and finish it off before we return
577 */
578 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400579
Jan Karaefa7c9f2017-02-02 15:56:53 +0100580 bdi = device->bdev->bd_bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400581
Chris Mason8b712842008-06-11 16:50:36 -0400582loop:
583 spin_lock(&device->io_lock);
584
Chris Masona6837052009-02-04 09:19:41 -0500585loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400586 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400587
Chris Mason8b712842008-06-11 16:50:36 -0400588 /* take all the bios off the list at once and process them
589 * later on (without the lock held). But, remember the
590 * tail and other pointers so the bios can be properly reinserted
591 * into the list if we hit congestion
592 */
Chris Masond84275c2009-06-09 15:39:08 -0400593 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400594 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400595 force_reg = 1;
596 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400597 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400598 force_reg = 0;
599 }
Chris Masonffbd5172009-04-20 15:50:09 -0400600
601 pending = pending_bios->head;
602 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400603 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400604
605 /*
606 * if pending was null this time around, no bios need processing
607 * at all and we can stop. Otherwise it'll loop back up again
608 * and do an additional check so no bios are missed.
609 *
610 * device->running_pending is used to synchronize with the
611 * schedule_bio code.
612 */
Chris Masonffbd5172009-04-20 15:50:09 -0400613 if (device->pending_sync_bios.head == NULL &&
614 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400615 again = 0;
616 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400617 } else {
618 again = 1;
619 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400620 }
Chris Masonffbd5172009-04-20 15:50:09 -0400621
622 pending_bios->head = NULL;
623 pending_bios->tail = NULL;
624
Chris Mason8b712842008-06-11 16:50:36 -0400625 spin_unlock(&device->io_lock);
626
Chris Masond3977122009-01-05 21:25:51 -0500627 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400628
629 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400630 /* we want to work on both lists, but do more bios on the
631 * sync list than the regular list
632 */
633 if ((num_run > 32 &&
634 pending_bios != &device->pending_sync_bios &&
635 device->pending_sync_bios.head) ||
636 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
637 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400638 spin_lock(&device->io_lock);
639 requeue_list(pending_bios, pending, tail);
640 goto loop_lock;
641 }
642
Chris Mason8b712842008-06-11 16:50:36 -0400643 cur = pending;
644 pending = pending->bi_next;
645 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400646
Jens Axboedac56212015-04-17 16:23:59 -0600647 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400648
Chris Mason2ab1ba62011-08-04 14:28:36 -0400649 /*
650 * if we're doing the sync list, record that our
651 * plug has some sync requests on it
652 *
653 * If we're doing the regular list and there are
654 * sync requests sitting around, unplug before
655 * we add more
656 */
657 if (pending_bios == &device->pending_sync_bios) {
658 sync_pending = 1;
659 } else if (sync_pending) {
660 blk_finish_plug(&plug);
661 blk_start_plug(&plug);
662 sync_pending = 0;
663 }
664
Mike Christie4e49ea42016-06-05 14:31:41 -0500665 btrfsic_submit_bio(cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400666 num_run++;
667 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100668
669 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400670
671 /*
672 * we made progress, there is more work to do and the bdi
673 * is now congested. Back off and let other work structs
674 * run instead
675 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400676 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500677 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400678 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400679
Chris Masonb765ead2009-04-03 10:27:10 -0400680 ioc = current->io_context;
681
682 /*
683 * the main goal here is that we don't want to
684 * block if we're going to be able to submit
685 * more requests without blocking.
686 *
687 * This code does two great things, it pokes into
688 * the elevator code from a filesystem _and_
689 * it makes assumptions about how batching works.
690 */
691 if (ioc && ioc->nr_batch_requests > 0 &&
692 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
693 (last_waited == 0 ||
694 ioc->last_waited == last_waited)) {
695 /*
696 * we want to go through our batch of
697 * requests and stop. So, we copy out
698 * the ioc->last_waited time and test
699 * against it before looping
700 */
701 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100702 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400703 continue;
704 }
Chris Mason8b712842008-06-11 16:50:36 -0400705 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400706 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500707 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400708
709 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800710 btrfs_queue_work(fs_info->submit_workers,
711 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400712 goto done;
713 }
714 }
Chris Masonffbd5172009-04-20 15:50:09 -0400715
Chris Mason51684082010-03-10 15:33:32 -0500716 cond_resched();
717 if (again)
718 goto loop;
719
720 spin_lock(&device->io_lock);
721 if (device->pending_bios.head || device->pending_sync_bios.head)
722 goto loop_lock;
723 spin_unlock(&device->io_lock);
724
Chris Mason8b712842008-06-11 16:50:36 -0400725done:
Chris Mason211588a2011-04-19 20:12:40 -0400726 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400727}
728
Christoph Hellwigb2950862008-12-02 09:54:17 -0500729static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400730{
731 struct btrfs_device *device;
732
733 device = container_of(work, struct btrfs_device, work);
734 run_scheduled_bios(device);
735}
736
Anand Jain70bc7082019-01-04 13:31:53 +0800737static bool device_path_matched(const char *path, struct btrfs_device *device)
738{
739 int found;
740
741 rcu_read_lock();
742 found = strcmp(rcu_str_deref(device->name), path);
743 rcu_read_unlock();
744
745 return found == 0;
746}
747
Anand Jaind8367db2018-01-18 22:00:37 +0800748/*
749 * Search and remove all stale (devices which are not mounted) devices.
750 * When both inputs are NULL, it will search and release all stale devices.
751 * path: Optional. When provided will it release all unmounted devices
752 * matching this path only.
753 * skip_dev: Optional. Will skip this device when searching for the stale
754 * devices.
Anand Jain70bc7082019-01-04 13:31:53 +0800755 * Return: 0 for success or if @path is NULL.
756 * -EBUSY if @path is a mounted device.
757 * -ENOENT if @path does not match any device in the list.
Anand Jaind8367db2018-01-18 22:00:37 +0800758 */
Anand Jain70bc7082019-01-04 13:31:53 +0800759static int btrfs_free_stale_devices(const char *path,
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800760 struct btrfs_device *skip_device)
Anand Jain4fde46f2015-06-17 21:10:48 +0800761{
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800762 struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
763 struct btrfs_device *device, *tmp_device;
Anand Jain70bc7082019-01-04 13:31:53 +0800764 int ret = 0;
765
766 if (path)
767 ret = -ENOENT;
Anand Jain4fde46f2015-06-17 21:10:48 +0800768
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800769 list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
Anand Jain4fde46f2015-06-17 21:10:48 +0800770
Anand Jain70bc7082019-01-04 13:31:53 +0800771 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800772 list_for_each_entry_safe(device, tmp_device,
773 &fs_devices->devices, dev_list) {
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800774 if (skip_device && skip_device == device)
Anand Jaind8367db2018-01-18 22:00:37 +0800775 continue;
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800776 if (path && !device->name)
Anand Jain4fde46f2015-06-17 21:10:48 +0800777 continue;
Anand Jain70bc7082019-01-04 13:31:53 +0800778 if (path && !device_path_matched(path, device))
Anand Jain38cf6652018-01-18 22:00:34 +0800779 continue;
Anand Jain70bc7082019-01-04 13:31:53 +0800780 if (fs_devices->opened) {
781 /* for an already deleted device return 0 */
782 if (path && ret != 0)
783 ret = -EBUSY;
784 break;
785 }
Anand Jain4fde46f2015-06-17 21:10:48 +0800786
Anand Jain4fde46f2015-06-17 21:10:48 +0800787 /* delete the stale device */
Anand Jain7bcb8162018-05-29 17:23:20 +0800788 fs_devices->num_devices--;
789 list_del(&device->dev_list);
790 btrfs_free_device(device);
791
Anand Jain70bc7082019-01-04 13:31:53 +0800792 ret = 0;
Anand Jain7bcb8162018-05-29 17:23:20 +0800793 if (fs_devices->num_devices == 0)
Nikolay Borisovfd649f12018-01-30 16:07:37 +0200794 break;
Anand Jain7bcb8162018-05-29 17:23:20 +0800795 }
796 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain70bc7082019-01-04 13:31:53 +0800797
Anand Jain7bcb8162018-05-29 17:23:20 +0800798 if (fs_devices->num_devices == 0) {
799 btrfs_sysfs_remove_fsid(fs_devices);
800 list_del(&fs_devices->fs_list);
801 free_fs_devices(fs_devices);
Anand Jain4fde46f2015-06-17 21:10:48 +0800802 }
803 }
Anand Jain70bc7082019-01-04 13:31:53 +0800804
805 return ret;
Anand Jain4fde46f2015-06-17 21:10:48 +0800806}
807
Anand Jain0fb08bc2017-11-09 23:45:24 +0800808static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
809 struct btrfs_device *device, fmode_t flags,
810 void *holder)
811{
812 struct request_queue *q;
813 struct block_device *bdev;
814 struct buffer_head *bh;
815 struct btrfs_super_block *disk_super;
816 u64 devid;
817 int ret;
818
819 if (device->bdev)
820 return -EINVAL;
821 if (!device->name)
822 return -EINVAL;
823
824 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
825 &bdev, &bh);
826 if (ret)
827 return ret;
828
829 disk_super = (struct btrfs_super_block *)bh->b_data;
830 devid = btrfs_stack_device_id(&disk_super->dev_item);
831 if (devid != device->devid)
832 goto error_brelse;
833
834 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
835 goto error_brelse;
836
837 device->generation = btrfs_super_generation(disk_super);
838
839 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200840 if (btrfs_super_incompat_flags(disk_super) &
841 BTRFS_FEATURE_INCOMPAT_METADATA_UUID) {
842 pr_err(
843 "BTRFS: Invalid seeding and uuid-changed device detected\n");
844 goto error_brelse;
845 }
846
Anand Jainebbede42017-12-04 12:54:52 +0800847 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800848 fs_devices->seeding = 1;
849 } else {
Anand Jainebbede42017-12-04 12:54:52 +0800850 if (bdev_read_only(bdev))
851 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
852 else
853 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800854 }
855
856 q = bdev_get_queue(bdev);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800857 if (!blk_queue_nonrot(q))
858 fs_devices->rotating = 1;
859
860 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +0800861 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800862 device->mode = flags;
863
864 fs_devices->open_devices++;
Anand Jainebbede42017-12-04 12:54:52 +0800865 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
866 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Anand Jain0fb08bc2017-11-09 23:45:24 +0800867 fs_devices->rw_devices++;
Anand Jainb1b8e382018-01-22 14:49:37 -0800868 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800869 }
870 brelse(bh);
871
872 return 0;
873
874error_brelse:
875 brelse(bh);
876 blkdev_put(bdev, flags);
877
878 return -EINVAL;
879}
880
David Sterba60999ca2014-03-26 18:26:36 +0100881/*
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200882 * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices
883 * being created with a disk that has already completed its fsid change.
884 */
885static struct btrfs_fs_devices *find_fsid_inprogress(
886 struct btrfs_super_block *disk_super)
887{
888 struct btrfs_fs_devices *fs_devices;
889
890 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
891 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
892 BTRFS_FSID_SIZE) != 0 &&
893 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
894 BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) {
895 return fs_devices;
896 }
897 }
898
899 return NULL;
900}
901
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200902
903static struct btrfs_fs_devices *find_fsid_changed(
904 struct btrfs_super_block *disk_super)
905{
906 struct btrfs_fs_devices *fs_devices;
907
908 /*
909 * Handles the case where scanned device is part of an fs that had
910 * multiple successful changes of FSID but curently device didn't
911 * observe it. Meaning our fsid will be different than theirs.
912 */
913 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
914 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
915 BTRFS_FSID_SIZE) != 0 &&
916 memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid,
917 BTRFS_FSID_SIZE) == 0 &&
918 memcmp(fs_devices->fsid, disk_super->fsid,
919 BTRFS_FSID_SIZE) != 0) {
920 return fs_devices;
921 }
922 }
923
924 return NULL;
925}
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200926/*
David Sterba60999ca2014-03-26 18:26:36 +0100927 * Add new device to list of registered devices
928 *
929 * Returns:
Anand Jaine124ece2018-01-18 22:02:35 +0800930 * device pointer which was just added or updated when successful
931 * error pointer when failed
David Sterba60999ca2014-03-26 18:26:36 +0100932 */
Anand Jaine124ece2018-01-18 22:02:35 +0800933static noinline struct btrfs_device *device_list_add(const char *path,
Anand Jain4306a972018-05-29 12:28:37 +0800934 struct btrfs_super_block *disk_super,
935 bool *new_device_added)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400936{
937 struct btrfs_device *device;
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200938 struct btrfs_fs_devices *fs_devices = NULL;
Josef Bacik606686e2012-06-04 14:03:51 -0400939 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400940 u64 found_transid = btrfs_super_generation(disk_super);
Anand Jain3acbcbf2018-01-18 22:02:36 +0800941 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200942 bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
943 BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200944 bool fsid_change_in_progress = (btrfs_super_flags(disk_super) &
945 BTRFS_SUPER_FLAG_CHANGING_FSID_V2);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400946
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200947 if (fsid_change_in_progress) {
948 if (!has_metadata_uuid) {
949 /*
950 * When we have an image which has CHANGING_FSID_V2 set
951 * it might belong to either a filesystem which has
952 * disks with completed fsid change or it might belong
953 * to fs with no UUID changes in effect, handle both.
954 */
955 fs_devices = find_fsid_inprogress(disk_super);
956 if (!fs_devices)
957 fs_devices = find_fsid(disk_super->fsid, NULL);
958 } else {
959 fs_devices = find_fsid_changed(disk_super);
960 }
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200961 } else if (has_metadata_uuid) {
962 fs_devices = find_fsid(disk_super->fsid,
963 disk_super->metadata_uuid);
964 } else {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200965 fs_devices = find_fsid(disk_super->fsid, NULL);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200966 }
967
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200968
Chris Mason8a4b83c2008-03-24 15:02:07 -0400969 if (!fs_devices) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200970 if (has_metadata_uuid)
971 fs_devices = alloc_fs_devices(disk_super->fsid,
972 disk_super->metadata_uuid);
973 else
974 fs_devices = alloc_fs_devices(disk_super->fsid, NULL);
975
Ilya Dryomov2208a372013-08-12 14:33:03 +0300976 if (IS_ERR(fs_devices))
Anand Jaine124ece2018-01-18 22:02:35 +0800977 return ERR_CAST(fs_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300978
Al Viro92900e52019-01-27 04:58:00 +0000979 fs_devices->fsid_change = fsid_change_in_progress;
980
Anand Jain9c6d1732018-05-29 14:10:20 +0800981 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainc4babc52018-04-12 10:29:25 +0800982 list_add(&fs_devices->fs_list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300983
Chris Mason8a4b83c2008-03-24 15:02:07 -0400984 device = NULL;
985 } else {
Anand Jain9c6d1732018-05-29 14:10:20 +0800986 mutex_lock(&fs_devices->device_list_mutex);
David Sterba35c70102017-06-15 19:51:51 +0200987 device = find_device(fs_devices, devid,
988 disk_super->dev_item.uuid);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200989
990 /*
991 * If this disk has been pulled into an fs devices created by
992 * a device which had the CHANGING_FSID_V2 flag then replace the
993 * metadata_uuid/fsid values of the fs_devices.
994 */
995 if (has_metadata_uuid && fs_devices->fsid_change &&
996 found_transid > fs_devices->latest_generation) {
997 memcpy(fs_devices->fsid, disk_super->fsid,
998 BTRFS_FSID_SIZE);
999 memcpy(fs_devices->metadata_uuid,
1000 disk_super->metadata_uuid, BTRFS_FSID_SIZE);
1001
1002 fs_devices->fsid_change = false;
1003 }
Chris Mason8a4b83c2008-03-24 15:02:07 -04001004 }
Miao Xie443f24f2014-07-24 11:37:15 +08001005
Chris Mason8a4b83c2008-03-24 15:02:07 -04001006 if (!device) {
Anand Jain9c6d1732018-05-29 14:10:20 +08001007 if (fs_devices->opened) {
1008 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001009 return ERR_PTR(-EBUSY);
Anand Jain9c6d1732018-05-29 14:10:20 +08001010 }
Yan Zheng2b820322008-11-17 21:11:30 -05001011
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03001012 device = btrfs_alloc_device(NULL, &devid,
1013 disk_super->dev_item.uuid);
1014 if (IS_ERR(device)) {
Anand Jain9c6d1732018-05-29 14:10:20 +08001015 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001016 /* we can safely leave the fs_devices entry around */
Anand Jaine124ece2018-01-18 22:02:35 +08001017 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001018 }
Josef Bacik606686e2012-06-04 14:03:51 -04001019
1020 name = rcu_string_strdup(path, GFP_NOFS);
1021 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +01001022 btrfs_free_device(device);
Anand Jain9c6d1732018-05-29 14:10:20 +08001023 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001024 return ERR_PTR(-ENOMEM);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001025 }
Josef Bacik606686e2012-06-04 14:03:51 -04001026 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +02001027
Xiao Guangrong1f781602011-04-20 10:09:16 +00001028 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01001029 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -04001030
Yan Zheng2b820322008-11-17 21:11:30 -05001031 device->fs_devices = fs_devices;
Anand Jain4306a972018-05-29 12:28:37 +08001032 *new_device_added = true;
Anand Jain327f18c2018-01-18 22:02:33 +08001033
1034 if (disk_super->label[0])
1035 pr_info("BTRFS: device label %s devid %llu transid %llu %s\n",
1036 disk_super->label, devid, found_transid, path);
1037 else
1038 pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n",
1039 disk_super->fsid, devid, found_transid, path);
1040
Josef Bacik606686e2012-06-04 14:03:51 -04001041 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +08001042 /*
1043 * When FS is already mounted.
1044 * 1. If you are here and if the device->name is NULL that
1045 * means this device was missing at time of FS mount.
1046 * 2. If you are here and if the device->name is different
1047 * from 'path' that means either
1048 * a. The same device disappeared and reappeared with
1049 * different name. or
1050 * b. The missing-disk-which-was-replaced, has
1051 * reappeared now.
1052 *
1053 * We must allow 1 and 2a above. But 2b would be a spurious
1054 * and unintentional.
1055 *
1056 * Further in case of 1 and 2a above, the disk at 'path'
1057 * would have missed some transaction when it was away and
1058 * in case of 2a the stale bdev has to be updated as well.
1059 * 2b must not be allowed at all time.
1060 */
1061
1062 /*
Chris Mason0f23ae72014-09-18 07:49:05 -07001063 * For now, we do allow update to btrfs_fs_device through the
1064 * btrfs dev scan cli after FS has been mounted. We're still
1065 * tracking a problem where systems fail mount by subvolume id
1066 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +08001067 */
Chris Mason0f23ae72014-09-18 07:49:05 -07001068 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +08001069 /*
1070 * That is if the FS is _not_ mounted and if you
1071 * are here, that means there is more than one
1072 * disk with same uuid and devid.We keep the one
1073 * with larger generation number or the last-in if
1074 * generation are equal.
1075 */
Anand Jain9c6d1732018-05-29 14:10:20 +08001076 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001077 return ERR_PTR(-EEXIST);
Anand Jain77bdae42014-07-03 18:22:06 +08001078 }
Anand Jainb96de002014-07-03 18:22:05 +08001079
Anand Jaina9261d42018-10-15 10:45:17 +08001080 /*
1081 * We are going to replace the device path for a given devid,
1082 * make sure it's the same device if the device is mounted
1083 */
1084 if (device->bdev) {
1085 struct block_device *path_bdev;
1086
1087 path_bdev = lookup_bdev(path);
1088 if (IS_ERR(path_bdev)) {
1089 mutex_unlock(&fs_devices->device_list_mutex);
1090 return ERR_CAST(path_bdev);
1091 }
1092
1093 if (device->bdev != path_bdev) {
1094 bdput(path_bdev);
1095 mutex_unlock(&fs_devices->device_list_mutex);
1096 btrfs_warn_in_rcu(device->fs_info,
1097 "duplicate device fsid:devid for %pU:%llu old:%s new:%s",
1098 disk_super->fsid, devid,
1099 rcu_str_deref(device->name), path);
1100 return ERR_PTR(-EEXIST);
1101 }
1102 bdput(path_bdev);
1103 btrfs_info_in_rcu(device->fs_info,
1104 "device fsid %pU devid %llu moved old:%s new:%s",
1105 disk_super->fsid, devid,
1106 rcu_str_deref(device->name), path);
1107 }
1108
Josef Bacik606686e2012-06-04 14:03:51 -04001109 name = rcu_string_strdup(path, GFP_NOFS);
Anand Jain9c6d1732018-05-29 14:10:20 +08001110 if (!name) {
1111 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001112 return ERR_PTR(-ENOMEM);
Anand Jain9c6d1732018-05-29 14:10:20 +08001113 }
Josef Bacik606686e2012-06-04 14:03:51 -04001114 rcu_string_free(device->name);
1115 rcu_assign_pointer(device->name, name);
Anand Jaine6e674b2017-12-04 12:54:54 +08001116 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05001117 fs_devices->missing_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08001118 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05001119 }
Chris Mason8a4b83c2008-03-24 15:02:07 -04001120 }
1121
Anand Jain77bdae42014-07-03 18:22:06 +08001122 /*
1123 * Unmount does not free the btrfs_device struct but would zero
1124 * generation along with most of the other members. So just update
1125 * it back. We need it to pick the disk with largest generation
1126 * (as above).
1127 */
Nikolay Borisovd1a63002018-10-30 16:43:26 +02001128 if (!fs_devices->opened) {
Anand Jain77bdae42014-07-03 18:22:06 +08001129 device->generation = found_transid;
Nikolay Borisovd1a63002018-10-30 16:43:26 +02001130 fs_devices->latest_generation = max_t(u64, found_transid,
1131 fs_devices->latest_generation);
1132 }
Anand Jain77bdae42014-07-03 18:22:06 +08001133
Anand Jainf2788d22018-01-18 22:02:34 +08001134 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
1135
Anand Jain9c6d1732018-05-29 14:10:20 +08001136 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001137 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001138}
1139
Yan Zhenge4404d62008-12-12 10:03:26 -05001140static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
1141{
1142 struct btrfs_fs_devices *fs_devices;
1143 struct btrfs_device *device;
1144 struct btrfs_device *orig_dev;
1145
Nikolay Borisov7239ff42018-10-30 16:43:23 +02001146 fs_devices = alloc_fs_devices(orig->fsid, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03001147 if (IS_ERR(fs_devices))
1148 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001149
Miao Xieadbbb862014-09-03 21:35:42 +08001150 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -04001151 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001152
Xiao Guangrong46224702011-04-20 10:08:47 +00001153 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -05001154 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -04001155 struct rcu_string *name;
1156
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03001157 device = btrfs_alloc_device(NULL, &orig_dev->devid,
1158 orig_dev->uuid);
1159 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -05001160 goto error;
1161
Josef Bacik606686e2012-06-04 14:03:51 -04001162 /*
1163 * This is ok to do without rcu read locked because we hold the
1164 * uuid mutex so nothing we touch in here is going to disappear.
1165 */
Anand Jaine755f782014-06-30 17:12:47 +08001166 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +01001167 name = rcu_string_strdup(orig_dev->name->str,
1168 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +08001169 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +01001170 btrfs_free_device(device);
Anand Jaine755f782014-06-30 17:12:47 +08001171 goto error;
1172 }
1173 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -04001174 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001175
Yan Zhenge4404d62008-12-12 10:03:26 -05001176 list_add(&device->dev_list, &fs_devices->devices);
1177 device->fs_devices = fs_devices;
1178 fs_devices->num_devices++;
1179 }
Miao Xieadbbb862014-09-03 21:35:42 +08001180 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001181 return fs_devices;
1182error:
Miao Xieadbbb862014-09-03 21:35:42 +08001183 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001184 free_fs_devices(fs_devices);
1185 return ERR_PTR(-ENOMEM);
1186}
1187
Anand Jain9b99b112018-02-27 12:41:59 +08001188/*
1189 * After we have read the system tree and know devids belonging to
1190 * this filesystem, remove the device which does not belong there.
1191 */
1192void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -04001193{
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001194 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +08001195 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001196
Chris Masondfe25022008-05-13 13:46:40 -04001197 mutex_lock(&uuid_mutex);
1198again:
Xiao Guangrong46224702011-04-20 10:08:47 +00001199 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001200 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +08001201 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1202 &device->dev_state)) {
Anand Jain401e29c2017-12-04 12:54:55 +08001203 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1204 &device->dev_state) &&
1205 (!latest_dev ||
1206 device->generation > latest_dev->generation)) {
Miao Xie443f24f2014-07-24 11:37:15 +08001207 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001208 }
Yan Zheng2b820322008-11-17 21:11:30 -05001209 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001210 }
Yan Zheng2b820322008-11-17 21:11:30 -05001211
Stefan Behrens8dabb742012-11-06 13:15:27 +01001212 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
1213 /*
1214 * In the first step, keep the device which has
1215 * the correct fsid and the devid that is used
1216 * for the dev_replace procedure.
1217 * In the second step, the dev_replace state is
1218 * read from the device tree and it is known
1219 * whether the procedure is really active or
1220 * not, which means whether this device is
1221 * used or whether it should be removed.
1222 */
Anand Jain401e29c2017-12-04 12:54:55 +08001223 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1224 &device->dev_state)) {
Stefan Behrens8dabb742012-11-06 13:15:27 +01001225 continue;
1226 }
1227 }
Yan Zheng2b820322008-11-17 21:11:30 -05001228 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +01001229 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -05001230 device->bdev = NULL;
1231 fs_devices->open_devices--;
1232 }
Anand Jainebbede42017-12-04 12:54:52 +08001233 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001234 list_del_init(&device->dev_alloc_list);
Anand Jainebbede42017-12-04 12:54:52 +08001235 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08001236 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1237 &device->dev_state))
Stefan Behrens8dabb742012-11-06 13:15:27 +01001238 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001239 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001240 list_del_init(&device->dev_list);
1241 fs_devices->num_devices--;
David Sterbaa425f9d2018-03-20 15:47:33 +01001242 btrfs_free_device(device);
Chris Masondfe25022008-05-13 13:46:40 -04001243 }
Yan Zheng2b820322008-11-17 21:11:30 -05001244
1245 if (fs_devices->seed) {
1246 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001247 goto again;
1248 }
1249
Miao Xie443f24f2014-07-24 11:37:15 +08001250 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001251
Chris Masondfe25022008-05-13 13:46:40 -04001252 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001253}
Chris Masona0af4692008-05-13 16:03:06 -04001254
David Sterbaf06c5962017-06-06 17:08:23 +02001255static void free_device_rcu(struct rcu_head *head)
Xiao Guangrong1f781602011-04-20 10:09:16 +00001256{
1257 struct btrfs_device *device;
1258
1259 device = container_of(head, struct btrfs_device, rcu);
David Sterbaa425f9d2018-03-20 15:47:33 +01001260 btrfs_free_device(device);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001261}
1262
Anand Jain14238812016-07-22 06:04:53 +08001263static void btrfs_close_bdev(struct btrfs_device *device)
1264{
David Sterba08ffcae2017-06-19 16:55:35 +02001265 if (!device->bdev)
1266 return;
1267
Anand Jainebbede42017-12-04 12:54:52 +08001268 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Anand Jain14238812016-07-22 06:04:53 +08001269 sync_blockdev(device->bdev);
1270 invalidate_bdev(device->bdev);
1271 }
1272
David Sterba08ffcae2017-06-19 16:55:35 +02001273 blkdev_put(device->bdev, device->mode);
Anand Jain14238812016-07-22 06:04:53 +08001274}
1275
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001276static void btrfs_close_one_device(struct btrfs_device *device)
Anand Jainf448341a2016-06-14 18:55:25 +08001277{
1278 struct btrfs_fs_devices *fs_devices = device->fs_devices;
1279 struct btrfs_device *new_device;
1280 struct rcu_string *name;
1281
1282 if (device->bdev)
1283 fs_devices->open_devices--;
1284
Anand Jainebbede42017-12-04 12:54:52 +08001285 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jainf448341a2016-06-14 18:55:25 +08001286 device->devid != BTRFS_DEV_REPLACE_DEVID) {
1287 list_del_init(&device->dev_alloc_list);
1288 fs_devices->rw_devices--;
1289 }
1290
Anand Jaine6e674b2017-12-04 12:54:54 +08001291 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jainf448341a2016-06-14 18:55:25 +08001292 fs_devices->missing_devices--;
1293
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001294 btrfs_close_bdev(device);
1295
Anand Jainf448341a2016-06-14 18:55:25 +08001296 new_device = btrfs_alloc_device(NULL, &device->devid,
1297 device->uuid);
1298 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
1299
1300 /* Safe because we are under uuid_mutex */
1301 if (device->name) {
1302 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1303 BUG_ON(!name); /* -ENOMEM */
1304 rcu_assign_pointer(new_device->name, name);
1305 }
1306
1307 list_replace_rcu(&device->dev_list, &new_device->dev_list);
1308 new_device->fs_devices = device->fs_devices;
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001309
1310 call_rcu(&device->rcu, free_device_rcu);
Anand Jainf448341a2016-06-14 18:55:25 +08001311}
1312
Anand Jain0226e0e2018-04-12 10:29:27 +08001313static int close_fs_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001314{
Sasha Levin2037a092015-05-12 19:31:37 -04001315 struct btrfs_device *device, *tmp;
Yan Zhenge4404d62008-12-12 10:03:26 -05001316
Yan Zheng2b820322008-11-17 21:11:30 -05001317 if (--fs_devices->opened > 0)
1318 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001319
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001320 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -04001321 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001322 btrfs_close_one_device(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001323 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001324 mutex_unlock(&fs_devices->device_list_mutex);
1325
Yan Zhenge4404d62008-12-12 10:03:26 -05001326 WARN_ON(fs_devices->open_devices);
1327 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001328 fs_devices->opened = 0;
1329 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001330
Chris Mason8a4b83c2008-03-24 15:02:07 -04001331 return 0;
1332}
1333
Yan Zheng2b820322008-11-17 21:11:30 -05001334int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1335{
Yan Zhenge4404d62008-12-12 10:03:26 -05001336 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001337 int ret;
1338
1339 mutex_lock(&uuid_mutex);
Anand Jain0226e0e2018-04-12 10:29:27 +08001340 ret = close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001341 if (!fs_devices->opened) {
1342 seed_devices = fs_devices->seed;
1343 fs_devices->seed = NULL;
1344 }
Yan Zheng2b820322008-11-17 21:11:30 -05001345 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001346
1347 while (seed_devices) {
1348 fs_devices = seed_devices;
1349 seed_devices = fs_devices->seed;
Anand Jain0226e0e2018-04-12 10:29:27 +08001350 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001351 free_fs_devices(fs_devices);
1352 }
Yan Zheng2b820322008-11-17 21:11:30 -05001353 return ret;
1354}
1355
Anand Jain897fb572018-04-12 10:29:28 +08001356static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
Yan Zhenge4404d62008-12-12 10:03:26 -05001357 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001358{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001359 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +08001360 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -04001361 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001362
Tejun Heod4d77622010-11-13 11:55:18 +01001363 flags |= FMODE_EXCL;
1364
Anand Jainf117e292018-04-12 10:29:26 +08001365 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Eric Sandeenf63e0cc2013-04-04 20:45:08 +00001366 /* Just open everything we can; ignore failures here */
Anand Jain0fb08bc2017-11-09 23:45:24 +08001367 if (btrfs_open_one_device(fs_devices, device, flags, holder))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001368 continue;
Chris Masona0af4692008-05-13 16:03:06 -04001369
Anand Jain9f050db2017-11-09 23:45:25 +08001370 if (!latest_dev ||
1371 device->generation > latest_dev->generation)
1372 latest_dev = device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001373 }
Chris Masona0af4692008-05-13 16:03:06 -04001374 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +03001375 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -04001376 goto out;
1377 }
Yan Zheng2b820322008-11-17 21:11:30 -05001378 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +08001379 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -05001380 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -04001381out:
Yan Zheng2b820322008-11-17 21:11:30 -05001382 return ret;
1383}
1384
Anand Jainf8e10cd2018-01-22 14:49:36 -08001385static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1386{
1387 struct btrfs_device *dev1, *dev2;
1388
1389 dev1 = list_entry(a, struct btrfs_device, dev_list);
1390 dev2 = list_entry(b, struct btrfs_device, dev_list);
1391
1392 if (dev1->devid < dev2->devid)
1393 return -1;
1394 else if (dev1->devid > dev2->devid)
1395 return 1;
1396 return 0;
1397}
1398
Yan Zheng2b820322008-11-17 21:11:30 -05001399int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001400 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001401{
1402 int ret;
1403
David Sterbaf5194e32018-06-19 17:09:47 +02001404 lockdep_assert_held(&uuid_mutex);
1405
Anand Jain542c5902018-04-12 10:29:34 +08001406 mutex_lock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001407 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001408 fs_devices->opened++;
1409 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001410 } else {
Anand Jainf8e10cd2018-01-22 14:49:36 -08001411 list_sort(NULL, &fs_devices->devices, devid_cmp);
Anand Jain897fb572018-04-12 10:29:28 +08001412 ret = open_fs_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001413 }
Anand Jain542c5902018-04-12 10:29:34 +08001414 mutex_unlock(&fs_devices->device_list_mutex);
1415
Chris Mason8a4b83c2008-03-24 15:02:07 -04001416 return ret;
1417}
1418
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001419static void btrfs_release_disk_super(struct page *page)
Anand Jain6cf86a002016-02-13 10:01:29 +08001420{
1421 kunmap(page);
1422 put_page(page);
1423}
1424
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001425static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1426 struct page **page,
1427 struct btrfs_super_block **disk_super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001428{
1429 void *p;
1430 pgoff_t index;
1431
1432 /* make sure our super fits in the device */
1433 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1434 return 1;
1435
1436 /* make sure our super fits in the page */
1437 if (sizeof(**disk_super) > PAGE_SIZE)
1438 return 1;
1439
1440 /* make sure our super doesn't straddle pages on disk */
1441 index = bytenr >> PAGE_SHIFT;
1442 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1443 return 1;
1444
1445 /* pull in the page with our super */
1446 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1447 index, GFP_KERNEL);
1448
1449 if (IS_ERR_OR_NULL(*page))
1450 return 1;
1451
1452 p = kmap(*page);
1453
1454 /* align our pointer to the offset of the super block */
Johannes Thumshirn70730172018-12-05 15:23:03 +01001455 *disk_super = p + offset_in_page(bytenr);
Anand Jain6cf86a002016-02-13 10:01:29 +08001456
1457 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1458 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1459 btrfs_release_disk_super(*page);
1460 return 1;
1461 }
1462
1463 if ((*disk_super)->label[0] &&
1464 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1465 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1466
1467 return 0;
1468}
1469
David Sterba6f60cbd2013-02-15 11:31:02 -07001470/*
1471 * Look for a btrfs signature on a device. This may be called out of the mount path
1472 * and we are not allowed to call set_blocksize during the scan. The superblock
1473 * is read via pagecache
1474 */
Gu Jinxiang36350e92018-07-12 14:23:16 +08001475struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,
1476 void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001477{
1478 struct btrfs_super_block *disk_super;
Anand Jain4306a972018-05-29 12:28:37 +08001479 bool new_device_added = false;
Gu Jinxiang36350e92018-07-12 14:23:16 +08001480 struct btrfs_device *device = NULL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001481 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001482 struct page *page;
David Sterba6f60cbd2013-02-15 11:31:02 -07001483 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001484
David Sterba899f9302018-06-19 16:37:36 +02001485 lockdep_assert_held(&uuid_mutex);
1486
David Sterba6f60cbd2013-02-15 11:31:02 -07001487 /*
1488 * we would like to check all the supers, but that would make
1489 * a btrfs mount succeed after a mkfs from a different FS.
1490 * So, we need to add a special mount option to scan for
1491 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1492 */
1493 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001494 flags |= FMODE_EXCL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001495
1496 bdev = blkdev_get_by_path(path, flags, holder);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001497 if (IS_ERR(bdev))
Gu Jinxiang36350e92018-07-12 14:23:16 +08001498 return ERR_CAST(bdev);
David Sterba6f60cbd2013-02-15 11:31:02 -07001499
Anand Jain05a5c552017-12-15 15:40:16 +08001500 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
Gu Jinxiang36350e92018-07-12 14:23:16 +08001501 device = ERR_PTR(-EINVAL);
David Sterba6f60cbd2013-02-15 11:31:02 -07001502 goto error_bdev_put;
Anand Jain05a5c552017-12-15 15:40:16 +08001503 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001504
Anand Jain4306a972018-05-29 12:28:37 +08001505 device = device_list_add(path, disk_super, &new_device_added);
Gu Jinxiang36350e92018-07-12 14:23:16 +08001506 if (!IS_ERR(device)) {
Anand Jain4306a972018-05-29 12:28:37 +08001507 if (new_device_added)
1508 btrfs_free_stale_devices(path, device);
1509 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001510
Anand Jain6cf86a002016-02-13 10:01:29 +08001511 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001512
1513error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001514 blkdev_put(bdev, flags);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001515
Gu Jinxiang36350e92018-07-12 14:23:16 +08001516 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001517}
Chris Mason0b86a832008-03-24 15:01:56 -04001518
Jeff Mahoney499f3772015-06-15 09:41:17 -04001519static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001520 struct btrfs_device *device,
1521 u64 *start, u64 len)
1522{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001523 struct btrfs_fs_info *fs_info = device->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001524 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001525 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001526 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001527 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001528
Jeff Mahoney499f3772015-06-15 09:41:17 -04001529 if (transaction)
1530 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001531again:
1532 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001533 struct map_lookup *map;
1534 int i;
1535
Jeff Mahoney95617d62015-06-03 10:55:48 -04001536 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04001537 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001538 u64 end;
1539
Josef Bacik6df9a952013-06-27 13:22:46 -04001540 if (map->stripes[i].dev != device)
1541 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001542 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001543 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001544 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001545 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001546 /*
1547 * Make sure that while processing the pinned list we do
1548 * not override our *start with a lower value, because
1549 * we can have pinned chunks that fall within this
1550 * device hole and that have lower physical addresses
1551 * than the pending chunks we processed before. If we
1552 * do not take this special care we can end up getting
1553 * 2 pending chunks that start at the same physical
1554 * device offsets because the end offset of a pinned
1555 * chunk can be equal to the start offset of some
1556 * pending chunk.
1557 */
1558 end = map->stripes[i].physical + em->orig_block_len;
1559 if (end > *start) {
1560 *start = end;
1561 ret = 1;
1562 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001563 }
1564 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001565 if (search_list != &fs_info->pinned_chunks) {
1566 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001567 goto again;
1568 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001569
1570 return ret;
1571}
1572
1573
Chris Mason0b86a832008-03-24 15:01:56 -04001574/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001575 * find_free_dev_extent_start - find free space in the specified device
1576 * @device: the device which we search the free space in
1577 * @num_bytes: the size of the free space that we need
1578 * @search_start: the position from which to begin the search
1579 * @start: store the start of the free space.
1580 * @len: the size of the free space. that we find, or the size
1581 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001582 *
Chris Mason0b86a832008-03-24 15:01:56 -04001583 * this uses a pretty simple search, the expectation is that it is
1584 * called very infrequently and that a given device has a small number
1585 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001586 *
1587 * @start is used to store the start of the free space if we find. But if we
1588 * don't find suitable free space, it will be used to store the start position
1589 * of the max free space.
1590 *
1591 * @len is used to store the size of the free space that we find.
1592 * But if we don't find suitable free space, it is used to store the size of
1593 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001594 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001595int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1596 struct btrfs_device *device, u64 num_bytes,
1597 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001598{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001599 struct btrfs_fs_info *fs_info = device->fs_info;
1600 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001601 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001602 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001603 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001604 u64 hole_size;
1605 u64 max_hole_start;
1606 u64 max_hole_size;
1607 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001608 u64 search_end = device->total_bytes;
1609 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001610 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001611 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001612
1613 /*
1614 * We don't want to overwrite the superblock on the drive nor any area
1615 * used by the boot loader (grub for example), so we make sure to start
1616 * at an offset of at least 1MB.
1617 */
David Sterba0d0c71b2017-06-15 01:30:06 +02001618 search_start = max_t(u64, search_start, SZ_1M);
Chris Mason0b86a832008-03-24 15:01:56 -04001619
Josef Bacik6df9a952013-06-27 13:22:46 -04001620 path = btrfs_alloc_path();
1621 if (!path)
1622 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001623
Miao Xie7bfc8372011-01-05 10:07:26 +00001624 max_hole_start = search_start;
1625 max_hole_size = 0;
1626
Zhao Leif2ab7612015-02-16 18:52:17 +08001627again:
Anand Jain401e29c2017-12-04 12:54:55 +08001628 if (search_start >= search_end ||
1629 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001630 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001631 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001632 }
1633
David Sterbae4058b52015-11-27 16:31:35 +01001634 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001635 path->search_commit_root = 1;
1636 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001637
Chris Mason0b86a832008-03-24 15:01:56 -04001638 key.objectid = device->devid;
1639 key.offset = search_start;
1640 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001641
Li Zefan125ccb02011-12-08 15:07:24 +08001642 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001643 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001644 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001645 if (ret > 0) {
1646 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1647 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001648 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001649 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001650
Chris Mason0b86a832008-03-24 15:01:56 -04001651 while (1) {
1652 l = path->nodes[0];
1653 slot = path->slots[0];
1654 if (slot >= btrfs_header_nritems(l)) {
1655 ret = btrfs_next_leaf(root, path);
1656 if (ret == 0)
1657 continue;
1658 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001659 goto out;
1660
1661 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001662 }
1663 btrfs_item_key_to_cpu(l, &key, slot);
1664
1665 if (key.objectid < device->devid)
1666 goto next;
1667
1668 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001669 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001670
David Sterba962a2982014-06-04 18:41:45 +02001671 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001672 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001673
Miao Xie7bfc8372011-01-05 10:07:26 +00001674 if (key.offset > search_start) {
1675 hole_size = key.offset - search_start;
1676
Josef Bacik6df9a952013-06-27 13:22:46 -04001677 /*
1678 * Have to check before we set max_hole_start, otherwise
1679 * we could end up sending back this offset anyway.
1680 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001681 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001682 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001683 hole_size)) {
1684 if (key.offset >= search_start) {
1685 hole_size = key.offset - search_start;
1686 } else {
1687 WARN_ON_ONCE(1);
1688 hole_size = 0;
1689 }
1690 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001691
Miao Xie7bfc8372011-01-05 10:07:26 +00001692 if (hole_size > max_hole_size) {
1693 max_hole_start = search_start;
1694 max_hole_size = hole_size;
1695 }
1696
1697 /*
1698 * If this free space is greater than which we need,
1699 * it must be the max free space that we have found
1700 * until now, so max_hole_start must point to the start
1701 * of this free space and the length of this free space
1702 * is stored in max_hole_size. Thus, we return
1703 * max_hole_start and max_hole_size and go back to the
1704 * caller.
1705 */
1706 if (hole_size >= num_bytes) {
1707 ret = 0;
1708 goto out;
1709 }
1710 }
1711
Chris Mason0b86a832008-03-24 15:01:56 -04001712 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001713 extent_end = key.offset + btrfs_dev_extent_length(l,
1714 dev_extent);
1715 if (extent_end > search_start)
1716 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001717next:
1718 path->slots[0]++;
1719 cond_resched();
1720 }
Chris Mason0b86a832008-03-24 15:01:56 -04001721
liubo38c01b92011-08-02 02:39:03 +00001722 /*
1723 * At this point, search_start should be the end of
1724 * allocated dev extents, and when shrinking the device,
1725 * search_end may be smaller than search_start.
1726 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001727 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001728 hole_size = search_end - search_start;
1729
Jeff Mahoney499f3772015-06-15 09:41:17 -04001730 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001731 hole_size)) {
1732 btrfs_release_path(path);
1733 goto again;
1734 }
Chris Mason0b86a832008-03-24 15:01:56 -04001735
Zhao Leif2ab7612015-02-16 18:52:17 +08001736 if (hole_size > max_hole_size) {
1737 max_hole_start = search_start;
1738 max_hole_size = hole_size;
1739 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001740 }
1741
Miao Xie7bfc8372011-01-05 10:07:26 +00001742 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001743 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001744 ret = -ENOSPC;
1745 else
1746 ret = 0;
1747
1748out:
Yan Zheng2b820322008-11-17 21:11:30 -05001749 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001750 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001751 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001752 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001753 return ret;
1754}
1755
Jeff Mahoney499f3772015-06-15 09:41:17 -04001756int find_free_dev_extent(struct btrfs_trans_handle *trans,
1757 struct btrfs_device *device, u64 num_bytes,
1758 u64 *start, u64 *len)
1759{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001760 /* FIXME use last free of some kind */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001761 return find_free_dev_extent_start(trans->transaction, device,
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001762 num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001763}
1764
Christoph Hellwigb2950862008-12-02 09:54:17 -05001765static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001766 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001767 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001768{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001769 struct btrfs_fs_info *fs_info = device->fs_info;
1770 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001771 int ret;
1772 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001773 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001774 struct btrfs_key found_key;
1775 struct extent_buffer *leaf = NULL;
1776 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001777
1778 path = btrfs_alloc_path();
1779 if (!path)
1780 return -ENOMEM;
1781
1782 key.objectid = device->devid;
1783 key.offset = start;
1784 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001785again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001786 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001787 if (ret > 0) {
1788 ret = btrfs_previous_item(root, path, key.objectid,
1789 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001790 if (ret)
1791 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001792 leaf = path->nodes[0];
1793 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1794 extent = btrfs_item_ptr(leaf, path->slots[0],
1795 struct btrfs_dev_extent);
1796 BUG_ON(found_key.offset > start || found_key.offset +
1797 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001798 key = found_key;
1799 btrfs_release_path(path);
1800 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001801 } else if (ret == 0) {
1802 leaf = path->nodes[0];
1803 extent = btrfs_item_ptr(leaf, path->slots[0],
1804 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001805 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001806 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001807 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001808 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001809
Miao Xie2196d6e2014-09-03 21:35:41 +08001810 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1811
Chris Mason8f18cf12008-04-25 16:53:30 -04001812 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001813 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001814 btrfs_handle_fs_error(fs_info, ret,
1815 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001816 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001817 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001818 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001819out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001820 btrfs_free_path(path);
1821 return ret;
1822}
1823
Eric Sandeen48a3b632013-04-25 20:41:01 +00001824static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1825 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001826 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001827{
1828 int ret;
1829 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001830 struct btrfs_fs_info *fs_info = device->fs_info;
1831 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001832 struct btrfs_dev_extent *extent;
1833 struct extent_buffer *leaf;
1834 struct btrfs_key key;
1835
Anand Jaine12c9622017-12-04 12:54:53 +08001836 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
Anand Jain401e29c2017-12-04 12:54:55 +08001837 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
Chris Mason0b86a832008-03-24 15:01:56 -04001838 path = btrfs_alloc_path();
1839 if (!path)
1840 return -ENOMEM;
1841
Chris Mason0b86a832008-03-24 15:01:56 -04001842 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001843 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001844 key.type = BTRFS_DEV_EXTENT_KEY;
1845 ret = btrfs_insert_empty_item(trans, root, path, &key,
1846 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001847 if (ret)
1848 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001849
1850 leaf = path->nodes[0];
1851 extent = btrfs_item_ptr(leaf, path->slots[0],
1852 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001853 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1854 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001855 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1856 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001857 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1858
Chris Mason0b86a832008-03-24 15:01:56 -04001859 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1860 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001861out:
Chris Mason0b86a832008-03-24 15:01:56 -04001862 btrfs_free_path(path);
1863 return ret;
1864}
1865
Josef Bacik6df9a952013-06-27 13:22:46 -04001866static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001867{
Josef Bacik6df9a952013-06-27 13:22:46 -04001868 struct extent_map_tree *em_tree;
1869 struct extent_map *em;
1870 struct rb_node *n;
1871 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001872
Josef Bacik6df9a952013-06-27 13:22:46 -04001873 em_tree = &fs_info->mapping_tree.map_tree;
1874 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08001875 n = rb_last(&em_tree->map.rb_root);
Josef Bacik6df9a952013-06-27 13:22:46 -04001876 if (n) {
1877 em = rb_entry(n, struct extent_map, rb_node);
1878 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001879 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001880 read_unlock(&em_tree->lock);
1881
Chris Mason0b86a832008-03-24 15:01:56 -04001882 return ret;
1883}
1884
Ilya Dryomov53f10652013-08-12 14:33:01 +03001885static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1886 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001887{
1888 int ret;
1889 struct btrfs_key key;
1890 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001891 struct btrfs_path *path;
1892
Yan Zheng2b820322008-11-17 21:11:30 -05001893 path = btrfs_alloc_path();
1894 if (!path)
1895 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001896
1897 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1898 key.type = BTRFS_DEV_ITEM_KEY;
1899 key.offset = (u64)-1;
1900
Ilya Dryomov53f10652013-08-12 14:33:01 +03001901 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001902 if (ret < 0)
1903 goto error;
1904
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001905 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001906
Ilya Dryomov53f10652013-08-12 14:33:01 +03001907 ret = btrfs_previous_item(fs_info->chunk_root, path,
1908 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001909 BTRFS_DEV_ITEM_KEY);
1910 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001911 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001912 } else {
1913 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1914 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001915 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001916 }
1917 ret = 0;
1918error:
Yan Zheng2b820322008-11-17 21:11:30 -05001919 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001920 return ret;
1921}
1922
1923/*
1924 * the device information is stored in the chunk root
1925 * the btrfs_device struct should be fully filled in
1926 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001927static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001928 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001929{
1930 int ret;
1931 struct btrfs_path *path;
1932 struct btrfs_dev_item *dev_item;
1933 struct extent_buffer *leaf;
1934 struct btrfs_key key;
1935 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001936
Chris Mason0b86a832008-03-24 15:01:56 -04001937 path = btrfs_alloc_path();
1938 if (!path)
1939 return -ENOMEM;
1940
Chris Mason0b86a832008-03-24 15:01:56 -04001941 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1942 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001943 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001944
Nikolay Borisov8e87e852018-07-20 19:37:47 +03001945 ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path,
1946 &key, sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001947 if (ret)
1948 goto out;
1949
1950 leaf = path->nodes[0];
1951 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1952
1953 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001954 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001955 btrfs_set_device_type(leaf, dev_item, device->type);
1956 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1957 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1958 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001959 btrfs_set_device_total_bytes(leaf, dev_item,
1960 btrfs_device_get_disk_total_bytes(device));
1961 btrfs_set_device_bytes_used(leaf, dev_item,
1962 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001963 btrfs_set_device_group(leaf, dev_item, 0);
1964 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1965 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001966 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001967
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001968 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001969 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001970 ptr = btrfs_device_fsid(dev_item);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02001971 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid,
1972 ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001973 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001974
Yan Zheng2b820322008-11-17 21:11:30 -05001975 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001976out:
1977 btrfs_free_path(path);
1978 return ret;
1979}
Chris Mason8f18cf12008-04-25 16:53:30 -04001980
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001981/*
1982 * Function to update ctime/mtime for a given device path.
1983 * Mainly used for ctime/mtime based probe like libblkid.
1984 */
David Sterbada353f62017-02-14 17:55:53 +01001985static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001986{
1987 struct file *filp;
1988
1989 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001990 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001991 return;
1992 file_update_time(filp);
1993 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001994}
1995
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001996static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
Chris Masona061fc82008-05-07 11:43:44 -04001997 struct btrfs_device *device)
1998{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001999 struct btrfs_root *root = fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04002000 int ret;
2001 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04002002 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04002003 struct btrfs_trans_handle *trans;
2004
Chris Masona061fc82008-05-07 11:43:44 -04002005 path = btrfs_alloc_path();
2006 if (!path)
2007 return -ENOMEM;
2008
Yan, Zhenga22285a2010-05-16 10:48:46 -04002009 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002010 if (IS_ERR(trans)) {
2011 btrfs_free_path(path);
2012 return PTR_ERR(trans);
2013 }
Chris Masona061fc82008-05-07 11:43:44 -04002014 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2015 key.type = BTRFS_DEV_ITEM_KEY;
2016 key.offset = device->devid;
2017
2018 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03002019 if (ret) {
2020 if (ret > 0)
2021 ret = -ENOENT;
2022 btrfs_abort_transaction(trans, ret);
2023 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04002024 goto out;
2025 }
2026
2027 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03002028 if (ret) {
2029 btrfs_abort_transaction(trans, ret);
2030 btrfs_end_transaction(trans);
2031 }
2032
Chris Masona061fc82008-05-07 11:43:44 -04002033out:
2034 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03002035 if (!ret)
2036 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04002037 return ret;
2038}
2039
David Sterba3cc31a02016-02-15 16:00:26 +01002040/*
2041 * Verify that @num_devices satisfies the RAID profile constraints in the whole
2042 * filesystem. It's up to the caller to adjust that number regarding eg. device
2043 * replace.
2044 */
2045static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
2046 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04002047{
Chris Masona061fc82008-05-07 11:43:44 -04002048 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00002049 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01002050 int i;
Chris Masona061fc82008-05-07 11:43:44 -04002051
Miao Xiede98ced2013-01-29 10:13:12 +00002052 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08002053 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00002054
Anand Jainbd45ffb2016-02-13 10:01:34 +08002055 all_avail = fs_info->avail_data_alloc_bits |
2056 fs_info->avail_system_alloc_bits |
2057 fs_info->avail_metadata_alloc_bits;
2058 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08002059
David Sterba418775a2016-02-15 16:28:14 +01002060 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
Anand Jain41a6e892018-04-25 19:01:43 +08002061 if (!(all_avail & btrfs_raid_array[i].bg_flag))
David Sterba418775a2016-02-15 16:28:14 +01002062 continue;
Chris Masona061fc82008-05-07 11:43:44 -04002063
David Sterba418775a2016-02-15 16:28:14 +01002064 if (num_devices < btrfs_raid_array[i].devs_min) {
Anand Jainf9fbcaa2018-04-25 19:01:44 +08002065 int ret = btrfs_raid_array[i].mindev_error;
Chris Masona061fc82008-05-07 11:43:44 -04002066
David Sterba418775a2016-02-15 16:28:14 +01002067 if (ret)
2068 return ret;
2069 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08002070 }
2071
2072 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002073}
2074
Omar Sandovalc9162bd2017-08-22 23:46:04 -07002075static struct btrfs_device * btrfs_find_next_active_device(
2076 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08002077{
2078 struct btrfs_device *next_device;
2079
2080 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
2081 if (next_device != device &&
Anand Jaine6e674b2017-12-04 12:54:54 +08002082 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
2083 && next_device->bdev)
Anand Jain88acff62016-05-03 17:44:43 +08002084 return next_device;
2085 }
2086
2087 return NULL;
2088}
2089
2090/*
2091 * Helper function to check if the given device is part of s_bdev / latest_bdev
2092 * and replace it with the provided or the next active device, in the context
2093 * where this function called, there should be always be another device (or
2094 * this_dev) which is active.
2095 */
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002096void btrfs_assign_next_active_device(struct btrfs_device *device,
2097 struct btrfs_device *this_dev)
Anand Jain88acff62016-05-03 17:44:43 +08002098{
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002099 struct btrfs_fs_info *fs_info = device->fs_info;
Anand Jain88acff62016-05-03 17:44:43 +08002100 struct btrfs_device *next_device;
2101
2102 if (this_dev)
2103 next_device = this_dev;
2104 else
2105 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
2106 device);
2107 ASSERT(next_device);
2108
2109 if (fs_info->sb->s_bdev &&
2110 (fs_info->sb->s_bdev == device->bdev))
2111 fs_info->sb->s_bdev = next_device->bdev;
2112
2113 if (fs_info->fs_devices->latest_bdev == device->bdev)
2114 fs_info->fs_devices->latest_bdev = next_device->bdev;
2115}
2116
Anand Jain1da73962018-08-10 13:53:21 +08002117/*
2118 * Return btrfs_fs_devices::num_devices excluding the device that's being
2119 * currently replaced.
2120 */
2121static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info)
2122{
2123 u64 num_devices = fs_info->fs_devices->num_devices;
2124
David Sterbacb5583d2018-09-07 16:11:23 +02002125 down_read(&fs_info->dev_replace.rwsem);
Anand Jain1da73962018-08-10 13:53:21 +08002126 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
2127 ASSERT(num_devices > 1);
2128 num_devices--;
2129 }
David Sterbacb5583d2018-09-07 16:11:23 +02002130 up_read(&fs_info->dev_replace.rwsem);
Anand Jain1da73962018-08-10 13:53:21 +08002131
2132 return num_devices;
2133}
2134
David Sterbada353f62017-02-14 17:55:53 +01002135int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
2136 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08002137{
2138 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002139 struct btrfs_fs_devices *cur_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002140 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002141 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002142 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002143
2144 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002145
Anand Jain1da73962018-08-10 13:53:21 +08002146 num_devices = btrfs_num_devices(fs_info);
Chris Masona061fc82008-05-07 11:43:44 -04002147
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002148 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08002149 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04002150 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002151
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002152 device = btrfs_find_device_by_devspec(fs_info, devid, device_path);
2153
2154 if (IS_ERR(device)) {
2155 if (PTR_ERR(device) == -ENOENT &&
2156 strcmp(device_path, "missing") == 0)
2157 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
2158 else
2159 ret = PTR_ERR(device);
Chris Masona061fc82008-05-07 11:43:44 -04002160 goto out;
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002161 }
Yan Zheng2b820322008-11-17 21:11:30 -05002162
Omar Sandovaleede2bf2016-11-03 10:28:12 -07002163 if (btrfs_pinned_by_swapfile(fs_info, device)) {
2164 btrfs_warn_in_rcu(fs_info,
2165 "cannot remove device %s (devid %llu) due to active swapfile",
2166 rcu_str_deref(device->name), device->devid);
2167 ret = -ETXTBSY;
2168 goto out;
2169 }
2170
Anand Jain401e29c2017-12-04 12:54:55 +08002171 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Anand Jain183860f2013-05-17 10:52:45 +00002172 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08002173 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002174 }
2175
Anand Jainebbede42017-12-04 12:54:52 +08002176 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
2177 fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00002178 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08002179 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05002180 }
2181
Anand Jainebbede42017-12-04 12:54:52 +08002182 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002183 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002184 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002185 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02002186 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002187 }
Chris Masona061fc82008-05-07 11:43:44 -04002188
Carey Underwoodd7901552013-03-04 16:37:06 -06002189 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002190 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06002191 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002192 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002193 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002194
Stefan Behrens63a212a2012-11-05 18:29:28 +01002195 /*
2196 * TODO: the superblock still includes this device in its num_devices
2197 * counter although write_all_supers() is not locked out. This
2198 * could give a filesystem state which requires a degraded mount.
2199 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002200 ret = btrfs_rm_dev_item(fs_info, device);
Chris Masona061fc82008-05-07 11:43:44 -04002201 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002202 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002203
Anand Jaine12c9622017-12-04 12:54:53 +08002204 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002205 btrfs_scrub_cancel_dev(fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04002206
2207 /*
2208 * the device list mutex makes sure that we don't change
2209 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01002210 * the device supers. Whoever is writing all supers, should
2211 * lock the device list mutex before getting the number of
2212 * devices in the super block (super_copy). Conversely,
2213 * whoever updates the number of devices in the super block
2214 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04002215 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002216
Anand Jain41a52a02018-04-12 10:29:31 +08002217 /*
2218 * In normal cases the cur_devices == fs_devices. But in case
2219 * of deleting a seed device, the cur_devices should point to
2220 * its own fs_devices listed under the fs_devices->seed.
2221 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002222 cur_devices = device->fs_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002223 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002224 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002225
Anand Jain41a52a02018-04-12 10:29:31 +08002226 cur_devices->num_devices--;
2227 cur_devices->total_devices--;
Anand Jainb4993e62018-07-03 17:07:23 +08002228 /* Update total_devices of the parent fs_devices if it's seed */
2229 if (cur_devices != fs_devices)
2230 fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05002231
Anand Jaine6e674b2017-12-04 12:54:54 +08002232 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jain41a52a02018-04-12 10:29:31 +08002233 cur_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05002234
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002235 btrfs_assign_next_active_device(device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05002236
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002237 if (device->bdev) {
Anand Jain41a52a02018-04-12 10:29:31 +08002238 cur_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002239 /* remove sysfs entry */
Anand Jainb5185192018-04-12 10:29:30 +08002240 btrfs_sysfs_rm_device_link(fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002241 }
Anand Jain99994cd2014-06-03 11:36:00 +08002242
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002243 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2244 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
Anand Jainb5185192018-04-12 10:29:30 +08002245 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002246
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002247 /*
2248 * at this point, the device is zero sized and detached from
2249 * the devices list. All that's left is to zero out the old
2250 * supers and free the device.
2251 */
Anand Jainebbede42017-12-04 12:54:52 +08002252 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002253 btrfs_scratch_superblocks(device->bdev, device->name->str);
2254
2255 btrfs_close_bdev(device);
David Sterbaf06c5962017-06-06 17:08:23 +02002256 call_rcu(&device->rcu, free_device_rcu);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002257
Xiao Guangrong1f781602011-04-20 10:09:16 +00002258 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002259 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002260 if (fs_devices->seed == cur_devices) {
2261 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05002262 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002263 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002264 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05002265 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00002266 cur_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002267 close_fs_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002268 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002269 }
2270
Chris Masona061fc82008-05-07 11:43:44 -04002271out:
2272 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002273 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08002274
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002275error_undo:
Anand Jainebbede42017-12-04 12:54:52 +08002276 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002277 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002278 list_add(&device->dev_alloc_list,
Anand Jainb5185192018-04-12 10:29:30 +08002279 &fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002280 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002281 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002282 }
Anand Jain24fc5722016-02-13 10:01:36 +08002283 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002284}
2285
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002286void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002287{
Anand Jaind51908c2014-08-13 14:24:19 +08002288 struct btrfs_fs_devices *fs_devices;
2289
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002290 lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex);
Ilya Dryomov13572722013-10-02 20:41:01 +03002291
Anand Jain25e8e912014-08-20 10:56:56 +08002292 /*
2293 * in case of fs with no seed, srcdev->fs_devices will point
2294 * to fs_devices of fs_info. However when the dev being replaced is
2295 * a seed dev it will point to the seed's local fs_devices. In short
2296 * srcdev will have its correct fs_devices in both the cases.
2297 */
2298 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002299
Stefan Behrense93c89c2012-11-05 17:33:06 +01002300 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002301 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002302 fs_devices->num_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08002303 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
Anand Jaind51908c2014-08-13 14:24:19 +08002304 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002305
Anand Jainebbede42017-12-04 12:54:52 +08002306 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
Miao Xie82372bc2014-09-03 21:35:44 +08002307 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002308
Miao Xie82372bc2014-09-03 21:35:44 +08002309 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002310 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002311}
2312
2313void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2314 struct btrfs_device *srcdev)
2315{
2316 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002317
Anand Jainebbede42017-12-04 12:54:52 +08002318 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
Anand Jain48b3b9d2016-04-12 21:36:16 +08002319 /* zero out the old super if it is writable */
2320 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2321 }
Anand Jain14238812016-07-22 06:04:53 +08002322
2323 btrfs_close_bdev(srcdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002324 call_rcu(&srcdev->rcu, free_device_rcu);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002325
Anand Jain94d5f0c2014-08-13 14:24:22 +08002326 /* if this is no devs we rather delete the fs_devices */
2327 if (!fs_devices->num_devices) {
2328 struct btrfs_fs_devices *tmp_fs_devices;
2329
Anand Jain6dd38f82017-10-17 06:53:50 +08002330 /*
2331 * On a mounted FS, num_devices can't be zero unless it's a
2332 * seed. In case of a seed device being replaced, the replace
2333 * target added to the sprout FS, so there will be no more
2334 * device left under the seed FS.
2335 */
2336 ASSERT(fs_devices->seeding);
2337
Anand Jain94d5f0c2014-08-13 14:24:22 +08002338 tmp_fs_devices = fs_info->fs_devices;
2339 while (tmp_fs_devices) {
2340 if (tmp_fs_devices->seed == fs_devices) {
2341 tmp_fs_devices->seed = fs_devices->seed;
2342 break;
2343 }
2344 tmp_fs_devices = tmp_fs_devices->seed;
2345 }
2346 fs_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002347 close_fs_devices(fs_devices);
Anand Jain8bef8402014-08-13 14:24:23 +08002348 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002349 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002350}
2351
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002352void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002353{
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002354 struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices;
Anand Jaind2ff1b22015-03-10 06:38:42 +08002355
Anand Jaind9a071f2018-04-12 10:29:38 +08002356 WARN_ON(!tgtdev);
2357 mutex_lock(&fs_devices->device_list_mutex);
2358
2359 btrfs_sysfs_rm_device_link(fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002360
Anand Jain779bf3fe2016-04-18 16:51:23 +08002361 if (tgtdev->bdev)
Anand Jaind9a071f2018-04-12 10:29:38 +08002362 fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002363
Anand Jaind9a071f2018-04-12 10:29:38 +08002364 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002365
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002366 btrfs_assign_next_active_device(tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002367
Stefan Behrense93c89c2012-11-05 17:33:06 +01002368 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002369
Anand Jaind9a071f2018-04-12 10:29:38 +08002370 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002371
2372 /*
2373 * The update_dev_time() with in btrfs_scratch_superblocks()
2374 * may lead to a call to btrfs_show_devname() which will try
2375 * to hold device_list_mutex. And here this device
2376 * is already out of device list, so we don't have to hold
2377 * the device_list_mutex lock.
2378 */
2379 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002380
2381 btrfs_close_bdev(tgtdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002382 call_rcu(&tgtdev->rcu, free_device_rcu);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002383}
2384
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002385static struct btrfs_device *btrfs_find_device_by_path(
2386 struct btrfs_fs_info *fs_info, const char *device_path)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002387{
2388 int ret = 0;
2389 struct btrfs_super_block *disk_super;
2390 u64 devid;
2391 u8 *dev_uuid;
2392 struct block_device *bdev;
2393 struct buffer_head *bh;
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002394 struct btrfs_device *device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002395
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002396 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002397 fs_info->bdev_holder, 0, &bdev, &bh);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002398 if (ret)
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002399 return ERR_PTR(ret);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002400 disk_super = (struct btrfs_super_block *)bh->b_data;
2401 devid = btrfs_stack_device_id(&disk_super->dev_item);
2402 dev_uuid = disk_super->dev_item.uuid;
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002403 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
Anand Jaine4319cd2019-01-17 23:32:31 +08002404 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2405 disk_super->metadata_uuid);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002406 else
Anand Jaine4319cd2019-01-17 23:32:31 +08002407 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2408 disk_super->fsid);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002409
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002410 brelse(bh);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002411 if (!device)
2412 device = ERR_PTR(-ENOENT);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002413 blkdev_put(bdev, FMODE_READ);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002414 return device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002415}
2416
Yan Zheng2b820322008-11-17 21:11:30 -05002417/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002418 * Lookup a device given by device id, or the path if the id is 0.
2419 */
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002420struct btrfs_device *btrfs_find_device_by_devspec(
Anand Jain6e927ce2019-01-17 23:32:29 +08002421 struct btrfs_fs_info *fs_info, u64 devid,
2422 const char *device_path)
Anand Jain24e04742016-02-13 10:01:35 +08002423{
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002424 struct btrfs_device *device;
Anand Jain24e04742016-02-13 10:01:35 +08002425
David Sterba5c5c0df2016-02-15 16:39:55 +01002426 if (devid) {
Anand Jaine4319cd2019-01-17 23:32:31 +08002427 device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
2428 NULL);
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002429 if (!device)
2430 return ERR_PTR(-ENOENT);
Anand Jain6e927ce2019-01-17 23:32:29 +08002431 return device;
Anand Jain24e04742016-02-13 10:01:35 +08002432 }
Anand Jain6e927ce2019-01-17 23:32:29 +08002433
2434 if (!device_path || !device_path[0])
2435 return ERR_PTR(-EINVAL);
2436
2437 if (strcmp(device_path, "missing") == 0) {
2438 /* Find first missing device */
2439 list_for_each_entry(device, &fs_info->fs_devices->devices,
2440 dev_list) {
2441 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2442 &device->dev_state) && !device->bdev)
2443 return device;
2444 }
2445 return ERR_PTR(-ENOENT);
2446 }
2447
2448 return btrfs_find_device_by_path(fs_info, device_path);
Anand Jain24e04742016-02-13 10:01:35 +08002449}
2450
Yan Zheng2b820322008-11-17 21:11:30 -05002451/*
2452 * does all the dirty work required for changing file system's UUID.
2453 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002454static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002455{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002456 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002457 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002458 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002459 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002460 struct btrfs_device *device;
2461 u64 super_flags;
2462
David Sterbaa32bf9a2018-03-16 02:21:22 +01002463 lockdep_assert_held(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002464 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002465 return -EINVAL;
2466
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002467 seed_devices = alloc_fs_devices(NULL, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002468 if (IS_ERR(seed_devices))
2469 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002470
Yan Zhenge4404d62008-12-12 10:03:26 -05002471 old_devices = clone_fs_devices(fs_devices);
2472 if (IS_ERR(old_devices)) {
2473 kfree(seed_devices);
2474 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002475 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002476
Anand Jainc4babc52018-04-12 10:29:25 +08002477 list_add(&old_devices->fs_list, &fs_uuids);
Yan Zheng2b820322008-11-17 21:11:30 -05002478
Yan Zhenge4404d62008-12-12 10:03:26 -05002479 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2480 seed_devices->opened = 1;
2481 INIT_LIST_HEAD(&seed_devices->devices);
2482 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002483 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002484
Anand Jain321a4bf2018-07-16 22:58:09 +08002485 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002486 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2487 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002488 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002489 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002490
David Sterba34441362016-10-04 19:34:27 +02002491 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002492 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002493 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002494
Yan Zheng2b820322008-11-17 21:11:30 -05002495 fs_devices->seeding = 0;
2496 fs_devices->num_devices = 0;
2497 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002498 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002499 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002500 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002501
2502 generate_random_uuid(fs_devices->fsid);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002503 memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002504 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Anand Jain321a4bf2018-07-16 22:58:09 +08002505 mutex_unlock(&fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002506
Yan Zheng2b820322008-11-17 21:11:30 -05002507 super_flags = btrfs_super_flags(disk_super) &
2508 ~BTRFS_SUPER_FLAG_SEEDING;
2509 btrfs_set_super_flags(disk_super, super_flags);
2510
2511 return 0;
2512}
2513
2514/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002515 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002516 */
2517static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002518 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002519{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002520 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002521 struct btrfs_path *path;
2522 struct extent_buffer *leaf;
2523 struct btrfs_dev_item *dev_item;
2524 struct btrfs_device *device;
2525 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002526 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002527 u8 dev_uuid[BTRFS_UUID_SIZE];
2528 u64 devid;
2529 int ret;
2530
2531 path = btrfs_alloc_path();
2532 if (!path)
2533 return -ENOMEM;
2534
Yan Zheng2b820322008-11-17 21:11:30 -05002535 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2536 key.offset = 0;
2537 key.type = BTRFS_DEV_ITEM_KEY;
2538
2539 while (1) {
2540 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2541 if (ret < 0)
2542 goto error;
2543
2544 leaf = path->nodes[0];
2545next_slot:
2546 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2547 ret = btrfs_next_leaf(root, path);
2548 if (ret > 0)
2549 break;
2550 if (ret < 0)
2551 goto error;
2552 leaf = path->nodes[0];
2553 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002554 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002555 continue;
2556 }
2557
2558 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2559 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2560 key.type != BTRFS_DEV_ITEM_KEY)
2561 break;
2562
2563 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2564 struct btrfs_dev_item);
2565 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002566 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002567 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002568 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002569 BTRFS_FSID_SIZE);
Anand Jaine4319cd2019-01-17 23:32:31 +08002570 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2571 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002572 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002573
2574 if (device->fs_devices->seeding) {
2575 btrfs_set_device_generation(leaf, dev_item,
2576 device->generation);
2577 btrfs_mark_buffer_dirty(leaf);
2578 }
2579
2580 path->slots[0]++;
2581 goto next_slot;
2582 }
2583 ret = 0;
2584error:
2585 btrfs_free_path(path);
2586 return ret;
2587}
2588
David Sterbada353f62017-02-14 17:55:53 +01002589int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002590{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002591 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002592 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002593 struct btrfs_trans_handle *trans;
2594 struct btrfs_device *device;
2595 struct block_device *bdev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002596 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002597 struct rcu_string *name;
Anand Jain5da54bc2018-07-03 13:14:50 +08002598 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Naohiro Aota39379fa2018-07-27 09:04:55 +09002599 u64 orig_super_total_bytes;
2600 u64 orig_super_num_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002601 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002602 int ret = 0;
Anand Jain7132a262017-09-28 14:51:11 +08002603 bool unlocked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002604
Anand Jain5da54bc2018-07-03 13:14:50 +08002605 if (sb_rdonly(sb) && !fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002606 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002607
Li Zefana5d163332011-12-07 20:08:40 -05002608 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002609 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002610 if (IS_ERR(bdev))
2611 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002612
Anand Jain5da54bc2018-07-03 13:14:50 +08002613 if (fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002614 seeding_dev = 1;
2615 down_write(&sb->s_umount);
2616 mutex_lock(&uuid_mutex);
2617 }
2618
Chris Mason8c8bee12008-09-29 11:19:10 -04002619 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002620
Anand Jain5da54bc2018-07-03 13:14:50 +08002621 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain694c51f2018-07-03 13:14:51 +08002622 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002623 if (device->bdev == bdev) {
2624 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002625 mutex_unlock(
Anand Jain5da54bc2018-07-03 13:14:50 +08002626 &fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002627 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002628 }
2629 }
Anand Jain5da54bc2018-07-03 13:14:50 +08002630 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002631
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002632 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002633 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002634 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002635 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002636 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002637 }
2638
David Sterba78f2c9e2016-02-11 14:25:38 +01002639 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002640 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002641 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002642 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002643 }
Josef Bacik606686e2012-06-04 14:03:51 -04002644 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002645
Yan, Zhenga22285a2010-05-16 10:48:46 -04002646 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002647 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002648 ret = PTR_ERR(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002649 goto error_free_device;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002650 }
2651
Josef Bacikd5e20032011-08-04 14:52:27 +00002652 q = bdev_get_queue(bdev);
Anand Jainebbede42017-12-04 12:54:52 +08002653 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05002654 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002655 device->io_width = fs_info->sectorsize;
2656 device->io_align = fs_info->sectorsize;
2657 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002658 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2659 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002660 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002661 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002662 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002663 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002664 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002665 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002666 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002667 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002668 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002669
Yan Zheng2b820322008-11-17 21:11:30 -05002670 if (seeding_dev) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002671 sb->s_flags &= ~SB_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002672 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002673 if (ret) {
2674 btrfs_abort_transaction(trans, ret);
2675 goto error_trans;
2676 }
Yan Zheng2b820322008-11-17 21:11:30 -05002677 }
2678
Anand Jain5da54bc2018-07-03 13:14:50 +08002679 device->fs_devices = fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002680
Anand Jain5da54bc2018-07-03 13:14:50 +08002681 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002682 mutex_lock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002683 list_add_rcu(&device->dev_list, &fs_devices->devices);
2684 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
2685 fs_devices->num_devices++;
2686 fs_devices->open_devices++;
2687 fs_devices->rw_devices++;
2688 fs_devices->total_devices++;
2689 fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002690
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002691 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002692
Anand Jaine884f4f2017-04-04 18:40:19 +08002693 if (!blk_queue_nonrot(q))
Anand Jain5da54bc2018-07-03 13:14:50 +08002694 fs_devices->rotating = 1;
Chris Masonc2898112009-06-10 09:51:32 -04002695
Naohiro Aota39379fa2018-07-27 09:04:55 +09002696 orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002697 btrfs_set_super_total_bytes(fs_info->super_copy,
Naohiro Aota39379fa2018-07-27 09:04:55 +09002698 round_down(orig_super_total_bytes + device->total_bytes,
2699 fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002700
Naohiro Aota39379fa2018-07-27 09:04:55 +09002701 orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy);
2702 btrfs_set_super_num_devices(fs_info->super_copy,
2703 orig_super_num_devices + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002704
2705 /* add sysfs device entry */
Anand Jain5da54bc2018-07-03 13:14:50 +08002706 btrfs_sysfs_add_device_link(fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002707
Miao Xie2196d6e2014-09-03 21:35:41 +08002708 /*
2709 * we've got more storage, clear any full flags on the space
2710 * infos
2711 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002712 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002713
David Sterba34441362016-10-04 19:34:27 +02002714 mutex_unlock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002715 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002716
Yan Zheng2b820322008-11-17 21:11:30 -05002717 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002718 mutex_lock(&fs_info->chunk_mutex);
David Sterbae4a4dce2017-02-10 19:49:01 +01002719 ret = init_first_rw_device(trans, fs_info);
David Sterba34441362016-10-04 19:34:27 +02002720 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002721 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002722 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002723 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002724 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002725 }
2726
Nikolay Borisov8e87e852018-07-20 19:37:47 +03002727 ret = btrfs_add_dev_item(trans, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002728 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002729 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002730 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002731 }
2732
2733 if (seeding_dev) {
2734 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2735
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002736 ret = btrfs_finish_sprout(trans, fs_info);
David Sterba005d6422012-09-18 07:52:32 -06002737 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002738 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002739 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002740 }
Anand Jainb2373f22014-06-03 11:36:03 +08002741
2742 /* Sprouting would change fsid of the mounted root,
2743 * so rename the fsid on the sysfs
2744 */
2745 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
Nikolay Borisovde37aa52018-10-30 16:43:24 +02002746 fs_info->fs_devices->fsid);
Anand Jain5da54bc2018-07-03 13:14:50 +08002747 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002748 btrfs_warn(fs_info,
2749 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002750 }
2751
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002752 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002753
2754 if (seeding_dev) {
2755 mutex_unlock(&uuid_mutex);
2756 up_write(&sb->s_umount);
Anand Jain7132a262017-09-28 14:51:11 +08002757 unlocked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002758
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002759 if (ret) /* transaction commit */
2760 return ret;
2761
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002762 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002763 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002764 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002765 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002766 trans = btrfs_attach_transaction(root);
2767 if (IS_ERR(trans)) {
2768 if (PTR_ERR(trans) == -ENOENT)
2769 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002770 ret = PTR_ERR(trans);
2771 trans = NULL;
2772 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002773 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002774 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002775 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002776
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002777 /* Update ctime/mtime for libblkid */
2778 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002779 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002780
Anand Jaind31c32f2017-09-28 14:51:10 +08002781error_sysfs:
Anand Jain5da54bc2018-07-03 13:14:50 +08002782 btrfs_sysfs_rm_device_link(fs_devices, device);
Naohiro Aota39379fa2018-07-27 09:04:55 +09002783 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2784 mutex_lock(&fs_info->chunk_mutex);
2785 list_del_rcu(&device->dev_list);
2786 list_del(&device->dev_alloc_list);
2787 fs_info->fs_devices->num_devices--;
2788 fs_info->fs_devices->open_devices--;
2789 fs_info->fs_devices->rw_devices--;
2790 fs_info->fs_devices->total_devices--;
2791 fs_info->fs_devices->total_rw_bytes -= device->total_bytes;
2792 atomic64_sub(device->total_bytes, &fs_info->free_chunk_space);
2793 btrfs_set_super_total_bytes(fs_info->super_copy,
2794 orig_super_total_bytes);
2795 btrfs_set_super_num_devices(fs_info->super_copy,
2796 orig_super_num_devices);
2797 mutex_unlock(&fs_info->chunk_mutex);
2798 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002799error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002800 if (seeding_dev)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002801 sb->s_flags |= SB_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002802 if (trans)
2803 btrfs_end_transaction(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002804error_free_device:
David Sterbaa425f9d2018-03-20 15:47:33 +01002805 btrfs_free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002806error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002807 blkdev_put(bdev, FMODE_EXCL);
Anand Jain7132a262017-09-28 14:51:11 +08002808 if (seeding_dev && !unlocked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002809 mutex_unlock(&uuid_mutex);
2810 up_write(&sb->s_umount);
2811 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002812 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002813}
2814
Chris Masond3977122009-01-05 21:25:51 -05002815static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2816 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002817{
2818 int ret;
2819 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002820 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002821 struct btrfs_dev_item *dev_item;
2822 struct extent_buffer *leaf;
2823 struct btrfs_key key;
2824
Chris Mason0b86a832008-03-24 15:01:56 -04002825 path = btrfs_alloc_path();
2826 if (!path)
2827 return -ENOMEM;
2828
2829 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2830 key.type = BTRFS_DEV_ITEM_KEY;
2831 key.offset = device->devid;
2832
2833 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2834 if (ret < 0)
2835 goto out;
2836
2837 if (ret > 0) {
2838 ret = -ENOENT;
2839 goto out;
2840 }
2841
2842 leaf = path->nodes[0];
2843 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2844
2845 btrfs_set_device_id(leaf, dev_item, device->devid);
2846 btrfs_set_device_type(leaf, dev_item, device->type);
2847 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2848 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2849 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002850 btrfs_set_device_total_bytes(leaf, dev_item,
2851 btrfs_device_get_disk_total_bytes(device));
2852 btrfs_set_device_bytes_used(leaf, dev_item,
2853 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002854 btrfs_mark_buffer_dirty(leaf);
2855
2856out:
2857 btrfs_free_path(path);
2858 return ret;
2859}
2860
Miao Xie2196d6e2014-09-03 21:35:41 +08002861int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002862 struct btrfs_device *device, u64 new_size)
2863{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002864 struct btrfs_fs_info *fs_info = device->fs_info;
2865 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002866 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002867 u64 old_total;
2868 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002869
Anand Jainebbede42017-12-04 12:54:52 +08002870 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Yan Zheng2b820322008-11-17 21:11:30 -05002871 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002872
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002873 new_size = round_down(new_size, fs_info->sectorsize);
2874
David Sterba34441362016-10-04 19:34:27 +02002875 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002876 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002877 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002878
Stefan Behrens63a212a2012-11-05 18:29:28 +01002879 if (new_size <= device->total_bytes ||
Anand Jain401e29c2017-12-04 12:54:55 +08002880 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002881 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002882 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002883 }
Yan Zheng2b820322008-11-17 21:11:30 -05002884
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002885 fs_devices = fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002886
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002887 btrfs_set_super_total_bytes(super_copy,
2888 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002889 device->fs_devices->total_rw_bytes += diff;
2890
Miao Xie7cc8e582014-09-03 21:35:38 +08002891 btrfs_device_set_total_bytes(device, new_size);
2892 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002893 btrfs_clear_space_info_full(device->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002894 if (list_empty(&device->resized_list))
2895 list_add_tail(&device->resized_list,
2896 &fs_devices->resized_devices);
David Sterba34441362016-10-04 19:34:27 +02002897 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002898
Chris Mason8f18cf12008-04-25 16:53:30 -04002899 return btrfs_update_device(trans, device);
2900}
2901
Nikolay Borisovf4208792018-07-20 19:37:52 +03002902static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002903{
Nikolay Borisovf4208792018-07-20 19:37:52 +03002904 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002905 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002906 int ret;
2907 struct btrfs_path *path;
2908 struct btrfs_key key;
2909
Chris Mason8f18cf12008-04-25 16:53:30 -04002910 path = btrfs_alloc_path();
2911 if (!path)
2912 return -ENOMEM;
2913
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002914 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002915 key.offset = chunk_offset;
2916 key.type = BTRFS_CHUNK_ITEM_KEY;
2917
2918 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002919 if (ret < 0)
2920 goto out;
2921 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002922 btrfs_handle_fs_error(fs_info, -ENOENT,
2923 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002924 ret = -ENOENT;
2925 goto out;
2926 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002927
2928 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002929 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002930 btrfs_handle_fs_error(fs_info, ret,
2931 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002932out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002933 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002934 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002935}
2936
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002937static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002938{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002939 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002940 struct btrfs_disk_key *disk_key;
2941 struct btrfs_chunk *chunk;
2942 u8 *ptr;
2943 int ret = 0;
2944 u32 num_stripes;
2945 u32 array_size;
2946 u32 len = 0;
2947 u32 cur;
2948 struct btrfs_key key;
2949
David Sterba34441362016-10-04 19:34:27 +02002950 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002951 array_size = btrfs_super_sys_array_size(super_copy);
2952
2953 ptr = super_copy->sys_chunk_array;
2954 cur = 0;
2955
2956 while (cur < array_size) {
2957 disk_key = (struct btrfs_disk_key *)ptr;
2958 btrfs_disk_key_to_cpu(&key, disk_key);
2959
2960 len = sizeof(*disk_key);
2961
2962 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2963 chunk = (struct btrfs_chunk *)(ptr + len);
2964 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2965 len += btrfs_chunk_item_size(num_stripes);
2966 } else {
2967 ret = -EIO;
2968 break;
2969 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002970 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002971 key.offset == chunk_offset) {
2972 memmove(ptr, ptr + len, array_size - (cur + len));
2973 array_size -= len;
2974 btrfs_set_super_sys_array_size(super_copy, array_size);
2975 } else {
2976 ptr += len;
2977 cur += len;
2978 }
2979 }
David Sterba34441362016-10-04 19:34:27 +02002980 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002981 return ret;
2982}
2983
Omar Sandoval60ca8422018-05-16 16:34:31 -07002984/*
2985 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2986 * @logical: Logical block offset in bytes.
2987 * @length: Length of extent in bytes.
2988 *
2989 * Return: Chunk mapping or ERR_PTR.
2990 */
2991struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
2992 u64 logical, u64 length)
Liu Bo592d92e2017-03-14 13:33:55 -07002993{
2994 struct extent_map_tree *em_tree;
2995 struct extent_map *em;
2996
2997 em_tree = &fs_info->mapping_tree.map_tree;
2998 read_lock(&em_tree->lock);
2999 em = lookup_extent_mapping(em_tree, logical, length);
3000 read_unlock(&em_tree->lock);
3001
3002 if (!em) {
3003 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
3004 logical, length);
3005 return ERR_PTR(-EINVAL);
3006 }
3007
3008 if (em->start > logical || em->start + em->len < logical) {
3009 btrfs_crit(fs_info,
3010 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
3011 logical, length, em->start, em->start + em->len);
3012 free_extent_map(em);
3013 return ERR_PTR(-EINVAL);
3014 }
3015
3016 /* callers are responsible for dropping em's ref. */
3017 return em;
3018}
3019
Nikolay Borisov97aff912018-07-20 19:37:53 +03003020int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04003021{
Nikolay Borisov97aff912018-07-20 19:37:53 +03003022 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003023 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003024 struct map_lookup *map;
3025 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003026 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003027 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003028
Omar Sandoval60ca8422018-05-16 16:34:31 -07003029 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07003030 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04003031 /*
3032 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08003033 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04003034 * do anything we still error out.
3035 */
3036 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07003037 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003038 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04003039 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02003040 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisov451a2c12018-06-20 15:49:07 +03003041 check_system_chunk(trans, map->type);
David Sterba34441362016-10-04 19:34:27 +02003042 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003043
Filipe Manana57ba4cb2016-05-20 04:34:23 +01003044 /*
3045 * Take the device list mutex to prevent races with the final phase of
3046 * a device replace operation that replaces the device object associated
3047 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
3048 */
3049 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003050 for (i = 0; i < map->num_stripes; i++) {
3051 struct btrfs_device *device = map->stripes[i].dev;
3052 ret = btrfs_free_dev_extent(trans, device,
3053 map->stripes[i].physical,
3054 &dev_extent_len);
3055 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01003056 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04003057 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003058 goto out;
3059 }
3060
3061 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02003062 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003063 btrfs_device_set_bytes_used(device,
3064 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03003065 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003066 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02003067 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003068 }
3069
Nikolay Borisov64bc6c22018-10-26 14:43:19 +03003070 ret = btrfs_update_device(trans, device);
3071 if (ret) {
3072 mutex_unlock(&fs_devices->device_list_mutex);
3073 btrfs_abort_transaction(trans, ret);
3074 goto out;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003075 }
3076 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01003077 mutex_unlock(&fs_devices->device_list_mutex);
3078
Nikolay Borisovf4208792018-07-20 19:37:52 +03003079 ret = btrfs_free_chunk(trans, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003080 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003081 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003082 goto out;
3083 }
3084
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003085 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003086
3087 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03003088 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003089 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003090 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003091 goto out;
3092 }
3093 }
3094
Nikolay Borisov5a98ec02018-06-20 15:48:56 +03003095 ret = btrfs_remove_block_group(trans, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003096 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003097 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003098 goto out;
3099 }
3100
Josef Bacik47ab2a62014-09-18 11:20:02 -04003101out:
3102 /* once for us */
3103 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04003104 return ret;
3105}
3106
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003107static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04003108{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003109 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07003110 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04003111 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003112
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003113 /*
3114 * Prevent races with automatic removal of unused block groups.
3115 * After we relocate and before we remove the chunk with offset
3116 * chunk_offset, automatic removal of the block group can kick in,
3117 * resulting in a failure when calling btrfs_remove_chunk() below.
3118 *
3119 * Make sure to acquire this mutex before doing a tree search (dev
3120 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
3121 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
3122 * we release the path used to search the chunk/dev tree and before
3123 * the current task acquires this mutex and calls us.
3124 */
David Sterbaa32bf9a2018-03-16 02:21:22 +01003125 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003126
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003127 ret = btrfs_can_relocate(fs_info, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003128 if (ret)
3129 return -ENOSPC;
3130
Chris Mason8f18cf12008-04-25 16:53:30 -04003131 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003132 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003133 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003134 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003135 if (ret)
3136 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003137
Jeff Mahoney75cb3792018-03-20 15:25:26 -04003138 /*
3139 * We add the kobjects here (and after forcing data chunk creation)
3140 * since relocation is the only place we'll create chunks of a new
3141 * type at runtime. The only place where we'll remove the last
3142 * chunk of a type is the call immediately below this one. Even
3143 * so, we're protected against races with the cleaner thread since
3144 * we're covered by the delete_unused_bgs_mutex.
3145 */
3146 btrfs_add_raid_kobjects(fs_info);
3147
Chris Mason19c4d2f2016-10-10 13:43:31 -07003148 trans = btrfs_start_trans_remove_block_group(root->fs_info,
3149 chunk_offset);
3150 if (IS_ERR(trans)) {
3151 ret = PTR_ERR(trans);
3152 btrfs_handle_fs_error(root->fs_info, ret, NULL);
3153 return ret;
3154 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09003155
Chris Mason19c4d2f2016-10-10 13:43:31 -07003156 /*
3157 * step two, delete the device extents and the
3158 * chunk tree entries
3159 */
Nikolay Borisov97aff912018-07-20 19:37:53 +03003160 ret = btrfs_remove_chunk(trans, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003161 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07003162 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003163}
3164
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003165static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05003166{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003167 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05003168 struct btrfs_path *path;
3169 struct extent_buffer *leaf;
3170 struct btrfs_chunk *chunk;
3171 struct btrfs_key key;
3172 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05003173 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04003174 bool retried = false;
3175 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05003176 int ret;
3177
3178 path = btrfs_alloc_path();
3179 if (!path)
3180 return -ENOMEM;
3181
Josef Bacikba1bf482009-09-11 16:11:19 -04003182again:
Yan Zheng2b820322008-11-17 21:11:30 -05003183 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3184 key.offset = (u64)-1;
3185 key.type = BTRFS_CHUNK_ITEM_KEY;
3186
3187 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003188 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003189 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003190 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003191 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003192 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003193 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003194 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05003195
3196 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3197 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003198 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003199 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003200 if (ret < 0)
3201 goto error;
3202 if (ret > 0)
3203 break;
3204
3205 leaf = path->nodes[0];
3206 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3207
3208 chunk = btrfs_item_ptr(leaf, path->slots[0],
3209 struct btrfs_chunk);
3210 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003211 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003212
3213 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003214 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003215 if (ret == -ENOSPC)
3216 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303217 else
3218 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003219 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003220 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003221
3222 if (found_key.offset == 0)
3223 break;
3224 key.offset = found_key.offset - 1;
3225 }
3226 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003227 if (failed && !retried) {
3228 failed = 0;
3229 retried = true;
3230 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303231 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003232 ret = -ENOSPC;
3233 }
Yan Zheng2b820322008-11-17 21:11:30 -05003234error:
3235 btrfs_free_path(path);
3236 return ret;
3237}
3238
Liu Boa6f93c72017-11-15 16:28:11 -07003239/*
3240 * return 1 : allocate a data chunk successfully,
3241 * return <0: errors during allocating a data chunk,
3242 * return 0 : no need to allocate a data chunk.
3243 */
3244static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3245 u64 chunk_offset)
3246{
3247 struct btrfs_block_group_cache *cache;
3248 u64 bytes_used;
3249 u64 chunk_type;
3250
3251 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3252 ASSERT(cache);
3253 chunk_type = cache->flags;
3254 btrfs_put_block_group(cache);
3255
3256 if (chunk_type & BTRFS_BLOCK_GROUP_DATA) {
3257 spin_lock(&fs_info->data_sinfo->lock);
3258 bytes_used = fs_info->data_sinfo->bytes_used;
3259 spin_unlock(&fs_info->data_sinfo->lock);
3260
3261 if (!bytes_used) {
3262 struct btrfs_trans_handle *trans;
3263 int ret;
3264
3265 trans = btrfs_join_transaction(fs_info->tree_root);
3266 if (IS_ERR(trans))
3267 return PTR_ERR(trans);
3268
Nikolay Borisov43a7e992018-06-20 15:49:15 +03003269 ret = btrfs_force_chunk_alloc(trans,
Liu Boa6f93c72017-11-15 16:28:11 -07003270 BTRFS_BLOCK_GROUP_DATA);
3271 btrfs_end_transaction(trans);
3272 if (ret < 0)
3273 return ret;
3274
Jeff Mahoney75cb3792018-03-20 15:25:26 -04003275 btrfs_add_raid_kobjects(fs_info);
3276
Liu Boa6f93c72017-11-15 16:28:11 -07003277 return 1;
3278 }
3279 }
3280 return 0;
3281}
3282
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003283static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003284 struct btrfs_balance_control *bctl)
3285{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003286 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003287 struct btrfs_trans_handle *trans;
3288 struct btrfs_balance_item *item;
3289 struct btrfs_disk_balance_args disk_bargs;
3290 struct btrfs_path *path;
3291 struct extent_buffer *leaf;
3292 struct btrfs_key key;
3293 int ret, err;
3294
3295 path = btrfs_alloc_path();
3296 if (!path)
3297 return -ENOMEM;
3298
3299 trans = btrfs_start_transaction(root, 0);
3300 if (IS_ERR(trans)) {
3301 btrfs_free_path(path);
3302 return PTR_ERR(trans);
3303 }
3304
3305 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003306 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003307 key.offset = 0;
3308
3309 ret = btrfs_insert_empty_item(trans, root, path, &key,
3310 sizeof(*item));
3311 if (ret)
3312 goto out;
3313
3314 leaf = path->nodes[0];
3315 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3316
David Sterbab159fa22016-11-08 18:09:03 +01003317 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003318
3319 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3320 btrfs_set_balance_data(leaf, item, &disk_bargs);
3321 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3322 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3323 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3324 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3325
3326 btrfs_set_balance_flags(leaf, item, bctl->flags);
3327
3328 btrfs_mark_buffer_dirty(leaf);
3329out:
3330 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003331 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003332 if (err && !ret)
3333 ret = err;
3334 return ret;
3335}
3336
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003337static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003338{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003339 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003340 struct btrfs_trans_handle *trans;
3341 struct btrfs_path *path;
3342 struct btrfs_key key;
3343 int ret, err;
3344
3345 path = btrfs_alloc_path();
3346 if (!path)
3347 return -ENOMEM;
3348
3349 trans = btrfs_start_transaction(root, 0);
3350 if (IS_ERR(trans)) {
3351 btrfs_free_path(path);
3352 return PTR_ERR(trans);
3353 }
3354
3355 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003356 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003357 key.offset = 0;
3358
3359 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3360 if (ret < 0)
3361 goto out;
3362 if (ret > 0) {
3363 ret = -ENOENT;
3364 goto out;
3365 }
3366
3367 ret = btrfs_del_item(trans, root, path);
3368out:
3369 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003370 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003371 if (err && !ret)
3372 ret = err;
3373 return ret;
3374}
3375
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003376/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003377 * This is a heuristic used to reduce the number of chunks balanced on
3378 * resume after balance was interrupted.
3379 */
3380static void update_balance_args(struct btrfs_balance_control *bctl)
3381{
3382 /*
3383 * Turn on soft mode for chunk types that were being converted.
3384 */
3385 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3386 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3387 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3388 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3389 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3390 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3391
3392 /*
3393 * Turn on usage filter if is not already used. The idea is
3394 * that chunks that we have already balanced should be
3395 * reasonably full. Don't do it for chunks that are being
3396 * converted - that will keep us from relocating unconverted
3397 * (albeit full) chunks.
3398 */
3399 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003400 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003401 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3402 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3403 bctl->data.usage = 90;
3404 }
3405 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003406 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003407 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3408 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3409 bctl->sys.usage = 90;
3410 }
3411 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003412 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003413 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3414 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3415 bctl->meta.usage = 90;
3416 }
3417}
3418
3419/*
David Sterba149196a2018-03-20 20:23:09 +01003420 * Clear the balance status in fs_info and delete the balance item from disk.
3421 */
3422static void reset_balance_state(struct btrfs_fs_info *fs_info)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003423{
3424 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
David Sterba149196a2018-03-20 20:23:09 +01003425 int ret;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003426
3427 BUG_ON(!fs_info->balance_ctl);
3428
3429 spin_lock(&fs_info->balance_lock);
3430 fs_info->balance_ctl = NULL;
3431 spin_unlock(&fs_info->balance_lock);
3432
3433 kfree(bctl);
David Sterba149196a2018-03-20 20:23:09 +01003434 ret = del_balance_item(fs_info);
3435 if (ret)
3436 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003437}
3438
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003439/*
3440 * Balance filters. Return 1 if chunk should be filtered out
3441 * (should not be balanced).
3442 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003443static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003444 struct btrfs_balance_args *bargs)
3445{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003446 chunk_type = chunk_to_extended(chunk_type) &
3447 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003448
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003449 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003450 return 0;
3451
3452 return 1;
3453}
3454
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003455static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003456 struct btrfs_balance_args *bargs)
3457{
3458 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003459 u64 chunk_used;
3460 u64 user_thresh_min;
3461 u64 user_thresh_max;
3462 int ret = 1;
3463
3464 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3465 chunk_used = btrfs_block_group_used(&cache->item);
3466
3467 if (bargs->usage_min == 0)
3468 user_thresh_min = 0;
3469 else
3470 user_thresh_min = div_factor_fine(cache->key.offset,
3471 bargs->usage_min);
3472
3473 if (bargs->usage_max == 0)
3474 user_thresh_max = 1;
3475 else if (bargs->usage_max > 100)
3476 user_thresh_max = cache->key.offset;
3477 else
3478 user_thresh_max = div_factor_fine(cache->key.offset,
3479 bargs->usage_max);
3480
3481 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3482 ret = 0;
3483
3484 btrfs_put_block_group(cache);
3485 return ret;
3486}
3487
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003488static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003489 u64 chunk_offset, struct btrfs_balance_args *bargs)
3490{
3491 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003492 u64 chunk_used, user_thresh;
3493 int ret = 1;
3494
3495 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3496 chunk_used = btrfs_block_group_used(&cache->item);
3497
David Sterbabc309462015-10-20 18:22:13 +02003498 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003499 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003500 else if (bargs->usage > 100)
3501 user_thresh = cache->key.offset;
3502 else
3503 user_thresh = div_factor_fine(cache->key.offset,
3504 bargs->usage);
3505
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003506 if (chunk_used < user_thresh)
3507 ret = 0;
3508
3509 btrfs_put_block_group(cache);
3510 return ret;
3511}
3512
Ilya Dryomov409d4042012-01-16 22:04:47 +02003513static int chunk_devid_filter(struct extent_buffer *leaf,
3514 struct btrfs_chunk *chunk,
3515 struct btrfs_balance_args *bargs)
3516{
3517 struct btrfs_stripe *stripe;
3518 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3519 int i;
3520
3521 for (i = 0; i < num_stripes; i++) {
3522 stripe = btrfs_stripe_nr(chunk, i);
3523 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3524 return 0;
3525 }
3526
3527 return 1;
3528}
3529
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003530/* [pstart, pend) */
3531static int chunk_drange_filter(struct extent_buffer *leaf,
3532 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003533 struct btrfs_balance_args *bargs)
3534{
3535 struct btrfs_stripe *stripe;
3536 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3537 u64 stripe_offset;
3538 u64 stripe_length;
3539 int factor;
3540 int i;
3541
3542 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3543 return 0;
3544
3545 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003546 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3547 factor = num_stripes / 2;
3548 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3549 factor = num_stripes - 1;
3550 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3551 factor = num_stripes - 2;
3552 } else {
3553 factor = num_stripes;
3554 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003555
3556 for (i = 0; i < num_stripes; i++) {
3557 stripe = btrfs_stripe_nr(chunk, i);
3558 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3559 continue;
3560
3561 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3562 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003563 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003564
3565 if (stripe_offset < bargs->pend &&
3566 stripe_offset + stripe_length > bargs->pstart)
3567 return 0;
3568 }
3569
3570 return 1;
3571}
3572
Ilya Dryomovea671762012-01-16 22:04:48 +02003573/* [vstart, vend) */
3574static int chunk_vrange_filter(struct extent_buffer *leaf,
3575 struct btrfs_chunk *chunk,
3576 u64 chunk_offset,
3577 struct btrfs_balance_args *bargs)
3578{
3579 if (chunk_offset < bargs->vend &&
3580 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3581 /* at least part of the chunk is inside this vrange */
3582 return 0;
3583
3584 return 1;
3585}
3586
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003587static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3588 struct btrfs_chunk *chunk,
3589 struct btrfs_balance_args *bargs)
3590{
3591 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3592
3593 if (bargs->stripes_min <= num_stripes
3594 && num_stripes <= bargs->stripes_max)
3595 return 0;
3596
3597 return 1;
3598}
3599
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003600static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003601 struct btrfs_balance_args *bargs)
3602{
3603 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3604 return 0;
3605
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003606 chunk_type = chunk_to_extended(chunk_type) &
3607 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003608
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003609 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003610 return 1;
3611
3612 return 0;
3613}
3614
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003615static int should_balance_chunk(struct btrfs_fs_info *fs_info,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003616 struct extent_buffer *leaf,
3617 struct btrfs_chunk *chunk, u64 chunk_offset)
3618{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003619 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003620 struct btrfs_balance_args *bargs = NULL;
3621 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3622
3623 /* type filter */
3624 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3625 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3626 return 0;
3627 }
3628
3629 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3630 bargs = &bctl->data;
3631 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3632 bargs = &bctl->sys;
3633 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3634 bargs = &bctl->meta;
3635
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003636 /* profiles filter */
3637 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3638 chunk_profiles_filter(chunk_type, bargs)) {
3639 return 0;
3640 }
3641
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003642 /* usage filter */
3643 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003644 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003645 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003646 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003647 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003648 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003649 }
3650
Ilya Dryomov409d4042012-01-16 22:04:47 +02003651 /* devid filter */
3652 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3653 chunk_devid_filter(leaf, chunk, bargs)) {
3654 return 0;
3655 }
3656
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003657 /* drange filter, makes sense only with devid filter */
3658 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003659 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003660 return 0;
3661 }
3662
Ilya Dryomovea671762012-01-16 22:04:48 +02003663 /* vrange filter */
3664 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3665 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3666 return 0;
3667 }
3668
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003669 /* stripes filter */
3670 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3671 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3672 return 0;
3673 }
3674
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003675 /* soft profile changing mode */
3676 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3677 chunk_soft_convert_filter(chunk_type, bargs)) {
3678 return 0;
3679 }
3680
David Sterba7d824b62014-05-07 17:37:51 +02003681 /*
3682 * limited by count, must be the last filter
3683 */
3684 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3685 if (bargs->limit == 0)
3686 return 0;
3687 else
3688 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003689 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3690 /*
3691 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003692 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003693 * about the count of all chunks that satisfy the filters.
3694 */
3695 if (bargs->limit_max == 0)
3696 return 0;
3697 else
3698 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003699 }
3700
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003701 return 1;
3702}
3703
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003704static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003705{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003706 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003707 struct btrfs_root *chunk_root = fs_info->chunk_root;
David Sterba12907fc2015-10-10 17:16:50 +02003708 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003709 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003710 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003711 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003712 struct btrfs_key found_key;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003713 struct extent_buffer *leaf;
3714 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003715 int ret;
3716 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003717 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003718 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003719 u64 limit_data = bctl->data.limit;
3720 u64 limit_meta = bctl->meta.limit;
3721 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003722 u32 count_data = 0;
3723 u32 count_meta = 0;
3724 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003725 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003726
Chris Masonec44a352008-04-28 15:29:52 -04003727 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003728 if (!path) {
3729 ret = -ENOMEM;
3730 goto error;
3731 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003732
3733 /* zero out stat counters */
3734 spin_lock(&fs_info->balance_lock);
3735 memset(&bctl->stat, 0, sizeof(bctl->stat));
3736 spin_unlock(&fs_info->balance_lock);
3737again:
David Sterba7d824b62014-05-07 17:37:51 +02003738 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003739 /*
3740 * The single value limit and min/max limits use the same bytes
3741 * in the
3742 */
David Sterba7d824b62014-05-07 17:37:51 +02003743 bctl->data.limit = limit_data;
3744 bctl->meta.limit = limit_meta;
3745 bctl->sys.limit = limit_sys;
3746 }
Chris Masonec44a352008-04-28 15:29:52 -04003747 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3748 key.offset = (u64)-1;
3749 key.type = BTRFS_CHUNK_ITEM_KEY;
3750
Chris Masond3977122009-01-05 21:25:51 -05003751 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003752 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003753 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003754 ret = -ECANCELED;
3755 goto error;
3756 }
3757
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003758 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003759 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003760 if (ret < 0) {
3761 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003762 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003763 }
Chris Masonec44a352008-04-28 15:29:52 -04003764
3765 /*
3766 * this shouldn't happen, it means the last relocate
3767 * failed
3768 */
3769 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003770 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003771
3772 ret = btrfs_previous_item(chunk_root, path, 0,
3773 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003774 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003775 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003776 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003777 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003778 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003779
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003780 leaf = path->nodes[0];
3781 slot = path->slots[0];
3782 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3783
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003784 if (found_key.objectid != key.objectid) {
3785 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003786 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003787 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003788
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003789 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003790 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003791
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003792 if (!counting) {
3793 spin_lock(&fs_info->balance_lock);
3794 bctl->stat.considered++;
3795 spin_unlock(&fs_info->balance_lock);
3796 }
3797
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003798 ret = should_balance_chunk(fs_info, leaf, chunk,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003799 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003800
David Sterbab3b4aa72011-04-21 01:20:15 +02003801 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003802 if (!ret) {
3803 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003804 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003805 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003806
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003807 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003808 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003809 spin_lock(&fs_info->balance_lock);
3810 bctl->stat.expected++;
3811 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003812
3813 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3814 count_data++;
3815 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3816 count_sys++;
3817 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3818 count_meta++;
3819
3820 goto loop;
3821 }
3822
3823 /*
3824 * Apply limit_min filter, no need to check if the LIMITS
3825 * filter is used, limit_min is 0 by default
3826 */
3827 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3828 count_data < bctl->data.limit_min)
3829 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3830 count_meta < bctl->meta.limit_min)
3831 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3832 count_sys < bctl->sys.limit_min)) {
3833 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003834 goto loop;
3835 }
3836
Liu Boa6f93c72017-11-15 16:28:11 -07003837 if (!chunk_reserved) {
3838 /*
3839 * We may be relocating the only data chunk we have,
3840 * which could potentially end up with losing data's
3841 * raid profile, so lets allocate an empty one in
3842 * advance.
3843 */
3844 ret = btrfs_may_alloc_data_chunk(fs_info,
3845 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003846 if (ret < 0) {
3847 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3848 goto error;
Liu Boa6f93c72017-11-15 16:28:11 -07003849 } else if (ret == 1) {
3850 chunk_reserved = 1;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003851 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003852 }
3853
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003854 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003855 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003856 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003857 enospc_errors++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07003858 } else if (ret == -ETXTBSY) {
3859 btrfs_info(fs_info,
3860 "skipping relocation of block group %llu due to active swapfile",
3861 found_key.offset);
3862 ret = 0;
3863 } else if (ret) {
3864 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003865 } else {
3866 spin_lock(&fs_info->balance_lock);
3867 bctl->stat.completed++;
3868 spin_unlock(&fs_info->balance_lock);
3869 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003870loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003871 if (found_key.offset == 0)
3872 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003873 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003874 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003875
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003876 if (counting) {
3877 btrfs_release_path(path);
3878 counting = false;
3879 goto again;
3880 }
Chris Masonec44a352008-04-28 15:29:52 -04003881error:
3882 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003883 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003884 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003885 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003886 if (!ret)
3887 ret = -ENOSPC;
3888 }
3889
Chris Masonec44a352008-04-28 15:29:52 -04003890 return ret;
3891}
3892
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003893/**
3894 * alloc_profile_is_valid - see if a given profile is valid and reduced
3895 * @flags: profile to validate
3896 * @extended: if true @flags is treated as an extended profile
3897 */
3898static int alloc_profile_is_valid(u64 flags, int extended)
3899{
3900 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3901 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3902
3903 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3904
3905 /* 1) check that all other bits are zeroed */
3906 if (flags & ~mask)
3907 return 0;
3908
3909 /* 2) see if profile is reduced */
3910 if (flags == 0)
3911 return !extended; /* "0" is valid for usual profiles */
3912
3913 /* true if exactly one bit set */
David Sterba818255f2018-09-21 14:26:34 +02003914 return is_power_of_2(flags);
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003915}
3916
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003917static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3918{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003919 /* cancel requested || normal exit path */
3920 return atomic_read(&fs_info->balance_cancel_req) ||
3921 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3922 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003923}
3924
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003925/* Non-zero return value signifies invalidity */
3926static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3927 u64 allowed)
3928{
3929 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3930 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3931 (bctl_arg->target & ~allowed)));
3932}
3933
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003934/*
Anand Jain56fc37d2018-11-20 16:12:56 +08003935 * Fill @buf with textual description of balance filter flags @bargs, up to
3936 * @size_buf including the terminating null. The output may be trimmed if it
3937 * does not fit into the provided buffer.
3938 */
3939static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf,
3940 u32 size_buf)
3941{
3942 int ret;
3943 u32 size_bp = size_buf;
3944 char *bp = buf;
3945 u64 flags = bargs->flags;
3946 char tmp_buf[128] = {'\0'};
3947
3948 if (!flags)
3949 return;
3950
3951#define CHECK_APPEND_NOARG(a) \
3952 do { \
3953 ret = snprintf(bp, size_bp, (a)); \
3954 if (ret < 0 || ret >= size_bp) \
3955 goto out_overflow; \
3956 size_bp -= ret; \
3957 bp += ret; \
3958 } while (0)
3959
3960#define CHECK_APPEND_1ARG(a, v1) \
3961 do { \
3962 ret = snprintf(bp, size_bp, (a), (v1)); \
3963 if (ret < 0 || ret >= size_bp) \
3964 goto out_overflow; \
3965 size_bp -= ret; \
3966 bp += ret; \
3967 } while (0)
3968
3969#define CHECK_APPEND_2ARG(a, v1, v2) \
3970 do { \
3971 ret = snprintf(bp, size_bp, (a), (v1), (v2)); \
3972 if (ret < 0 || ret >= size_bp) \
3973 goto out_overflow; \
3974 size_bp -= ret; \
3975 bp += ret; \
3976 } while (0)
3977
3978 if (flags & BTRFS_BALANCE_ARGS_CONVERT) {
3979 int index = btrfs_bg_flags_to_raid_index(bargs->target);
3980
3981 CHECK_APPEND_1ARG("convert=%s,", get_raid_name(index));
3982 }
3983
3984 if (flags & BTRFS_BALANCE_ARGS_SOFT)
3985 CHECK_APPEND_NOARG("soft,");
3986
3987 if (flags & BTRFS_BALANCE_ARGS_PROFILES) {
3988 btrfs_describe_block_groups(bargs->profiles, tmp_buf,
3989 sizeof(tmp_buf));
3990 CHECK_APPEND_1ARG("profiles=%s,", tmp_buf);
3991 }
3992
3993 if (flags & BTRFS_BALANCE_ARGS_USAGE)
3994 CHECK_APPEND_1ARG("usage=%llu,", bargs->usage);
3995
3996 if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE)
3997 CHECK_APPEND_2ARG("usage=%u..%u,",
3998 bargs->usage_min, bargs->usage_max);
3999
4000 if (flags & BTRFS_BALANCE_ARGS_DEVID)
4001 CHECK_APPEND_1ARG("devid=%llu,", bargs->devid);
4002
4003 if (flags & BTRFS_BALANCE_ARGS_DRANGE)
4004 CHECK_APPEND_2ARG("drange=%llu..%llu,",
4005 bargs->pstart, bargs->pend);
4006
4007 if (flags & BTRFS_BALANCE_ARGS_VRANGE)
4008 CHECK_APPEND_2ARG("vrange=%llu..%llu,",
4009 bargs->vstart, bargs->vend);
4010
4011 if (flags & BTRFS_BALANCE_ARGS_LIMIT)
4012 CHECK_APPEND_1ARG("limit=%llu,", bargs->limit);
4013
4014 if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)
4015 CHECK_APPEND_2ARG("limit=%u..%u,",
4016 bargs->limit_min, bargs->limit_max);
4017
4018 if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE)
4019 CHECK_APPEND_2ARG("stripes=%u..%u,",
4020 bargs->stripes_min, bargs->stripes_max);
4021
4022#undef CHECK_APPEND_2ARG
4023#undef CHECK_APPEND_1ARG
4024#undef CHECK_APPEND_NOARG
4025
4026out_overflow:
4027
4028 if (size_bp < size_buf)
4029 buf[size_buf - size_bp - 1] = '\0'; /* remove last , */
4030 else
4031 buf[0] = '\0';
4032}
4033
4034static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info)
4035{
4036 u32 size_buf = 1024;
4037 char tmp_buf[192] = {'\0'};
4038 char *buf;
4039 char *bp;
4040 u32 size_bp = size_buf;
4041 int ret;
4042 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4043
4044 buf = kzalloc(size_buf, GFP_KERNEL);
4045 if (!buf)
4046 return;
4047
4048 bp = buf;
4049
4050#define CHECK_APPEND_1ARG(a, v1) \
4051 do { \
4052 ret = snprintf(bp, size_bp, (a), (v1)); \
4053 if (ret < 0 || ret >= size_bp) \
4054 goto out_overflow; \
4055 size_bp -= ret; \
4056 bp += ret; \
4057 } while (0)
4058
4059 if (bctl->flags & BTRFS_BALANCE_FORCE)
4060 CHECK_APPEND_1ARG("%s", "-f ");
4061
4062 if (bctl->flags & BTRFS_BALANCE_DATA) {
4063 describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf));
4064 CHECK_APPEND_1ARG("-d%s ", tmp_buf);
4065 }
4066
4067 if (bctl->flags & BTRFS_BALANCE_METADATA) {
4068 describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf));
4069 CHECK_APPEND_1ARG("-m%s ", tmp_buf);
4070 }
4071
4072 if (bctl->flags & BTRFS_BALANCE_SYSTEM) {
4073 describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf));
4074 CHECK_APPEND_1ARG("-s%s ", tmp_buf);
4075 }
4076
4077#undef CHECK_APPEND_1ARG
4078
4079out_overflow:
4080
4081 if (size_bp < size_buf)
4082 buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */
4083 btrfs_info(fs_info, "balance: %s %s",
4084 (bctl->flags & BTRFS_BALANCE_RESUME) ?
4085 "resume" : "start", buf);
4086
4087 kfree(buf);
4088}
4089
4090/*
David Sterbadccdb072018-03-21 00:20:05 +01004091 * Should be called with balance mutexe held
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004092 */
David Sterba6fcf6e22018-05-07 17:44:03 +02004093int btrfs_balance(struct btrfs_fs_info *fs_info,
4094 struct btrfs_balance_control *bctl,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004095 struct btrfs_ioctl_balance_args *bargs)
4096{
Adam Borowski14506122017-03-07 23:34:44 +01004097 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004098 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03004099 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004100 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01004101 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00004102 unsigned seq;
Filipe Manana5a8067c2018-11-19 09:48:12 +00004103 bool reducing_integrity;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004104
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004105 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004106 atomic_read(&fs_info->balance_pause_req) ||
4107 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004108 ret = -EINVAL;
4109 goto out;
4110 }
4111
Ilya Dryomove4837f82012-03-27 17:09:17 +03004112 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
4113 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
4114 mixed = 1;
4115
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004116 /*
4117 * In case of mixed groups both data and meta should be picked,
4118 * and identical options should be given for both of them.
4119 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03004120 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
4121 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004122 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
4123 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
4124 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004125 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004126 "balance: mixed groups data and metadata options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004127 ret = -EINVAL;
4128 goto out;
4129 }
4130 }
4131
Anand Jain1da73962018-08-10 13:53:21 +08004132 num_devices = btrfs_num_devices(fs_info);
4133
Austin S. Hemmelgarn88be1592016-03-23 14:22:59 -04004134 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
4135 if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004136 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00004137 if (num_devices > 2)
4138 allowed |= BTRFS_BLOCK_GROUP_RAID5;
4139 if (num_devices > 3)
4140 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
4141 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00004142 if (validate_convert_profile(&bctl->data, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004143 int index = btrfs_bg_flags_to_raid_index(bctl->data.target);
4144
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004145 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004146 "balance: invalid convert data profile %s",
4147 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004148 ret = -EINVAL;
4149 goto out;
4150 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00004151 if (validate_convert_profile(&bctl->meta, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004152 int index = btrfs_bg_flags_to_raid_index(bctl->meta.target);
4153
Frank Holtonefe120a2013-12-20 11:37:06 -05004154 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004155 "balance: invalid convert metadata profile %s",
4156 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004157 ret = -EINVAL;
4158 goto out;
4159 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00004160 if (validate_convert_profile(&bctl->sys, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004161 int index = btrfs_bg_flags_to_raid_index(bctl->sys.target);
4162
Frank Holtonefe120a2013-12-20 11:37:06 -05004163 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004164 "balance: invalid convert system profile %s",
4165 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004166 ret = -EINVAL;
4167 goto out;
4168 }
4169
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004170 /* allow to reduce meta or sys integrity only if force set */
4171 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05004172 BTRFS_BLOCK_GROUP_RAID10 |
4173 BTRFS_BLOCK_GROUP_RAID5 |
4174 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00004175 do {
4176 seq = read_seqbegin(&fs_info->profiles_lock);
4177
4178 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4179 (fs_info->avail_system_alloc_bits & allowed) &&
4180 !(bctl->sys.target & allowed)) ||
4181 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4182 (fs_info->avail_metadata_alloc_bits & allowed) &&
Filipe Manana5a8067c2018-11-19 09:48:12 +00004183 !(bctl->meta.target & allowed)))
4184 reducing_integrity = true;
4185 else
4186 reducing_integrity = false;
4187
4188 /* if we're not converting, the target field is uninitialized */
4189 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4190 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
4191 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4192 bctl->data.target : fs_info->avail_data_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004193 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004194
Filipe Manana5a8067c2018-11-19 09:48:12 +00004195 if (reducing_integrity) {
4196 if (bctl->flags & BTRFS_BALANCE_FORCE) {
4197 btrfs_info(fs_info,
4198 "balance: force reducing metadata integrity");
4199 } else {
4200 btrfs_err(fs_info,
4201 "balance: reduces metadata integrity, use --force if you want this");
4202 ret = -EINVAL;
4203 goto out;
4204 }
4205 }
4206
Adam Borowski14506122017-03-07 23:34:44 +01004207 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
4208 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004209 int meta_index = btrfs_bg_flags_to_raid_index(meta_target);
4210 int data_index = btrfs_bg_flags_to_raid_index(data_target);
4211
Sam Tygieree592d02016-01-06 08:46:12 +00004212 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004213 "balance: metadata profile %s has lower redundancy than data profile %s",
4214 get_raid_name(meta_index), get_raid_name(data_index));
Sam Tygieree592d02016-01-06 08:46:12 +00004215 }
4216
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004217 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02004218 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02004219 goto out;
4220
Ilya Dryomov59641012012-01-16 22:04:48 +02004221 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
4222 BUG_ON(ret == -EEXIST);
David Sterba833aae12018-03-21 02:41:30 +01004223 BUG_ON(fs_info->balance_ctl);
4224 spin_lock(&fs_info->balance_lock);
4225 fs_info->balance_ctl = bctl;
4226 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov59641012012-01-16 22:04:48 +02004227 } else {
4228 BUG_ON(ret != -EEXIST);
4229 spin_lock(&fs_info->balance_lock);
4230 update_balance_args(bctl);
4231 spin_unlock(&fs_info->balance_lock);
4232 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004233
David Sterba3009a622018-03-21 01:31:04 +01004234 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4235 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Anand Jain56fc37d2018-11-20 16:12:56 +08004236 describe_balance_start_or_resume(fs_info);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004237 mutex_unlock(&fs_info->balance_mutex);
4238
4239 ret = __btrfs_balance(fs_info);
4240
4241 mutex_lock(&fs_info->balance_mutex);
Anand Jain7333bd02018-11-20 16:12:57 +08004242 if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req))
4243 btrfs_info(fs_info, "balance: paused");
4244 else if (ret == -ECANCELED && atomic_read(&fs_info->balance_cancel_req))
4245 btrfs_info(fs_info, "balance: canceled");
4246 else
4247 btrfs_info(fs_info, "balance: ended with status: %d", ret);
4248
David Sterba3009a622018-03-21 01:31:04 +01004249 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004250
4251 if (bargs) {
4252 memset(bargs, 0, sizeof(*bargs));
David Sterba008ef092018-03-21 02:05:27 +01004253 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004254 }
4255
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004256 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
4257 balance_need_close(fs_info)) {
David Sterba149196a2018-03-20 20:23:09 +01004258 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004259 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004260 }
4261
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004262 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004263
4264 return ret;
4265out:
Ilya Dryomov59641012012-01-16 22:04:48 +02004266 if (bctl->flags & BTRFS_BALANCE_RESUME)
David Sterba149196a2018-03-20 20:23:09 +01004267 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004268 else
Ilya Dryomov59641012012-01-16 22:04:48 +02004269 kfree(bctl);
David Sterbaa17c95d2018-03-20 17:28:05 +01004270 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4271
Ilya Dryomov59641012012-01-16 22:04:48 +02004272 return ret;
4273}
4274
4275static int balance_kthread(void *data)
4276{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004277 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02004278 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02004279
Ilya Dryomov59641012012-01-16 22:04:48 +02004280 mutex_lock(&fs_info->balance_mutex);
Anand Jain56fc37d2018-11-20 16:12:56 +08004281 if (fs_info->balance_ctl)
David Sterba6fcf6e22018-05-07 17:44:03 +02004282 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
Ilya Dryomov59641012012-01-16 22:04:48 +02004283 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004284
Ilya Dryomov59641012012-01-16 22:04:48 +02004285 return ret;
4286}
4287
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004288int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
4289{
4290 struct task_struct *tsk;
4291
David Sterba1354e1a2018-03-21 02:29:13 +01004292 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004293 if (!fs_info->balance_ctl) {
David Sterba1354e1a2018-03-21 02:29:13 +01004294 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004295 return 0;
4296 }
David Sterba1354e1a2018-03-21 02:29:13 +01004297 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004298
Jeff Mahoney3cdde222016-06-09 21:38:35 -04004299 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004300 btrfs_info(fs_info, "balance: resume skipped");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004301 return 0;
4302 }
4303
Anand Jain02ee6542018-05-17 15:16:51 +08004304 /*
4305 * A ro->rw remount sequence should continue with the paused balance
4306 * regardless of who pauses it, system or the user as of now, so set
4307 * the resume flag.
4308 */
4309 spin_lock(&fs_info->balance_lock);
4310 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
4311 spin_unlock(&fs_info->balance_lock);
4312
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004313 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05304314 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004315}
4316
Ilya Dryomov68310a52012-06-22 12:24:12 -06004317int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02004318{
Ilya Dryomov59641012012-01-16 22:04:48 +02004319 struct btrfs_balance_control *bctl;
4320 struct btrfs_balance_item *item;
4321 struct btrfs_disk_balance_args disk_bargs;
4322 struct btrfs_path *path;
4323 struct extent_buffer *leaf;
4324 struct btrfs_key key;
4325 int ret;
4326
4327 path = btrfs_alloc_path();
4328 if (!path)
4329 return -ENOMEM;
4330
Ilya Dryomov68310a52012-06-22 12:24:12 -06004331 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01004332 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06004333 key.offset = 0;
4334
4335 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4336 if (ret < 0)
4337 goto out;
4338 if (ret > 0) { /* ret = -ENOENT; */
4339 ret = 0;
4340 goto out;
4341 }
4342
Ilya Dryomov59641012012-01-16 22:04:48 +02004343 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4344 if (!bctl) {
4345 ret = -ENOMEM;
4346 goto out;
4347 }
4348
Ilya Dryomov59641012012-01-16 22:04:48 +02004349 leaf = path->nodes[0];
4350 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4351
Ilya Dryomov68310a52012-06-22 12:24:12 -06004352 bctl->flags = btrfs_balance_flags(leaf, item);
4353 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004354
4355 btrfs_balance_data(leaf, item, &disk_bargs);
4356 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4357 btrfs_balance_meta(leaf, item, &disk_bargs);
4358 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4359 btrfs_balance_sys(leaf, item, &disk_bargs);
4360 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4361
David Sterbaeee95e32018-03-20 20:07:58 +01004362 /*
4363 * This should never happen, as the paused balance state is recovered
4364 * during mount without any chance of other exclusive ops to collide.
4365 *
4366 * This gives the exclusive op status to balance and keeps in paused
4367 * state until user intervention (cancel or umount). If the ownership
4368 * cannot be assigned, show a message but do not fail. The balance
4369 * is in a paused state and must have fs_info::balance_ctl properly
4370 * set up.
4371 */
4372 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
4373 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004374 "balance: cannot set exclusive op status, resume manually");
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004375
Ilya Dryomov68310a52012-06-22 12:24:12 -06004376 mutex_lock(&fs_info->balance_mutex);
David Sterba833aae12018-03-21 02:41:30 +01004377 BUG_ON(fs_info->balance_ctl);
4378 spin_lock(&fs_info->balance_lock);
4379 fs_info->balance_ctl = bctl;
4380 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov68310a52012-06-22 12:24:12 -06004381 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004382out:
4383 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004384 return ret;
4385}
4386
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004387int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4388{
4389 int ret = 0;
4390
4391 mutex_lock(&fs_info->balance_mutex);
4392 if (!fs_info->balance_ctl) {
4393 mutex_unlock(&fs_info->balance_mutex);
4394 return -ENOTCONN;
4395 }
4396
David Sterba3009a622018-03-21 01:31:04 +01004397 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004398 atomic_inc(&fs_info->balance_pause_req);
4399 mutex_unlock(&fs_info->balance_mutex);
4400
4401 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004402 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004403
4404 mutex_lock(&fs_info->balance_mutex);
4405 /* we are good with balance_ctl ripped off from under us */
David Sterba3009a622018-03-21 01:31:04 +01004406 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004407 atomic_dec(&fs_info->balance_pause_req);
4408 } else {
4409 ret = -ENOTCONN;
4410 }
4411
4412 mutex_unlock(&fs_info->balance_mutex);
4413 return ret;
4414}
4415
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004416int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4417{
4418 mutex_lock(&fs_info->balance_mutex);
4419 if (!fs_info->balance_ctl) {
4420 mutex_unlock(&fs_info->balance_mutex);
4421 return -ENOTCONN;
4422 }
4423
David Sterbacf7d20f2018-03-21 01:45:32 +01004424 /*
4425 * A paused balance with the item stored on disk can be resumed at
4426 * mount time if the mount is read-write. Otherwise it's still paused
4427 * and we must not allow cancelling as it deletes the item.
4428 */
4429 if (sb_rdonly(fs_info->sb)) {
4430 mutex_unlock(&fs_info->balance_mutex);
4431 return -EROFS;
4432 }
4433
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004434 atomic_inc(&fs_info->balance_cancel_req);
4435 /*
4436 * if we are running just wait and return, balance item is
4437 * deleted in btrfs_balance in this case
4438 */
David Sterba3009a622018-03-21 01:31:04 +01004439 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004440 mutex_unlock(&fs_info->balance_mutex);
4441 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004442 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004443 mutex_lock(&fs_info->balance_mutex);
4444 } else {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004445 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004446 /*
4447 * Lock released to allow other waiters to continue, we'll
4448 * reexamine the status again.
4449 */
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004450 mutex_lock(&fs_info->balance_mutex);
4451
David Sterbaa17c95d2018-03-20 17:28:05 +01004452 if (fs_info->balance_ctl) {
David Sterba149196a2018-03-20 20:23:09 +01004453 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004454 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain6dac13f2018-05-16 10:51:26 +08004455 btrfs_info(fs_info, "balance: canceled");
David Sterbaa17c95d2018-03-20 17:28:05 +01004456 }
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004457 }
4458
David Sterba3009a622018-03-21 01:31:04 +01004459 BUG_ON(fs_info->balance_ctl ||
4460 test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004461 atomic_dec(&fs_info->balance_cancel_req);
4462 mutex_unlock(&fs_info->balance_mutex);
4463 return 0;
4464}
4465
Stefan Behrens803b2f52013-08-15 17:11:21 +02004466static int btrfs_uuid_scan_kthread(void *data)
4467{
4468 struct btrfs_fs_info *fs_info = data;
4469 struct btrfs_root *root = fs_info->tree_root;
4470 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004471 struct btrfs_path *path = NULL;
4472 int ret = 0;
4473 struct extent_buffer *eb;
4474 int slot;
4475 struct btrfs_root_item root_item;
4476 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004477 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004478
4479 path = btrfs_alloc_path();
4480 if (!path) {
4481 ret = -ENOMEM;
4482 goto out;
4483 }
4484
4485 key.objectid = 0;
4486 key.type = BTRFS_ROOT_ITEM_KEY;
4487 key.offset = 0;
4488
Stefan Behrens803b2f52013-08-15 17:11:21 +02004489 while (1) {
Anand Jain7c829b72018-03-07 17:29:18 +08004490 ret = btrfs_search_forward(root, &key, path,
4491 BTRFS_OLDEST_GENERATION);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004492 if (ret) {
4493 if (ret > 0)
4494 ret = 0;
4495 break;
4496 }
4497
4498 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4499 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4500 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4501 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4502 goto skip;
4503
4504 eb = path->nodes[0];
4505 slot = path->slots[0];
4506 item_size = btrfs_item_size_nr(eb, slot);
4507 if (item_size < sizeof(root_item))
4508 goto skip;
4509
Stefan Behrens803b2f52013-08-15 17:11:21 +02004510 read_extent_buffer(eb, &root_item,
4511 btrfs_item_ptr_offset(eb, slot),
4512 (int)sizeof(root_item));
4513 if (btrfs_root_refs(&root_item) == 0)
4514 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004515
4516 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4517 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4518 if (trans)
4519 goto update_tree;
4520
4521 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004522 /*
4523 * 1 - subvol uuid item
4524 * 1 - received_subvol uuid item
4525 */
4526 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4527 if (IS_ERR(trans)) {
4528 ret = PTR_ERR(trans);
4529 break;
4530 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004531 continue;
4532 } else {
4533 goto skip;
4534 }
4535update_tree:
4536 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004537 ret = btrfs_uuid_tree_add(trans, root_item.uuid,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004538 BTRFS_UUID_KEY_SUBVOL,
4539 key.objectid);
4540 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004541 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004542 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004543 break;
4544 }
4545 }
4546
4547 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004548 ret = btrfs_uuid_tree_add(trans,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004549 root_item.received_uuid,
4550 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4551 key.objectid);
4552 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004553 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004554 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004555 break;
4556 }
4557 }
4558
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004559skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004560 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004561 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004562 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004563 if (ret)
4564 break;
4565 }
4566
Stefan Behrens803b2f52013-08-15 17:11:21 +02004567 btrfs_release_path(path);
4568 if (key.offset < (u64)-1) {
4569 key.offset++;
4570 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4571 key.offset = 0;
4572 key.type = BTRFS_ROOT_ITEM_KEY;
4573 } else if (key.objectid < (u64)-1) {
4574 key.offset = 0;
4575 key.type = BTRFS_ROOT_ITEM_KEY;
4576 key.objectid++;
4577 } else {
4578 break;
4579 }
4580 cond_resched();
4581 }
4582
4583out:
4584 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004585 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004586 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004587 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004588 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004589 else
Josef Bacikafcdd122016-09-02 15:40:02 -04004590 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004591 up(&fs_info->uuid_tree_rescan_sem);
4592 return 0;
4593}
4594
Stefan Behrens70f80172013-08-15 17:11:23 +02004595/*
4596 * Callback for btrfs_uuid_tree_iterate().
4597 * returns:
4598 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004599 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004600 * > 0 if the check failed, which means the caller shall remove the entry.
4601 */
4602static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4603 u8 *uuid, u8 type, u64 subid)
4604{
4605 struct btrfs_key key;
4606 int ret = 0;
4607 struct btrfs_root *subvol_root;
4608
4609 if (type != BTRFS_UUID_KEY_SUBVOL &&
4610 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4611 goto out;
4612
4613 key.objectid = subid;
4614 key.type = BTRFS_ROOT_ITEM_KEY;
4615 key.offset = (u64)-1;
4616 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4617 if (IS_ERR(subvol_root)) {
4618 ret = PTR_ERR(subvol_root);
4619 if (ret == -ENOENT)
4620 ret = 1;
4621 goto out;
4622 }
4623
4624 switch (type) {
4625 case BTRFS_UUID_KEY_SUBVOL:
4626 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4627 ret = 1;
4628 break;
4629 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4630 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4631 BTRFS_UUID_SIZE))
4632 ret = 1;
4633 break;
4634 }
4635
4636out:
4637 return ret;
4638}
4639
4640static int btrfs_uuid_rescan_kthread(void *data)
4641{
4642 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4643 int ret;
4644
4645 /*
4646 * 1st step is to iterate through the existing UUID tree and
4647 * to delete all entries that contain outdated data.
4648 * 2nd step is to add all missing entries to the UUID tree.
4649 */
4650 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4651 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004652 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004653 up(&fs_info->uuid_tree_rescan_sem);
4654 return ret;
4655 }
4656 return btrfs_uuid_scan_kthread(data);
4657}
4658
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004659int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4660{
4661 struct btrfs_trans_handle *trans;
4662 struct btrfs_root *tree_root = fs_info->tree_root;
4663 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004664 struct task_struct *task;
4665 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004666
4667 /*
4668 * 1 - root node
4669 * 1 - root item
4670 */
4671 trans = btrfs_start_transaction(tree_root, 2);
4672 if (IS_ERR(trans))
4673 return PTR_ERR(trans);
4674
4675 uuid_root = btrfs_create_tree(trans, fs_info,
4676 BTRFS_UUID_TREE_OBJECTID);
4677 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004678 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004679 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004680 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004681 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004682 }
4683
4684 fs_info->uuid_root = uuid_root;
4685
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004686 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004687 if (ret)
4688 return ret;
4689
4690 down(&fs_info->uuid_tree_rescan_sem);
4691 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4692 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004693 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004694 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004695 up(&fs_info->uuid_tree_rescan_sem);
4696 return PTR_ERR(task);
4697 }
4698
4699 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004700}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004701
Stefan Behrens70f80172013-08-15 17:11:23 +02004702int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4703{
4704 struct task_struct *task;
4705
4706 down(&fs_info->uuid_tree_rescan_sem);
4707 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4708 if (IS_ERR(task)) {
4709 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004710 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004711 up(&fs_info->uuid_tree_rescan_sem);
4712 return PTR_ERR(task);
4713 }
4714
4715 return 0;
4716}
4717
Chris Mason8f18cf12008-04-25 16:53:30 -04004718/*
4719 * shrinking a device means finding all of the device extents past
4720 * the new size, and then following the back refs to the chunks.
4721 * The chunk relocation code actually frees the device extent
4722 */
4723int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4724{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004725 struct btrfs_fs_info *fs_info = device->fs_info;
4726 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004727 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004728 struct btrfs_dev_extent *dev_extent = NULL;
4729 struct btrfs_path *path;
4730 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004731 u64 chunk_offset;
4732 int ret;
4733 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004734 int failed = 0;
4735 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004736 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004737 struct extent_buffer *l;
4738 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004739 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004740 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004741 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004742 u64 diff;
4743
4744 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004745 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004746
Anand Jain401e29c2017-12-04 12:54:55 +08004747 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Stefan Behrens63a212a2012-11-05 18:29:28 +01004748 return -EINVAL;
4749
Chris Mason8f18cf12008-04-25 16:53:30 -04004750 path = btrfs_alloc_path();
4751 if (!path)
4752 return -ENOMEM;
4753
Gu Jinxiang0338dff2018-04-27 16:22:07 +08004754 path->reada = READA_BACK;
Chris Mason8f18cf12008-04-25 16:53:30 -04004755
David Sterba34441362016-10-04 19:34:27 +02004756 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004757
Miao Xie7cc8e582014-09-03 21:35:38 +08004758 btrfs_device_set_total_bytes(device, new_size);
Anand Jainebbede42017-12-04 12:54:52 +08004759 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05004760 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004761 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004762 }
David Sterba34441362016-10-04 19:34:27 +02004763 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004764
Josef Bacikba1bf482009-09-11 16:11:19 -04004765again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004766 key.objectid = device->devid;
4767 key.offset = (u64)-1;
4768 key.type = BTRFS_DEV_EXTENT_KEY;
4769
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004770 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004771 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004772 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004773 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004774 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004775 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004776 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004777
4778 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004779 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004780 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004781 if (ret < 0)
4782 goto done;
4783 if (ret) {
4784 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004785 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004786 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004787 }
4788
4789 l = path->nodes[0];
4790 slot = path->slots[0];
4791 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4792
Josef Bacikba1bf482009-09-11 16:11:19 -04004793 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004794 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004795 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004796 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004797 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004798
4799 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4800 length = btrfs_dev_extent_length(l, dev_extent);
4801
Josef Bacikba1bf482009-09-11 16:11:19 -04004802 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004803 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004804 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004805 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004806 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004807
Chris Mason8f18cf12008-04-25 16:53:30 -04004808 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004809 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004810
Liu Boa6f93c72017-11-15 16:28:11 -07004811 /*
4812 * We may be relocating the only data chunk we have,
4813 * which could potentially end up with losing data's
4814 * raid profile, so lets allocate an empty one in
4815 * advance.
4816 */
4817 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4818 if (ret < 0) {
4819 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4820 goto done;
4821 }
4822
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004823 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4824 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004825 if (ret == -ENOSPC) {
Josef Bacikba1bf482009-09-11 16:11:19 -04004826 failed++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004827 } else if (ret) {
4828 if (ret == -ETXTBSY) {
4829 btrfs_warn(fs_info,
4830 "could not shrink block group %llu due to active swapfile",
4831 chunk_offset);
4832 }
4833 goto done;
4834 }
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004835 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004836
4837 if (failed && !retried) {
4838 failed = 0;
4839 retried = true;
4840 goto again;
4841 } else if (failed && retried) {
4842 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004843 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004844 }
4845
Chris Balld6397ba2009-04-27 07:29:03 -04004846 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004847 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004848 if (IS_ERR(trans)) {
4849 ret = PTR_ERR(trans);
4850 goto done;
4851 }
4852
David Sterba34441362016-10-04 19:34:27 +02004853 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004854
4855 /*
4856 * We checked in the above loop all device extents that were already in
4857 * the device tree. However before we have updated the device's
4858 * total_bytes to the new size, we might have had chunk allocations that
4859 * have not complete yet (new block groups attached to transaction
4860 * handles), and therefore their device extents were not yet in the
4861 * device tree and we missed them in the loop above. So if we have any
4862 * pending chunk using a device extent that overlaps the device range
4863 * that we can not use anymore, commit the current transaction and
4864 * repeat the search on the device tree - this way we guarantee we will
4865 * not have chunks using device extents that end beyond 'new_size'.
4866 */
4867 if (!checked_pending_chunks) {
4868 u64 start = new_size;
4869 u64 len = old_size - new_size;
4870
Jeff Mahoney499f3772015-06-15 09:41:17 -04004871 if (contains_pending_extent(trans->transaction, device,
4872 &start, len)) {
David Sterba34441362016-10-04 19:34:27 +02004873 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004874 checked_pending_chunks = true;
4875 failed = 0;
4876 retried = false;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004877 ret = btrfs_commit_transaction(trans);
Filipe Manana53e489b2015-06-02 14:43:21 +01004878 if (ret)
4879 goto done;
4880 goto again;
4881 }
4882 }
4883
Miao Xie7cc8e582014-09-03 21:35:38 +08004884 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004885 if (list_empty(&device->resized_list))
4886 list_add_tail(&device->resized_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004887 &fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004888
Chris Balld6397ba2009-04-27 07:29:03 -04004889 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004890 btrfs_set_super_total_bytes(super_copy,
4891 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004892 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004893
4894 /* Now btrfs_update_device() will change the on-disk size. */
4895 ret = btrfs_update_device(trans, device);
Anand Jain801660b2018-08-06 18:12:37 +08004896 if (ret < 0) {
4897 btrfs_abort_transaction(trans, ret);
4898 btrfs_end_transaction(trans);
4899 } else {
4900 ret = btrfs_commit_transaction(trans);
4901 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004902done:
4903 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004904 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004905 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004906 btrfs_device_set_total_bytes(device, old_size);
Anand Jainebbede42017-12-04 12:54:52 +08004907 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Filipe Manana53e489b2015-06-02 14:43:21 +01004908 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004909 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004910 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004911 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004912 return ret;
4913}
4914
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004915static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004916 struct btrfs_key *key,
4917 struct btrfs_chunk *chunk, int item_size)
4918{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004919 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004920 struct btrfs_disk_key disk_key;
4921 u32 array_size;
4922 u8 *ptr;
4923
David Sterba34441362016-10-04 19:34:27 +02004924 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004925 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004926 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004927 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004928 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004929 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004930 }
Chris Mason0b86a832008-03-24 15:01:56 -04004931
4932 ptr = super_copy->sys_chunk_array + array_size;
4933 btrfs_cpu_key_to_disk(&disk_key, key);
4934 memcpy(ptr, &disk_key, sizeof(disk_key));
4935 ptr += sizeof(disk_key);
4936 memcpy(ptr, chunk, item_size);
4937 item_size += sizeof(disk_key);
4938 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004939 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004940
Chris Mason0b86a832008-03-24 15:01:56 -04004941 return 0;
4942}
4943
Miao Xieb2117a32011-01-05 10:07:28 +00004944/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004945 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004946 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004947static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004948{
Arne Jansen73c5de02011-04-12 12:07:57 +02004949 const struct btrfs_device_info *di_a = a;
4950 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004951
Arne Jansen73c5de02011-04-12 12:07:57 +02004952 if (di_a->max_avail > di_b->max_avail)
4953 return -1;
4954 if (di_a->max_avail < di_b->max_avail)
4955 return 1;
4956 if (di_a->total_avail > di_b->total_avail)
4957 return -1;
4958 if (di_a->total_avail < di_b->total_avail)
4959 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004960 return 0;
4961}
4962
David Woodhouse53b381b2013-01-29 18:40:14 -05004963static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4964{
Zhao Leiffe2d202015-01-20 15:11:44 +08004965 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004966 return;
4967
Miao Xieceda0862013-04-11 10:30:16 +00004968 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004969}
4970
Qu Wenruo062d4d12018-01-30 18:20:46 +08004971#define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004972 - sizeof(struct btrfs_chunk)) \
4973 / sizeof(struct btrfs_stripe) + 1)
4974
4975#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4976 - 2 * sizeof(struct btrfs_disk_key) \
4977 - 2 * sizeof(struct btrfs_chunk)) \
4978 / sizeof(struct btrfs_stripe) + 1)
4979
Miao Xieb2117a32011-01-05 10:07:28 +00004980static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
David Sterba72b468c2017-02-10 19:46:27 +01004981 u64 start, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004982{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004983 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004984 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004985 struct btrfs_device *device;
Arne Jansen73c5de02011-04-12 12:07:57 +02004986 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004987 struct extent_map_tree *em_tree;
4988 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004989 struct btrfs_device_info *devices_info = NULL;
4990 u64 total_avail;
4991 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004992 int data_stripes; /* number of stripes that count for
4993 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004994 int sub_stripes; /* sub_stripes info for map */
4995 int dev_stripes; /* stripes per dev */
4996 int devs_max; /* max devs to use */
4997 int devs_min; /* min devs needed */
4998 int devs_increment; /* ndevs has to be a multiple of this */
4999 int ncopies; /* how many copies to data has */
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02005000 int nparity; /* number of stripes worth of bytes to
5001 store parity information */
Miao Xieb2117a32011-01-05 10:07:28 +00005002 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02005003 u64 max_stripe_size;
5004 u64 max_chunk_size;
5005 u64 stripe_size;
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005006 u64 chunk_size;
Arne Jansen73c5de02011-04-12 12:07:57 +02005007 int ndevs;
5008 int i;
5009 int j;
Liu Bo31e50222012-11-21 14:18:10 +00005010 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00005011
Ilya Dryomov0c460c02012-03-27 17:09:17 +03005012 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02005013
Qu Wenruo4117f202018-01-22 13:50:54 +08005014 if (list_empty(&fs_devices->alloc_list)) {
5015 if (btrfs_test_opt(info, ENOSPC_DEBUG))
5016 btrfs_debug(info, "%s: no writable device", __func__);
Miao Xieb2117a32011-01-05 10:07:28 +00005017 return -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08005018 }
Miao Xieb2117a32011-01-05 10:07:28 +00005019
Qu Wenruo3e72ee82018-01-30 18:20:45 +08005020 index = btrfs_bg_flags_to_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02005021
Liu Bo31e50222012-11-21 14:18:10 +00005022 sub_stripes = btrfs_raid_array[index].sub_stripes;
5023 dev_stripes = btrfs_raid_array[index].dev_stripes;
5024 devs_max = btrfs_raid_array[index].devs_max;
5025 devs_min = btrfs_raid_array[index].devs_min;
5026 devs_increment = btrfs_raid_array[index].devs_increment;
5027 ncopies = btrfs_raid_array[index].ncopies;
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02005028 nparity = btrfs_raid_array[index].nparity;
Arne Jansen73c5de02011-04-12 12:07:57 +02005029
5030 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09005031 max_stripe_size = SZ_1G;
Qu Wenruofce466e2018-07-03 17:10:05 +08005032 max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08005033 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08005034 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02005035 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05005036 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09005037 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
5038 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05005039 else
Byongho Leeee221842015-12-15 01:42:10 +09005040 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02005041 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08005042 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08005043 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02005044 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09005045 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02005046 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08005047 if (!devs_max)
5048 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02005049 } else {
David Sterba351fd352014-05-15 16:48:20 +02005050 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02005051 type);
5052 BUG_ON(1);
5053 }
5054
Andrea Gelmini52042d82018-11-28 12:05:13 +01005055 /* We don't want a chunk larger than 10% of writable space */
Arne Jansen73c5de02011-04-12 12:07:57 +02005056 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
5057 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00005058
David Sterba31e818f2015-02-20 18:00:26 +01005059 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00005060 GFP_NOFS);
5061 if (!devices_info)
5062 return -ENOMEM;
5063
Arne Jansen73c5de02011-04-12 12:07:57 +02005064 /*
5065 * in the first pass through the devices list, we gather information
5066 * about the available holes on each device.
5067 */
5068 ndevs = 0;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03005069 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Arne Jansen73c5de02011-04-12 12:07:57 +02005070 u64 max_avail;
5071 u64 dev_offset;
5072
Anand Jainebbede42017-12-04 12:54:52 +08005073 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00005074 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05005075 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02005076 continue;
5077 }
5078
Anand Jaine12c9622017-12-04 12:54:53 +08005079 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
5080 &device->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08005081 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Arne Jansen73c5de02011-04-12 12:07:57 +02005082 continue;
5083
5084 if (device->total_bytes > device->bytes_used)
5085 total_avail = device->total_bytes - device->bytes_used;
5086 else
5087 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00005088
5089 /* If there is no space on this device, skip it. */
5090 if (total_avail == 0)
5091 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02005092
Josef Bacik6df9a952013-06-27 13:22:46 -04005093 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02005094 max_stripe_size * dev_stripes,
5095 &dev_offset, &max_avail);
5096 if (ret && ret != -ENOSPC)
5097 goto error;
5098
5099 if (ret == 0)
5100 max_avail = max_stripe_size * dev_stripes;
5101
Qu Wenruo4117f202018-01-22 13:50:54 +08005102 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) {
5103 if (btrfs_test_opt(info, ENOSPC_DEBUG))
5104 btrfs_debug(info,
5105 "%s: devid %llu has no free space, have=%llu want=%u",
5106 __func__, device->devid, max_avail,
5107 BTRFS_STRIPE_LEN * dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02005108 continue;
Qu Wenruo4117f202018-01-22 13:50:54 +08005109 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005110
Eric Sandeen063d0062013-01-31 00:55:01 +00005111 if (ndevs == fs_devices->rw_devices) {
5112 WARN(1, "%s: found more than %llu devices\n",
5113 __func__, fs_devices->rw_devices);
5114 break;
5115 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005116 devices_info[ndevs].dev_offset = dev_offset;
5117 devices_info[ndevs].max_avail = max_avail;
5118 devices_info[ndevs].total_avail = total_avail;
5119 devices_info[ndevs].dev = device;
5120 ++ndevs;
5121 }
5122
5123 /*
5124 * now sort the devices by hole size / available space
5125 */
5126 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
5127 btrfs_cmp_device_info, NULL);
5128
5129 /* round down to number of usable stripes */
Nikolay Borisove5600fd2017-06-27 10:02:25 +03005130 ndevs = round_down(ndevs, devs_increment);
Arne Jansen73c5de02011-04-12 12:07:57 +02005131
Qu Wenruoba89b802018-01-31 13:56:15 +08005132 if (ndevs < devs_min) {
Arne Jansen73c5de02011-04-12 12:07:57 +02005133 ret = -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08005134 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
5135 btrfs_debug(info,
5136 "%s: not enough devices with free space: have=%d minimum required=%d",
Qu Wenruoba89b802018-01-31 13:56:15 +08005137 __func__, ndevs, devs_min);
Qu Wenruo4117f202018-01-22 13:50:54 +08005138 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005139 goto error;
5140 }
5141
Nikolay Borisovf148ef42017-06-27 10:02:26 +03005142 ndevs = min(ndevs, devs_max);
5143
Arne Jansen73c5de02011-04-12 12:07:57 +02005144 /*
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01005145 * The primary goal is to maximize the number of stripes, so use as
5146 * many devices as possible, even if the stripes are not maximum sized.
5147 *
5148 * The DUP profile stores more than one stripe per device, the
5149 * max_avail is the total size so we have to adjust.
Arne Jansen73c5de02011-04-12 12:07:57 +02005150 */
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01005151 stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02005152 num_stripes = ndevs * dev_stripes;
5153
David Woodhouse53b381b2013-01-29 18:40:14 -05005154 /*
5155 * this will have to be fixed for RAID1 and RAID10 over
5156 * more drives
5157 */
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02005158 data_stripes = (num_stripes - nparity) / ncopies;
Chris Mason86db2572013-02-20 16:23:40 -05005159
5160 /*
5161 * Use the number of data stripes to figure out how big this chunk
5162 * is really going to be in terms of logical address space,
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02005163 * and compare that answer with the max chunk size. If it's higher,
5164 * we try to reduce stripe_size.
Chris Mason86db2572013-02-20 16:23:40 -05005165 */
5166 if (stripe_size * data_stripes > max_chunk_size) {
Qu Wenruo793ff2c2018-01-31 14:16:34 +08005167 /*
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02005168 * Reduce stripe_size, round it up to a 16MB boundary again and
5169 * then use it, unless it ends up being even bigger than the
5170 * previous value we had already.
Chris Mason86db2572013-02-20 16:23:40 -05005171 */
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02005172 stripe_size = min(round_up(div_u64(max_chunk_size,
5173 data_stripes), SZ_16M),
Qu Wenruo793ff2c2018-01-31 14:16:34 +08005174 stripe_size);
Chris Mason86db2572013-02-20 16:23:40 -05005175 }
5176
Ilya Dryomov37db63a2012-04-13 17:05:08 +03005177 /* align to BTRFS_STRIPE_LEN */
Nikolay Borisov500ceed2017-07-14 09:55:41 +03005178 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
Arne Jansen73c5de02011-04-12 12:07:57 +02005179
Miao Xieb2117a32011-01-05 10:07:28 +00005180 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
5181 if (!map) {
5182 ret = -ENOMEM;
5183 goto error;
5184 }
5185 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04005186
Arne Jansen73c5de02011-04-12 12:07:57 +02005187 for (i = 0; i < ndevs; ++i) {
5188 for (j = 0; j < dev_stripes; ++j) {
5189 int s = i * dev_stripes + j;
5190 map->stripes[s].dev = devices_info[i].dev;
5191 map->stripes[s].physical = devices_info[i].dev_offset +
5192 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04005193 }
Chris Mason6324fbf2008-03-24 15:01:59 -04005194 }
Nikolay Borisov500ceed2017-07-14 09:55:41 +03005195 map->stripe_len = BTRFS_STRIPE_LEN;
5196 map->io_align = BTRFS_STRIPE_LEN;
5197 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04005198 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04005199 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005200
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005201 chunk_size = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005202
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005203 trace_btrfs_chunk_alloc(info, map, start, chunk_size);
liubo1abe9b82011-03-24 11:18:59 +00005204
David Sterba172ddd62011-04-21 00:48:27 +02005205 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05005206 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08005207 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00005208 ret = -ENOMEM;
5209 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05005210 }
Wang Shilong298a8f92014-06-19 10:42:52 +08005211 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04005212 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05005213 em->start = start;
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005214 em->len = chunk_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005215 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005216 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04005217 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005218
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005219 em_tree = &info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04005220 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005221 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005222 if (ret) {
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03005223 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005224 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07005225 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005226 }
Yan Zheng2b820322008-11-17 21:11:30 -05005227
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03005228 list_add_tail(&em->list, &trans->transaction->pending_chunks);
5229 refcount_inc(&em->refs);
5230 write_unlock(&em_tree->lock);
5231
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005232 ret = btrfs_make_block_group(trans, 0, type, start, chunk_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005233 if (ret)
5234 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05005235
Hans van Kranenburg2f29df42018-10-04 23:24:38 +02005236 for (i = 0; i < map->num_stripes; i++)
5237 btrfs_device_set_bytes_used(map->stripes[i].dev,
5238 map->stripes[i].dev->bytes_used + stripe_size);
Miao Xie43530c42014-09-03 21:35:36 +08005239
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03005240 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
Miao Xie1c116182014-09-03 21:35:37 +08005241
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005242 free_extent_map(em);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005243 check_raid56_incompat_flag(info, type);
David Woodhouse53b381b2013-01-29 18:40:14 -05005244
Miao Xieb2117a32011-01-05 10:07:28 +00005245 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05005246 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00005247
Josef Bacik6df9a952013-06-27 13:22:46 -04005248error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005249 write_lock(&em_tree->lock);
5250 remove_extent_mapping(em_tree, em);
5251 write_unlock(&em_tree->lock);
5252
5253 /* One for our allocation */
5254 free_extent_map(em);
5255 /* One for the tree reference */
5256 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00005257 /* One for the pending_chunks list reference */
5258 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00005259error:
Miao Xieb2117a32011-01-05 10:07:28 +00005260 kfree(devices_info);
5261 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005262}
5263
Josef Bacik6df9a952013-06-27 13:22:46 -04005264int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Nikolay Borisov97aff912018-07-20 19:37:53 +03005265 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05005266{
Nikolay Borisov97aff912018-07-20 19:37:53 +03005267 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04005268 struct btrfs_root *extent_root = fs_info->extent_root;
5269 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05005270 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05005271 struct btrfs_device *device;
5272 struct btrfs_chunk *chunk;
5273 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005274 struct extent_map *em;
5275 struct map_lookup *map;
5276 size_t item_size;
5277 u64 dev_offset;
5278 u64 stripe_size;
5279 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08005280 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005281
Omar Sandoval60ca8422018-05-16 16:34:31 -07005282 em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
Liu Bo592d92e2017-03-14 13:33:55 -07005283 if (IS_ERR(em))
5284 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04005285
Jeff Mahoney95617d62015-06-03 10:55:48 -04005286 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04005287 item_size = btrfs_chunk_item_size(map->num_stripes);
5288 stripe_size = em->orig_block_len;
5289
5290 chunk = kzalloc(item_size, GFP_NOFS);
5291 if (!chunk) {
5292 ret = -ENOMEM;
5293 goto out;
5294 }
5295
Filipe Manana50460e32015-11-20 10:42:47 +00005296 /*
5297 * Take the device list mutex to prevent races with the final phase of
5298 * a device replace operation that replaces the device object associated
5299 * with the map's stripes, because the device object's id can change
5300 * at any time during that final phase of the device replace operation
5301 * (dev-replace.c:btrfs_dev_replace_finishing()).
5302 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005303 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04005304 for (i = 0; i < map->num_stripes; i++) {
5305 device = map->stripes[i].dev;
5306 dev_offset = map->stripes[i].physical;
5307
Yan Zheng2b820322008-11-17 21:11:30 -05005308 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07005309 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005310 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03005311 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
5312 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005313 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005314 break;
5315 }
5316 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005317 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00005318 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05005319 }
5320
Yan Zheng2b820322008-11-17 21:11:30 -05005321 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005322 for (i = 0; i < map->num_stripes; i++) {
5323 device = map->stripes[i].dev;
5324 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05005325
5326 btrfs_set_stack_stripe_devid(stripe, device->devid);
5327 btrfs_set_stack_stripe_offset(stripe, dev_offset);
5328 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
5329 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05005330 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005331 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05005332
5333 btrfs_set_stack_chunk_length(chunk, chunk_size);
5334 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
5335 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
5336 btrfs_set_stack_chunk_type(chunk, map->type);
5337 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
5338 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
5339 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005340 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05005341 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
5342
5343 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5344 key.type = BTRFS_CHUNK_ITEM_KEY;
5345 key.offset = chunk_offset;
5346
5347 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005348 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5349 /*
5350 * TODO: Cleanup of inserted chunk root in case of
5351 * failure.
5352 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005353 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05005354 }
liubo1abe9b82011-03-24 11:18:59 +00005355
Josef Bacik6df9a952013-06-27 13:22:46 -04005356out:
Yan Zheng2b820322008-11-17 21:11:30 -05005357 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04005358 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005359 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005360}
5361
5362/*
Andrea Gelmini52042d82018-11-28 12:05:13 +01005363 * Chunk allocation falls into two parts. The first part does work
5364 * that makes the new allocated chunk usable, but does not do any operation
5365 * that modifies the chunk tree. The second part does the work that
5366 * requires modifying the chunk tree. This division is important for the
Yan Zheng2b820322008-11-17 21:11:30 -05005367 * bootstrap process of adding storage to a seed btrfs.
5368 */
Nikolay Borisovc216b202018-06-20 15:49:06 +03005369int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type)
Yan Zheng2b820322008-11-17 21:11:30 -05005370{
5371 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005372
Nikolay Borisovc216b202018-06-20 15:49:06 +03005373 lockdep_assert_held(&trans->fs_info->chunk_mutex);
5374 chunk_offset = find_next_chunk(trans->fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005375 return __btrfs_alloc_chunk(trans, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05005376}
5377
Chris Masond3977122009-01-05 21:25:51 -05005378static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +01005379 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05005380{
5381 u64 chunk_offset;
5382 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005383 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05005384 int ret;
5385
Josef Bacik6df9a952013-06-27 13:22:46 -04005386 chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005387 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005388 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005389 if (ret)
5390 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005391
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005392 sys_chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005393 alloc_profile = btrfs_system_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005394 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005395 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005396}
5397
Miao Xied20983b2014-07-03 18:22:13 +08005398static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5399{
5400 int max_errors;
5401
5402 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5403 BTRFS_BLOCK_GROUP_RAID10 |
5404 BTRFS_BLOCK_GROUP_RAID5 |
5405 BTRFS_BLOCK_GROUP_DUP)) {
5406 max_errors = 1;
5407 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5408 max_errors = 2;
5409 } else {
5410 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005411 }
5412
Miao Xied20983b2014-07-03 18:22:13 +08005413 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05005414}
5415
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005416int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005417{
5418 struct extent_map *em;
5419 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005420 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005421 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005422 int i;
5423
Omar Sandoval60ca8422018-05-16 16:34:31 -07005424 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07005425 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005426 return 1;
5427
Jeff Mahoney95617d62015-06-03 10:55:48 -04005428 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005429 for (i = 0; i < map->num_stripes; i++) {
Anand Jaine6e674b2017-12-04 12:54:54 +08005430 if (test_bit(BTRFS_DEV_STATE_MISSING,
5431 &map->stripes[i].dev->dev_state)) {
Miao Xied20983b2014-07-03 18:22:13 +08005432 miss_ndevs++;
5433 continue;
5434 }
Anand Jainebbede42017-12-04 12:54:52 +08005435 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5436 &map->stripes[i].dev->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05005437 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005438 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005439 }
5440 }
Miao Xied20983b2014-07-03 18:22:13 +08005441
5442 /*
5443 * If the number of missing devices is larger than max errors,
5444 * we can not write the data into that chunk successfully, so
5445 * set it readonly.
5446 */
5447 if (miss_ndevs > btrfs_chunk_max_errors(map))
5448 readonly = 1;
5449end:
Yan Zheng2b820322008-11-17 21:11:30 -05005450 free_extent_map(em);
5451 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005452}
5453
5454void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
5455{
David Sterbaa8067e02011-04-21 00:34:43 +02005456 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04005457}
5458
5459void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
5460{
5461 struct extent_map *em;
5462
Chris Masond3977122009-01-05 21:25:51 -05005463 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04005464 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005465 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
5466 if (em)
5467 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005468 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005469 if (!em)
5470 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005471 /* once for us */
5472 free_extent_map(em);
5473 /* once for the tree */
5474 free_extent_map(em);
5475 }
5476}
5477
Stefan Behrens5d964052012-11-05 14:59:07 +01005478int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005479{
5480 struct extent_map *em;
5481 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005482 int ret;
5483
Omar Sandoval60ca8422018-05-16 16:34:31 -07005484 em = btrfs_get_chunk_map(fs_info, logical, len);
Liu Bo592d92e2017-03-14 13:33:55 -07005485 if (IS_ERR(em))
5486 /*
5487 * We could return errors for these cases, but that could get
5488 * ugly and we'd probably do the same thing which is just not do
5489 * anything else and exit, so return 1 so the callers don't try
5490 * to use other copies.
5491 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005492 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005493
Jeff Mahoney95617d62015-06-03 10:55:48 -04005494 map = em->map_lookup;
Chris Masonf1885912008-04-09 16:28:12 -04005495 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5496 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005497 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5498 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005499 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5500 ret = 2;
5501 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
Liu Bo8810f752018-01-02 13:36:41 -07005502 /*
5503 * There could be two corrupted data stripes, we need
5504 * to loop retry in order to rebuild the correct data.
Nikolay Borisove7e02092018-06-20 15:48:55 +03005505 *
Liu Bo8810f752018-01-02 13:36:41 -07005506 * Fail a stripe at a time on every retry except the
5507 * stripe under reconstruction.
5508 */
5509 ret = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04005510 else
5511 ret = 1;
5512 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005513
David Sterbacb5583d2018-09-07 16:11:23 +02005514 down_read(&fs_info->dev_replace.rwsem);
Liu Bo6fad8232017-03-14 13:33:59 -07005515 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5516 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005517 ret++;
David Sterbacb5583d2018-09-07 16:11:23 +02005518 up_read(&fs_info->dev_replace.rwsem);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005519
Chris Masonf1885912008-04-09 16:28:12 -04005520 return ret;
5521}
5522
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005523unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005524 u64 logical)
5525{
5526 struct extent_map *em;
5527 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005528 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005529
Omar Sandoval60ca8422018-05-16 16:34:31 -07005530 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005531
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005532 if (!WARN_ON(IS_ERR(em))) {
5533 map = em->map_lookup;
5534 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5535 len = map->stripe_len * nr_data_stripes(map);
5536 free_extent_map(em);
5537 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005538 return len;
5539}
5540
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005541int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005542{
5543 struct extent_map *em;
5544 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005545 int ret = 0;
5546
Omar Sandoval60ca8422018-05-16 16:34:31 -07005547 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005548
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005549 if(!WARN_ON(IS_ERR(em))) {
5550 map = em->map_lookup;
5551 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5552 ret = 1;
5553 free_extent_map(em);
5554 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005555 return ret;
5556}
5557
Stefan Behrens30d98612012-11-06 14:52:18 +01005558static int find_live_mirror(struct btrfs_fs_info *fs_info,
Anand Jain99f92a72018-03-14 16:29:12 +08005559 struct map_lookup *map, int first,
Anand Jain8ba0ae72018-03-14 16:29:13 +08005560 int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005561{
5562 int i;
Anand Jain99f92a72018-03-14 16:29:12 +08005563 int num_stripes;
Anand Jain8ba0ae72018-03-14 16:29:13 +08005564 int preferred_mirror;
Stefan Behrens30d98612012-11-06 14:52:18 +01005565 int tolerance;
5566 struct btrfs_device *srcdev;
5567
Anand Jain99f92a72018-03-14 16:29:12 +08005568 ASSERT((map->type &
5569 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)));
5570
5571 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5572 num_stripes = map->sub_stripes;
5573 else
5574 num_stripes = map->num_stripes;
5575
Anand Jain8ba0ae72018-03-14 16:29:13 +08005576 preferred_mirror = first + current->pid % num_stripes;
5577
Stefan Behrens30d98612012-11-06 14:52:18 +01005578 if (dev_replace_is_ongoing &&
5579 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5580 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5581 srcdev = fs_info->dev_replace.srcdev;
5582 else
5583 srcdev = NULL;
5584
5585 /*
5586 * try to avoid the drive that is the source drive for a
5587 * dev-replace procedure, only choose it if no other non-missing
5588 * mirror is available
5589 */
5590 for (tolerance = 0; tolerance < 2; tolerance++) {
Anand Jain8ba0ae72018-03-14 16:29:13 +08005591 if (map->stripes[preferred_mirror].dev->bdev &&
5592 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5593 return preferred_mirror;
Anand Jain99f92a72018-03-14 16:29:12 +08005594 for (i = first; i < first + num_stripes; i++) {
Stefan Behrens30d98612012-11-06 14:52:18 +01005595 if (map->stripes[i].dev->bdev &&
5596 (tolerance || map->stripes[i].dev != srcdev))
5597 return i;
5598 }
Chris Masondfe25022008-05-13 13:46:40 -04005599 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005600
Chris Masondfe25022008-05-13 13:46:40 -04005601 /* we couldn't find one that doesn't fail. Just return something
5602 * and the io error handling code will clean up eventually
5603 */
Anand Jain8ba0ae72018-03-14 16:29:13 +08005604 return preferred_mirror;
Chris Masondfe25022008-05-13 13:46:40 -04005605}
5606
David Woodhouse53b381b2013-01-29 18:40:14 -05005607static inline int parity_smaller(u64 a, u64 b)
5608{
5609 return a > b;
5610}
5611
5612/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005613static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005614{
5615 struct btrfs_bio_stripe s;
5616 int i;
5617 u64 l;
5618 int again = 1;
5619
5620 while (again) {
5621 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005622 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005623 if (parity_smaller(bbio->raid_map[i],
5624 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005625 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005626 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005627 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005628 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005629 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005630 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005631
David Woodhouse53b381b2013-01-29 18:40:14 -05005632 again = 1;
5633 }
5634 }
5635 }
5636}
5637
Zhao Lei6e9606d2015-01-20 15:11:34 +08005638static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5639{
5640 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005641 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005642 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005643 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005644 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005645 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005646 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005647 /*
5648 * plus the raid_map, which includes both the tgt dev
5649 * and the stripes
5650 */
5651 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005652 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005653
5654 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005655 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005656
5657 return bbio;
5658}
5659
5660void btrfs_get_bbio(struct btrfs_bio *bbio)
5661{
Elena Reshetova140475a2017-03-03 10:55:10 +02005662 WARN_ON(!refcount_read(&bbio->refs));
5663 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005664}
5665
5666void btrfs_put_bbio(struct btrfs_bio *bbio)
5667{
5668 if (!bbio)
5669 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005670 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005671 kfree(bbio);
5672}
5673
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005674/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5675/*
5676 * Please note that, discard won't be sent to target device of device
5677 * replace.
5678 */
5679static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5680 u64 logical, u64 length,
5681 struct btrfs_bio **bbio_ret)
5682{
5683 struct extent_map *em;
5684 struct map_lookup *map;
5685 struct btrfs_bio *bbio;
5686 u64 offset;
5687 u64 stripe_nr;
5688 u64 stripe_nr_end;
5689 u64 stripe_end_offset;
5690 u64 stripe_cnt;
5691 u64 stripe_len;
5692 u64 stripe_offset;
5693 u64 num_stripes;
5694 u32 stripe_index;
5695 u32 factor = 0;
5696 u32 sub_stripes = 0;
5697 u64 stripes_per_dev = 0;
5698 u32 remaining_stripes = 0;
5699 u32 last_stripe = 0;
5700 int ret = 0;
5701 int i;
5702
5703 /* discard always return a bbio */
5704 ASSERT(bbio_ret);
5705
Omar Sandoval60ca8422018-05-16 16:34:31 -07005706 em = btrfs_get_chunk_map(fs_info, logical, length);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005707 if (IS_ERR(em))
5708 return PTR_ERR(em);
5709
5710 map = em->map_lookup;
5711 /* we don't discard raid56 yet */
5712 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5713 ret = -EOPNOTSUPP;
5714 goto out;
5715 }
5716
5717 offset = logical - em->start;
5718 length = min_t(u64, em->len - offset, length);
5719
5720 stripe_len = map->stripe_len;
5721 /*
5722 * stripe_nr counts the total number of stripes we have to stride
5723 * to get to this block
5724 */
5725 stripe_nr = div64_u64(offset, stripe_len);
5726
5727 /* stripe_offset is the offset of this block in its stripe */
5728 stripe_offset = offset - stripe_nr * stripe_len;
5729
5730 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005731 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005732 stripe_cnt = stripe_nr_end - stripe_nr;
5733 stripe_end_offset = stripe_nr_end * map->stripe_len -
5734 (offset + length);
5735 /*
5736 * after this, stripe_nr is the number of stripes on this
5737 * device we have to walk to find the data, and stripe_index is
5738 * the number of our device in the stripe array
5739 */
5740 num_stripes = 1;
5741 stripe_index = 0;
5742 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5743 BTRFS_BLOCK_GROUP_RAID10)) {
5744 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5745 sub_stripes = 1;
5746 else
5747 sub_stripes = map->sub_stripes;
5748
5749 factor = map->num_stripes / sub_stripes;
5750 num_stripes = min_t(u64, map->num_stripes,
5751 sub_stripes * stripe_cnt);
5752 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5753 stripe_index *= sub_stripes;
5754 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5755 &remaining_stripes);
5756 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5757 last_stripe *= sub_stripes;
5758 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5759 BTRFS_BLOCK_GROUP_DUP)) {
5760 num_stripes = map->num_stripes;
5761 } else {
5762 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5763 &stripe_index);
5764 }
5765
5766 bbio = alloc_btrfs_bio(num_stripes, 0);
5767 if (!bbio) {
5768 ret = -ENOMEM;
5769 goto out;
5770 }
5771
5772 for (i = 0; i < num_stripes; i++) {
5773 bbio->stripes[i].physical =
5774 map->stripes[stripe_index].physical +
5775 stripe_offset + stripe_nr * map->stripe_len;
5776 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5777
5778 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5779 BTRFS_BLOCK_GROUP_RAID10)) {
5780 bbio->stripes[i].length = stripes_per_dev *
5781 map->stripe_len;
5782
5783 if (i / sub_stripes < remaining_stripes)
5784 bbio->stripes[i].length +=
5785 map->stripe_len;
5786
5787 /*
5788 * Special for the first stripe and
5789 * the last stripe:
5790 *
5791 * |-------|...|-------|
5792 * |----------|
5793 * off end_off
5794 */
5795 if (i < sub_stripes)
5796 bbio->stripes[i].length -=
5797 stripe_offset;
5798
5799 if (stripe_index >= last_stripe &&
5800 stripe_index <= (last_stripe +
5801 sub_stripes - 1))
5802 bbio->stripes[i].length -=
5803 stripe_end_offset;
5804
5805 if (i == sub_stripes - 1)
5806 stripe_offset = 0;
5807 } else {
5808 bbio->stripes[i].length = length;
5809 }
5810
5811 stripe_index++;
5812 if (stripe_index == map->num_stripes) {
5813 stripe_index = 0;
5814 stripe_nr++;
5815 }
5816 }
5817
5818 *bbio_ret = bbio;
5819 bbio->map_type = map->type;
5820 bbio->num_stripes = num_stripes;
5821out:
5822 free_extent_map(em);
5823 return ret;
5824}
5825
Liu Bo5ab56092017-03-14 13:33:57 -07005826/*
5827 * In dev-replace case, for repair case (that's the only case where the mirror
5828 * is selected explicitly when calling btrfs_map_block), blocks left of the
5829 * left cursor can also be read from the target drive.
5830 *
5831 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5832 * array of stripes.
5833 * For READ, it also needs to be supported using the same mirror number.
5834 *
5835 * If the requested block is not left of the left cursor, EIO is returned. This
5836 * can happen because btrfs_num_copies() returns one more in the dev-replace
5837 * case.
5838 */
5839static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5840 u64 logical, u64 length,
5841 u64 srcdev_devid, int *mirror_num,
5842 u64 *physical)
5843{
5844 struct btrfs_bio *bbio = NULL;
5845 int num_stripes;
5846 int index_srcdev = 0;
5847 int found = 0;
5848 u64 physical_of_found = 0;
5849 int i;
5850 int ret = 0;
5851
5852 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5853 logical, &length, &bbio, 0, 0);
5854 if (ret) {
5855 ASSERT(bbio == NULL);
5856 return ret;
5857 }
5858
5859 num_stripes = bbio->num_stripes;
5860 if (*mirror_num > num_stripes) {
5861 /*
5862 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5863 * that means that the requested area is not left of the left
5864 * cursor
5865 */
5866 btrfs_put_bbio(bbio);
5867 return -EIO;
5868 }
5869
5870 /*
5871 * process the rest of the function using the mirror_num of the source
5872 * drive. Therefore look it up first. At the end, patch the device
5873 * pointer to the one of the target drive.
5874 */
5875 for (i = 0; i < num_stripes; i++) {
5876 if (bbio->stripes[i].dev->devid != srcdev_devid)
5877 continue;
5878
5879 /*
5880 * In case of DUP, in order to keep it simple, only add the
5881 * mirror with the lowest physical address
5882 */
5883 if (found &&
5884 physical_of_found <= bbio->stripes[i].physical)
5885 continue;
5886
5887 index_srcdev = i;
5888 found = 1;
5889 physical_of_found = bbio->stripes[i].physical;
5890 }
5891
5892 btrfs_put_bbio(bbio);
5893
5894 ASSERT(found);
5895 if (!found)
5896 return -EIO;
5897
5898 *mirror_num = index_srcdev + 1;
5899 *physical = physical_of_found;
5900 return ret;
5901}
5902
Liu Bo73c0f222017-03-14 13:33:58 -07005903static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5904 struct btrfs_bio **bbio_ret,
5905 struct btrfs_dev_replace *dev_replace,
5906 int *num_stripes_ret, int *max_errors_ret)
5907{
5908 struct btrfs_bio *bbio = *bbio_ret;
5909 u64 srcdev_devid = dev_replace->srcdev->devid;
5910 int tgtdev_indexes = 0;
5911 int num_stripes = *num_stripes_ret;
5912 int max_errors = *max_errors_ret;
5913 int i;
5914
5915 if (op == BTRFS_MAP_WRITE) {
5916 int index_where_to_add;
5917
5918 /*
5919 * duplicate the write operations while the dev replace
5920 * procedure is running. Since the copying of the old disk to
5921 * the new disk takes place at run time while the filesystem is
5922 * mounted writable, the regular write operations to the old
5923 * disk have to be duplicated to go to the new disk as well.
5924 *
5925 * Note that device->missing is handled by the caller, and that
5926 * the write to the old disk is already set up in the stripes
5927 * array.
5928 */
5929 index_where_to_add = num_stripes;
5930 for (i = 0; i < num_stripes; i++) {
5931 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5932 /* write to new disk, too */
5933 struct btrfs_bio_stripe *new =
5934 bbio->stripes + index_where_to_add;
5935 struct btrfs_bio_stripe *old =
5936 bbio->stripes + i;
5937
5938 new->physical = old->physical;
5939 new->length = old->length;
5940 new->dev = dev_replace->tgtdev;
5941 bbio->tgtdev_map[i] = index_where_to_add;
5942 index_where_to_add++;
5943 max_errors++;
5944 tgtdev_indexes++;
5945 }
5946 }
5947 num_stripes = index_where_to_add;
5948 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5949 int index_srcdev = 0;
5950 int found = 0;
5951 u64 physical_of_found = 0;
5952
5953 /*
5954 * During the dev-replace procedure, the target drive can also
5955 * be used to read data in case it is needed to repair a corrupt
5956 * block elsewhere. This is possible if the requested area is
5957 * left of the left cursor. In this area, the target drive is a
5958 * full copy of the source drive.
5959 */
5960 for (i = 0; i < num_stripes; i++) {
5961 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5962 /*
5963 * In case of DUP, in order to keep it simple,
5964 * only add the mirror with the lowest physical
5965 * address
5966 */
5967 if (found &&
5968 physical_of_found <=
5969 bbio->stripes[i].physical)
5970 continue;
5971 index_srcdev = i;
5972 found = 1;
5973 physical_of_found = bbio->stripes[i].physical;
5974 }
5975 }
5976 if (found) {
5977 struct btrfs_bio_stripe *tgtdev_stripe =
5978 bbio->stripes + num_stripes;
5979
5980 tgtdev_stripe->physical = physical_of_found;
5981 tgtdev_stripe->length =
5982 bbio->stripes[index_srcdev].length;
5983 tgtdev_stripe->dev = dev_replace->tgtdev;
5984 bbio->tgtdev_map[index_srcdev] = num_stripes;
5985
5986 tgtdev_indexes++;
5987 num_stripes++;
5988 }
5989 }
5990
5991 *num_stripes_ret = num_stripes;
5992 *max_errors_ret = max_errors;
5993 bbio->num_tgtdevs = tgtdev_indexes;
5994 *bbio_ret = bbio;
5995}
5996
Liu Bo2b19a1f2017-03-14 13:34:00 -07005997static bool need_full_stripe(enum btrfs_map_op op)
5998{
5999 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
6000}
6001
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006002static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
6003 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04006004 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02006005 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006006 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04006007{
6008 struct extent_map *em;
6009 struct map_lookup *map;
Chris Mason0b86a832008-03-24 15:01:56 -04006010 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04006011 u64 stripe_offset;
6012 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006013 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01006014 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04006015 int i;
Li Zefande11cc12011-12-01 12:55:47 +08006016 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04006017 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04006018 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08006019 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006020 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01006021 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
6022 int dev_replace_is_ongoing = 0;
6023 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006024 int patch_the_first_stripe_for_dev_replace = 0;
6025 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05006026 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04006027
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006028 if (op == BTRFS_MAP_DISCARD)
6029 return __btrfs_map_block_for_discard(fs_info, logical,
6030 *length, bbio_ret);
6031
Omar Sandoval60ca8422018-05-16 16:34:31 -07006032 em = btrfs_get_chunk_map(fs_info, logical, *length);
Liu Bo592d92e2017-03-14 13:33:55 -07006033 if (IS_ERR(em))
6034 return PTR_ERR(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04006035
Jeff Mahoney95617d62015-06-03 10:55:48 -04006036 map = em->map_lookup;
Chris Mason0b86a832008-03-24 15:01:56 -04006037 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04006038
David Woodhouse53b381b2013-01-29 18:40:14 -05006039 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04006040 stripe_nr = offset;
6041 /*
6042 * stripe_nr counts the total number of stripes we have to stride
6043 * to get to this block
6044 */
David Sterba47c57132015-02-20 18:43:47 +01006045 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04006046
David Woodhouse53b381b2013-01-29 18:40:14 -05006047 stripe_offset = stripe_nr * stripe_len;
Josef Bacike042d1e2016-04-12 12:54:40 -04006048 if (offset < stripe_offset) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006049 btrfs_crit(fs_info,
6050 "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
Josef Bacike042d1e2016-04-12 12:54:40 -04006051 stripe_offset, offset, em->start, logical,
6052 stripe_len);
6053 free_extent_map(em);
6054 return -EINVAL;
6055 }
Chris Mason593060d2008-03-25 16:50:33 -04006056
6057 /* stripe_offset is the offset of this block in its stripe*/
6058 stripe_offset = offset - stripe_offset;
6059
David Woodhouse53b381b2013-01-29 18:40:14 -05006060 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08006061 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006062 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
6063 raid56_full_stripe_start = offset;
6064
6065 /* allow a write of a full stripe, but make sure we don't
6066 * allow straddling of stripes
6067 */
David Sterba47c57132015-02-20 18:43:47 +01006068 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
6069 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05006070 raid56_full_stripe_start *= full_stripe_len;
6071 }
6072
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006073 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006074 u64 max_len;
6075 /* For writes to RAID[56], allow a full stripeset across all disks.
6076 For other RAID types and for RAID[56] reads, just allow a single
6077 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08006078 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006079 (op == BTRFS_MAP_WRITE)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006080 max_len = stripe_len * nr_data_stripes(map) -
6081 (offset - raid56_full_stripe_start);
6082 } else {
6083 /* we limit the length of each bio to what fits in a stripe */
6084 max_len = stripe_len - stripe_offset;
6085 }
6086 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04006087 } else {
6088 *length = em->len - offset;
6089 }
Chris Masonf2d8d742008-04-21 10:03:05 -04006090
Nikolay Borisovda12fe52018-11-27 20:57:58 +02006091 /*
6092 * This is for when we're called from btrfs_bio_fits_in_stripe and all
6093 * it cares about is the length
6094 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006095 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04006096 goto out;
6097
David Sterbacb5583d2018-09-07 16:11:23 +02006098 down_read(&dev_replace->rwsem);
Stefan Behrens472262f2012-11-06 14:43:46 +01006099 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
David Sterba53176dd2018-04-05 01:41:06 +02006100 /*
6101 * Hold the semaphore for read during the whole operation, write is
6102 * requested at commit time but must wait.
6103 */
Stefan Behrens472262f2012-11-06 14:43:46 +01006104 if (!dev_replace_is_ongoing)
David Sterbacb5583d2018-09-07 16:11:23 +02006105 up_read(&dev_replace->rwsem);
Stefan Behrens472262f2012-11-06 14:43:46 +01006106
Stefan Behrensad6d6202012-11-06 15:06:47 +01006107 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006108 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07006109 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
6110 dev_replace->srcdev->devid,
6111 &mirror_num,
6112 &physical_to_patch_in_first_stripe);
6113 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006114 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07006115 else
6116 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006117 } else if (mirror_num > map->num_stripes) {
6118 mirror_num = 0;
6119 }
6120
Chris Masonf2d8d742008-04-21 10:03:05 -04006121 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04006122 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00006123 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01006124 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6125 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006126 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08006127 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00006128 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Anand Jainde483732017-10-12 16:43:00 +08006129 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04006130 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04006131 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04006132 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04006133 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01006134 stripe_index = find_live_mirror(fs_info, map, 0,
Stefan Behrens30d98612012-11-06 14:52:18 +01006135 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006136 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006137 }
Chris Mason2fff7342008-04-29 14:12:09 -04006138
Chris Mason611f0e02008-04-03 16:29:03 -04006139 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Anand Jainde483732017-10-12 16:43:00 +08006140 if (need_full_stripe(op)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04006141 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006142 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04006143 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006144 } else {
6145 mirror_num = 1;
6146 }
Chris Mason2fff7342008-04-29 14:12:09 -04006147
Chris Mason321aecc2008-04-16 10:49:51 -04006148 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01006149 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04006150
David Sterba47c57132015-02-20 18:43:47 +01006151 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04006152 stripe_index *= map->sub_stripes;
6153
Anand Jainde483732017-10-12 16:43:00 +08006154 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04006155 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04006156 else if (mirror_num)
6157 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04006158 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04006159 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01006160 stripe_index = find_live_mirror(fs_info, map,
6161 stripe_index,
Stefan Behrens30d98612012-11-06 14:52:18 +01006162 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04006163 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006164 }
David Woodhouse53b381b2013-01-29 18:40:14 -05006165
Zhao Leiffe2d202015-01-20 15:11:44 +08006166 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08006167 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006168 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07006169 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbab8b93ad2015-01-16 17:26:13 +01006170 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05006171
6172 /* RAID[56] write or recovery. Return all stripes */
6173 num_stripes = map->num_stripes;
6174 max_errors = nr_parity_stripes(map);
6175
David Woodhouse53b381b2013-01-29 18:40:14 -05006176 *length = map->stripe_len;
6177 stripe_index = 0;
6178 stripe_offset = 0;
6179 } else {
6180 /*
6181 * Mirror #0 or #1 means the original data block.
6182 * Mirror #2 is RAID5 parity block.
6183 * Mirror #3 is RAID6 Q block.
6184 */
David Sterba47c57132015-02-20 18:43:47 +01006185 stripe_nr = div_u64_rem(stripe_nr,
6186 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05006187 if (mirror_num > 1)
6188 stripe_index = nr_data_stripes(map) +
6189 mirror_num - 2;
6190
6191 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01006192 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
6193 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006194 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08006195 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05006196 }
Chris Mason8790d502008-04-03 16:29:03 -04006197 } else {
6198 /*
David Sterba47c57132015-02-20 18:43:47 +01006199 * after this, stripe_nr is the number of stripes on this
6200 * device we have to walk to find the data, and stripe_index is
6201 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04006202 */
David Sterba47c57132015-02-20 18:43:47 +01006203 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6204 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006205 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04006206 }
Josef Bacike042d1e2016-04-12 12:54:40 -04006207 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006208 btrfs_crit(fs_info,
6209 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04006210 stripe_index, map->num_stripes);
6211 ret = -EINVAL;
6212 goto out;
6213 }
Chris Mason593060d2008-03-25 16:50:33 -04006214
Stefan Behrens472262f2012-11-06 14:43:46 +01006215 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07006216 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006217 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006218 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006219 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006220 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08006221 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006222 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08006223
Zhao Lei6e9606d2015-01-20 15:11:34 +08006224 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08006225 if (!bbio) {
6226 ret = -ENOMEM;
6227 goto out;
6228 }
Liu Bo6fad8232017-03-14 13:33:59 -07006229 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
Miao Xie2c8cdd62014-11-14 16:06:25 +08006230 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08006231
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006232 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07006233 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
6234 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006235 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01006236 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006237
6238 bbio->raid_map = (u64 *)((void *)bbio->stripes +
6239 sizeof(struct btrfs_bio_stripe) *
6240 num_alloc_stripes +
6241 sizeof(int) * tgtdev_indexes);
6242
6243 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01006244 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006245
6246 /* Fill in the logical address of each stripe */
6247 tmp = stripe_nr * nr_data_stripes(map);
6248 for (i = 0; i < nr_data_stripes(map); i++)
6249 bbio->raid_map[(i+rot) % num_stripes] =
6250 em->start + (tmp + i) * map->stripe_len;
6251
6252 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
6253 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
6254 bbio->raid_map[(i+rot+1) % num_stripes] =
6255 RAID6_Q_STRIPE;
6256 }
6257
Li Zefanec9ef7a2011-12-01 14:06:42 +08006258
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006259 for (i = 0; i < num_stripes; i++) {
6260 bbio->stripes[i].physical =
6261 map->stripes[stripe_index].physical +
6262 stripe_offset +
6263 stripe_nr * map->stripe_len;
6264 bbio->stripes[i].dev =
6265 map->stripes[stripe_index].dev;
6266 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04006267 }
Li Zefande11cc12011-12-01 12:55:47 +08006268
Liu Bo2b19a1f2017-03-14 13:34:00 -07006269 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08006270 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08006271
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006272 if (bbio->raid_map)
6273 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08006274
Liu Bo73c0f222017-03-14 13:33:58 -07006275 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006276 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07006277 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
6278 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01006279 }
6280
Li Zefande11cc12011-12-01 12:55:47 +08006281 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08006282 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08006283 bbio->num_stripes = num_stripes;
6284 bbio->max_errors = max_errors;
6285 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006286
6287 /*
6288 * this is the case that REQ_READ && dev_replace_is_ongoing &&
6289 * mirror_num == num_stripes + 1 && dev_replace target drive is
6290 * available as a mirror
6291 */
6292 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
6293 WARN_ON(num_stripes > 1);
6294 bbio->stripes[0].dev = dev_replace->tgtdev;
6295 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
6296 bbio->mirror_num = map->num_stripes + 1;
6297 }
Chris Masoncea9e442008-04-09 16:28:12 -04006298out:
Liu Bo73beece2015-07-17 16:49:19 +08006299 if (dev_replace_is_ongoing) {
David Sterba53176dd2018-04-05 01:41:06 +02006300 lockdep_assert_held(&dev_replace->rwsem);
6301 /* Unlock and let waiting writers proceed */
David Sterbacb5583d2018-09-07 16:11:23 +02006302 up_read(&dev_replace->rwsem);
Liu Bo73beece2015-07-17 16:49:19 +08006303 }
Chris Mason0b86a832008-03-24 15:01:56 -04006304 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08006305 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006306}
6307
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006308int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04006309 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02006310 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04006311{
Mike Christieb3d3fa52016-06-05 14:31:53 -05006312 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006313 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04006314}
6315
Miao Xieaf8e2d12014-10-23 14:42:50 +08006316/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006317int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08006318 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02006319 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08006320{
David Sterba825ad4c2017-03-28 14:45:22 +02006321 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08006322}
6323
Nikolay Borisov63a9c7b2018-05-04 10:53:05 +03006324int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
6325 u64 physical, u64 **logical, int *naddrs, int *stripe_len)
Yan Zhenga512bbf2008-12-08 16:46:26 -05006326{
Yan Zhenga512bbf2008-12-08 16:46:26 -05006327 struct extent_map *em;
6328 struct map_lookup *map;
6329 u64 *buf;
6330 u64 bytenr;
6331 u64 length;
6332 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006333 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006334 int i, j, nr = 0;
6335
Omar Sandoval60ca8422018-05-16 16:34:31 -07006336 em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07006337 if (IS_ERR(em))
Josef Bacik835d9742013-03-19 12:13:25 -04006338 return -EIO;
Josef Bacik835d9742013-03-19 12:13:25 -04006339
Jeff Mahoney95617d62015-06-03 10:55:48 -04006340 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006341 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05006342 rmap_len = map->stripe_len;
6343
Yan Zhenga512bbf2008-12-08 16:46:26 -05006344 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01006345 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006346 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01006347 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08006348 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01006349 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05006350 rmap_len = map->stripe_len * nr_data_stripes(map);
6351 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006352
David Sterba31e818f2015-02-20 18:00:26 +01006353 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006354 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05006355
6356 for (i = 0; i < map->num_stripes; i++) {
Yan Zhenga512bbf2008-12-08 16:46:26 -05006357 if (map->stripes[i].physical > physical ||
6358 map->stripes[i].physical + length <= physical)
6359 continue;
6360
6361 stripe_nr = physical - map->stripes[i].physical;
Liu Bo42c61ab2017-04-03 13:45:24 -07006362 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006363
6364 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
6365 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01006366 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006367 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
6368 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05006369 } /* else if RAID[56], multiply by nr_data_stripes().
6370 * Alternatively, just use rmap_len below instead of
6371 * map->stripe_len */
6372
6373 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05006374 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006375 for (j = 0; j < nr; j++) {
6376 if (buf[j] == bytenr)
6377 break;
6378 }
Chris Mason934d3752008-12-08 16:43:10 -05006379 if (j == nr) {
6380 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006381 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05006382 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006383 }
6384
Yan Zhenga512bbf2008-12-08 16:46:26 -05006385 *logical = buf;
6386 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006387 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006388
6389 free_extent_map(em);
6390 return 0;
6391}
6392
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006393static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08006394{
Mike Snitzer326e1db2015-05-22 09:14:03 -04006395 bio->bi_private = bbio->private;
6396 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006397 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04006398
Zhao Lei6e9606d2015-01-20 15:11:34 +08006399 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08006400}
6401
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006402static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04006403{
Chris Mason9be33952013-05-17 18:30:14 -04006404 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006405 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04006406
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006407 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02006408 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006409 if (bio->bi_status == BLK_STS_IOERR ||
6410 bio->bi_status == BLK_STS_TARGET) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02006411 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04006412 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08006413 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006414
6415 BUG_ON(stripe_index >= bbio->num_stripes);
6416 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02006417 if (dev->bdev) {
Mike Christie37226b22016-06-05 14:31:52 -05006418 if (bio_op(bio) == REQ_OP_WRITE)
Anand Jain1cb34c82017-10-21 01:45:33 +08006419 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006420 BTRFS_DEV_STAT_WRITE_ERRS);
6421 else
Anand Jain1cb34c82017-10-21 01:45:33 +08006422 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006423 BTRFS_DEV_STAT_READ_ERRS);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06006424 if (bio->bi_opf & REQ_PREFLUSH)
Anand Jain1cb34c82017-10-21 01:45:33 +08006425 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006426 BTRFS_DEV_STAT_FLUSH_ERRS);
Stefan Behrens597a60f2012-06-14 16:42:31 +02006427 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02006428 }
6429 }
Chris Mason8790d502008-04-03 16:29:03 -04006430
Jan Schmidta1d3c472011-08-04 17:15:33 +02006431 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006432 is_orig_bio = 1;
6433
Miao Xiec404e0d2014-01-30 16:46:55 +08006434 btrfs_bio_counter_dec(bbio->fs_info);
6435
Jan Schmidta1d3c472011-08-04 17:15:33 +02006436 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006437 if (!is_orig_bio) {
6438 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006439 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006440 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006441
Chris Mason9be33952013-05-17 18:30:14 -04006442 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006443 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006444 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006445 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006446 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006447 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006448 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006449 /*
6450 * this bio is actually up to date, we didn't
6451 * go over the max number of errors
6452 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006453 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006454 }
Miao Xiec55f1392014-06-19 10:42:54 +08006455
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006456 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006457 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006458 bio_put(bio);
6459 }
Chris Mason8790d502008-04-03 16:29:03 -04006460}
6461
Chris Mason8b712842008-06-11 16:50:36 -04006462/*
6463 * see run_scheduled_bios for a description of why bios are collected for
6464 * async submit.
6465 *
6466 * This will add one bio to the pending list for a device and make sure
6467 * the work struct is scheduled.
6468 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006469static noinline void btrfs_schedule_bio(struct btrfs_device *device,
Mike Christie4e49ea42016-06-05 14:31:41 -05006470 struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04006471{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006472 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -04006473 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04006474 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006475
6476 /* don't bother with additional async steps for reads, right now */
Mike Christie37226b22016-06-05 14:31:52 -05006477 if (bio_op(bio) == REQ_OP_READ) {
Mike Christie4e49ea42016-06-05 14:31:41 -05006478 btrfsic_submit_bio(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01006479 return;
Chris Mason8b712842008-06-11 16:50:36 -04006480 }
6481
Chris Mason492bb6de2008-07-31 16:29:02 -04006482 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04006483 bio->bi_next = NULL;
Chris Mason8b712842008-06-11 16:50:36 -04006484
6485 spin_lock(&device->io_lock);
Christoph Hellwig67f055c2016-11-01 07:40:06 -06006486 if (op_is_sync(bio->bi_opf))
Chris Masonffbd5172009-04-20 15:50:09 -04006487 pending_bios = &device->pending_sync_bios;
6488 else
6489 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006490
Chris Masonffbd5172009-04-20 15:50:09 -04006491 if (pending_bios->tail)
6492 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006493
Chris Masonffbd5172009-04-20 15:50:09 -04006494 pending_bios->tail = bio;
6495 if (!pending_bios->head)
6496 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006497 if (device->running_pending)
6498 should_queue = 0;
6499
6500 spin_unlock(&device->io_lock);
6501
6502 if (should_queue)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006503 btrfs_queue_work(fs_info->submit_workers, &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04006504}
6505
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006506static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6507 u64 physical, int dev_nr, int async)
Josef Bacikde1ee922012-10-19 16:50:56 -04006508{
6509 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006510 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006511
6512 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006513 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006514 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006515 bio->bi_iter.bi_sector = physical >> 9;
Misono Tomohiro672d5992018-08-02 16:19:07 +09006516 btrfs_debug_in_rcu(fs_info,
6517 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6518 bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector,
6519 (u_long)dev->bdev->bd_dev, rcu_str_deref(dev->name), dev->devid,
6520 bio->bi_iter.bi_size);
Christoph Hellwig74d46992017-08-23 19:10:32 +02006521 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006522
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006523 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006524
Josef Bacikde1ee922012-10-19 16:50:56 -04006525 if (async)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006526 btrfs_schedule_bio(dev, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006527 else
Mike Christie4e49ea42016-06-05 14:31:41 -05006528 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006529}
6530
Josef Bacikde1ee922012-10-19 16:50:56 -04006531static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6532{
6533 atomic_inc(&bbio->error);
6534 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006535 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006536 WARN_ON(bio != bbio->orig_bio);
6537
Chris Mason9be33952013-05-17 18:30:14 -04006538 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006539 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006540 if (atomic_read(&bbio->error) > bbio->max_errors)
6541 bio->bi_status = BLK_STS_IOERR;
6542 else
6543 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006544 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006545 }
6546}
6547
Omar Sandoval58efbc92017-08-22 23:45:59 -07006548blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6549 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006550{
Chris Mason0b86a832008-03-24 15:01:56 -04006551 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006552 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006553 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006554 u64 length = 0;
6555 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006556 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006557 int dev_nr;
6558 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006559 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006560
Kent Overstreet4f024f32013-10-11 15:44:27 -07006561 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006562 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006563
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006564 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006565 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006566 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006567 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006568 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006569 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006570 }
Chris Masoncea9e442008-04-09 16:28:12 -04006571
Jan Schmidta1d3c472011-08-04 17:15:33 +02006572 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006573 bbio->orig_bio = first_bio;
6574 bbio->private = first_bio->bi_private;
6575 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006576 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006577 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6578
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006579 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006580 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006581 /* In this case, map_length has been set to the length of
6582 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006583 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006584 ret = raid56_parity_write(fs_info, bio, bbio,
6585 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006586 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006587 ret = raid56_parity_recover(fs_info, bio, bbio,
6588 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006589 }
Miao Xie42452152014-11-25 16:39:28 +08006590
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006591 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006592 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006593 }
6594
Chris Masoncea9e442008-04-09 16:28:12 -04006595 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006596 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006597 "mapping failed logical %llu bio len %llu len %llu",
6598 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006599 BUG();
6600 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006601
Zhao Lei08da7572015-02-12 15:42:16 +08006602 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006603 dev = bbio->stripes[dev_nr].dev;
Nikolay Borisovfc8a1682018-11-08 16:16:38 +02006604 if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING,
6605 &dev->dev_state) ||
Anand Jainebbede42017-12-04 12:54:52 +08006606 (bio_op(first_bio) == REQ_OP_WRITE &&
6607 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006608 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006609 continue;
6610 }
6611
David Sterba3aa8e072017-06-02 17:38:30 +02006612 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006613 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006614 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006615 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006616
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006617 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6618 dev_nr, async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006619 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006620 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006621 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006622}
6623
Anand Jaine4319cd2019-01-17 23:32:31 +08006624struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
6625 u64 devid, u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006626{
Yan Zheng2b820322008-11-17 21:11:30 -05006627 struct btrfs_device *device;
Chris Mason0b86a832008-03-24 15:01:56 -04006628
Anand Jaine4319cd2019-01-17 23:32:31 +08006629 while (fs_devices) {
Yan Zheng2b820322008-11-17 21:11:30 -05006630 if (!fsid ||
Anand Jaine4319cd2019-01-17 23:32:31 +08006631 !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
6632 device = find_device(fs_devices, devid, uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006633 if (device)
6634 return device;
6635 }
Anand Jaine4319cd2019-01-17 23:32:31 +08006636 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006637 }
6638 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006639}
6640
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006641static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006642 u64 devid, u8 *dev_uuid)
6643{
6644 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006645
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006646 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6647 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006648 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006649
6650 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006651 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006652 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006653
Anand Jaine6e674b2017-12-04 12:54:54 +08006654 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05006655 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006656
Chris Masondfe25022008-05-13 13:46:40 -04006657 return device;
6658}
6659
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006660/**
6661 * btrfs_alloc_device - allocate struct btrfs_device
6662 * @fs_info: used only for generating a new devid, can be NULL if
6663 * devid is provided (i.e. @devid != NULL).
6664 * @devid: a pointer to devid for this device. If NULL a new devid
6665 * is generated.
6666 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6667 * is generated.
6668 *
6669 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006670 * on error. Returned struct is not linked onto any lists and must be
David Sterbaa425f9d2018-03-20 15:47:33 +01006671 * destroyed with btrfs_free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006672 */
6673struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6674 const u64 *devid,
6675 const u8 *uuid)
6676{
6677 struct btrfs_device *dev;
6678 u64 tmp;
6679
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306680 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006681 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006682
6683 dev = __alloc_device();
6684 if (IS_ERR(dev))
6685 return dev;
6686
6687 if (devid)
6688 tmp = *devid;
6689 else {
6690 int ret;
6691
6692 ret = find_next_devid(fs_info, &tmp);
6693 if (ret) {
David Sterbaa425f9d2018-03-20 15:47:33 +01006694 btrfs_free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006695 return ERR_PTR(ret);
6696 }
6697 }
6698 dev->devid = tmp;
6699
6700 if (uuid)
6701 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6702 else
6703 generate_random_uuid(dev->uuid);
6704
Liu Bo9e0af232014-08-15 23:36:53 +08006705 btrfs_init_work(&dev->work, btrfs_submit_helper,
6706 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006707
6708 return dev;
6709}
6710
Liu Boe06cd3d2016-06-03 12:05:15 -07006711/* Return -EIO if any error, otherwise return 0. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006712static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006713 struct extent_buffer *leaf,
6714 struct btrfs_chunk *chunk, u64 logical)
6715{
6716 u64 length;
6717 u64 stripe_len;
6718 u16 num_stripes;
6719 u16 sub_stripes;
6720 u64 type;
Gu Jinxiang315409b2018-07-04 18:16:39 +08006721 u64 features;
6722 bool mixed = false;
Liu Boe06cd3d2016-06-03 12:05:15 -07006723
6724 length = btrfs_chunk_length(leaf, chunk);
6725 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6726 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6727 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6728 type = btrfs_chunk_type(leaf, chunk);
6729
6730 if (!num_stripes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006731 btrfs_err(fs_info, "invalid chunk num_stripes: %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006732 num_stripes);
6733 return -EIO;
6734 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006735 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
6736 btrfs_err(fs_info, "invalid chunk logical %llu", logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006737 return -EIO;
6738 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006739 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
6740 btrfs_err(fs_info, "invalid chunk sectorsize %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006741 btrfs_chunk_sector_size(leaf, chunk));
6742 return -EIO;
6743 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006744 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
6745 btrfs_err(fs_info, "invalid chunk length %llu", length);
Liu Boe06cd3d2016-06-03 12:05:15 -07006746 return -EIO;
6747 }
6748 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006749 btrfs_err(fs_info, "invalid chunk stripe length: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006750 stripe_len);
6751 return -EIO;
6752 }
6753 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6754 type) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006755 btrfs_err(fs_info, "unrecognized chunk type: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006756 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6757 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6758 btrfs_chunk_type(leaf, chunk));
6759 return -EIO;
6760 }
Gu Jinxiang315409b2018-07-04 18:16:39 +08006761
6762 if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0) {
6763 btrfs_err(fs_info, "missing chunk type flag: 0x%llx", type);
6764 return -EIO;
6765 }
6766
6767 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) &&
6768 (type & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA))) {
6769 btrfs_err(fs_info,
6770 "system chunk with data or metadata type: 0x%llx", type);
6771 return -EIO;
6772 }
6773
6774 features = btrfs_super_incompat_flags(fs_info->super_copy);
6775 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
6776 mixed = true;
6777
6778 if (!mixed) {
6779 if ((type & BTRFS_BLOCK_GROUP_METADATA) &&
6780 (type & BTRFS_BLOCK_GROUP_DATA)) {
6781 btrfs_err(fs_info,
6782 "mixed chunk type in non-mixed mode: 0x%llx", type);
6783 return -EIO;
6784 }
6785 }
6786
Liu Boe06cd3d2016-06-03 12:05:15 -07006787 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
6788 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
6789 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
6790 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
6791 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
6792 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
6793 num_stripes != 1)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006794 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006795 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
6796 num_stripes, sub_stripes,
6797 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
6798 return -EIO;
6799 }
6800
6801 return 0;
6802}
6803
Anand Jain5a2b8e62017-10-09 11:07:45 +08006804static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006805 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006806{
Anand Jain2b902df2017-10-09 11:07:46 +08006807 if (error)
6808 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6809 devid, uuid);
6810 else
6811 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6812 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006813}
6814
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006815static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
Chris Mason0b86a832008-03-24 15:01:56 -04006816 struct extent_buffer *leaf,
6817 struct btrfs_chunk *chunk)
6818{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006819 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006820 struct map_lookup *map;
6821 struct extent_map *em;
6822 u64 logical;
6823 u64 length;
6824 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006825 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006826 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006827 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006828 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006829
Chris Masone17cade2008-04-15 15:41:47 -04006830 logical = key->offset;
6831 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006832 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006833
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006834 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006835 if (ret)
6836 return ret;
Chris Masona061fc82008-05-07 11:43:44 -04006837
Chris Mason890871b2009-09-02 16:24:52 -04006838 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006839 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006840 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006841
6842 /* already mapped? */
6843 if (em && em->start <= logical && em->start + em->len > logical) {
6844 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006845 return 0;
6846 } else if (em) {
6847 free_extent_map(em);
6848 }
Chris Mason0b86a832008-03-24 15:01:56 -04006849
David Sterba172ddd62011-04-21 00:48:27 +02006850 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006851 if (!em)
6852 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006853 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006854 if (!map) {
6855 free_extent_map(em);
6856 return -ENOMEM;
6857 }
6858
Wang Shilong298a8f92014-06-19 10:42:52 +08006859 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006860 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006861 em->start = logical;
6862 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006863 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006864 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006865 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006866
Chris Mason593060d2008-03-25 16:50:33 -04006867 map->num_stripes = num_stripes;
6868 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6869 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006870 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6871 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006872 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Qu Wenruocf90d882018-08-01 10:37:19 +08006873 map->verified_stripes = 0;
Chris Mason593060d2008-03-25 16:50:33 -04006874 for (i = 0; i < num_stripes; i++) {
6875 map->stripes[i].physical =
6876 btrfs_stripe_offset_nr(leaf, chunk, i);
6877 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006878 read_extent_buffer(leaf, uuid, (unsigned long)
6879 btrfs_stripe_dev_uuid_nr(chunk, i),
6880 BTRFS_UUID_SIZE);
Anand Jaine4319cd2019-01-17 23:32:31 +08006881 map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
6882 devid, uuid, NULL);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006883 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006884 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006885 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006886 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006887 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006888 }
Chris Masondfe25022008-05-13 13:46:40 -04006889 if (!map->stripes[i].dev) {
6890 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006891 add_missing_dev(fs_info->fs_devices, devid,
6892 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006893 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006894 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006895 btrfs_err(fs_info,
6896 "failed to init missing dev %llu: %ld",
6897 devid, PTR_ERR(map->stripes[i].dev));
6898 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006899 }
Anand Jain2b902df2017-10-09 11:07:46 +08006900 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006901 }
Anand Jaine12c9622017-12-04 12:54:53 +08006902 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6903 &(map->stripes[i].dev->dev_state));
6904
Chris Mason0b86a832008-03-24 15:01:56 -04006905 }
6906
Chris Mason890871b2009-09-02 16:24:52 -04006907 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006908 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006909 write_unlock(&map_tree->map_tree.lock);
Qu Wenruo64f64f42018-08-01 10:37:20 +08006910 if (ret < 0) {
6911 btrfs_err(fs_info,
6912 "failed to add chunk map, start=%llu len=%llu: %d",
6913 em->start, em->len, ret);
6914 }
Chris Mason0b86a832008-03-24 15:01:56 -04006915 free_extent_map(em);
6916
Qu Wenruo64f64f42018-08-01 10:37:20 +08006917 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006918}
6919
Jeff Mahoney143bede2012-03-01 14:56:26 +01006920static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006921 struct btrfs_dev_item *dev_item,
6922 struct btrfs_device *device)
6923{
6924 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006925
6926 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006927 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6928 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006929 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006930 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006931 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006932 device->type = btrfs_device_type(leaf, dev_item);
6933 device->io_align = btrfs_device_io_align(leaf, dev_item);
6934 device->io_width = btrfs_device_io_width(leaf, dev_item);
6935 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006936 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Anand Jain401e29c2017-12-04 12:54:55 +08006937 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Chris Mason0b86a832008-03-24 15:01:56 -04006938
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006939 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006940 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006941}
6942
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006943static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006944 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006945{
6946 struct btrfs_fs_devices *fs_devices;
6947 int ret;
6948
David Sterbaa32bf9a2018-03-16 02:21:22 +01006949 lockdep_assert_held(&uuid_mutex);
David Sterba2dfeca92017-06-14 02:48:07 +02006950 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006951
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006952 fs_devices = fs_info->fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006953 while (fs_devices) {
Anand Jain44880fd2017-07-29 17:50:09 +08006954 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006955 return fs_devices;
6956
Yan Zheng2b820322008-11-17 21:11:30 -05006957 fs_devices = fs_devices->seed;
6958 }
6959
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006960 fs_devices = find_fsid(fsid, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05006961 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006962 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006963 return ERR_PTR(-ENOENT);
6964
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006965 fs_devices = alloc_fs_devices(fsid, NULL);
Miao Xie5f375832014-09-03 21:35:46 +08006966 if (IS_ERR(fs_devices))
6967 return fs_devices;
6968
6969 fs_devices->seeding = 1;
6970 fs_devices->opened = 1;
6971 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006972 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006973
6974 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006975 if (IS_ERR(fs_devices))
6976 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006977
Anand Jain897fb572018-04-12 10:29:28 +08006978 ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006979 if (ret) {
6980 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006981 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006982 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006983 }
Yan Zheng2b820322008-11-17 21:11:30 -05006984
6985 if (!fs_devices->seeding) {
Anand Jain0226e0e2018-04-12 10:29:27 +08006986 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006987 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006988 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006989 goto out;
6990 }
6991
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006992 fs_devices->seed = fs_info->fs_devices->seed;
6993 fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006994out:
Miao Xie5f375832014-09-03 21:35:46 +08006995 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006996}
6997
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006998static int read_one_dev(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04006999 struct extent_buffer *leaf,
7000 struct btrfs_dev_item *dev_item)
7001{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007002 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04007003 struct btrfs_device *device;
7004 u64 devid;
7005 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08007006 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04007007 u8 dev_uuid[BTRFS_UUID_SIZE];
7008
Chris Mason0b86a832008-03-24 15:01:56 -04007009 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02007010 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04007011 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02007012 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08007013 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05007014
Nikolay Borisovde37aa52018-10-30 16:43:24 +02007015 if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007016 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08007017 if (IS_ERR(fs_devices))
7018 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05007019 }
7020
Anand Jaine4319cd2019-01-17 23:32:31 +08007021 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
7022 fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08007023 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08007024 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08007025 btrfs_report_missing_device(fs_info, devid,
7026 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08007027 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08007028 }
Yan Zheng2b820322008-11-17 21:11:30 -05007029
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007030 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08007031 if (IS_ERR(device)) {
7032 btrfs_err(fs_info,
7033 "failed to add missing dev %llu: %ld",
7034 devid, PTR_ERR(device));
7035 return PTR_ERR(device);
7036 }
Anand Jain2b902df2017-10-09 11:07:46 +08007037 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08007038 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08007039 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08007040 if (!btrfs_test_opt(fs_info, DEGRADED)) {
7041 btrfs_report_missing_device(fs_info,
7042 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08007043 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08007044 }
7045 btrfs_report_missing_device(fs_info, devid,
7046 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08007047 }
Miao Xie5f375832014-09-03 21:35:46 +08007048
Anand Jaine6e674b2017-12-04 12:54:54 +08007049 if (!device->bdev &&
7050 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05007051 /*
7052 * this happens when a device that was properly setup
7053 * in the device info lists suddenly goes bad.
7054 * device->bdev is NULL, and so we have to set
7055 * device->missing to one here
7056 */
Miao Xie5f375832014-09-03 21:35:46 +08007057 device->fs_devices->missing_devices++;
Anand Jaine6e674b2017-12-04 12:54:54 +08007058 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05007059 }
Miao Xie5f375832014-09-03 21:35:46 +08007060
7061 /* Move the device to its own fs_devices */
7062 if (device->fs_devices != fs_devices) {
Anand Jaine6e674b2017-12-04 12:54:54 +08007063 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
7064 &device->dev_state));
Miao Xie5f375832014-09-03 21:35:46 +08007065
7066 list_move(&device->dev_list, &fs_devices->devices);
7067 device->fs_devices->num_devices--;
7068 fs_devices->num_devices++;
7069
7070 device->fs_devices->missing_devices--;
7071 fs_devices->missing_devices++;
7072
7073 device->fs_devices = fs_devices;
7074 }
Yan Zheng2b820322008-11-17 21:11:30 -05007075 }
7076
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007077 if (device->fs_devices != fs_info->fs_devices) {
Anand Jainebbede42017-12-04 12:54:52 +08007078 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
Yan Zheng2b820322008-11-17 21:11:30 -05007079 if (device->generation !=
7080 btrfs_device_generation(leaf, dev_item))
7081 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04007082 }
Chris Mason0b86a832008-03-24 15:01:56 -04007083
7084 fill_device_from_item(leaf, dev_item, device);
Anand Jaine12c9622017-12-04 12:54:53 +08007085 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jainebbede42017-12-04 12:54:52 +08007086 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jain401e29c2017-12-04 12:54:55 +08007087 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05007088 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03007089 atomic64_add(device->total_bytes - device->bytes_used,
7090 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04007091 }
Chris Mason0b86a832008-03-24 15:01:56 -04007092 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007093 return ret;
7094}
7095
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007096int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04007097{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007098 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007099 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04007100 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04007101 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04007102 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01007103 u8 *array_ptr;
7104 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04007105 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007106 u32 num_stripes;
7107 u32 array_size;
7108 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01007109 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07007110 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04007111 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04007112
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007113 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02007114 /*
7115 * This will create extent buffer of nodesize, superblock size is
7116 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
7117 * overallocate but we can keep it as-is, only the first page is used.
7118 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007119 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07007120 if (IS_ERR(sb))
7121 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01007122 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04007123 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02007124 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04007125 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01007126 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02007127 * pages up-to-date when the page is larger: extent does not cover the
7128 * whole page and consequently check_page_uptodate does not find all
7129 * the page's extents up-to-date (the hole beyond sb),
7130 * write_extent_buffer then triggers a WARN_ON.
7131 *
7132 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
7133 * but sb spans only this function. Add an explicit SetPageUptodate call
7134 * to silence the warning eg. on PowerPC 64.
7135 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007136 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04007137 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05007138
Chris Masona061fc82008-05-07 11:43:44 -04007139 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04007140 array_size = btrfs_super_sys_array_size(super_copy);
7141
David Sterba1ffb22c2014-10-31 19:02:42 +01007142 array_ptr = super_copy->sys_chunk_array;
7143 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
7144 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007145
David Sterba1ffb22c2014-10-31 19:02:42 +01007146 while (cur_offset < array_size) {
7147 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01007148 len = sizeof(*disk_key);
7149 if (cur_offset + len > array_size)
7150 goto out_short_read;
7151
Chris Mason0b86a832008-03-24 15:01:56 -04007152 btrfs_disk_key_to_cpu(&key, disk_key);
7153
David Sterba1ffb22c2014-10-31 19:02:42 +01007154 array_ptr += len;
7155 sb_array_offset += len;
7156 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04007157
Chris Mason0d81ba52008-03-24 15:02:07 -04007158 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01007159 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01007160 /*
7161 * At least one btrfs_chunk with one stripe must be
7162 * present, exact stripe count check comes afterwards
7163 */
7164 len = btrfs_chunk_item_size(1);
7165 if (cur_offset + len > array_size)
7166 goto out_short_read;
7167
7168 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01007169 if (!num_stripes) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007170 btrfs_err(fs_info,
7171 "invalid number of stripes %u in sys_array at offset %u",
David Sterbaf5cdedd2015-11-30 17:27:06 +01007172 num_stripes, cur_offset);
7173 ret = -EIO;
7174 break;
7175 }
7176
Liu Boe06cd3d2016-06-03 12:05:15 -07007177 type = btrfs_chunk_type(sb, chunk);
7178 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007179 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07007180 "invalid chunk type %llu in sys_array at offset %u",
7181 type, cur_offset);
7182 ret = -EIO;
7183 break;
7184 }
7185
David Sterbae3540ea2014-11-05 15:24:51 +01007186 len = btrfs_chunk_item_size(num_stripes);
7187 if (cur_offset + len > array_size)
7188 goto out_short_read;
7189
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007190 ret = read_one_chunk(fs_info, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04007191 if (ret)
7192 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007193 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007194 btrfs_err(fs_info,
7195 "unexpected item type %u in sys_array at offset %u",
7196 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04007197 ret = -EIO;
7198 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007199 }
David Sterba1ffb22c2014-10-31 19:02:42 +01007200 array_ptr += len;
7201 sb_array_offset += len;
7202 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04007203 }
Liu Bod8651772016-06-03 17:41:42 -07007204 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07007205 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04007206 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01007207
7208out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007209 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01007210 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07007211 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07007212 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01007213 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04007214}
7215
Qu Wenruo21634a12017-03-09 09:34:36 +08007216/*
7217 * Check if all chunks in the fs are OK for read-write degraded mount
7218 *
Anand Jain6528b992017-12-18 17:08:59 +08007219 * If the @failing_dev is specified, it's accounted as missing.
7220 *
Qu Wenruo21634a12017-03-09 09:34:36 +08007221 * Return true if all chunks meet the minimal RW mount requirements.
7222 * Return false if any chunk doesn't meet the minimal RW mount requirements.
7223 */
Anand Jain6528b992017-12-18 17:08:59 +08007224bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
7225 struct btrfs_device *failing_dev)
Qu Wenruo21634a12017-03-09 09:34:36 +08007226{
7227 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
7228 struct extent_map *em;
7229 u64 next_start = 0;
7230 bool ret = true;
7231
7232 read_lock(&map_tree->map_tree.lock);
7233 em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1);
7234 read_unlock(&map_tree->map_tree.lock);
7235 /* No chunk at all? Return false anyway */
7236 if (!em) {
7237 ret = false;
7238 goto out;
7239 }
7240 while (em) {
7241 struct map_lookup *map;
7242 int missing = 0;
7243 int max_tolerated;
7244 int i;
7245
7246 map = em->map_lookup;
7247 max_tolerated =
7248 btrfs_get_num_tolerated_disk_barrier_failures(
7249 map->type);
7250 for (i = 0; i < map->num_stripes; i++) {
7251 struct btrfs_device *dev = map->stripes[i].dev;
7252
Anand Jaine6e674b2017-12-04 12:54:54 +08007253 if (!dev || !dev->bdev ||
7254 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
Qu Wenruo21634a12017-03-09 09:34:36 +08007255 dev->last_flush_error)
7256 missing++;
Anand Jain6528b992017-12-18 17:08:59 +08007257 else if (failing_dev && failing_dev == dev)
7258 missing++;
Qu Wenruo21634a12017-03-09 09:34:36 +08007259 }
7260 if (missing > max_tolerated) {
Anand Jain6528b992017-12-18 17:08:59 +08007261 if (!failing_dev)
7262 btrfs_warn(fs_info,
Andrea Gelmini52042d82018-11-28 12:05:13 +01007263 "chunk %llu missing %d devices, max tolerance is %d for writable mount",
Qu Wenruo21634a12017-03-09 09:34:36 +08007264 em->start, missing, max_tolerated);
7265 free_extent_map(em);
7266 ret = false;
7267 goto out;
7268 }
7269 next_start = extent_map_end(em);
7270 free_extent_map(em);
7271
7272 read_lock(&map_tree->map_tree.lock);
7273 em = lookup_extent_mapping(&map_tree->map_tree, next_start,
7274 (u64)(-1) - next_start);
7275 read_unlock(&map_tree->map_tree.lock);
7276 }
7277out:
7278 return ret;
7279}
7280
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007281int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04007282{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007283 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04007284 struct btrfs_path *path;
7285 struct extent_buffer *leaf;
7286 struct btrfs_key key;
7287 struct btrfs_key found_key;
7288 int ret;
7289 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007290 u64 total_dev = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007291
Chris Mason0b86a832008-03-24 15:01:56 -04007292 path = btrfs_alloc_path();
7293 if (!path)
7294 return -ENOMEM;
7295
Anand Jain3dd0f7a2018-04-12 10:29:32 +08007296 /*
7297 * uuid_mutex is needed only if we are mounting a sprout FS
7298 * otherwise we don't need it.
7299 */
Li Zefanb367e472011-12-07 11:38:24 +08007300 mutex_lock(&uuid_mutex);
David Sterba34441362016-10-04 19:34:27 +02007301 mutex_lock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007302
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007303 /*
7304 * Read all device items, and then all the chunk items. All
7305 * device items are found before any chunk item (their object id
7306 * is smaller than the lowest possible object id for a chunk
7307 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04007308 */
7309 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
7310 key.offset = 0;
7311 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007312 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00007313 if (ret < 0)
7314 goto error;
Chris Masond3977122009-01-05 21:25:51 -05007315 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007316 leaf = path->nodes[0];
7317 slot = path->slots[0];
7318 if (slot >= btrfs_header_nritems(leaf)) {
7319 ret = btrfs_next_leaf(root, path);
7320 if (ret == 0)
7321 continue;
7322 if (ret < 0)
7323 goto error;
7324 break;
7325 }
7326 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007327 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
7328 struct btrfs_dev_item *dev_item;
7329 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04007330 struct btrfs_dev_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007331 ret = read_one_dev(fs_info, leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007332 if (ret)
7333 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007334 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04007335 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
7336 struct btrfs_chunk *chunk;
7337 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007338 ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05007339 if (ret)
7340 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04007341 }
7342 path->slots[0]++;
7343 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07007344
7345 /*
7346 * After loading chunk tree, we've got all device information,
7347 * do another round of validation checks.
7348 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007349 if (total_dev != fs_info->fs_devices->total_devices) {
7350 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007351 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007352 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07007353 total_dev);
7354 ret = -EINVAL;
7355 goto error;
7356 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007357 if (btrfs_super_total_bytes(fs_info->super_copy) <
7358 fs_info->fs_devices->total_rw_bytes) {
7359 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007360 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007361 btrfs_super_total_bytes(fs_info->super_copy),
7362 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07007363 ret = -EINVAL;
7364 goto error;
7365 }
Chris Mason0b86a832008-03-24 15:01:56 -04007366 ret = 0;
7367error:
David Sterba34441362016-10-04 19:34:27 +02007368 mutex_unlock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007369 mutex_unlock(&uuid_mutex);
7370
Yan Zheng2b820322008-11-17 21:11:30 -05007371 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007372 return ret;
7373}
Stefan Behrens442a4f62012-05-25 16:06:08 +02007374
Miao Xiecb517ea2013-05-15 07:48:19 +00007375void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7376{
7377 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7378 struct btrfs_device *device;
7379
Liu Bo29cc83f2014-05-11 23:14:59 +08007380 while (fs_devices) {
7381 mutex_lock(&fs_devices->device_list_mutex);
7382 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007383 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007384 mutex_unlock(&fs_devices->device_list_mutex);
7385
7386 fs_devices = fs_devices->seed;
7387 }
Miao Xiecb517ea2013-05-15 07:48:19 +00007388}
7389
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007390static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
7391{
7392 int i;
7393
7394 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7395 btrfs_dev_stat_reset(dev, i);
7396}
7397
7398int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7399{
7400 struct btrfs_key key;
7401 struct btrfs_key found_key;
7402 struct btrfs_root *dev_root = fs_info->dev_root;
7403 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7404 struct extent_buffer *eb;
7405 int slot;
7406 int ret = 0;
7407 struct btrfs_device *device;
7408 struct btrfs_path *path = NULL;
7409 int i;
7410
7411 path = btrfs_alloc_path();
7412 if (!path) {
7413 ret = -ENOMEM;
7414 goto out;
7415 }
7416
7417 mutex_lock(&fs_devices->device_list_mutex);
7418 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7419 int item_size;
7420 struct btrfs_dev_stats_item *ptr;
7421
David Sterba242e2952016-01-25 17:51:31 +01007422 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7423 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007424 key.offset = device->devid;
7425 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7426 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007427 __btrfs_reset_dev_stats(device);
7428 device->dev_stats_valid = 1;
7429 btrfs_release_path(path);
7430 continue;
7431 }
7432 slot = path->slots[0];
7433 eb = path->nodes[0];
7434 btrfs_item_key_to_cpu(eb, &found_key, slot);
7435 item_size = btrfs_item_size_nr(eb, slot);
7436
7437 ptr = btrfs_item_ptr(eb, slot,
7438 struct btrfs_dev_stats_item);
7439
7440 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7441 if (item_size >= (1 + i) * sizeof(__le64))
7442 btrfs_dev_stat_set(device, i,
7443 btrfs_dev_stats_value(eb, ptr, i));
7444 else
7445 btrfs_dev_stat_reset(device, i);
7446 }
7447
7448 device->dev_stats_valid = 1;
7449 btrfs_dev_stat_print_on_load(device);
7450 btrfs_release_path(path);
7451 }
7452 mutex_unlock(&fs_devices->device_list_mutex);
7453
7454out:
7455 btrfs_free_path(path);
7456 return ret < 0 ? ret : 0;
7457}
7458
7459static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007460 struct btrfs_device *device)
7461{
Nikolay Borisov5495f192018-07-20 19:37:49 +03007462 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007463 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007464 struct btrfs_path *path;
7465 struct btrfs_key key;
7466 struct extent_buffer *eb;
7467 struct btrfs_dev_stats_item *ptr;
7468 int ret;
7469 int i;
7470
David Sterba242e2952016-01-25 17:51:31 +01007471 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7472 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007473 key.offset = device->devid;
7474
7475 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007476 if (!path)
7477 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007478 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7479 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007480 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007481 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007482 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007483 goto out;
7484 }
7485
7486 if (ret == 0 &&
7487 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7488 /* need to delete old one and insert a new one */
7489 ret = btrfs_del_item(trans, dev_root, path);
7490 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007491 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007492 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007493 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007494 goto out;
7495 }
7496 ret = 1;
7497 }
7498
7499 if (ret == 1) {
7500 /* need to insert a new item */
7501 btrfs_release_path(path);
7502 ret = btrfs_insert_empty_item(trans, dev_root, path,
7503 &key, sizeof(*ptr));
7504 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007505 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007506 "insert dev_stats item for device %s failed %d",
7507 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007508 goto out;
7509 }
7510 }
7511
7512 eb = path->nodes[0];
7513 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7514 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7515 btrfs_set_dev_stats_value(eb, ptr, i,
7516 btrfs_dev_stat_read(device, i));
7517 btrfs_mark_buffer_dirty(eb);
7518
7519out:
7520 btrfs_free_path(path);
7521 return ret;
7522}
7523
7524/*
7525 * called from commit_transaction. Writes all changed device stats to disk.
7526 */
7527int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
7528 struct btrfs_fs_info *fs_info)
7529{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007530 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7531 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007532 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007533 int ret = 0;
7534
7535 mutex_lock(&fs_devices->device_list_mutex);
7536 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007537 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7538 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007539 continue;
7540
Nikolay Borisov9deae962017-10-24 13:47:37 +03007541
7542 /*
7543 * There is a LOAD-LOAD control dependency between the value of
7544 * dev_stats_ccnt and updating the on-disk values which requires
7545 * reading the in-memory counters. Such control dependencies
7546 * require explicit read memory barriers.
7547 *
7548 * This memory barriers pairs with smp_mb__before_atomic in
7549 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7550 * barrier implied by atomic_xchg in
7551 * btrfs_dev_stats_read_and_reset
7552 */
7553 smp_rmb();
7554
Nikolay Borisov5495f192018-07-20 19:37:49 +03007555 ret = update_dev_stat_item(trans, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007556 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007557 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007558 }
7559 mutex_unlock(&fs_devices->device_list_mutex);
7560
7561 return ret;
7562}
7563
Stefan Behrens442a4f62012-05-25 16:06:08 +02007564void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7565{
7566 btrfs_dev_stat_inc(dev, index);
7567 btrfs_dev_stat_print_on_error(dev);
7568}
7569
Eric Sandeen48a3b632013-04-25 20:41:01 +00007570static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007571{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007572 if (!dev->dev_stats_valid)
7573 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007574 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007575 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007576 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007577 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7578 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7579 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007580 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7581 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007582}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007583
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007584static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7585{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007586 int i;
7587
7588 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7589 if (btrfs_dev_stat_read(dev, i) != 0)
7590 break;
7591 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7592 return; /* all values == 0, suppress message */
7593
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007594 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007595 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007596 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007597 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7598 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7599 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7600 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7601 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7602}
7603
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007604int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007605 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007606{
7607 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007608 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007609 int i;
7610
7611 mutex_lock(&fs_devices->device_list_mutex);
Anand Jaine4319cd2019-01-17 23:32:31 +08007612 dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007613 mutex_unlock(&fs_devices->device_list_mutex);
7614
7615 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007616 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007617 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007618 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007619 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007620 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007621 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007622 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7623 if (stats->nr_items > i)
7624 stats->values[i] =
7625 btrfs_dev_stat_read_and_reset(dev, i);
7626 else
7627 btrfs_dev_stat_reset(dev, i);
7628 }
7629 } else {
7630 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7631 if (stats->nr_items > i)
7632 stats->values[i] = btrfs_dev_stat_read(dev, i);
7633 }
7634 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7635 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7636 return 0;
7637}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007638
David Sterbada353f62017-02-14 17:55:53 +01007639void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007640{
7641 struct buffer_head *bh;
7642 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08007643 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007644
Anand Jain12b1c262015-08-14 18:32:59 +08007645 if (!bdev)
7646 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007647
Anand Jain12b1c262015-08-14 18:32:59 +08007648 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7649 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007650
Anand Jain12b1c262015-08-14 18:32:59 +08007651 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7652 continue;
7653
7654 disk_super = (struct btrfs_super_block *)bh->b_data;
7655
7656 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7657 set_buffer_dirty(bh);
7658 sync_dirty_buffer(bh);
7659 brelse(bh);
7660 }
7661
7662 /* Notify udev that device has changed */
7663 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7664
7665 /* Update ctime/mtime for device path for libblkid */
7666 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007667}
Miao Xie935e5cc2014-09-03 21:35:33 +08007668
7669/*
7670 * Update the size of all devices, which is used for writing out the
7671 * super blocks.
7672 */
7673void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
7674{
7675 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7676 struct btrfs_device *curr, *next;
7677
7678 if (list_empty(&fs_devices->resized_devices))
7679 return;
7680
7681 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02007682 mutex_lock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007683 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
7684 resized_list) {
7685 list_del_init(&curr->resized_list);
7686 curr->commit_total_bytes = curr->disk_total_bytes;
7687 }
David Sterba34441362016-10-04 19:34:27 +02007688 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007689 mutex_unlock(&fs_devices->device_list_mutex);
7690}
Miao Xiece7213c2014-09-03 21:35:34 +08007691
7692/* Must be invoked during the transaction commit */
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007693void btrfs_update_commit_device_bytes_used(struct btrfs_transaction *trans)
Miao Xiece7213c2014-09-03 21:35:34 +08007694{
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007695 struct btrfs_fs_info *fs_info = trans->fs_info;
Miao Xiece7213c2014-09-03 21:35:34 +08007696 struct extent_map *em;
7697 struct map_lookup *map;
7698 struct btrfs_device *dev;
7699 int i;
7700
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007701 if (list_empty(&trans->pending_chunks))
Miao Xiece7213c2014-09-03 21:35:34 +08007702 return;
7703
7704 /* In order to kick the device replace finish process */
David Sterba34441362016-10-04 19:34:27 +02007705 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007706 list_for_each_entry(em, &trans->pending_chunks, list) {
Jeff Mahoney95617d62015-06-03 10:55:48 -04007707 map = em->map_lookup;
Miao Xiece7213c2014-09-03 21:35:34 +08007708
7709 for (i = 0; i < map->num_stripes; i++) {
7710 dev = map->stripes[i].dev;
7711 dev->commit_bytes_used = dev->bytes_used;
7712 }
7713 }
David Sterba34441362016-10-04 19:34:27 +02007714 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007715}
Anand Jain5a13f432015-03-10 06:38:31 +08007716
7717void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7718{
7719 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7720 while (fs_devices) {
7721 fs_devices->fs_info = fs_info;
7722 fs_devices = fs_devices->seed;
7723 }
7724}
7725
7726void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7727{
7728 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7729 while (fs_devices) {
7730 fs_devices->fs_info = NULL;
7731 fs_devices = fs_devices->seed;
7732 }
7733}
David Sterba46df06b2018-07-13 20:46:30 +02007734
7735/*
7736 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7737 */
7738int btrfs_bg_type_to_factor(u64 flags)
7739{
7740 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
7741 BTRFS_BLOCK_GROUP_RAID10))
7742 return 2;
7743 return 1;
7744}
Qu Wenruocf90d882018-08-01 10:37:19 +08007745
7746
7747static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes)
7748{
7749 int index = btrfs_bg_flags_to_raid_index(type);
7750 int ncopies = btrfs_raid_array[index].ncopies;
7751 int data_stripes;
7752
7753 switch (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
7754 case BTRFS_BLOCK_GROUP_RAID5:
7755 data_stripes = num_stripes - 1;
7756 break;
7757 case BTRFS_BLOCK_GROUP_RAID6:
7758 data_stripes = num_stripes - 2;
7759 break;
7760 default:
7761 data_stripes = num_stripes / ncopies;
7762 break;
7763 }
7764 return div_u64(chunk_len, data_stripes);
7765}
7766
7767static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
7768 u64 chunk_offset, u64 devid,
7769 u64 physical_offset, u64 physical_len)
7770{
7771 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
7772 struct extent_map *em;
7773 struct map_lookup *map;
Qu Wenruo05a37c42018-10-05 17:45:55 +08007774 struct btrfs_device *dev;
Qu Wenruocf90d882018-08-01 10:37:19 +08007775 u64 stripe_len;
7776 bool found = false;
7777 int ret = 0;
7778 int i;
7779
7780 read_lock(&em_tree->lock);
7781 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
7782 read_unlock(&em_tree->lock);
7783
7784 if (!em) {
7785 btrfs_err(fs_info,
7786"dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7787 physical_offset, devid);
7788 ret = -EUCLEAN;
7789 goto out;
7790 }
7791
7792 map = em->map_lookup;
7793 stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes);
7794 if (physical_len != stripe_len) {
7795 btrfs_err(fs_info,
7796"dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7797 physical_offset, devid, em->start, physical_len,
7798 stripe_len);
7799 ret = -EUCLEAN;
7800 goto out;
7801 }
7802
7803 for (i = 0; i < map->num_stripes; i++) {
7804 if (map->stripes[i].dev->devid == devid &&
7805 map->stripes[i].physical == physical_offset) {
7806 found = true;
7807 if (map->verified_stripes >= map->num_stripes) {
7808 btrfs_err(fs_info,
7809 "too many dev extents for chunk %llu found",
7810 em->start);
7811 ret = -EUCLEAN;
7812 goto out;
7813 }
7814 map->verified_stripes++;
7815 break;
7816 }
7817 }
7818 if (!found) {
7819 btrfs_err(fs_info,
7820 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7821 physical_offset, devid);
7822 ret = -EUCLEAN;
7823 }
Qu Wenruo05a37c42018-10-05 17:45:55 +08007824
7825 /* Make sure no dev extent is beyond device bondary */
Anand Jaine4319cd2019-01-17 23:32:31 +08007826 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
Qu Wenruo05a37c42018-10-05 17:45:55 +08007827 if (!dev) {
7828 btrfs_err(fs_info, "failed to find devid %llu", devid);
7829 ret = -EUCLEAN;
7830 goto out;
7831 }
Qu Wenruo1b3922a2019-01-08 14:08:18 +08007832
7833 /* It's possible this device is a dummy for seed device */
7834 if (dev->disk_total_bytes == 0) {
7835 dev = find_device(fs_info->fs_devices->seed, devid, NULL);
7836 if (!dev) {
7837 btrfs_err(fs_info, "failed to find seed devid %llu",
7838 devid);
7839 ret = -EUCLEAN;
7840 goto out;
7841 }
7842 }
7843
Qu Wenruo05a37c42018-10-05 17:45:55 +08007844 if (physical_offset + physical_len > dev->disk_total_bytes) {
7845 btrfs_err(fs_info,
7846"dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7847 devid, physical_offset, physical_len,
7848 dev->disk_total_bytes);
7849 ret = -EUCLEAN;
7850 goto out;
7851 }
Qu Wenruocf90d882018-08-01 10:37:19 +08007852out:
7853 free_extent_map(em);
7854 return ret;
7855}
7856
7857static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)
7858{
7859 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
7860 struct extent_map *em;
7861 struct rb_node *node;
7862 int ret = 0;
7863
7864 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08007865 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
Qu Wenruocf90d882018-08-01 10:37:19 +08007866 em = rb_entry(node, struct extent_map, rb_node);
7867 if (em->map_lookup->num_stripes !=
7868 em->map_lookup->verified_stripes) {
7869 btrfs_err(fs_info,
7870 "chunk %llu has missing dev extent, have %d expect %d",
7871 em->start, em->map_lookup->verified_stripes,
7872 em->map_lookup->num_stripes);
7873 ret = -EUCLEAN;
7874 goto out;
7875 }
7876 }
7877out:
7878 read_unlock(&em_tree->lock);
7879 return ret;
7880}
7881
7882/*
7883 * Ensure that all dev extents are mapped to correct chunk, otherwise
7884 * later chunk allocation/free would cause unexpected behavior.
7885 *
7886 * NOTE: This will iterate through the whole device tree, which should be of
7887 * the same size level as the chunk tree. This slightly increases mount time.
7888 */
7889int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info)
7890{
7891 struct btrfs_path *path;
7892 struct btrfs_root *root = fs_info->dev_root;
7893 struct btrfs_key key;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007894 u64 prev_devid = 0;
7895 u64 prev_dev_ext_end = 0;
Qu Wenruocf90d882018-08-01 10:37:19 +08007896 int ret = 0;
7897
7898 key.objectid = 1;
7899 key.type = BTRFS_DEV_EXTENT_KEY;
7900 key.offset = 0;
7901
7902 path = btrfs_alloc_path();
7903 if (!path)
7904 return -ENOMEM;
7905
7906 path->reada = READA_FORWARD;
7907 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7908 if (ret < 0)
7909 goto out;
7910
7911 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7912 ret = btrfs_next_item(root, path);
7913 if (ret < 0)
7914 goto out;
7915 /* No dev extents at all? Not good */
7916 if (ret > 0) {
7917 ret = -EUCLEAN;
7918 goto out;
7919 }
7920 }
7921 while (1) {
7922 struct extent_buffer *leaf = path->nodes[0];
7923 struct btrfs_dev_extent *dext;
7924 int slot = path->slots[0];
7925 u64 chunk_offset;
7926 u64 physical_offset;
7927 u64 physical_len;
7928 u64 devid;
7929
7930 btrfs_item_key_to_cpu(leaf, &key, slot);
7931 if (key.type != BTRFS_DEV_EXTENT_KEY)
7932 break;
7933 devid = key.objectid;
7934 physical_offset = key.offset;
7935
7936 dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
7937 chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext);
7938 physical_len = btrfs_dev_extent_length(leaf, dext);
7939
Qu Wenruo5eb19382018-10-05 17:45:54 +08007940 /* Check if this dev extent overlaps with the previous one */
7941 if (devid == prev_devid && physical_offset < prev_dev_ext_end) {
7942 btrfs_err(fs_info,
7943"dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
7944 devid, physical_offset, prev_dev_ext_end);
7945 ret = -EUCLEAN;
7946 goto out;
7947 }
7948
Qu Wenruocf90d882018-08-01 10:37:19 +08007949 ret = verify_one_dev_extent(fs_info, chunk_offset, devid,
7950 physical_offset, physical_len);
7951 if (ret < 0)
7952 goto out;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007953 prev_devid = devid;
7954 prev_dev_ext_end = physical_offset + physical_len;
7955
Qu Wenruocf90d882018-08-01 10:37:19 +08007956 ret = btrfs_next_item(root, path);
7957 if (ret < 0)
7958 goto out;
7959 if (ret > 0) {
7960 ret = 0;
7961 break;
7962 }
7963 }
7964
7965 /* Ensure all chunks have corresponding dev extents */
7966 ret = verify_chunk_dev_extent_mapping(fs_info);
7967out:
7968 btrfs_free_path(path);
7969 return ret;
7970}
Omar Sandovaleede2bf2016-11-03 10:28:12 -07007971
7972/*
7973 * Check whether the given block group or device is pinned by any inode being
7974 * used as a swapfile.
7975 */
7976bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr)
7977{
7978 struct btrfs_swapfile_pin *sp;
7979 struct rb_node *node;
7980
7981 spin_lock(&fs_info->swapfile_pins_lock);
7982 node = fs_info->swapfile_pins.rb_node;
7983 while (node) {
7984 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
7985 if (ptr < sp->ptr)
7986 node = node->rb_left;
7987 else if (ptr > sp->ptr)
7988 node = node->rb_right;
7989 else
7990 break;
7991 }
7992 spin_unlock(&fs_info->swapfile_pins_lock);
7993 return node != NULL;
7994}