blob: db934ceae9c109f39eade85457a25b8ce99604ae [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
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200418static noinline struct btrfs_fs_devices *find_fsid(
419 const u8 *fsid, const u8 *metadata_fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400420{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400421 struct btrfs_fs_devices *fs_devices;
422
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200423 ASSERT(fsid);
424
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200425 if (metadata_fsid) {
426 /*
427 * Handle scanned device having completed its fsid change but
428 * belonging to a fs_devices that was created by first scanning
429 * a device which didn't have its fsid/metadata_uuid changed
430 * at all and the CHANGING_FSID_V2 flag set.
431 */
432 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
433 if (fs_devices->fsid_change &&
434 memcmp(metadata_fsid, fs_devices->fsid,
435 BTRFS_FSID_SIZE) == 0 &&
436 memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
437 BTRFS_FSID_SIZE) == 0) {
438 return fs_devices;
439 }
440 }
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200441 /*
442 * Handle scanned device having completed its fsid change but
443 * belonging to a fs_devices that was created by a device that
444 * has an outdated pair of fsid/metadata_uuid and
445 * CHANGING_FSID_V2 flag set.
446 */
447 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
448 if (fs_devices->fsid_change &&
449 memcmp(fs_devices->metadata_uuid,
450 fs_devices->fsid, BTRFS_FSID_SIZE) != 0 &&
451 memcmp(metadata_fsid, fs_devices->metadata_uuid,
452 BTRFS_FSID_SIZE) == 0) {
453 return fs_devices;
454 }
455 }
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200456 }
457
458 /* Handle non-split brain cases */
Anand Jainc4babc52018-04-12 10:29:25 +0800459 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200460 if (metadata_fsid) {
461 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0
462 && memcmp(metadata_fsid, fs_devices->metadata_uuid,
463 BTRFS_FSID_SIZE) == 0)
464 return fs_devices;
465 } else {
466 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
467 return fs_devices;
468 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400469 }
470 return NULL;
471}
472
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100473static int
474btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
475 int flush, struct block_device **bdev,
476 struct buffer_head **bh)
477{
478 int ret;
479
480 *bdev = blkdev_get_by_path(device_path, flags, holder);
481
482 if (IS_ERR(*bdev)) {
483 ret = PTR_ERR(*bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100484 goto error;
485 }
486
487 if (flush)
488 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
David Sterba9f6d2512017-06-16 01:48:05 +0200489 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100490 if (ret) {
491 blkdev_put(*bdev, flags);
492 goto error;
493 }
494 invalidate_bdev(*bdev);
495 *bh = btrfs_read_dev_super(*bdev);
Anand Jain92fc03f2015-08-14 18:32:51 +0800496 if (IS_ERR(*bh)) {
497 ret = PTR_ERR(*bh);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100498 blkdev_put(*bdev, flags);
499 goto error;
500 }
501
502 return 0;
503
504error:
505 *bdev = NULL;
506 *bh = NULL;
507 return ret;
508}
509
Chris Masonffbd5172009-04-20 15:50:09 -0400510static void requeue_list(struct btrfs_pending_bios *pending_bios,
511 struct bio *head, struct bio *tail)
512{
513
514 struct bio *old_head;
515
516 old_head = pending_bios->head;
517 pending_bios->head = head;
518 if (pending_bios->tail)
519 tail->bi_next = old_head;
520 else
521 pending_bios->tail = tail;
522}
523
Chris Mason8b712842008-06-11 16:50:36 -0400524/*
525 * we try to collect pending bios for a device so we don't get a large
526 * number of procs sending bios down to the same device. This greatly
527 * improves the schedulers ability to collect and merge the bios.
528 *
529 * But, it also turns into a long list of bios to process and that is sure
530 * to eventually make the worker thread block. The solution here is to
531 * make some progress and then put this work struct back at the end of
532 * the list if the block device is congested. This way, multiple devices
533 * can make progress from a single worker thread.
534 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100535static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400536{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400537 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -0400538 struct bio *pending;
539 struct backing_dev_info *bdi;
Chris Masonffbd5172009-04-20 15:50:09 -0400540 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400541 struct bio *tail;
542 struct bio *cur;
543 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400544 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400545 unsigned long batch_run = 0;
Chris Masonb765ead2009-04-03 10:27:10 -0400546 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400547 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000548 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400549 struct blk_plug plug;
550
551 /*
552 * this function runs all the bios we've collected for
553 * a particular device. We don't want to wander off to
554 * another device without first sending all of these down.
555 * So, setup a plug here and finish it off before we return
556 */
557 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400558
Jan Karaefa7c9f2017-02-02 15:56:53 +0100559 bdi = device->bdev->bd_bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400560
Chris Mason8b712842008-06-11 16:50:36 -0400561loop:
562 spin_lock(&device->io_lock);
563
Chris Masona6837052009-02-04 09:19:41 -0500564loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400565 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400566
Chris Mason8b712842008-06-11 16:50:36 -0400567 /* take all the bios off the list at once and process them
568 * later on (without the lock held). But, remember the
569 * tail and other pointers so the bios can be properly reinserted
570 * into the list if we hit congestion
571 */
Chris Masond84275c2009-06-09 15:39:08 -0400572 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400573 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400574 force_reg = 1;
575 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400576 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400577 force_reg = 0;
578 }
Chris Masonffbd5172009-04-20 15:50:09 -0400579
580 pending = pending_bios->head;
581 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400582 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400583
584 /*
585 * if pending was null this time around, no bios need processing
586 * at all and we can stop. Otherwise it'll loop back up again
587 * and do an additional check so no bios are missed.
588 *
589 * device->running_pending is used to synchronize with the
590 * schedule_bio code.
591 */
Chris Masonffbd5172009-04-20 15:50:09 -0400592 if (device->pending_sync_bios.head == NULL &&
593 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400594 again = 0;
595 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400596 } else {
597 again = 1;
598 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400599 }
Chris Masonffbd5172009-04-20 15:50:09 -0400600
601 pending_bios->head = NULL;
602 pending_bios->tail = NULL;
603
Chris Mason8b712842008-06-11 16:50:36 -0400604 spin_unlock(&device->io_lock);
605
Chris Masond3977122009-01-05 21:25:51 -0500606 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400607
608 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400609 /* we want to work on both lists, but do more bios on the
610 * sync list than the regular list
611 */
612 if ((num_run > 32 &&
613 pending_bios != &device->pending_sync_bios &&
614 device->pending_sync_bios.head) ||
615 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
616 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400617 spin_lock(&device->io_lock);
618 requeue_list(pending_bios, pending, tail);
619 goto loop_lock;
620 }
621
Chris Mason8b712842008-06-11 16:50:36 -0400622 cur = pending;
623 pending = pending->bi_next;
624 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400625
Jens Axboedac56212015-04-17 16:23:59 -0600626 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400627
Chris Mason2ab1ba62011-08-04 14:28:36 -0400628 /*
629 * if we're doing the sync list, record that our
630 * plug has some sync requests on it
631 *
632 * If we're doing the regular list and there are
633 * sync requests sitting around, unplug before
634 * we add more
635 */
636 if (pending_bios == &device->pending_sync_bios) {
637 sync_pending = 1;
638 } else if (sync_pending) {
639 blk_finish_plug(&plug);
640 blk_start_plug(&plug);
641 sync_pending = 0;
642 }
643
Mike Christie4e49ea42016-06-05 14:31:41 -0500644 btrfsic_submit_bio(cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400645 num_run++;
646 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100647
648 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400649
650 /*
651 * we made progress, there is more work to do and the bdi
652 * is now congested. Back off and let other work structs
653 * run instead
654 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400655 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500656 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400657 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400658
Chris Masonb765ead2009-04-03 10:27:10 -0400659 ioc = current->io_context;
660
661 /*
662 * the main goal here is that we don't want to
663 * block if we're going to be able to submit
664 * more requests without blocking.
665 *
666 * This code does two great things, it pokes into
667 * the elevator code from a filesystem _and_
668 * it makes assumptions about how batching works.
669 */
670 if (ioc && ioc->nr_batch_requests > 0 &&
671 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
672 (last_waited == 0 ||
673 ioc->last_waited == last_waited)) {
674 /*
675 * we want to go through our batch of
676 * requests and stop. So, we copy out
677 * the ioc->last_waited time and test
678 * against it before looping
679 */
680 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100681 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400682 continue;
683 }
Chris Mason8b712842008-06-11 16:50:36 -0400684 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400685 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500686 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400687
688 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800689 btrfs_queue_work(fs_info->submit_workers,
690 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400691 goto done;
692 }
693 }
Chris Masonffbd5172009-04-20 15:50:09 -0400694
Chris Mason51684082010-03-10 15:33:32 -0500695 cond_resched();
696 if (again)
697 goto loop;
698
699 spin_lock(&device->io_lock);
700 if (device->pending_bios.head || device->pending_sync_bios.head)
701 goto loop_lock;
702 spin_unlock(&device->io_lock);
703
Chris Mason8b712842008-06-11 16:50:36 -0400704done:
Chris Mason211588a2011-04-19 20:12:40 -0400705 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400706}
707
Christoph Hellwigb2950862008-12-02 09:54:17 -0500708static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400709{
710 struct btrfs_device *device;
711
712 device = container_of(work, struct btrfs_device, work);
713 run_scheduled_bios(device);
714}
715
Anand Jain70bc7082019-01-04 13:31:53 +0800716static bool device_path_matched(const char *path, struct btrfs_device *device)
717{
718 int found;
719
720 rcu_read_lock();
721 found = strcmp(rcu_str_deref(device->name), path);
722 rcu_read_unlock();
723
724 return found == 0;
725}
726
Anand Jaind8367db2018-01-18 22:00:37 +0800727/*
728 * Search and remove all stale (devices which are not mounted) devices.
729 * When both inputs are NULL, it will search and release all stale devices.
730 * path: Optional. When provided will it release all unmounted devices
731 * matching this path only.
732 * skip_dev: Optional. Will skip this device when searching for the stale
733 * devices.
Anand Jain70bc7082019-01-04 13:31:53 +0800734 * Return: 0 for success or if @path is NULL.
735 * -EBUSY if @path is a mounted device.
736 * -ENOENT if @path does not match any device in the list.
Anand Jaind8367db2018-01-18 22:00:37 +0800737 */
Anand Jain70bc7082019-01-04 13:31:53 +0800738static int btrfs_free_stale_devices(const char *path,
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800739 struct btrfs_device *skip_device)
Anand Jain4fde46f2015-06-17 21:10:48 +0800740{
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800741 struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
742 struct btrfs_device *device, *tmp_device;
Anand Jain70bc7082019-01-04 13:31:53 +0800743 int ret = 0;
744
745 if (path)
746 ret = -ENOENT;
Anand Jain4fde46f2015-06-17 21:10:48 +0800747
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800748 list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
Anand Jain4fde46f2015-06-17 21:10:48 +0800749
Anand Jain70bc7082019-01-04 13:31:53 +0800750 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800751 list_for_each_entry_safe(device, tmp_device,
752 &fs_devices->devices, dev_list) {
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800753 if (skip_device && skip_device == device)
Anand Jaind8367db2018-01-18 22:00:37 +0800754 continue;
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800755 if (path && !device->name)
Anand Jain4fde46f2015-06-17 21:10:48 +0800756 continue;
Anand Jain70bc7082019-01-04 13:31:53 +0800757 if (path && !device_path_matched(path, device))
Anand Jain38cf6652018-01-18 22:00:34 +0800758 continue;
Anand Jain70bc7082019-01-04 13:31:53 +0800759 if (fs_devices->opened) {
760 /* for an already deleted device return 0 */
761 if (path && ret != 0)
762 ret = -EBUSY;
763 break;
764 }
Anand Jain4fde46f2015-06-17 21:10:48 +0800765
Anand Jain4fde46f2015-06-17 21:10:48 +0800766 /* delete the stale device */
Anand Jain7bcb8162018-05-29 17:23:20 +0800767 fs_devices->num_devices--;
768 list_del(&device->dev_list);
769 btrfs_free_device(device);
770
Anand Jain70bc7082019-01-04 13:31:53 +0800771 ret = 0;
Anand Jain7bcb8162018-05-29 17:23:20 +0800772 if (fs_devices->num_devices == 0)
Nikolay Borisovfd649f12018-01-30 16:07:37 +0200773 break;
Anand Jain7bcb8162018-05-29 17:23:20 +0800774 }
775 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain70bc7082019-01-04 13:31:53 +0800776
Anand Jain7bcb8162018-05-29 17:23:20 +0800777 if (fs_devices->num_devices == 0) {
778 btrfs_sysfs_remove_fsid(fs_devices);
779 list_del(&fs_devices->fs_list);
780 free_fs_devices(fs_devices);
Anand Jain4fde46f2015-06-17 21:10:48 +0800781 }
782 }
Anand Jain70bc7082019-01-04 13:31:53 +0800783
784 return ret;
Anand Jain4fde46f2015-06-17 21:10:48 +0800785}
786
Anand Jain0fb08bc2017-11-09 23:45:24 +0800787static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
788 struct btrfs_device *device, fmode_t flags,
789 void *holder)
790{
791 struct request_queue *q;
792 struct block_device *bdev;
793 struct buffer_head *bh;
794 struct btrfs_super_block *disk_super;
795 u64 devid;
796 int ret;
797
798 if (device->bdev)
799 return -EINVAL;
800 if (!device->name)
801 return -EINVAL;
802
803 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
804 &bdev, &bh);
805 if (ret)
806 return ret;
807
808 disk_super = (struct btrfs_super_block *)bh->b_data;
809 devid = btrfs_stack_device_id(&disk_super->dev_item);
810 if (devid != device->devid)
811 goto error_brelse;
812
813 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
814 goto error_brelse;
815
816 device->generation = btrfs_super_generation(disk_super);
817
818 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200819 if (btrfs_super_incompat_flags(disk_super) &
820 BTRFS_FEATURE_INCOMPAT_METADATA_UUID) {
821 pr_err(
822 "BTRFS: Invalid seeding and uuid-changed device detected\n");
823 goto error_brelse;
824 }
825
Anand Jainebbede42017-12-04 12:54:52 +0800826 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800827 fs_devices->seeding = 1;
828 } else {
Anand Jainebbede42017-12-04 12:54:52 +0800829 if (bdev_read_only(bdev))
830 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
831 else
832 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800833 }
834
835 q = bdev_get_queue(bdev);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800836 if (!blk_queue_nonrot(q))
837 fs_devices->rotating = 1;
838
839 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +0800840 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800841 device->mode = flags;
842
843 fs_devices->open_devices++;
Anand Jainebbede42017-12-04 12:54:52 +0800844 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
845 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Anand Jain0fb08bc2017-11-09 23:45:24 +0800846 fs_devices->rw_devices++;
Anand Jainb1b8e382018-01-22 14:49:37 -0800847 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800848 }
849 brelse(bh);
850
851 return 0;
852
853error_brelse:
854 brelse(bh);
855 blkdev_put(bdev, flags);
856
857 return -EINVAL;
858}
859
David Sterba60999ca2014-03-26 18:26:36 +0100860/*
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200861 * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices
862 * being created with a disk that has already completed its fsid change.
863 */
864static struct btrfs_fs_devices *find_fsid_inprogress(
865 struct btrfs_super_block *disk_super)
866{
867 struct btrfs_fs_devices *fs_devices;
868
869 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
870 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
871 BTRFS_FSID_SIZE) != 0 &&
872 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
873 BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) {
874 return fs_devices;
875 }
876 }
877
878 return NULL;
879}
880
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200881
882static struct btrfs_fs_devices *find_fsid_changed(
883 struct btrfs_super_block *disk_super)
884{
885 struct btrfs_fs_devices *fs_devices;
886
887 /*
888 * Handles the case where scanned device is part of an fs that had
889 * multiple successful changes of FSID but curently device didn't
890 * observe it. Meaning our fsid will be different than theirs.
891 */
892 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
893 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
894 BTRFS_FSID_SIZE) != 0 &&
895 memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid,
896 BTRFS_FSID_SIZE) == 0 &&
897 memcmp(fs_devices->fsid, disk_super->fsid,
898 BTRFS_FSID_SIZE) != 0) {
899 return fs_devices;
900 }
901 }
902
903 return NULL;
904}
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200905/*
David Sterba60999ca2014-03-26 18:26:36 +0100906 * Add new device to list of registered devices
907 *
908 * Returns:
Anand Jaine124ece2018-01-18 22:02:35 +0800909 * device pointer which was just added or updated when successful
910 * error pointer when failed
David Sterba60999ca2014-03-26 18:26:36 +0100911 */
Anand Jaine124ece2018-01-18 22:02:35 +0800912static noinline struct btrfs_device *device_list_add(const char *path,
Anand Jain4306a972018-05-29 12:28:37 +0800913 struct btrfs_super_block *disk_super,
914 bool *new_device_added)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400915{
916 struct btrfs_device *device;
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200917 struct btrfs_fs_devices *fs_devices = NULL;
Josef Bacik606686e2012-06-04 14:03:51 -0400918 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400919 u64 found_transid = btrfs_super_generation(disk_super);
Anand Jain3acbcbf2018-01-18 22:02:36 +0800920 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200921 bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
922 BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200923 bool fsid_change_in_progress = (btrfs_super_flags(disk_super) &
924 BTRFS_SUPER_FLAG_CHANGING_FSID_V2);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400925
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200926 if (fsid_change_in_progress) {
927 if (!has_metadata_uuid) {
928 /*
929 * When we have an image which has CHANGING_FSID_V2 set
930 * it might belong to either a filesystem which has
931 * disks with completed fsid change or it might belong
932 * to fs with no UUID changes in effect, handle both.
933 */
934 fs_devices = find_fsid_inprogress(disk_super);
935 if (!fs_devices)
936 fs_devices = find_fsid(disk_super->fsid, NULL);
937 } else {
938 fs_devices = find_fsid_changed(disk_super);
939 }
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200940 } else if (has_metadata_uuid) {
941 fs_devices = find_fsid(disk_super->fsid,
942 disk_super->metadata_uuid);
943 } else {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200944 fs_devices = find_fsid(disk_super->fsid, NULL);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200945 }
946
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200947
Chris Mason8a4b83c2008-03-24 15:02:07 -0400948 if (!fs_devices) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200949 if (has_metadata_uuid)
950 fs_devices = alloc_fs_devices(disk_super->fsid,
951 disk_super->metadata_uuid);
952 else
953 fs_devices = alloc_fs_devices(disk_super->fsid, NULL);
954
Ilya Dryomov2208a372013-08-12 14:33:03 +0300955 if (IS_ERR(fs_devices))
Anand Jaine124ece2018-01-18 22:02:35 +0800956 return ERR_CAST(fs_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300957
Al Viro92900e52019-01-27 04:58:00 +0000958 fs_devices->fsid_change = fsid_change_in_progress;
959
Anand Jain9c6d1732018-05-29 14:10:20 +0800960 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainc4babc52018-04-12 10:29:25 +0800961 list_add(&fs_devices->fs_list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300962
Chris Mason8a4b83c2008-03-24 15:02:07 -0400963 device = NULL;
964 } else {
Anand Jain9c6d1732018-05-29 14:10:20 +0800965 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain09ba3bc2019-01-19 14:48:55 +0800966 device = btrfs_find_device(fs_devices, devid,
967 disk_super->dev_item.uuid, NULL, false);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200968
969 /*
970 * If this disk has been pulled into an fs devices created by
971 * a device which had the CHANGING_FSID_V2 flag then replace the
972 * metadata_uuid/fsid values of the fs_devices.
973 */
974 if (has_metadata_uuid && fs_devices->fsid_change &&
975 found_transid > fs_devices->latest_generation) {
976 memcpy(fs_devices->fsid, disk_super->fsid,
977 BTRFS_FSID_SIZE);
978 memcpy(fs_devices->metadata_uuid,
979 disk_super->metadata_uuid, BTRFS_FSID_SIZE);
980
981 fs_devices->fsid_change = false;
982 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400983 }
Miao Xie443f24f2014-07-24 11:37:15 +0800984
Chris Mason8a4b83c2008-03-24 15:02:07 -0400985 if (!device) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800986 if (fs_devices->opened) {
987 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800988 return ERR_PTR(-EBUSY);
Anand Jain9c6d1732018-05-29 14:10:20 +0800989 }
Yan Zheng2b820322008-11-17 21:11:30 -0500990
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300991 device = btrfs_alloc_device(NULL, &devid,
992 disk_super->dev_item.uuid);
993 if (IS_ERR(device)) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800994 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400995 /* we can safely leave the fs_devices entry around */
Anand Jaine124ece2018-01-18 22:02:35 +0800996 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400997 }
Josef Bacik606686e2012-06-04 14:03:51 -0400998
999 name = rcu_string_strdup(path, GFP_NOFS);
1000 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +01001001 btrfs_free_device(device);
Anand Jain9c6d1732018-05-29 14:10:20 +08001002 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001003 return ERR_PTR(-ENOMEM);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001004 }
Josef Bacik606686e2012-06-04 14:03:51 -04001005 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +02001006
Xiao Guangrong1f781602011-04-20 10:09:16 +00001007 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01001008 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -04001009
Yan Zheng2b820322008-11-17 21:11:30 -05001010 device->fs_devices = fs_devices;
Anand Jain4306a972018-05-29 12:28:37 +08001011 *new_device_added = true;
Anand Jain327f18c2018-01-18 22:02:33 +08001012
1013 if (disk_super->label[0])
1014 pr_info("BTRFS: device label %s devid %llu transid %llu %s\n",
1015 disk_super->label, devid, found_transid, path);
1016 else
1017 pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n",
1018 disk_super->fsid, devid, found_transid, path);
1019
Josef Bacik606686e2012-06-04 14:03:51 -04001020 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +08001021 /*
1022 * When FS is already mounted.
1023 * 1. If you are here and if the device->name is NULL that
1024 * means this device was missing at time of FS mount.
1025 * 2. If you are here and if the device->name is different
1026 * from 'path' that means either
1027 * a. The same device disappeared and reappeared with
1028 * different name. or
1029 * b. The missing-disk-which-was-replaced, has
1030 * reappeared now.
1031 *
1032 * We must allow 1 and 2a above. But 2b would be a spurious
1033 * and unintentional.
1034 *
1035 * Further in case of 1 and 2a above, the disk at 'path'
1036 * would have missed some transaction when it was away and
1037 * in case of 2a the stale bdev has to be updated as well.
1038 * 2b must not be allowed at all time.
1039 */
1040
1041 /*
Chris Mason0f23ae72014-09-18 07:49:05 -07001042 * For now, we do allow update to btrfs_fs_device through the
1043 * btrfs dev scan cli after FS has been mounted. We're still
1044 * tracking a problem where systems fail mount by subvolume id
1045 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +08001046 */
Chris Mason0f23ae72014-09-18 07:49:05 -07001047 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +08001048 /*
1049 * That is if the FS is _not_ mounted and if you
1050 * are here, that means there is more than one
1051 * disk with same uuid and devid.We keep the one
1052 * with larger generation number or the last-in if
1053 * generation are equal.
1054 */
Anand Jain9c6d1732018-05-29 14:10:20 +08001055 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001056 return ERR_PTR(-EEXIST);
Anand Jain77bdae42014-07-03 18:22:06 +08001057 }
Anand Jainb96de002014-07-03 18:22:05 +08001058
Anand Jaina9261d42018-10-15 10:45:17 +08001059 /*
1060 * We are going to replace the device path for a given devid,
1061 * make sure it's the same device if the device is mounted
1062 */
1063 if (device->bdev) {
1064 struct block_device *path_bdev;
1065
1066 path_bdev = lookup_bdev(path);
1067 if (IS_ERR(path_bdev)) {
1068 mutex_unlock(&fs_devices->device_list_mutex);
1069 return ERR_CAST(path_bdev);
1070 }
1071
1072 if (device->bdev != path_bdev) {
1073 bdput(path_bdev);
1074 mutex_unlock(&fs_devices->device_list_mutex);
1075 btrfs_warn_in_rcu(device->fs_info,
1076 "duplicate device fsid:devid for %pU:%llu old:%s new:%s",
1077 disk_super->fsid, devid,
1078 rcu_str_deref(device->name), path);
1079 return ERR_PTR(-EEXIST);
1080 }
1081 bdput(path_bdev);
1082 btrfs_info_in_rcu(device->fs_info,
1083 "device fsid %pU devid %llu moved old:%s new:%s",
1084 disk_super->fsid, devid,
1085 rcu_str_deref(device->name), path);
1086 }
1087
Josef Bacik606686e2012-06-04 14:03:51 -04001088 name = rcu_string_strdup(path, GFP_NOFS);
Anand Jain9c6d1732018-05-29 14:10:20 +08001089 if (!name) {
1090 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001091 return ERR_PTR(-ENOMEM);
Anand Jain9c6d1732018-05-29 14:10:20 +08001092 }
Josef Bacik606686e2012-06-04 14:03:51 -04001093 rcu_string_free(device->name);
1094 rcu_assign_pointer(device->name, name);
Anand Jaine6e674b2017-12-04 12:54:54 +08001095 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05001096 fs_devices->missing_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08001097 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05001098 }
Chris Mason8a4b83c2008-03-24 15:02:07 -04001099 }
1100
Anand Jain77bdae42014-07-03 18:22:06 +08001101 /*
1102 * Unmount does not free the btrfs_device struct but would zero
1103 * generation along with most of the other members. So just update
1104 * it back. We need it to pick the disk with largest generation
1105 * (as above).
1106 */
Nikolay Borisovd1a63002018-10-30 16:43:26 +02001107 if (!fs_devices->opened) {
Anand Jain77bdae42014-07-03 18:22:06 +08001108 device->generation = found_transid;
Nikolay Borisovd1a63002018-10-30 16:43:26 +02001109 fs_devices->latest_generation = max_t(u64, found_transid,
1110 fs_devices->latest_generation);
1111 }
Anand Jain77bdae42014-07-03 18:22:06 +08001112
Anand Jainf2788d22018-01-18 22:02:34 +08001113 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
1114
Anand Jain9c6d1732018-05-29 14:10:20 +08001115 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001116 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001117}
1118
Yan Zhenge4404d62008-12-12 10:03:26 -05001119static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
1120{
1121 struct btrfs_fs_devices *fs_devices;
1122 struct btrfs_device *device;
1123 struct btrfs_device *orig_dev;
1124
Nikolay Borisov7239ff42018-10-30 16:43:23 +02001125 fs_devices = alloc_fs_devices(orig->fsid, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03001126 if (IS_ERR(fs_devices))
1127 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001128
Miao Xieadbbb862014-09-03 21:35:42 +08001129 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -04001130 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001131
1132 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -04001133 struct rcu_string *name;
1134
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03001135 device = btrfs_alloc_device(NULL, &orig_dev->devid,
1136 orig_dev->uuid);
1137 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -05001138 goto error;
1139
Josef Bacik606686e2012-06-04 14:03:51 -04001140 /*
1141 * This is ok to do without rcu read locked because we hold the
1142 * uuid mutex so nothing we touch in here is going to disappear.
1143 */
Anand Jaine755f782014-06-30 17:12:47 +08001144 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +01001145 name = rcu_string_strdup(orig_dev->name->str,
1146 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +08001147 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +01001148 btrfs_free_device(device);
Anand Jaine755f782014-06-30 17:12:47 +08001149 goto error;
1150 }
1151 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -04001152 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001153
Yan Zhenge4404d62008-12-12 10:03:26 -05001154 list_add(&device->dev_list, &fs_devices->devices);
1155 device->fs_devices = fs_devices;
1156 fs_devices->num_devices++;
1157 }
Miao Xieadbbb862014-09-03 21:35:42 +08001158 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001159 return fs_devices;
1160error:
Miao Xieadbbb862014-09-03 21:35:42 +08001161 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001162 free_fs_devices(fs_devices);
1163 return ERR_PTR(-ENOMEM);
1164}
1165
Anand Jain9b99b112018-02-27 12:41:59 +08001166/*
1167 * After we have read the system tree and know devids belonging to
1168 * this filesystem, remove the device which does not belong there.
1169 */
1170void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -04001171{
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001172 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +08001173 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001174
Chris Masondfe25022008-05-13 13:46:40 -04001175 mutex_lock(&uuid_mutex);
1176again:
Xiao Guangrong46224702011-04-20 10:08:47 +00001177 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001178 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +08001179 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1180 &device->dev_state)) {
Anand Jain401e29c2017-12-04 12:54:55 +08001181 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1182 &device->dev_state) &&
1183 (!latest_dev ||
1184 device->generation > latest_dev->generation)) {
Miao Xie443f24f2014-07-24 11:37:15 +08001185 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001186 }
Yan Zheng2b820322008-11-17 21:11:30 -05001187 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001188 }
Yan Zheng2b820322008-11-17 21:11:30 -05001189
Stefan Behrens8dabb742012-11-06 13:15:27 +01001190 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
1191 /*
1192 * In the first step, keep the device which has
1193 * the correct fsid and the devid that is used
1194 * for the dev_replace procedure.
1195 * In the second step, the dev_replace state is
1196 * read from the device tree and it is known
1197 * whether the procedure is really active or
1198 * not, which means whether this device is
1199 * used or whether it should be removed.
1200 */
Anand Jain401e29c2017-12-04 12:54:55 +08001201 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1202 &device->dev_state)) {
Stefan Behrens8dabb742012-11-06 13:15:27 +01001203 continue;
1204 }
1205 }
Yan Zheng2b820322008-11-17 21:11:30 -05001206 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +01001207 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -05001208 device->bdev = NULL;
1209 fs_devices->open_devices--;
1210 }
Anand Jainebbede42017-12-04 12:54:52 +08001211 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001212 list_del_init(&device->dev_alloc_list);
Anand Jainebbede42017-12-04 12:54:52 +08001213 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08001214 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1215 &device->dev_state))
Stefan Behrens8dabb742012-11-06 13:15:27 +01001216 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001217 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001218 list_del_init(&device->dev_list);
1219 fs_devices->num_devices--;
David Sterbaa425f9d2018-03-20 15:47:33 +01001220 btrfs_free_device(device);
Chris Masondfe25022008-05-13 13:46:40 -04001221 }
Yan Zheng2b820322008-11-17 21:11:30 -05001222
1223 if (fs_devices->seed) {
1224 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001225 goto again;
1226 }
1227
Miao Xie443f24f2014-07-24 11:37:15 +08001228 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001229
Chris Masondfe25022008-05-13 13:46:40 -04001230 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001231}
Chris Masona0af4692008-05-13 16:03:06 -04001232
David Sterbaf06c5962017-06-06 17:08:23 +02001233static void free_device_rcu(struct rcu_head *head)
Xiao Guangrong1f781602011-04-20 10:09:16 +00001234{
1235 struct btrfs_device *device;
1236
1237 device = container_of(head, struct btrfs_device, rcu);
David Sterbaa425f9d2018-03-20 15:47:33 +01001238 btrfs_free_device(device);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001239}
1240
Anand Jain14238812016-07-22 06:04:53 +08001241static void btrfs_close_bdev(struct btrfs_device *device)
1242{
David Sterba08ffcae2017-06-19 16:55:35 +02001243 if (!device->bdev)
1244 return;
1245
Anand Jainebbede42017-12-04 12:54:52 +08001246 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Anand Jain14238812016-07-22 06:04:53 +08001247 sync_blockdev(device->bdev);
1248 invalidate_bdev(device->bdev);
1249 }
1250
David Sterba08ffcae2017-06-19 16:55:35 +02001251 blkdev_put(device->bdev, device->mode);
Anand Jain14238812016-07-22 06:04:53 +08001252}
1253
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001254static void btrfs_close_one_device(struct btrfs_device *device)
Anand Jainf448341a2016-06-14 18:55:25 +08001255{
1256 struct btrfs_fs_devices *fs_devices = device->fs_devices;
1257 struct btrfs_device *new_device;
1258 struct rcu_string *name;
1259
1260 if (device->bdev)
1261 fs_devices->open_devices--;
1262
Anand Jainebbede42017-12-04 12:54:52 +08001263 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jainf448341a2016-06-14 18:55:25 +08001264 device->devid != BTRFS_DEV_REPLACE_DEVID) {
1265 list_del_init(&device->dev_alloc_list);
1266 fs_devices->rw_devices--;
1267 }
1268
Anand Jaine6e674b2017-12-04 12:54:54 +08001269 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jainf448341a2016-06-14 18:55:25 +08001270 fs_devices->missing_devices--;
1271
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001272 btrfs_close_bdev(device);
1273
Anand Jainf448341a2016-06-14 18:55:25 +08001274 new_device = btrfs_alloc_device(NULL, &device->devid,
1275 device->uuid);
1276 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
1277
1278 /* Safe because we are under uuid_mutex */
1279 if (device->name) {
1280 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1281 BUG_ON(!name); /* -ENOMEM */
1282 rcu_assign_pointer(new_device->name, name);
1283 }
1284
1285 list_replace_rcu(&device->dev_list, &new_device->dev_list);
1286 new_device->fs_devices = device->fs_devices;
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001287
1288 call_rcu(&device->rcu, free_device_rcu);
Anand Jainf448341a2016-06-14 18:55:25 +08001289}
1290
Anand Jain0226e0e2018-04-12 10:29:27 +08001291static int close_fs_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001292{
Sasha Levin2037a092015-05-12 19:31:37 -04001293 struct btrfs_device *device, *tmp;
Yan Zhenge4404d62008-12-12 10:03:26 -05001294
Yan Zheng2b820322008-11-17 21:11:30 -05001295 if (--fs_devices->opened > 0)
1296 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001297
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001298 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -04001299 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001300 btrfs_close_one_device(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001301 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001302 mutex_unlock(&fs_devices->device_list_mutex);
1303
Yan Zhenge4404d62008-12-12 10:03:26 -05001304 WARN_ON(fs_devices->open_devices);
1305 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001306 fs_devices->opened = 0;
1307 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001308
Chris Mason8a4b83c2008-03-24 15:02:07 -04001309 return 0;
1310}
1311
Yan Zheng2b820322008-11-17 21:11:30 -05001312int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1313{
Yan Zhenge4404d62008-12-12 10:03:26 -05001314 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001315 int ret;
1316
1317 mutex_lock(&uuid_mutex);
Anand Jain0226e0e2018-04-12 10:29:27 +08001318 ret = close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001319 if (!fs_devices->opened) {
1320 seed_devices = fs_devices->seed;
1321 fs_devices->seed = NULL;
1322 }
Yan Zheng2b820322008-11-17 21:11:30 -05001323 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001324
1325 while (seed_devices) {
1326 fs_devices = seed_devices;
1327 seed_devices = fs_devices->seed;
Anand Jain0226e0e2018-04-12 10:29:27 +08001328 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001329 free_fs_devices(fs_devices);
1330 }
Yan Zheng2b820322008-11-17 21:11:30 -05001331 return ret;
1332}
1333
Anand Jain897fb572018-04-12 10:29:28 +08001334static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
Yan Zhenge4404d62008-12-12 10:03:26 -05001335 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001336{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001337 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +08001338 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -04001339 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001340
Tejun Heod4d77622010-11-13 11:55:18 +01001341 flags |= FMODE_EXCL;
1342
Anand Jainf117e292018-04-12 10:29:26 +08001343 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Eric Sandeenf63e0cc2013-04-04 20:45:08 +00001344 /* Just open everything we can; ignore failures here */
Anand Jain0fb08bc2017-11-09 23:45:24 +08001345 if (btrfs_open_one_device(fs_devices, device, flags, holder))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001346 continue;
Chris Masona0af4692008-05-13 16:03:06 -04001347
Anand Jain9f050db2017-11-09 23:45:25 +08001348 if (!latest_dev ||
1349 device->generation > latest_dev->generation)
1350 latest_dev = device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001351 }
Chris Masona0af4692008-05-13 16:03:06 -04001352 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +03001353 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -04001354 goto out;
1355 }
Yan Zheng2b820322008-11-17 21:11:30 -05001356 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +08001357 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -05001358 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -04001359out:
Yan Zheng2b820322008-11-17 21:11:30 -05001360 return ret;
1361}
1362
Anand Jainf8e10cd2018-01-22 14:49:36 -08001363static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1364{
1365 struct btrfs_device *dev1, *dev2;
1366
1367 dev1 = list_entry(a, struct btrfs_device, dev_list);
1368 dev2 = list_entry(b, struct btrfs_device, dev_list);
1369
1370 if (dev1->devid < dev2->devid)
1371 return -1;
1372 else if (dev1->devid > dev2->devid)
1373 return 1;
1374 return 0;
1375}
1376
Yan Zheng2b820322008-11-17 21:11:30 -05001377int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001378 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001379{
1380 int ret;
1381
David Sterbaf5194e32018-06-19 17:09:47 +02001382 lockdep_assert_held(&uuid_mutex);
1383
Anand Jain542c5902018-04-12 10:29:34 +08001384 mutex_lock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001385 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001386 fs_devices->opened++;
1387 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001388 } else {
Anand Jainf8e10cd2018-01-22 14:49:36 -08001389 list_sort(NULL, &fs_devices->devices, devid_cmp);
Anand Jain897fb572018-04-12 10:29:28 +08001390 ret = open_fs_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001391 }
Anand Jain542c5902018-04-12 10:29:34 +08001392 mutex_unlock(&fs_devices->device_list_mutex);
1393
Chris Mason8a4b83c2008-03-24 15:02:07 -04001394 return ret;
1395}
1396
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001397static void btrfs_release_disk_super(struct page *page)
Anand Jain6cf86a002016-02-13 10:01:29 +08001398{
1399 kunmap(page);
1400 put_page(page);
1401}
1402
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001403static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1404 struct page **page,
1405 struct btrfs_super_block **disk_super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001406{
1407 void *p;
1408 pgoff_t index;
1409
1410 /* make sure our super fits in the device */
1411 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1412 return 1;
1413
1414 /* make sure our super fits in the page */
1415 if (sizeof(**disk_super) > PAGE_SIZE)
1416 return 1;
1417
1418 /* make sure our super doesn't straddle pages on disk */
1419 index = bytenr >> PAGE_SHIFT;
1420 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1421 return 1;
1422
1423 /* pull in the page with our super */
1424 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1425 index, GFP_KERNEL);
1426
1427 if (IS_ERR_OR_NULL(*page))
1428 return 1;
1429
1430 p = kmap(*page);
1431
1432 /* align our pointer to the offset of the super block */
Johannes Thumshirn70730172018-12-05 15:23:03 +01001433 *disk_super = p + offset_in_page(bytenr);
Anand Jain6cf86a002016-02-13 10:01:29 +08001434
1435 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1436 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1437 btrfs_release_disk_super(*page);
1438 return 1;
1439 }
1440
1441 if ((*disk_super)->label[0] &&
1442 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1443 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1444
1445 return 0;
1446}
1447
Anand Jain228a73a2019-01-04 13:31:54 +08001448int btrfs_forget_devices(const char *path)
1449{
1450 int ret;
1451
1452 mutex_lock(&uuid_mutex);
1453 ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL);
1454 mutex_unlock(&uuid_mutex);
1455
1456 return ret;
1457}
1458
David Sterba6f60cbd2013-02-15 11:31:02 -07001459/*
1460 * Look for a btrfs signature on a device. This may be called out of the mount path
1461 * and we are not allowed to call set_blocksize during the scan. The superblock
1462 * is read via pagecache
1463 */
Gu Jinxiang36350e92018-07-12 14:23:16 +08001464struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,
1465 void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001466{
1467 struct btrfs_super_block *disk_super;
Anand Jain4306a972018-05-29 12:28:37 +08001468 bool new_device_added = false;
Gu Jinxiang36350e92018-07-12 14:23:16 +08001469 struct btrfs_device *device = NULL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001470 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001471 struct page *page;
David Sterba6f60cbd2013-02-15 11:31:02 -07001472 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001473
David Sterba899f9302018-06-19 16:37:36 +02001474 lockdep_assert_held(&uuid_mutex);
1475
David Sterba6f60cbd2013-02-15 11:31:02 -07001476 /*
1477 * we would like to check all the supers, but that would make
1478 * a btrfs mount succeed after a mkfs from a different FS.
1479 * So, we need to add a special mount option to scan for
1480 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1481 */
1482 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001483 flags |= FMODE_EXCL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001484
1485 bdev = blkdev_get_by_path(path, flags, holder);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001486 if (IS_ERR(bdev))
Gu Jinxiang36350e92018-07-12 14:23:16 +08001487 return ERR_CAST(bdev);
David Sterba6f60cbd2013-02-15 11:31:02 -07001488
Anand Jain05a5c552017-12-15 15:40:16 +08001489 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
Gu Jinxiang36350e92018-07-12 14:23:16 +08001490 device = ERR_PTR(-EINVAL);
David Sterba6f60cbd2013-02-15 11:31:02 -07001491 goto error_bdev_put;
Anand Jain05a5c552017-12-15 15:40:16 +08001492 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001493
Anand Jain4306a972018-05-29 12:28:37 +08001494 device = device_list_add(path, disk_super, &new_device_added);
Gu Jinxiang36350e92018-07-12 14:23:16 +08001495 if (!IS_ERR(device)) {
Anand Jain4306a972018-05-29 12:28:37 +08001496 if (new_device_added)
1497 btrfs_free_stale_devices(path, device);
1498 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001499
Anand Jain6cf86a002016-02-13 10:01:29 +08001500 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001501
1502error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001503 blkdev_put(bdev, flags);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001504
Gu Jinxiang36350e92018-07-12 14:23:16 +08001505 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001506}
Chris Mason0b86a832008-03-24 15:01:56 -04001507
Jeff Mahoney499f3772015-06-15 09:41:17 -04001508static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001509 struct btrfs_device *device,
1510 u64 *start, u64 len)
1511{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001512 struct btrfs_fs_info *fs_info = device->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001513 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001514 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001515 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001516 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001517
Jeff Mahoney499f3772015-06-15 09:41:17 -04001518 if (transaction)
1519 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001520again:
1521 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001522 struct map_lookup *map;
1523 int i;
1524
Jeff Mahoney95617d62015-06-03 10:55:48 -04001525 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04001526 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001527 u64 end;
1528
Josef Bacik6df9a952013-06-27 13:22:46 -04001529 if (map->stripes[i].dev != device)
1530 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001531 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001532 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001533 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001534 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001535 /*
1536 * Make sure that while processing the pinned list we do
1537 * not override our *start with a lower value, because
1538 * we can have pinned chunks that fall within this
1539 * device hole and that have lower physical addresses
1540 * than the pending chunks we processed before. If we
1541 * do not take this special care we can end up getting
1542 * 2 pending chunks that start at the same physical
1543 * device offsets because the end offset of a pinned
1544 * chunk can be equal to the start offset of some
1545 * pending chunk.
1546 */
1547 end = map->stripes[i].physical + em->orig_block_len;
1548 if (end > *start) {
1549 *start = end;
1550 ret = 1;
1551 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001552 }
1553 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001554 if (search_list != &fs_info->pinned_chunks) {
1555 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001556 goto again;
1557 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001558
1559 return ret;
1560}
1561
1562
Chris Mason0b86a832008-03-24 15:01:56 -04001563/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001564 * find_free_dev_extent_start - find free space in the specified device
1565 * @device: the device which we search the free space in
1566 * @num_bytes: the size of the free space that we need
1567 * @search_start: the position from which to begin the search
1568 * @start: store the start of the free space.
1569 * @len: the size of the free space. that we find, or the size
1570 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001571 *
Chris Mason0b86a832008-03-24 15:01:56 -04001572 * this uses a pretty simple search, the expectation is that it is
1573 * called very infrequently and that a given device has a small number
1574 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001575 *
1576 * @start is used to store the start of the free space if we find. But if we
1577 * don't find suitable free space, it will be used to store the start position
1578 * of the max free space.
1579 *
1580 * @len is used to store the size of the free space that we find.
1581 * But if we don't find suitable free space, it is used to store the size of
1582 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001583 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001584int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1585 struct btrfs_device *device, u64 num_bytes,
1586 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001587{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001588 struct btrfs_fs_info *fs_info = device->fs_info;
1589 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001590 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001591 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001592 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001593 u64 hole_size;
1594 u64 max_hole_start;
1595 u64 max_hole_size;
1596 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001597 u64 search_end = device->total_bytes;
1598 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001599 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001600 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001601
1602 /*
1603 * We don't want to overwrite the superblock on the drive nor any area
1604 * used by the boot loader (grub for example), so we make sure to start
1605 * at an offset of at least 1MB.
1606 */
David Sterba0d0c71b2017-06-15 01:30:06 +02001607 search_start = max_t(u64, search_start, SZ_1M);
Chris Mason0b86a832008-03-24 15:01:56 -04001608
Josef Bacik6df9a952013-06-27 13:22:46 -04001609 path = btrfs_alloc_path();
1610 if (!path)
1611 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001612
Miao Xie7bfc8372011-01-05 10:07:26 +00001613 max_hole_start = search_start;
1614 max_hole_size = 0;
1615
Zhao Leif2ab7612015-02-16 18:52:17 +08001616again:
Anand Jain401e29c2017-12-04 12:54:55 +08001617 if (search_start >= search_end ||
1618 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001619 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001620 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001621 }
1622
David Sterbae4058b52015-11-27 16:31:35 +01001623 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001624 path->search_commit_root = 1;
1625 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001626
Chris Mason0b86a832008-03-24 15:01:56 -04001627 key.objectid = device->devid;
1628 key.offset = search_start;
1629 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001630
Li Zefan125ccb02011-12-08 15:07:24 +08001631 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001632 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001633 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001634 if (ret > 0) {
1635 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1636 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001637 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001638 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001639
Chris Mason0b86a832008-03-24 15:01:56 -04001640 while (1) {
1641 l = path->nodes[0];
1642 slot = path->slots[0];
1643 if (slot >= btrfs_header_nritems(l)) {
1644 ret = btrfs_next_leaf(root, path);
1645 if (ret == 0)
1646 continue;
1647 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001648 goto out;
1649
1650 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001651 }
1652 btrfs_item_key_to_cpu(l, &key, slot);
1653
1654 if (key.objectid < device->devid)
1655 goto next;
1656
1657 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001658 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001659
David Sterba962a2982014-06-04 18:41:45 +02001660 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001661 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001662
Miao Xie7bfc8372011-01-05 10:07:26 +00001663 if (key.offset > search_start) {
1664 hole_size = key.offset - search_start;
1665
Josef Bacik6df9a952013-06-27 13:22:46 -04001666 /*
1667 * Have to check before we set max_hole_start, otherwise
1668 * we could end up sending back this offset anyway.
1669 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001670 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001671 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001672 hole_size)) {
1673 if (key.offset >= search_start) {
1674 hole_size = key.offset - search_start;
1675 } else {
1676 WARN_ON_ONCE(1);
1677 hole_size = 0;
1678 }
1679 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001680
Miao Xie7bfc8372011-01-05 10:07:26 +00001681 if (hole_size > max_hole_size) {
1682 max_hole_start = search_start;
1683 max_hole_size = hole_size;
1684 }
1685
1686 /*
1687 * If this free space is greater than which we need,
1688 * it must be the max free space that we have found
1689 * until now, so max_hole_start must point to the start
1690 * of this free space and the length of this free space
1691 * is stored in max_hole_size. Thus, we return
1692 * max_hole_start and max_hole_size and go back to the
1693 * caller.
1694 */
1695 if (hole_size >= num_bytes) {
1696 ret = 0;
1697 goto out;
1698 }
1699 }
1700
Chris Mason0b86a832008-03-24 15:01:56 -04001701 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001702 extent_end = key.offset + btrfs_dev_extent_length(l,
1703 dev_extent);
1704 if (extent_end > search_start)
1705 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001706next:
1707 path->slots[0]++;
1708 cond_resched();
1709 }
Chris Mason0b86a832008-03-24 15:01:56 -04001710
liubo38c01b92011-08-02 02:39:03 +00001711 /*
1712 * At this point, search_start should be the end of
1713 * allocated dev extents, and when shrinking the device,
1714 * search_end may be smaller than search_start.
1715 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001716 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001717 hole_size = search_end - search_start;
1718
Jeff Mahoney499f3772015-06-15 09:41:17 -04001719 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001720 hole_size)) {
1721 btrfs_release_path(path);
1722 goto again;
1723 }
Chris Mason0b86a832008-03-24 15:01:56 -04001724
Zhao Leif2ab7612015-02-16 18:52:17 +08001725 if (hole_size > max_hole_size) {
1726 max_hole_start = search_start;
1727 max_hole_size = hole_size;
1728 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001729 }
1730
Miao Xie7bfc8372011-01-05 10:07:26 +00001731 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001732 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001733 ret = -ENOSPC;
1734 else
1735 ret = 0;
1736
1737out:
Yan Zheng2b820322008-11-17 21:11:30 -05001738 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001739 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001740 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001741 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001742 return ret;
1743}
1744
Jeff Mahoney499f3772015-06-15 09:41:17 -04001745int find_free_dev_extent(struct btrfs_trans_handle *trans,
1746 struct btrfs_device *device, u64 num_bytes,
1747 u64 *start, u64 *len)
1748{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001749 /* FIXME use last free of some kind */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001750 return find_free_dev_extent_start(trans->transaction, device,
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001751 num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001752}
1753
Christoph Hellwigb2950862008-12-02 09:54:17 -05001754static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001755 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001756 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001757{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001758 struct btrfs_fs_info *fs_info = device->fs_info;
1759 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001760 int ret;
1761 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001762 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001763 struct btrfs_key found_key;
1764 struct extent_buffer *leaf = NULL;
1765 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001766
1767 path = btrfs_alloc_path();
1768 if (!path)
1769 return -ENOMEM;
1770
1771 key.objectid = device->devid;
1772 key.offset = start;
1773 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001774again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001775 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001776 if (ret > 0) {
1777 ret = btrfs_previous_item(root, path, key.objectid,
1778 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001779 if (ret)
1780 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001781 leaf = path->nodes[0];
1782 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1783 extent = btrfs_item_ptr(leaf, path->slots[0],
1784 struct btrfs_dev_extent);
1785 BUG_ON(found_key.offset > start || found_key.offset +
1786 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001787 key = found_key;
1788 btrfs_release_path(path);
1789 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001790 } else if (ret == 0) {
1791 leaf = path->nodes[0];
1792 extent = btrfs_item_ptr(leaf, path->slots[0],
1793 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001794 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001795 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001796 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001797 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001798
Miao Xie2196d6e2014-09-03 21:35:41 +08001799 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1800
Chris Mason8f18cf12008-04-25 16:53:30 -04001801 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001802 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001803 btrfs_handle_fs_error(fs_info, ret,
1804 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001805 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001806 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001807 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001808out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001809 btrfs_free_path(path);
1810 return ret;
1811}
1812
Eric Sandeen48a3b632013-04-25 20:41:01 +00001813static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1814 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001815 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001816{
1817 int ret;
1818 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001819 struct btrfs_fs_info *fs_info = device->fs_info;
1820 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001821 struct btrfs_dev_extent *extent;
1822 struct extent_buffer *leaf;
1823 struct btrfs_key key;
1824
Anand Jaine12c9622017-12-04 12:54:53 +08001825 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
Anand Jain401e29c2017-12-04 12:54:55 +08001826 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
Chris Mason0b86a832008-03-24 15:01:56 -04001827 path = btrfs_alloc_path();
1828 if (!path)
1829 return -ENOMEM;
1830
Chris Mason0b86a832008-03-24 15:01:56 -04001831 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001832 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001833 key.type = BTRFS_DEV_EXTENT_KEY;
1834 ret = btrfs_insert_empty_item(trans, root, path, &key,
1835 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001836 if (ret)
1837 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001838
1839 leaf = path->nodes[0];
1840 extent = btrfs_item_ptr(leaf, path->slots[0],
1841 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001842 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1843 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001844 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1845 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001846 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1847
Chris Mason0b86a832008-03-24 15:01:56 -04001848 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1849 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001850out:
Chris Mason0b86a832008-03-24 15:01:56 -04001851 btrfs_free_path(path);
1852 return ret;
1853}
1854
Josef Bacik6df9a952013-06-27 13:22:46 -04001855static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001856{
Josef Bacik6df9a952013-06-27 13:22:46 -04001857 struct extent_map_tree *em_tree;
1858 struct extent_map *em;
1859 struct rb_node *n;
1860 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001861
Josef Bacik6df9a952013-06-27 13:22:46 -04001862 em_tree = &fs_info->mapping_tree.map_tree;
1863 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08001864 n = rb_last(&em_tree->map.rb_root);
Josef Bacik6df9a952013-06-27 13:22:46 -04001865 if (n) {
1866 em = rb_entry(n, struct extent_map, rb_node);
1867 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001868 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001869 read_unlock(&em_tree->lock);
1870
Chris Mason0b86a832008-03-24 15:01:56 -04001871 return ret;
1872}
1873
Ilya Dryomov53f10652013-08-12 14:33:01 +03001874static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1875 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001876{
1877 int ret;
1878 struct btrfs_key key;
1879 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001880 struct btrfs_path *path;
1881
Yan Zheng2b820322008-11-17 21:11:30 -05001882 path = btrfs_alloc_path();
1883 if (!path)
1884 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001885
1886 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1887 key.type = BTRFS_DEV_ITEM_KEY;
1888 key.offset = (u64)-1;
1889
Ilya Dryomov53f10652013-08-12 14:33:01 +03001890 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001891 if (ret < 0)
1892 goto error;
1893
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001894 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001895
Ilya Dryomov53f10652013-08-12 14:33:01 +03001896 ret = btrfs_previous_item(fs_info->chunk_root, path,
1897 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001898 BTRFS_DEV_ITEM_KEY);
1899 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001900 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001901 } else {
1902 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1903 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001904 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001905 }
1906 ret = 0;
1907error:
Yan Zheng2b820322008-11-17 21:11:30 -05001908 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001909 return ret;
1910}
1911
1912/*
1913 * the device information is stored in the chunk root
1914 * the btrfs_device struct should be fully filled in
1915 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001916static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001917 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001918{
1919 int ret;
1920 struct btrfs_path *path;
1921 struct btrfs_dev_item *dev_item;
1922 struct extent_buffer *leaf;
1923 struct btrfs_key key;
1924 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001925
Chris Mason0b86a832008-03-24 15:01:56 -04001926 path = btrfs_alloc_path();
1927 if (!path)
1928 return -ENOMEM;
1929
Chris Mason0b86a832008-03-24 15:01:56 -04001930 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1931 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001932 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001933
Nikolay Borisov8e87e852018-07-20 19:37:47 +03001934 ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path,
1935 &key, sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001936 if (ret)
1937 goto out;
1938
1939 leaf = path->nodes[0];
1940 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1941
1942 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001943 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001944 btrfs_set_device_type(leaf, dev_item, device->type);
1945 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1946 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1947 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001948 btrfs_set_device_total_bytes(leaf, dev_item,
1949 btrfs_device_get_disk_total_bytes(device));
1950 btrfs_set_device_bytes_used(leaf, dev_item,
1951 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001952 btrfs_set_device_group(leaf, dev_item, 0);
1953 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1954 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001955 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001956
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001957 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001958 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001959 ptr = btrfs_device_fsid(dev_item);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02001960 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid,
1961 ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001962 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001963
Yan Zheng2b820322008-11-17 21:11:30 -05001964 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001965out:
1966 btrfs_free_path(path);
1967 return ret;
1968}
Chris Mason8f18cf12008-04-25 16:53:30 -04001969
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001970/*
1971 * Function to update ctime/mtime for a given device path.
1972 * Mainly used for ctime/mtime based probe like libblkid.
1973 */
David Sterbada353f62017-02-14 17:55:53 +01001974static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001975{
1976 struct file *filp;
1977
1978 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001979 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001980 return;
1981 file_update_time(filp);
1982 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001983}
1984
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001985static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
Chris Masona061fc82008-05-07 11:43:44 -04001986 struct btrfs_device *device)
1987{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001988 struct btrfs_root *root = fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001989 int ret;
1990 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001991 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001992 struct btrfs_trans_handle *trans;
1993
Chris Masona061fc82008-05-07 11:43:44 -04001994 path = btrfs_alloc_path();
1995 if (!path)
1996 return -ENOMEM;
1997
Yan, Zhenga22285a2010-05-16 10:48:46 -04001998 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001999 if (IS_ERR(trans)) {
2000 btrfs_free_path(path);
2001 return PTR_ERR(trans);
2002 }
Chris Masona061fc82008-05-07 11:43:44 -04002003 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2004 key.type = BTRFS_DEV_ITEM_KEY;
2005 key.offset = device->devid;
2006
2007 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03002008 if (ret) {
2009 if (ret > 0)
2010 ret = -ENOENT;
2011 btrfs_abort_transaction(trans, ret);
2012 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04002013 goto out;
2014 }
2015
2016 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03002017 if (ret) {
2018 btrfs_abort_transaction(trans, ret);
2019 btrfs_end_transaction(trans);
2020 }
2021
Chris Masona061fc82008-05-07 11:43:44 -04002022out:
2023 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03002024 if (!ret)
2025 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04002026 return ret;
2027}
2028
David Sterba3cc31a02016-02-15 16:00:26 +01002029/*
2030 * Verify that @num_devices satisfies the RAID profile constraints in the whole
2031 * filesystem. It's up to the caller to adjust that number regarding eg. device
2032 * replace.
2033 */
2034static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
2035 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04002036{
Chris Masona061fc82008-05-07 11:43:44 -04002037 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00002038 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01002039 int i;
Chris Masona061fc82008-05-07 11:43:44 -04002040
Miao Xiede98ced2013-01-29 10:13:12 +00002041 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08002042 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00002043
Anand Jainbd45ffb2016-02-13 10:01:34 +08002044 all_avail = fs_info->avail_data_alloc_bits |
2045 fs_info->avail_system_alloc_bits |
2046 fs_info->avail_metadata_alloc_bits;
2047 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08002048
David Sterba418775a2016-02-15 16:28:14 +01002049 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
Anand Jain41a6e892018-04-25 19:01:43 +08002050 if (!(all_avail & btrfs_raid_array[i].bg_flag))
David Sterba418775a2016-02-15 16:28:14 +01002051 continue;
Chris Masona061fc82008-05-07 11:43:44 -04002052
David Sterba418775a2016-02-15 16:28:14 +01002053 if (num_devices < btrfs_raid_array[i].devs_min) {
Anand Jainf9fbcaa2018-04-25 19:01:44 +08002054 int ret = btrfs_raid_array[i].mindev_error;
Chris Masona061fc82008-05-07 11:43:44 -04002055
David Sterba418775a2016-02-15 16:28:14 +01002056 if (ret)
2057 return ret;
2058 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08002059 }
2060
2061 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002062}
2063
Omar Sandovalc9162bd2017-08-22 23:46:04 -07002064static struct btrfs_device * btrfs_find_next_active_device(
2065 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08002066{
2067 struct btrfs_device *next_device;
2068
2069 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
2070 if (next_device != device &&
Anand Jaine6e674b2017-12-04 12:54:54 +08002071 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
2072 && next_device->bdev)
Anand Jain88acff62016-05-03 17:44:43 +08002073 return next_device;
2074 }
2075
2076 return NULL;
2077}
2078
2079/*
2080 * Helper function to check if the given device is part of s_bdev / latest_bdev
2081 * and replace it with the provided or the next active device, in the context
2082 * where this function called, there should be always be another device (or
2083 * this_dev) which is active.
2084 */
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002085void btrfs_assign_next_active_device(struct btrfs_device *device,
2086 struct btrfs_device *this_dev)
Anand Jain88acff62016-05-03 17:44:43 +08002087{
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002088 struct btrfs_fs_info *fs_info = device->fs_info;
Anand Jain88acff62016-05-03 17:44:43 +08002089 struct btrfs_device *next_device;
2090
2091 if (this_dev)
2092 next_device = this_dev;
2093 else
2094 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
2095 device);
2096 ASSERT(next_device);
2097
2098 if (fs_info->sb->s_bdev &&
2099 (fs_info->sb->s_bdev == device->bdev))
2100 fs_info->sb->s_bdev = next_device->bdev;
2101
2102 if (fs_info->fs_devices->latest_bdev == device->bdev)
2103 fs_info->fs_devices->latest_bdev = next_device->bdev;
2104}
2105
Anand Jain1da73962018-08-10 13:53:21 +08002106/*
2107 * Return btrfs_fs_devices::num_devices excluding the device that's being
2108 * currently replaced.
2109 */
2110static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info)
2111{
2112 u64 num_devices = fs_info->fs_devices->num_devices;
2113
David Sterbacb5583d2018-09-07 16:11:23 +02002114 down_read(&fs_info->dev_replace.rwsem);
Anand Jain1da73962018-08-10 13:53:21 +08002115 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
2116 ASSERT(num_devices > 1);
2117 num_devices--;
2118 }
David Sterbacb5583d2018-09-07 16:11:23 +02002119 up_read(&fs_info->dev_replace.rwsem);
Anand Jain1da73962018-08-10 13:53:21 +08002120
2121 return num_devices;
2122}
2123
David Sterbada353f62017-02-14 17:55:53 +01002124int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
2125 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08002126{
2127 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002128 struct btrfs_fs_devices *cur_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002129 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002130 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002131 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002132
2133 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002134
Anand Jain1da73962018-08-10 13:53:21 +08002135 num_devices = btrfs_num_devices(fs_info);
Chris Masona061fc82008-05-07 11:43:44 -04002136
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002137 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08002138 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04002139 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002140
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002141 device = btrfs_find_device_by_devspec(fs_info, devid, device_path);
2142
2143 if (IS_ERR(device)) {
2144 if (PTR_ERR(device) == -ENOENT &&
2145 strcmp(device_path, "missing") == 0)
2146 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
2147 else
2148 ret = PTR_ERR(device);
Chris Masona061fc82008-05-07 11:43:44 -04002149 goto out;
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002150 }
Yan Zheng2b820322008-11-17 21:11:30 -05002151
Omar Sandovaleede2bf2016-11-03 10:28:12 -07002152 if (btrfs_pinned_by_swapfile(fs_info, device)) {
2153 btrfs_warn_in_rcu(fs_info,
2154 "cannot remove device %s (devid %llu) due to active swapfile",
2155 rcu_str_deref(device->name), device->devid);
2156 ret = -ETXTBSY;
2157 goto out;
2158 }
2159
Anand Jain401e29c2017-12-04 12:54:55 +08002160 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Anand Jain183860f2013-05-17 10:52:45 +00002161 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08002162 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002163 }
2164
Anand Jainebbede42017-12-04 12:54:52 +08002165 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
2166 fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00002167 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08002168 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05002169 }
2170
Anand Jainebbede42017-12-04 12:54:52 +08002171 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002172 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002173 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002174 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02002175 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002176 }
Chris Masona061fc82008-05-07 11:43:44 -04002177
Carey Underwoodd7901552013-03-04 16:37:06 -06002178 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002179 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06002180 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002181 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002182 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002183
Stefan Behrens63a212a2012-11-05 18:29:28 +01002184 /*
2185 * TODO: the superblock still includes this device in its num_devices
2186 * counter although write_all_supers() is not locked out. This
2187 * could give a filesystem state which requires a degraded mount.
2188 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002189 ret = btrfs_rm_dev_item(fs_info, device);
Chris Masona061fc82008-05-07 11:43:44 -04002190 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002191 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002192
Anand Jaine12c9622017-12-04 12:54:53 +08002193 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002194 btrfs_scrub_cancel_dev(fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04002195
2196 /*
2197 * the device list mutex makes sure that we don't change
2198 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01002199 * the device supers. Whoever is writing all supers, should
2200 * lock the device list mutex before getting the number of
2201 * devices in the super block (super_copy). Conversely,
2202 * whoever updates the number of devices in the super block
2203 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04002204 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002205
Anand Jain41a52a02018-04-12 10:29:31 +08002206 /*
2207 * In normal cases the cur_devices == fs_devices. But in case
2208 * of deleting a seed device, the cur_devices should point to
2209 * its own fs_devices listed under the fs_devices->seed.
2210 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002211 cur_devices = device->fs_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002212 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002213 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002214
Anand Jain41a52a02018-04-12 10:29:31 +08002215 cur_devices->num_devices--;
2216 cur_devices->total_devices--;
Anand Jainb4993e62018-07-03 17:07:23 +08002217 /* Update total_devices of the parent fs_devices if it's seed */
2218 if (cur_devices != fs_devices)
2219 fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05002220
Anand Jaine6e674b2017-12-04 12:54:54 +08002221 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jain41a52a02018-04-12 10:29:31 +08002222 cur_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05002223
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002224 btrfs_assign_next_active_device(device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05002225
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002226 if (device->bdev) {
Anand Jain41a52a02018-04-12 10:29:31 +08002227 cur_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002228 /* remove sysfs entry */
Anand Jainb5185192018-04-12 10:29:30 +08002229 btrfs_sysfs_rm_device_link(fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002230 }
Anand Jain99994cd2014-06-03 11:36:00 +08002231
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002232 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2233 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
Anand Jainb5185192018-04-12 10:29:30 +08002234 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002235
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002236 /*
2237 * at this point, the device is zero sized and detached from
2238 * the devices list. All that's left is to zero out the old
2239 * supers and free the device.
2240 */
Anand Jainebbede42017-12-04 12:54:52 +08002241 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002242 btrfs_scratch_superblocks(device->bdev, device->name->str);
2243
2244 btrfs_close_bdev(device);
David Sterbaf06c5962017-06-06 17:08:23 +02002245 call_rcu(&device->rcu, free_device_rcu);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002246
Xiao Guangrong1f781602011-04-20 10:09:16 +00002247 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002248 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002249 if (fs_devices->seed == cur_devices) {
2250 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05002251 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002252 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002253 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05002254 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00002255 cur_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002256 close_fs_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002257 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002258 }
2259
Chris Masona061fc82008-05-07 11:43:44 -04002260out:
2261 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002262 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08002263
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002264error_undo:
Anand Jainebbede42017-12-04 12:54:52 +08002265 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002266 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002267 list_add(&device->dev_alloc_list,
Anand Jainb5185192018-04-12 10:29:30 +08002268 &fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002269 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002270 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002271 }
Anand Jain24fc5722016-02-13 10:01:36 +08002272 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002273}
2274
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002275void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002276{
Anand Jaind51908c2014-08-13 14:24:19 +08002277 struct btrfs_fs_devices *fs_devices;
2278
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002279 lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex);
Ilya Dryomov13572722013-10-02 20:41:01 +03002280
Anand Jain25e8e912014-08-20 10:56:56 +08002281 /*
2282 * in case of fs with no seed, srcdev->fs_devices will point
2283 * to fs_devices of fs_info. However when the dev being replaced is
2284 * a seed dev it will point to the seed's local fs_devices. In short
2285 * srcdev will have its correct fs_devices in both the cases.
2286 */
2287 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002288
Stefan Behrense93c89c2012-11-05 17:33:06 +01002289 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002290 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002291 fs_devices->num_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08002292 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
Anand Jaind51908c2014-08-13 14:24:19 +08002293 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002294
Anand Jainebbede42017-12-04 12:54:52 +08002295 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
Miao Xie82372bc2014-09-03 21:35:44 +08002296 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002297
Miao Xie82372bc2014-09-03 21:35:44 +08002298 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002299 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002300}
2301
2302void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2303 struct btrfs_device *srcdev)
2304{
2305 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002306
Anand Jainebbede42017-12-04 12:54:52 +08002307 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
Anand Jain48b3b9d2016-04-12 21:36:16 +08002308 /* zero out the old super if it is writable */
2309 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2310 }
Anand Jain14238812016-07-22 06:04:53 +08002311
2312 btrfs_close_bdev(srcdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002313 call_rcu(&srcdev->rcu, free_device_rcu);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002314
Anand Jain94d5f0c2014-08-13 14:24:22 +08002315 /* if this is no devs we rather delete the fs_devices */
2316 if (!fs_devices->num_devices) {
2317 struct btrfs_fs_devices *tmp_fs_devices;
2318
Anand Jain6dd38f82017-10-17 06:53:50 +08002319 /*
2320 * On a mounted FS, num_devices can't be zero unless it's a
2321 * seed. In case of a seed device being replaced, the replace
2322 * target added to the sprout FS, so there will be no more
2323 * device left under the seed FS.
2324 */
2325 ASSERT(fs_devices->seeding);
2326
Anand Jain94d5f0c2014-08-13 14:24:22 +08002327 tmp_fs_devices = fs_info->fs_devices;
2328 while (tmp_fs_devices) {
2329 if (tmp_fs_devices->seed == fs_devices) {
2330 tmp_fs_devices->seed = fs_devices->seed;
2331 break;
2332 }
2333 tmp_fs_devices = tmp_fs_devices->seed;
2334 }
2335 fs_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002336 close_fs_devices(fs_devices);
Anand Jain8bef8402014-08-13 14:24:23 +08002337 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002338 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002339}
2340
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002341void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002342{
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002343 struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices;
Anand Jaind2ff1b22015-03-10 06:38:42 +08002344
Anand Jaind9a071f2018-04-12 10:29:38 +08002345 WARN_ON(!tgtdev);
2346 mutex_lock(&fs_devices->device_list_mutex);
2347
2348 btrfs_sysfs_rm_device_link(fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002349
Anand Jain779bf3fe2016-04-18 16:51:23 +08002350 if (tgtdev->bdev)
Anand Jaind9a071f2018-04-12 10:29:38 +08002351 fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002352
Anand Jaind9a071f2018-04-12 10:29:38 +08002353 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002354
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002355 btrfs_assign_next_active_device(tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002356
Stefan Behrense93c89c2012-11-05 17:33:06 +01002357 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002358
Anand Jaind9a071f2018-04-12 10:29:38 +08002359 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002360
2361 /*
2362 * The update_dev_time() with in btrfs_scratch_superblocks()
2363 * may lead to a call to btrfs_show_devname() which will try
2364 * to hold device_list_mutex. And here this device
2365 * is already out of device list, so we don't have to hold
2366 * the device_list_mutex lock.
2367 */
2368 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002369
2370 btrfs_close_bdev(tgtdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002371 call_rcu(&tgtdev->rcu, free_device_rcu);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002372}
2373
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002374static struct btrfs_device *btrfs_find_device_by_path(
2375 struct btrfs_fs_info *fs_info, const char *device_path)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002376{
2377 int ret = 0;
2378 struct btrfs_super_block *disk_super;
2379 u64 devid;
2380 u8 *dev_uuid;
2381 struct block_device *bdev;
2382 struct buffer_head *bh;
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002383 struct btrfs_device *device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002384
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002385 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002386 fs_info->bdev_holder, 0, &bdev, &bh);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002387 if (ret)
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002388 return ERR_PTR(ret);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002389 disk_super = (struct btrfs_super_block *)bh->b_data;
2390 devid = btrfs_stack_device_id(&disk_super->dev_item);
2391 dev_uuid = disk_super->dev_item.uuid;
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002392 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
Anand Jaine4319cd2019-01-17 23:32:31 +08002393 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002394 disk_super->metadata_uuid, true);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002395 else
Anand Jaine4319cd2019-01-17 23:32:31 +08002396 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002397 disk_super->fsid, true);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002398
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002399 brelse(bh);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002400 if (!device)
2401 device = ERR_PTR(-ENOENT);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002402 blkdev_put(bdev, FMODE_READ);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002403 return device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002404}
2405
Yan Zheng2b820322008-11-17 21:11:30 -05002406/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002407 * Lookup a device given by device id, or the path if the id is 0.
2408 */
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002409struct btrfs_device *btrfs_find_device_by_devspec(
Anand Jain6e927ce2019-01-17 23:32:29 +08002410 struct btrfs_fs_info *fs_info, u64 devid,
2411 const char *device_path)
Anand Jain24e04742016-02-13 10:01:35 +08002412{
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002413 struct btrfs_device *device;
Anand Jain24e04742016-02-13 10:01:35 +08002414
David Sterba5c5c0df2016-02-15 16:39:55 +01002415 if (devid) {
Anand Jaine4319cd2019-01-17 23:32:31 +08002416 device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002417 NULL, true);
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002418 if (!device)
2419 return ERR_PTR(-ENOENT);
Anand Jain6e927ce2019-01-17 23:32:29 +08002420 return device;
Anand Jain24e04742016-02-13 10:01:35 +08002421 }
Anand Jain6e927ce2019-01-17 23:32:29 +08002422
2423 if (!device_path || !device_path[0])
2424 return ERR_PTR(-EINVAL);
2425
2426 if (strcmp(device_path, "missing") == 0) {
2427 /* Find first missing device */
2428 list_for_each_entry(device, &fs_info->fs_devices->devices,
2429 dev_list) {
2430 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2431 &device->dev_state) && !device->bdev)
2432 return device;
2433 }
2434 return ERR_PTR(-ENOENT);
2435 }
2436
2437 return btrfs_find_device_by_path(fs_info, device_path);
Anand Jain24e04742016-02-13 10:01:35 +08002438}
2439
Yan Zheng2b820322008-11-17 21:11:30 -05002440/*
2441 * does all the dirty work required for changing file system's UUID.
2442 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002443static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002444{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002445 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002446 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002447 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002448 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002449 struct btrfs_device *device;
2450 u64 super_flags;
2451
David Sterbaa32bf9a2018-03-16 02:21:22 +01002452 lockdep_assert_held(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002453 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002454 return -EINVAL;
2455
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002456 seed_devices = alloc_fs_devices(NULL, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002457 if (IS_ERR(seed_devices))
2458 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002459
Yan Zhenge4404d62008-12-12 10:03:26 -05002460 old_devices = clone_fs_devices(fs_devices);
2461 if (IS_ERR(old_devices)) {
2462 kfree(seed_devices);
2463 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002464 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002465
Anand Jainc4babc52018-04-12 10:29:25 +08002466 list_add(&old_devices->fs_list, &fs_uuids);
Yan Zheng2b820322008-11-17 21:11:30 -05002467
Yan Zhenge4404d62008-12-12 10:03:26 -05002468 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2469 seed_devices->opened = 1;
2470 INIT_LIST_HEAD(&seed_devices->devices);
2471 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002472 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002473
Anand Jain321a4bf2018-07-16 22:58:09 +08002474 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002475 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2476 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002477 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002478 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002479
David Sterba34441362016-10-04 19:34:27 +02002480 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002481 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002482 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002483
Yan Zheng2b820322008-11-17 21:11:30 -05002484 fs_devices->seeding = 0;
2485 fs_devices->num_devices = 0;
2486 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002487 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002488 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002489 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002490
2491 generate_random_uuid(fs_devices->fsid);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002492 memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002493 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Anand Jain321a4bf2018-07-16 22:58:09 +08002494 mutex_unlock(&fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002495
Yan Zheng2b820322008-11-17 21:11:30 -05002496 super_flags = btrfs_super_flags(disk_super) &
2497 ~BTRFS_SUPER_FLAG_SEEDING;
2498 btrfs_set_super_flags(disk_super, super_flags);
2499
2500 return 0;
2501}
2502
2503/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002504 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002505 */
2506static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002507 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002508{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002509 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002510 struct btrfs_path *path;
2511 struct extent_buffer *leaf;
2512 struct btrfs_dev_item *dev_item;
2513 struct btrfs_device *device;
2514 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002515 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002516 u8 dev_uuid[BTRFS_UUID_SIZE];
2517 u64 devid;
2518 int ret;
2519
2520 path = btrfs_alloc_path();
2521 if (!path)
2522 return -ENOMEM;
2523
Yan Zheng2b820322008-11-17 21:11:30 -05002524 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2525 key.offset = 0;
2526 key.type = BTRFS_DEV_ITEM_KEY;
2527
2528 while (1) {
2529 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2530 if (ret < 0)
2531 goto error;
2532
2533 leaf = path->nodes[0];
2534next_slot:
2535 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2536 ret = btrfs_next_leaf(root, path);
2537 if (ret > 0)
2538 break;
2539 if (ret < 0)
2540 goto error;
2541 leaf = path->nodes[0];
2542 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002543 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002544 continue;
2545 }
2546
2547 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2548 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2549 key.type != BTRFS_DEV_ITEM_KEY)
2550 break;
2551
2552 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2553 struct btrfs_dev_item);
2554 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002555 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002556 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002557 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002558 BTRFS_FSID_SIZE);
Anand Jaine4319cd2019-01-17 23:32:31 +08002559 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002560 fs_uuid, true);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002561 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002562
2563 if (device->fs_devices->seeding) {
2564 btrfs_set_device_generation(leaf, dev_item,
2565 device->generation);
2566 btrfs_mark_buffer_dirty(leaf);
2567 }
2568
2569 path->slots[0]++;
2570 goto next_slot;
2571 }
2572 ret = 0;
2573error:
2574 btrfs_free_path(path);
2575 return ret;
2576}
2577
David Sterbada353f62017-02-14 17:55:53 +01002578int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002579{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002580 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002581 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002582 struct btrfs_trans_handle *trans;
2583 struct btrfs_device *device;
2584 struct block_device *bdev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002585 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002586 struct rcu_string *name;
Anand Jain5da54bc2018-07-03 13:14:50 +08002587 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Naohiro Aota39379fa2018-07-27 09:04:55 +09002588 u64 orig_super_total_bytes;
2589 u64 orig_super_num_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002590 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002591 int ret = 0;
Anand Jain7132a262017-09-28 14:51:11 +08002592 bool unlocked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002593
Anand Jain5da54bc2018-07-03 13:14:50 +08002594 if (sb_rdonly(sb) && !fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002595 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002596
Li Zefana5d163332011-12-07 20:08:40 -05002597 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002598 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002599 if (IS_ERR(bdev))
2600 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002601
Anand Jain5da54bc2018-07-03 13:14:50 +08002602 if (fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002603 seeding_dev = 1;
2604 down_write(&sb->s_umount);
2605 mutex_lock(&uuid_mutex);
2606 }
2607
Chris Mason8c8bee12008-09-29 11:19:10 -04002608 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002609
Anand Jain5da54bc2018-07-03 13:14:50 +08002610 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain694c51f2018-07-03 13:14:51 +08002611 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002612 if (device->bdev == bdev) {
2613 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002614 mutex_unlock(
Anand Jain5da54bc2018-07-03 13:14:50 +08002615 &fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002616 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002617 }
2618 }
Anand Jain5da54bc2018-07-03 13:14:50 +08002619 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002620
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002621 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002622 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002623 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002624 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002625 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002626 }
2627
David Sterba78f2c9e2016-02-11 14:25:38 +01002628 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002629 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002630 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002631 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002632 }
Josef Bacik606686e2012-06-04 14:03:51 -04002633 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002634
Yan, Zhenga22285a2010-05-16 10:48:46 -04002635 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002636 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002637 ret = PTR_ERR(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002638 goto error_free_device;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002639 }
2640
Josef Bacikd5e20032011-08-04 14:52:27 +00002641 q = bdev_get_queue(bdev);
Anand Jainebbede42017-12-04 12:54:52 +08002642 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05002643 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002644 device->io_width = fs_info->sectorsize;
2645 device->io_align = fs_info->sectorsize;
2646 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002647 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2648 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002649 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002650 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002651 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002652 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002653 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002654 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002655 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002656 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002657 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002658
Yan Zheng2b820322008-11-17 21:11:30 -05002659 if (seeding_dev) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002660 sb->s_flags &= ~SB_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002661 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002662 if (ret) {
2663 btrfs_abort_transaction(trans, ret);
2664 goto error_trans;
2665 }
Yan Zheng2b820322008-11-17 21:11:30 -05002666 }
2667
Anand Jain5da54bc2018-07-03 13:14:50 +08002668 device->fs_devices = fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002669
Anand Jain5da54bc2018-07-03 13:14:50 +08002670 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002671 mutex_lock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002672 list_add_rcu(&device->dev_list, &fs_devices->devices);
2673 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
2674 fs_devices->num_devices++;
2675 fs_devices->open_devices++;
2676 fs_devices->rw_devices++;
2677 fs_devices->total_devices++;
2678 fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002679
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002680 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002681
Anand Jaine884f4f2017-04-04 18:40:19 +08002682 if (!blk_queue_nonrot(q))
Anand Jain5da54bc2018-07-03 13:14:50 +08002683 fs_devices->rotating = 1;
Chris Masonc2898112009-06-10 09:51:32 -04002684
Naohiro Aota39379fa2018-07-27 09:04:55 +09002685 orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002686 btrfs_set_super_total_bytes(fs_info->super_copy,
Naohiro Aota39379fa2018-07-27 09:04:55 +09002687 round_down(orig_super_total_bytes + device->total_bytes,
2688 fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002689
Naohiro Aota39379fa2018-07-27 09:04:55 +09002690 orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy);
2691 btrfs_set_super_num_devices(fs_info->super_copy,
2692 orig_super_num_devices + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002693
2694 /* add sysfs device entry */
Anand Jain5da54bc2018-07-03 13:14:50 +08002695 btrfs_sysfs_add_device_link(fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002696
Miao Xie2196d6e2014-09-03 21:35:41 +08002697 /*
2698 * we've got more storage, clear any full flags on the space
2699 * infos
2700 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002701 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002702
David Sterba34441362016-10-04 19:34:27 +02002703 mutex_unlock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002704 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002705
Yan Zheng2b820322008-11-17 21:11:30 -05002706 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002707 mutex_lock(&fs_info->chunk_mutex);
David Sterbae4a4dce2017-02-10 19:49:01 +01002708 ret = init_first_rw_device(trans, fs_info);
David Sterba34441362016-10-04 19:34:27 +02002709 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002710 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002711 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002712 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002713 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002714 }
2715
Nikolay Borisov8e87e852018-07-20 19:37:47 +03002716 ret = btrfs_add_dev_item(trans, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002717 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002718 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002719 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002720 }
2721
2722 if (seeding_dev) {
2723 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2724
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002725 ret = btrfs_finish_sprout(trans, fs_info);
David Sterba005d6422012-09-18 07:52:32 -06002726 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002727 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002728 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002729 }
Anand Jainb2373f22014-06-03 11:36:03 +08002730
2731 /* Sprouting would change fsid of the mounted root,
2732 * so rename the fsid on the sysfs
2733 */
2734 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
Nikolay Borisovde37aa52018-10-30 16:43:24 +02002735 fs_info->fs_devices->fsid);
Anand Jain5da54bc2018-07-03 13:14:50 +08002736 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002737 btrfs_warn(fs_info,
2738 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002739 }
2740
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002741 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002742
2743 if (seeding_dev) {
2744 mutex_unlock(&uuid_mutex);
2745 up_write(&sb->s_umount);
Anand Jain7132a262017-09-28 14:51:11 +08002746 unlocked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002747
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002748 if (ret) /* transaction commit */
2749 return ret;
2750
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002751 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002752 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002753 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002754 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002755 trans = btrfs_attach_transaction(root);
2756 if (IS_ERR(trans)) {
2757 if (PTR_ERR(trans) == -ENOENT)
2758 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002759 ret = PTR_ERR(trans);
2760 trans = NULL;
2761 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002762 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002763 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002764 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002765
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002766 /* Update ctime/mtime for libblkid */
2767 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002768 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002769
Anand Jaind31c32f2017-09-28 14:51:10 +08002770error_sysfs:
Anand Jain5da54bc2018-07-03 13:14:50 +08002771 btrfs_sysfs_rm_device_link(fs_devices, device);
Naohiro Aota39379fa2018-07-27 09:04:55 +09002772 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2773 mutex_lock(&fs_info->chunk_mutex);
2774 list_del_rcu(&device->dev_list);
2775 list_del(&device->dev_alloc_list);
2776 fs_info->fs_devices->num_devices--;
2777 fs_info->fs_devices->open_devices--;
2778 fs_info->fs_devices->rw_devices--;
2779 fs_info->fs_devices->total_devices--;
2780 fs_info->fs_devices->total_rw_bytes -= device->total_bytes;
2781 atomic64_sub(device->total_bytes, &fs_info->free_chunk_space);
2782 btrfs_set_super_total_bytes(fs_info->super_copy,
2783 orig_super_total_bytes);
2784 btrfs_set_super_num_devices(fs_info->super_copy,
2785 orig_super_num_devices);
2786 mutex_unlock(&fs_info->chunk_mutex);
2787 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002788error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002789 if (seeding_dev)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002790 sb->s_flags |= SB_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002791 if (trans)
2792 btrfs_end_transaction(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002793error_free_device:
David Sterbaa425f9d2018-03-20 15:47:33 +01002794 btrfs_free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002795error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002796 blkdev_put(bdev, FMODE_EXCL);
Anand Jain7132a262017-09-28 14:51:11 +08002797 if (seeding_dev && !unlocked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002798 mutex_unlock(&uuid_mutex);
2799 up_write(&sb->s_umount);
2800 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002801 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002802}
2803
Chris Masond3977122009-01-05 21:25:51 -05002804static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2805 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002806{
2807 int ret;
2808 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002809 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002810 struct btrfs_dev_item *dev_item;
2811 struct extent_buffer *leaf;
2812 struct btrfs_key key;
2813
Chris Mason0b86a832008-03-24 15:01:56 -04002814 path = btrfs_alloc_path();
2815 if (!path)
2816 return -ENOMEM;
2817
2818 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2819 key.type = BTRFS_DEV_ITEM_KEY;
2820 key.offset = device->devid;
2821
2822 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2823 if (ret < 0)
2824 goto out;
2825
2826 if (ret > 0) {
2827 ret = -ENOENT;
2828 goto out;
2829 }
2830
2831 leaf = path->nodes[0];
2832 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2833
2834 btrfs_set_device_id(leaf, dev_item, device->devid);
2835 btrfs_set_device_type(leaf, dev_item, device->type);
2836 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2837 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2838 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002839 btrfs_set_device_total_bytes(leaf, dev_item,
2840 btrfs_device_get_disk_total_bytes(device));
2841 btrfs_set_device_bytes_used(leaf, dev_item,
2842 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002843 btrfs_mark_buffer_dirty(leaf);
2844
2845out:
2846 btrfs_free_path(path);
2847 return ret;
2848}
2849
Miao Xie2196d6e2014-09-03 21:35:41 +08002850int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002851 struct btrfs_device *device, u64 new_size)
2852{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002853 struct btrfs_fs_info *fs_info = device->fs_info;
2854 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002855 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002856 u64 old_total;
2857 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002858
Anand Jainebbede42017-12-04 12:54:52 +08002859 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Yan Zheng2b820322008-11-17 21:11:30 -05002860 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002861
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002862 new_size = round_down(new_size, fs_info->sectorsize);
2863
David Sterba34441362016-10-04 19:34:27 +02002864 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002865 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002866 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002867
Stefan Behrens63a212a2012-11-05 18:29:28 +01002868 if (new_size <= device->total_bytes ||
Anand Jain401e29c2017-12-04 12:54:55 +08002869 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002870 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002871 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002872 }
Yan Zheng2b820322008-11-17 21:11:30 -05002873
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002874 fs_devices = fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002875
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002876 btrfs_set_super_total_bytes(super_copy,
2877 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002878 device->fs_devices->total_rw_bytes += diff;
2879
Miao Xie7cc8e582014-09-03 21:35:38 +08002880 btrfs_device_set_total_bytes(device, new_size);
2881 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002882 btrfs_clear_space_info_full(device->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002883 if (list_empty(&device->resized_list))
2884 list_add_tail(&device->resized_list,
2885 &fs_devices->resized_devices);
David Sterba34441362016-10-04 19:34:27 +02002886 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002887
Chris Mason8f18cf12008-04-25 16:53:30 -04002888 return btrfs_update_device(trans, device);
2889}
2890
Nikolay Borisovf4208792018-07-20 19:37:52 +03002891static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002892{
Nikolay Borisovf4208792018-07-20 19:37:52 +03002893 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002894 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002895 int ret;
2896 struct btrfs_path *path;
2897 struct btrfs_key key;
2898
Chris Mason8f18cf12008-04-25 16:53:30 -04002899 path = btrfs_alloc_path();
2900 if (!path)
2901 return -ENOMEM;
2902
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002903 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002904 key.offset = chunk_offset;
2905 key.type = BTRFS_CHUNK_ITEM_KEY;
2906
2907 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002908 if (ret < 0)
2909 goto out;
2910 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002911 btrfs_handle_fs_error(fs_info, -ENOENT,
2912 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002913 ret = -ENOENT;
2914 goto out;
2915 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002916
2917 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002918 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002919 btrfs_handle_fs_error(fs_info, ret,
2920 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002921out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002922 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002923 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002924}
2925
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002926static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002927{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002928 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002929 struct btrfs_disk_key *disk_key;
2930 struct btrfs_chunk *chunk;
2931 u8 *ptr;
2932 int ret = 0;
2933 u32 num_stripes;
2934 u32 array_size;
2935 u32 len = 0;
2936 u32 cur;
2937 struct btrfs_key key;
2938
David Sterba34441362016-10-04 19:34:27 +02002939 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002940 array_size = btrfs_super_sys_array_size(super_copy);
2941
2942 ptr = super_copy->sys_chunk_array;
2943 cur = 0;
2944
2945 while (cur < array_size) {
2946 disk_key = (struct btrfs_disk_key *)ptr;
2947 btrfs_disk_key_to_cpu(&key, disk_key);
2948
2949 len = sizeof(*disk_key);
2950
2951 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2952 chunk = (struct btrfs_chunk *)(ptr + len);
2953 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2954 len += btrfs_chunk_item_size(num_stripes);
2955 } else {
2956 ret = -EIO;
2957 break;
2958 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002959 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002960 key.offset == chunk_offset) {
2961 memmove(ptr, ptr + len, array_size - (cur + len));
2962 array_size -= len;
2963 btrfs_set_super_sys_array_size(super_copy, array_size);
2964 } else {
2965 ptr += len;
2966 cur += len;
2967 }
2968 }
David Sterba34441362016-10-04 19:34:27 +02002969 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002970 return ret;
2971}
2972
Omar Sandoval60ca8422018-05-16 16:34:31 -07002973/*
2974 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2975 * @logical: Logical block offset in bytes.
2976 * @length: Length of extent in bytes.
2977 *
2978 * Return: Chunk mapping or ERR_PTR.
2979 */
2980struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
2981 u64 logical, u64 length)
Liu Bo592d92e2017-03-14 13:33:55 -07002982{
2983 struct extent_map_tree *em_tree;
2984 struct extent_map *em;
2985
2986 em_tree = &fs_info->mapping_tree.map_tree;
2987 read_lock(&em_tree->lock);
2988 em = lookup_extent_mapping(em_tree, logical, length);
2989 read_unlock(&em_tree->lock);
2990
2991 if (!em) {
2992 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2993 logical, length);
2994 return ERR_PTR(-EINVAL);
2995 }
2996
2997 if (em->start > logical || em->start + em->len < logical) {
2998 btrfs_crit(fs_info,
2999 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
3000 logical, length, em->start, em->start + em->len);
3001 free_extent_map(em);
3002 return ERR_PTR(-EINVAL);
3003 }
3004
3005 /* callers are responsible for dropping em's ref. */
3006 return em;
3007}
3008
Nikolay Borisov97aff912018-07-20 19:37:53 +03003009int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04003010{
Nikolay Borisov97aff912018-07-20 19:37:53 +03003011 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003012 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003013 struct map_lookup *map;
3014 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003015 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003016 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003017
Omar Sandoval60ca8422018-05-16 16:34:31 -07003018 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07003019 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04003020 /*
3021 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08003022 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04003023 * do anything we still error out.
3024 */
3025 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07003026 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003027 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04003028 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02003029 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisov451a2c12018-06-20 15:49:07 +03003030 check_system_chunk(trans, map->type);
David Sterba34441362016-10-04 19:34:27 +02003031 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003032
Filipe Manana57ba4cb2016-05-20 04:34:23 +01003033 /*
3034 * Take the device list mutex to prevent races with the final phase of
3035 * a device replace operation that replaces the device object associated
3036 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
3037 */
3038 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003039 for (i = 0; i < map->num_stripes; i++) {
3040 struct btrfs_device *device = map->stripes[i].dev;
3041 ret = btrfs_free_dev_extent(trans, device,
3042 map->stripes[i].physical,
3043 &dev_extent_len);
3044 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01003045 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04003046 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003047 goto out;
3048 }
3049
3050 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02003051 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003052 btrfs_device_set_bytes_used(device,
3053 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03003054 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003055 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02003056 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003057 }
3058
Nikolay Borisov64bc6c22018-10-26 14:43:19 +03003059 ret = btrfs_update_device(trans, device);
3060 if (ret) {
3061 mutex_unlock(&fs_devices->device_list_mutex);
3062 btrfs_abort_transaction(trans, ret);
3063 goto out;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003064 }
3065 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01003066 mutex_unlock(&fs_devices->device_list_mutex);
3067
Nikolay Borisovf4208792018-07-20 19:37:52 +03003068 ret = btrfs_free_chunk(trans, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003069 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003070 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003071 goto out;
3072 }
3073
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003074 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003075
3076 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03003077 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003078 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003079 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003080 goto out;
3081 }
3082 }
3083
Nikolay Borisov5a98ec02018-06-20 15:48:56 +03003084 ret = btrfs_remove_block_group(trans, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003085 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003086 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003087 goto out;
3088 }
3089
Josef Bacik47ab2a62014-09-18 11:20:02 -04003090out:
3091 /* once for us */
3092 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04003093 return ret;
3094}
3095
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003096static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04003097{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003098 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07003099 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04003100 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003101
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003102 /*
3103 * Prevent races with automatic removal of unused block groups.
3104 * After we relocate and before we remove the chunk with offset
3105 * chunk_offset, automatic removal of the block group can kick in,
3106 * resulting in a failure when calling btrfs_remove_chunk() below.
3107 *
3108 * Make sure to acquire this mutex before doing a tree search (dev
3109 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
3110 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
3111 * we release the path used to search the chunk/dev tree and before
3112 * the current task acquires this mutex and calls us.
3113 */
David Sterbaa32bf9a2018-03-16 02:21:22 +01003114 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003115
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003116 ret = btrfs_can_relocate(fs_info, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003117 if (ret)
3118 return -ENOSPC;
3119
Chris Mason8f18cf12008-04-25 16:53:30 -04003120 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003121 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003122 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003123 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003124 if (ret)
3125 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003126
Jeff Mahoney75cb3792018-03-20 15:25:26 -04003127 /*
3128 * We add the kobjects here (and after forcing data chunk creation)
3129 * since relocation is the only place we'll create chunks of a new
3130 * type at runtime. The only place where we'll remove the last
3131 * chunk of a type is the call immediately below this one. Even
3132 * so, we're protected against races with the cleaner thread since
3133 * we're covered by the delete_unused_bgs_mutex.
3134 */
3135 btrfs_add_raid_kobjects(fs_info);
3136
Chris Mason19c4d2f2016-10-10 13:43:31 -07003137 trans = btrfs_start_trans_remove_block_group(root->fs_info,
3138 chunk_offset);
3139 if (IS_ERR(trans)) {
3140 ret = PTR_ERR(trans);
3141 btrfs_handle_fs_error(root->fs_info, ret, NULL);
3142 return ret;
3143 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09003144
Chris Mason19c4d2f2016-10-10 13:43:31 -07003145 /*
3146 * step two, delete the device extents and the
3147 * chunk tree entries
3148 */
Nikolay Borisov97aff912018-07-20 19:37:53 +03003149 ret = btrfs_remove_chunk(trans, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003150 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07003151 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003152}
3153
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003154static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05003155{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003156 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05003157 struct btrfs_path *path;
3158 struct extent_buffer *leaf;
3159 struct btrfs_chunk *chunk;
3160 struct btrfs_key key;
3161 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05003162 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04003163 bool retried = false;
3164 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05003165 int ret;
3166
3167 path = btrfs_alloc_path();
3168 if (!path)
3169 return -ENOMEM;
3170
Josef Bacikba1bf482009-09-11 16:11:19 -04003171again:
Yan Zheng2b820322008-11-17 21:11:30 -05003172 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3173 key.offset = (u64)-1;
3174 key.type = BTRFS_CHUNK_ITEM_KEY;
3175
3176 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003177 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003178 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003179 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003180 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003181 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003182 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003183 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05003184
3185 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3186 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003187 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003188 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003189 if (ret < 0)
3190 goto error;
3191 if (ret > 0)
3192 break;
3193
3194 leaf = path->nodes[0];
3195 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3196
3197 chunk = btrfs_item_ptr(leaf, path->slots[0],
3198 struct btrfs_chunk);
3199 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003200 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003201
3202 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003203 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003204 if (ret == -ENOSPC)
3205 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303206 else
3207 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003208 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003209 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003210
3211 if (found_key.offset == 0)
3212 break;
3213 key.offset = found_key.offset - 1;
3214 }
3215 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003216 if (failed && !retried) {
3217 failed = 0;
3218 retried = true;
3219 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303220 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003221 ret = -ENOSPC;
3222 }
Yan Zheng2b820322008-11-17 21:11:30 -05003223error:
3224 btrfs_free_path(path);
3225 return ret;
3226}
3227
Liu Boa6f93c72017-11-15 16:28:11 -07003228/*
3229 * return 1 : allocate a data chunk successfully,
3230 * return <0: errors during allocating a data chunk,
3231 * return 0 : no need to allocate a data chunk.
3232 */
3233static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3234 u64 chunk_offset)
3235{
3236 struct btrfs_block_group_cache *cache;
3237 u64 bytes_used;
3238 u64 chunk_type;
3239
3240 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3241 ASSERT(cache);
3242 chunk_type = cache->flags;
3243 btrfs_put_block_group(cache);
3244
3245 if (chunk_type & BTRFS_BLOCK_GROUP_DATA) {
3246 spin_lock(&fs_info->data_sinfo->lock);
3247 bytes_used = fs_info->data_sinfo->bytes_used;
3248 spin_unlock(&fs_info->data_sinfo->lock);
3249
3250 if (!bytes_used) {
3251 struct btrfs_trans_handle *trans;
3252 int ret;
3253
3254 trans = btrfs_join_transaction(fs_info->tree_root);
3255 if (IS_ERR(trans))
3256 return PTR_ERR(trans);
3257
Nikolay Borisov43a7e992018-06-20 15:49:15 +03003258 ret = btrfs_force_chunk_alloc(trans,
Liu Boa6f93c72017-11-15 16:28:11 -07003259 BTRFS_BLOCK_GROUP_DATA);
3260 btrfs_end_transaction(trans);
3261 if (ret < 0)
3262 return ret;
3263
Jeff Mahoney75cb3792018-03-20 15:25:26 -04003264 btrfs_add_raid_kobjects(fs_info);
3265
Liu Boa6f93c72017-11-15 16:28:11 -07003266 return 1;
3267 }
3268 }
3269 return 0;
3270}
3271
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003272static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003273 struct btrfs_balance_control *bctl)
3274{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003275 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003276 struct btrfs_trans_handle *trans;
3277 struct btrfs_balance_item *item;
3278 struct btrfs_disk_balance_args disk_bargs;
3279 struct btrfs_path *path;
3280 struct extent_buffer *leaf;
3281 struct btrfs_key key;
3282 int ret, err;
3283
3284 path = btrfs_alloc_path();
3285 if (!path)
3286 return -ENOMEM;
3287
3288 trans = btrfs_start_transaction(root, 0);
3289 if (IS_ERR(trans)) {
3290 btrfs_free_path(path);
3291 return PTR_ERR(trans);
3292 }
3293
3294 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003295 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003296 key.offset = 0;
3297
3298 ret = btrfs_insert_empty_item(trans, root, path, &key,
3299 sizeof(*item));
3300 if (ret)
3301 goto out;
3302
3303 leaf = path->nodes[0];
3304 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3305
David Sterbab159fa22016-11-08 18:09:03 +01003306 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003307
3308 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3309 btrfs_set_balance_data(leaf, item, &disk_bargs);
3310 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3311 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3312 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3313 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3314
3315 btrfs_set_balance_flags(leaf, item, bctl->flags);
3316
3317 btrfs_mark_buffer_dirty(leaf);
3318out:
3319 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003320 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003321 if (err && !ret)
3322 ret = err;
3323 return ret;
3324}
3325
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003326static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003327{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003328 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003329 struct btrfs_trans_handle *trans;
3330 struct btrfs_path *path;
3331 struct btrfs_key key;
3332 int ret, err;
3333
3334 path = btrfs_alloc_path();
3335 if (!path)
3336 return -ENOMEM;
3337
3338 trans = btrfs_start_transaction(root, 0);
3339 if (IS_ERR(trans)) {
3340 btrfs_free_path(path);
3341 return PTR_ERR(trans);
3342 }
3343
3344 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003345 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003346 key.offset = 0;
3347
3348 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3349 if (ret < 0)
3350 goto out;
3351 if (ret > 0) {
3352 ret = -ENOENT;
3353 goto out;
3354 }
3355
3356 ret = btrfs_del_item(trans, root, path);
3357out:
3358 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003359 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003360 if (err && !ret)
3361 ret = err;
3362 return ret;
3363}
3364
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003365/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003366 * This is a heuristic used to reduce the number of chunks balanced on
3367 * resume after balance was interrupted.
3368 */
3369static void update_balance_args(struct btrfs_balance_control *bctl)
3370{
3371 /*
3372 * Turn on soft mode for chunk types that were being converted.
3373 */
3374 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3375 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3376 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3377 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3378 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3379 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3380
3381 /*
3382 * Turn on usage filter if is not already used. The idea is
3383 * that chunks that we have already balanced should be
3384 * reasonably full. Don't do it for chunks that are being
3385 * converted - that will keep us from relocating unconverted
3386 * (albeit full) chunks.
3387 */
3388 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003389 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003390 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3391 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3392 bctl->data.usage = 90;
3393 }
3394 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003395 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003396 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3397 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3398 bctl->sys.usage = 90;
3399 }
3400 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003401 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003402 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3403 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3404 bctl->meta.usage = 90;
3405 }
3406}
3407
3408/*
David Sterba149196a2018-03-20 20:23:09 +01003409 * Clear the balance status in fs_info and delete the balance item from disk.
3410 */
3411static void reset_balance_state(struct btrfs_fs_info *fs_info)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003412{
3413 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
David Sterba149196a2018-03-20 20:23:09 +01003414 int ret;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003415
3416 BUG_ON(!fs_info->balance_ctl);
3417
3418 spin_lock(&fs_info->balance_lock);
3419 fs_info->balance_ctl = NULL;
3420 spin_unlock(&fs_info->balance_lock);
3421
3422 kfree(bctl);
David Sterba149196a2018-03-20 20:23:09 +01003423 ret = del_balance_item(fs_info);
3424 if (ret)
3425 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003426}
3427
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003428/*
3429 * Balance filters. Return 1 if chunk should be filtered out
3430 * (should not be balanced).
3431 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003432static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003433 struct btrfs_balance_args *bargs)
3434{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003435 chunk_type = chunk_to_extended(chunk_type) &
3436 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003437
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003438 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003439 return 0;
3440
3441 return 1;
3442}
3443
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003444static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003445 struct btrfs_balance_args *bargs)
3446{
3447 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003448 u64 chunk_used;
3449 u64 user_thresh_min;
3450 u64 user_thresh_max;
3451 int ret = 1;
3452
3453 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3454 chunk_used = btrfs_block_group_used(&cache->item);
3455
3456 if (bargs->usage_min == 0)
3457 user_thresh_min = 0;
3458 else
3459 user_thresh_min = div_factor_fine(cache->key.offset,
3460 bargs->usage_min);
3461
3462 if (bargs->usage_max == 0)
3463 user_thresh_max = 1;
3464 else if (bargs->usage_max > 100)
3465 user_thresh_max = cache->key.offset;
3466 else
3467 user_thresh_max = div_factor_fine(cache->key.offset,
3468 bargs->usage_max);
3469
3470 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3471 ret = 0;
3472
3473 btrfs_put_block_group(cache);
3474 return ret;
3475}
3476
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003477static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003478 u64 chunk_offset, struct btrfs_balance_args *bargs)
3479{
3480 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003481 u64 chunk_used, user_thresh;
3482 int ret = 1;
3483
3484 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3485 chunk_used = btrfs_block_group_used(&cache->item);
3486
David Sterbabc309462015-10-20 18:22:13 +02003487 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003488 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003489 else if (bargs->usage > 100)
3490 user_thresh = cache->key.offset;
3491 else
3492 user_thresh = div_factor_fine(cache->key.offset,
3493 bargs->usage);
3494
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003495 if (chunk_used < user_thresh)
3496 ret = 0;
3497
3498 btrfs_put_block_group(cache);
3499 return ret;
3500}
3501
Ilya Dryomov409d4042012-01-16 22:04:47 +02003502static int chunk_devid_filter(struct extent_buffer *leaf,
3503 struct btrfs_chunk *chunk,
3504 struct btrfs_balance_args *bargs)
3505{
3506 struct btrfs_stripe *stripe;
3507 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3508 int i;
3509
3510 for (i = 0; i < num_stripes; i++) {
3511 stripe = btrfs_stripe_nr(chunk, i);
3512 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3513 return 0;
3514 }
3515
3516 return 1;
3517}
3518
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003519/* [pstart, pend) */
3520static int chunk_drange_filter(struct extent_buffer *leaf,
3521 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003522 struct btrfs_balance_args *bargs)
3523{
3524 struct btrfs_stripe *stripe;
3525 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3526 u64 stripe_offset;
3527 u64 stripe_length;
3528 int factor;
3529 int i;
3530
3531 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3532 return 0;
3533
3534 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003535 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3536 factor = num_stripes / 2;
3537 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3538 factor = num_stripes - 1;
3539 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3540 factor = num_stripes - 2;
3541 } else {
3542 factor = num_stripes;
3543 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003544
3545 for (i = 0; i < num_stripes; i++) {
3546 stripe = btrfs_stripe_nr(chunk, i);
3547 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3548 continue;
3549
3550 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3551 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003552 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003553
3554 if (stripe_offset < bargs->pend &&
3555 stripe_offset + stripe_length > bargs->pstart)
3556 return 0;
3557 }
3558
3559 return 1;
3560}
3561
Ilya Dryomovea671762012-01-16 22:04:48 +02003562/* [vstart, vend) */
3563static int chunk_vrange_filter(struct extent_buffer *leaf,
3564 struct btrfs_chunk *chunk,
3565 u64 chunk_offset,
3566 struct btrfs_balance_args *bargs)
3567{
3568 if (chunk_offset < bargs->vend &&
3569 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3570 /* at least part of the chunk is inside this vrange */
3571 return 0;
3572
3573 return 1;
3574}
3575
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003576static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3577 struct btrfs_chunk *chunk,
3578 struct btrfs_balance_args *bargs)
3579{
3580 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3581
3582 if (bargs->stripes_min <= num_stripes
3583 && num_stripes <= bargs->stripes_max)
3584 return 0;
3585
3586 return 1;
3587}
3588
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003589static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003590 struct btrfs_balance_args *bargs)
3591{
3592 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3593 return 0;
3594
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003595 chunk_type = chunk_to_extended(chunk_type) &
3596 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003597
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003598 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003599 return 1;
3600
3601 return 0;
3602}
3603
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003604static int should_balance_chunk(struct btrfs_fs_info *fs_info,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003605 struct extent_buffer *leaf,
3606 struct btrfs_chunk *chunk, u64 chunk_offset)
3607{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003608 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003609 struct btrfs_balance_args *bargs = NULL;
3610 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3611
3612 /* type filter */
3613 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3614 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3615 return 0;
3616 }
3617
3618 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3619 bargs = &bctl->data;
3620 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3621 bargs = &bctl->sys;
3622 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3623 bargs = &bctl->meta;
3624
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003625 /* profiles filter */
3626 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3627 chunk_profiles_filter(chunk_type, bargs)) {
3628 return 0;
3629 }
3630
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003631 /* usage filter */
3632 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003633 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003634 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003635 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003636 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003637 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003638 }
3639
Ilya Dryomov409d4042012-01-16 22:04:47 +02003640 /* devid filter */
3641 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3642 chunk_devid_filter(leaf, chunk, bargs)) {
3643 return 0;
3644 }
3645
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003646 /* drange filter, makes sense only with devid filter */
3647 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003648 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003649 return 0;
3650 }
3651
Ilya Dryomovea671762012-01-16 22:04:48 +02003652 /* vrange filter */
3653 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3654 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3655 return 0;
3656 }
3657
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003658 /* stripes filter */
3659 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3660 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3661 return 0;
3662 }
3663
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003664 /* soft profile changing mode */
3665 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3666 chunk_soft_convert_filter(chunk_type, bargs)) {
3667 return 0;
3668 }
3669
David Sterba7d824b62014-05-07 17:37:51 +02003670 /*
3671 * limited by count, must be the last filter
3672 */
3673 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3674 if (bargs->limit == 0)
3675 return 0;
3676 else
3677 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003678 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3679 /*
3680 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003681 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003682 * about the count of all chunks that satisfy the filters.
3683 */
3684 if (bargs->limit_max == 0)
3685 return 0;
3686 else
3687 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003688 }
3689
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003690 return 1;
3691}
3692
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003693static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003694{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003695 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003696 struct btrfs_root *chunk_root = fs_info->chunk_root;
David Sterba12907fc2015-10-10 17:16:50 +02003697 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003698 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003699 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003700 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003701 struct btrfs_key found_key;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003702 struct extent_buffer *leaf;
3703 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003704 int ret;
3705 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003706 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003707 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003708 u64 limit_data = bctl->data.limit;
3709 u64 limit_meta = bctl->meta.limit;
3710 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003711 u32 count_data = 0;
3712 u32 count_meta = 0;
3713 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003714 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003715
Chris Masonec44a352008-04-28 15:29:52 -04003716 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003717 if (!path) {
3718 ret = -ENOMEM;
3719 goto error;
3720 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003721
3722 /* zero out stat counters */
3723 spin_lock(&fs_info->balance_lock);
3724 memset(&bctl->stat, 0, sizeof(bctl->stat));
3725 spin_unlock(&fs_info->balance_lock);
3726again:
David Sterba7d824b62014-05-07 17:37:51 +02003727 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003728 /*
3729 * The single value limit and min/max limits use the same bytes
3730 * in the
3731 */
David Sterba7d824b62014-05-07 17:37:51 +02003732 bctl->data.limit = limit_data;
3733 bctl->meta.limit = limit_meta;
3734 bctl->sys.limit = limit_sys;
3735 }
Chris Masonec44a352008-04-28 15:29:52 -04003736 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3737 key.offset = (u64)-1;
3738 key.type = BTRFS_CHUNK_ITEM_KEY;
3739
Chris Masond3977122009-01-05 21:25:51 -05003740 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003741 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003742 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003743 ret = -ECANCELED;
3744 goto error;
3745 }
3746
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003747 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003748 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003749 if (ret < 0) {
3750 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003751 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003752 }
Chris Masonec44a352008-04-28 15:29:52 -04003753
3754 /*
3755 * this shouldn't happen, it means the last relocate
3756 * failed
3757 */
3758 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003759 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003760
3761 ret = btrfs_previous_item(chunk_root, path, 0,
3762 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003763 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003764 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003765 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003766 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003767 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003768
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003769 leaf = path->nodes[0];
3770 slot = path->slots[0];
3771 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3772
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003773 if (found_key.objectid != key.objectid) {
3774 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003775 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003776 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003777
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003778 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003779 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003780
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003781 if (!counting) {
3782 spin_lock(&fs_info->balance_lock);
3783 bctl->stat.considered++;
3784 spin_unlock(&fs_info->balance_lock);
3785 }
3786
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003787 ret = should_balance_chunk(fs_info, leaf, chunk,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003788 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003789
David Sterbab3b4aa72011-04-21 01:20:15 +02003790 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003791 if (!ret) {
3792 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003793 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003794 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003795
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003796 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003797 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003798 spin_lock(&fs_info->balance_lock);
3799 bctl->stat.expected++;
3800 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003801
3802 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3803 count_data++;
3804 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3805 count_sys++;
3806 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3807 count_meta++;
3808
3809 goto loop;
3810 }
3811
3812 /*
3813 * Apply limit_min filter, no need to check if the LIMITS
3814 * filter is used, limit_min is 0 by default
3815 */
3816 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3817 count_data < bctl->data.limit_min)
3818 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3819 count_meta < bctl->meta.limit_min)
3820 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3821 count_sys < bctl->sys.limit_min)) {
3822 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003823 goto loop;
3824 }
3825
Liu Boa6f93c72017-11-15 16:28:11 -07003826 if (!chunk_reserved) {
3827 /*
3828 * We may be relocating the only data chunk we have,
3829 * which could potentially end up with losing data's
3830 * raid profile, so lets allocate an empty one in
3831 * advance.
3832 */
3833 ret = btrfs_may_alloc_data_chunk(fs_info,
3834 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003835 if (ret < 0) {
3836 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3837 goto error;
Liu Boa6f93c72017-11-15 16:28:11 -07003838 } else if (ret == 1) {
3839 chunk_reserved = 1;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003840 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003841 }
3842
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003843 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003844 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003845 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003846 enospc_errors++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07003847 } else if (ret == -ETXTBSY) {
3848 btrfs_info(fs_info,
3849 "skipping relocation of block group %llu due to active swapfile",
3850 found_key.offset);
3851 ret = 0;
3852 } else if (ret) {
3853 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003854 } else {
3855 spin_lock(&fs_info->balance_lock);
3856 bctl->stat.completed++;
3857 spin_unlock(&fs_info->balance_lock);
3858 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003859loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003860 if (found_key.offset == 0)
3861 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003862 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003863 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003864
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003865 if (counting) {
3866 btrfs_release_path(path);
3867 counting = false;
3868 goto again;
3869 }
Chris Masonec44a352008-04-28 15:29:52 -04003870error:
3871 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003872 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003873 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003874 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003875 if (!ret)
3876 ret = -ENOSPC;
3877 }
3878
Chris Masonec44a352008-04-28 15:29:52 -04003879 return ret;
3880}
3881
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003882/**
3883 * alloc_profile_is_valid - see if a given profile is valid and reduced
3884 * @flags: profile to validate
3885 * @extended: if true @flags is treated as an extended profile
3886 */
3887static int alloc_profile_is_valid(u64 flags, int extended)
3888{
3889 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3890 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3891
3892 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3893
3894 /* 1) check that all other bits are zeroed */
3895 if (flags & ~mask)
3896 return 0;
3897
3898 /* 2) see if profile is reduced */
3899 if (flags == 0)
3900 return !extended; /* "0" is valid for usual profiles */
3901
3902 /* true if exactly one bit set */
David Sterba818255f2018-09-21 14:26:34 +02003903 return is_power_of_2(flags);
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003904}
3905
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003906static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3907{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003908 /* cancel requested || normal exit path */
3909 return atomic_read(&fs_info->balance_cancel_req) ||
3910 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3911 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003912}
3913
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003914/* Non-zero return value signifies invalidity */
3915static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3916 u64 allowed)
3917{
3918 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3919 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3920 (bctl_arg->target & ~allowed)));
3921}
3922
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003923/*
Anand Jain56fc37d2018-11-20 16:12:56 +08003924 * Fill @buf with textual description of balance filter flags @bargs, up to
3925 * @size_buf including the terminating null. The output may be trimmed if it
3926 * does not fit into the provided buffer.
3927 */
3928static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf,
3929 u32 size_buf)
3930{
3931 int ret;
3932 u32 size_bp = size_buf;
3933 char *bp = buf;
3934 u64 flags = bargs->flags;
3935 char tmp_buf[128] = {'\0'};
3936
3937 if (!flags)
3938 return;
3939
3940#define CHECK_APPEND_NOARG(a) \
3941 do { \
3942 ret = snprintf(bp, size_bp, (a)); \
3943 if (ret < 0 || ret >= size_bp) \
3944 goto out_overflow; \
3945 size_bp -= ret; \
3946 bp += ret; \
3947 } while (0)
3948
3949#define CHECK_APPEND_1ARG(a, v1) \
3950 do { \
3951 ret = snprintf(bp, size_bp, (a), (v1)); \
3952 if (ret < 0 || ret >= size_bp) \
3953 goto out_overflow; \
3954 size_bp -= ret; \
3955 bp += ret; \
3956 } while (0)
3957
3958#define CHECK_APPEND_2ARG(a, v1, v2) \
3959 do { \
3960 ret = snprintf(bp, size_bp, (a), (v1), (v2)); \
3961 if (ret < 0 || ret >= size_bp) \
3962 goto out_overflow; \
3963 size_bp -= ret; \
3964 bp += ret; \
3965 } while (0)
3966
3967 if (flags & BTRFS_BALANCE_ARGS_CONVERT) {
3968 int index = btrfs_bg_flags_to_raid_index(bargs->target);
3969
3970 CHECK_APPEND_1ARG("convert=%s,", get_raid_name(index));
3971 }
3972
3973 if (flags & BTRFS_BALANCE_ARGS_SOFT)
3974 CHECK_APPEND_NOARG("soft,");
3975
3976 if (flags & BTRFS_BALANCE_ARGS_PROFILES) {
3977 btrfs_describe_block_groups(bargs->profiles, tmp_buf,
3978 sizeof(tmp_buf));
3979 CHECK_APPEND_1ARG("profiles=%s,", tmp_buf);
3980 }
3981
3982 if (flags & BTRFS_BALANCE_ARGS_USAGE)
3983 CHECK_APPEND_1ARG("usage=%llu,", bargs->usage);
3984
3985 if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE)
3986 CHECK_APPEND_2ARG("usage=%u..%u,",
3987 bargs->usage_min, bargs->usage_max);
3988
3989 if (flags & BTRFS_BALANCE_ARGS_DEVID)
3990 CHECK_APPEND_1ARG("devid=%llu,", bargs->devid);
3991
3992 if (flags & BTRFS_BALANCE_ARGS_DRANGE)
3993 CHECK_APPEND_2ARG("drange=%llu..%llu,",
3994 bargs->pstart, bargs->pend);
3995
3996 if (flags & BTRFS_BALANCE_ARGS_VRANGE)
3997 CHECK_APPEND_2ARG("vrange=%llu..%llu,",
3998 bargs->vstart, bargs->vend);
3999
4000 if (flags & BTRFS_BALANCE_ARGS_LIMIT)
4001 CHECK_APPEND_1ARG("limit=%llu,", bargs->limit);
4002
4003 if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)
4004 CHECK_APPEND_2ARG("limit=%u..%u,",
4005 bargs->limit_min, bargs->limit_max);
4006
4007 if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE)
4008 CHECK_APPEND_2ARG("stripes=%u..%u,",
4009 bargs->stripes_min, bargs->stripes_max);
4010
4011#undef CHECK_APPEND_2ARG
4012#undef CHECK_APPEND_1ARG
4013#undef CHECK_APPEND_NOARG
4014
4015out_overflow:
4016
4017 if (size_bp < size_buf)
4018 buf[size_buf - size_bp - 1] = '\0'; /* remove last , */
4019 else
4020 buf[0] = '\0';
4021}
4022
4023static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info)
4024{
4025 u32 size_buf = 1024;
4026 char tmp_buf[192] = {'\0'};
4027 char *buf;
4028 char *bp;
4029 u32 size_bp = size_buf;
4030 int ret;
4031 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4032
4033 buf = kzalloc(size_buf, GFP_KERNEL);
4034 if (!buf)
4035 return;
4036
4037 bp = buf;
4038
4039#define CHECK_APPEND_1ARG(a, v1) \
4040 do { \
4041 ret = snprintf(bp, size_bp, (a), (v1)); \
4042 if (ret < 0 || ret >= size_bp) \
4043 goto out_overflow; \
4044 size_bp -= ret; \
4045 bp += ret; \
4046 } while (0)
4047
4048 if (bctl->flags & BTRFS_BALANCE_FORCE)
4049 CHECK_APPEND_1ARG("%s", "-f ");
4050
4051 if (bctl->flags & BTRFS_BALANCE_DATA) {
4052 describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf));
4053 CHECK_APPEND_1ARG("-d%s ", tmp_buf);
4054 }
4055
4056 if (bctl->flags & BTRFS_BALANCE_METADATA) {
4057 describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf));
4058 CHECK_APPEND_1ARG("-m%s ", tmp_buf);
4059 }
4060
4061 if (bctl->flags & BTRFS_BALANCE_SYSTEM) {
4062 describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf));
4063 CHECK_APPEND_1ARG("-s%s ", tmp_buf);
4064 }
4065
4066#undef CHECK_APPEND_1ARG
4067
4068out_overflow:
4069
4070 if (size_bp < size_buf)
4071 buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */
4072 btrfs_info(fs_info, "balance: %s %s",
4073 (bctl->flags & BTRFS_BALANCE_RESUME) ?
4074 "resume" : "start", buf);
4075
4076 kfree(buf);
4077}
4078
4079/*
David Sterbadccdb072018-03-21 00:20:05 +01004080 * Should be called with balance mutexe held
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004081 */
David Sterba6fcf6e22018-05-07 17:44:03 +02004082int btrfs_balance(struct btrfs_fs_info *fs_info,
4083 struct btrfs_balance_control *bctl,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004084 struct btrfs_ioctl_balance_args *bargs)
4085{
Adam Borowski14506122017-03-07 23:34:44 +01004086 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004087 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03004088 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004089 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01004090 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00004091 unsigned seq;
Filipe Manana5a8067c2018-11-19 09:48:12 +00004092 bool reducing_integrity;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004093
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004094 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004095 atomic_read(&fs_info->balance_pause_req) ||
4096 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004097 ret = -EINVAL;
4098 goto out;
4099 }
4100
Ilya Dryomove4837f82012-03-27 17:09:17 +03004101 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
4102 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
4103 mixed = 1;
4104
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004105 /*
4106 * In case of mixed groups both data and meta should be picked,
4107 * and identical options should be given for both of them.
4108 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03004109 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
4110 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004111 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
4112 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
4113 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004114 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004115 "balance: mixed groups data and metadata options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004116 ret = -EINVAL;
4117 goto out;
4118 }
4119 }
4120
Anand Jain1da73962018-08-10 13:53:21 +08004121 num_devices = btrfs_num_devices(fs_info);
4122
Austin S. Hemmelgarn88be1592016-03-23 14:22:59 -04004123 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
4124 if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004125 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00004126 if (num_devices > 2)
4127 allowed |= BTRFS_BLOCK_GROUP_RAID5;
4128 if (num_devices > 3)
4129 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
4130 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00004131 if (validate_convert_profile(&bctl->data, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004132 int index = btrfs_bg_flags_to_raid_index(bctl->data.target);
4133
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004134 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004135 "balance: invalid convert data profile %s",
4136 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004137 ret = -EINVAL;
4138 goto out;
4139 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00004140 if (validate_convert_profile(&bctl->meta, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004141 int index = btrfs_bg_flags_to_raid_index(bctl->meta.target);
4142
Frank Holtonefe120a2013-12-20 11:37:06 -05004143 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004144 "balance: invalid convert metadata profile %s",
4145 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004146 ret = -EINVAL;
4147 goto out;
4148 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00004149 if (validate_convert_profile(&bctl->sys, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004150 int index = btrfs_bg_flags_to_raid_index(bctl->sys.target);
4151
Frank Holtonefe120a2013-12-20 11:37:06 -05004152 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004153 "balance: invalid convert system profile %s",
4154 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004155 ret = -EINVAL;
4156 goto out;
4157 }
4158
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004159 /* allow to reduce meta or sys integrity only if force set */
4160 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05004161 BTRFS_BLOCK_GROUP_RAID10 |
4162 BTRFS_BLOCK_GROUP_RAID5 |
4163 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00004164 do {
4165 seq = read_seqbegin(&fs_info->profiles_lock);
4166
4167 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4168 (fs_info->avail_system_alloc_bits & allowed) &&
4169 !(bctl->sys.target & allowed)) ||
4170 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4171 (fs_info->avail_metadata_alloc_bits & allowed) &&
Filipe Manana5a8067c2018-11-19 09:48:12 +00004172 !(bctl->meta.target & allowed)))
4173 reducing_integrity = true;
4174 else
4175 reducing_integrity = false;
4176
4177 /* if we're not converting, the target field is uninitialized */
4178 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4179 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
4180 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4181 bctl->data.target : fs_info->avail_data_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004182 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004183
Filipe Manana5a8067c2018-11-19 09:48:12 +00004184 if (reducing_integrity) {
4185 if (bctl->flags & BTRFS_BALANCE_FORCE) {
4186 btrfs_info(fs_info,
4187 "balance: force reducing metadata integrity");
4188 } else {
4189 btrfs_err(fs_info,
4190 "balance: reduces metadata integrity, use --force if you want this");
4191 ret = -EINVAL;
4192 goto out;
4193 }
4194 }
4195
Adam Borowski14506122017-03-07 23:34:44 +01004196 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
4197 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004198 int meta_index = btrfs_bg_flags_to_raid_index(meta_target);
4199 int data_index = btrfs_bg_flags_to_raid_index(data_target);
4200
Sam Tygieree592d02016-01-06 08:46:12 +00004201 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004202 "balance: metadata profile %s has lower redundancy than data profile %s",
4203 get_raid_name(meta_index), get_raid_name(data_index));
Sam Tygieree592d02016-01-06 08:46:12 +00004204 }
4205
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004206 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02004207 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02004208 goto out;
4209
Ilya Dryomov59641012012-01-16 22:04:48 +02004210 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
4211 BUG_ON(ret == -EEXIST);
David Sterba833aae12018-03-21 02:41:30 +01004212 BUG_ON(fs_info->balance_ctl);
4213 spin_lock(&fs_info->balance_lock);
4214 fs_info->balance_ctl = bctl;
4215 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov59641012012-01-16 22:04:48 +02004216 } else {
4217 BUG_ON(ret != -EEXIST);
4218 spin_lock(&fs_info->balance_lock);
4219 update_balance_args(bctl);
4220 spin_unlock(&fs_info->balance_lock);
4221 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004222
David Sterba3009a622018-03-21 01:31:04 +01004223 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4224 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Anand Jain56fc37d2018-11-20 16:12:56 +08004225 describe_balance_start_or_resume(fs_info);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004226 mutex_unlock(&fs_info->balance_mutex);
4227
4228 ret = __btrfs_balance(fs_info);
4229
4230 mutex_lock(&fs_info->balance_mutex);
Anand Jain7333bd02018-11-20 16:12:57 +08004231 if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req))
4232 btrfs_info(fs_info, "balance: paused");
4233 else if (ret == -ECANCELED && atomic_read(&fs_info->balance_cancel_req))
4234 btrfs_info(fs_info, "balance: canceled");
4235 else
4236 btrfs_info(fs_info, "balance: ended with status: %d", ret);
4237
David Sterba3009a622018-03-21 01:31:04 +01004238 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004239
4240 if (bargs) {
4241 memset(bargs, 0, sizeof(*bargs));
David Sterba008ef092018-03-21 02:05:27 +01004242 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004243 }
4244
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004245 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
4246 balance_need_close(fs_info)) {
David Sterba149196a2018-03-20 20:23:09 +01004247 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004248 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004249 }
4250
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004251 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004252
4253 return ret;
4254out:
Ilya Dryomov59641012012-01-16 22:04:48 +02004255 if (bctl->flags & BTRFS_BALANCE_RESUME)
David Sterba149196a2018-03-20 20:23:09 +01004256 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004257 else
Ilya Dryomov59641012012-01-16 22:04:48 +02004258 kfree(bctl);
David Sterbaa17c95d2018-03-20 17:28:05 +01004259 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4260
Ilya Dryomov59641012012-01-16 22:04:48 +02004261 return ret;
4262}
4263
4264static int balance_kthread(void *data)
4265{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004266 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02004267 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02004268
Ilya Dryomov59641012012-01-16 22:04:48 +02004269 mutex_lock(&fs_info->balance_mutex);
Anand Jain56fc37d2018-11-20 16:12:56 +08004270 if (fs_info->balance_ctl)
David Sterba6fcf6e22018-05-07 17:44:03 +02004271 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
Ilya Dryomov59641012012-01-16 22:04:48 +02004272 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004273
Ilya Dryomov59641012012-01-16 22:04:48 +02004274 return ret;
4275}
4276
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004277int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
4278{
4279 struct task_struct *tsk;
4280
David Sterba1354e1a2018-03-21 02:29:13 +01004281 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004282 if (!fs_info->balance_ctl) {
David Sterba1354e1a2018-03-21 02:29:13 +01004283 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004284 return 0;
4285 }
David Sterba1354e1a2018-03-21 02:29:13 +01004286 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004287
Jeff Mahoney3cdde222016-06-09 21:38:35 -04004288 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004289 btrfs_info(fs_info, "balance: resume skipped");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004290 return 0;
4291 }
4292
Anand Jain02ee6542018-05-17 15:16:51 +08004293 /*
4294 * A ro->rw remount sequence should continue with the paused balance
4295 * regardless of who pauses it, system or the user as of now, so set
4296 * the resume flag.
4297 */
4298 spin_lock(&fs_info->balance_lock);
4299 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
4300 spin_unlock(&fs_info->balance_lock);
4301
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004302 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05304303 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004304}
4305
Ilya Dryomov68310a52012-06-22 12:24:12 -06004306int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02004307{
Ilya Dryomov59641012012-01-16 22:04:48 +02004308 struct btrfs_balance_control *bctl;
4309 struct btrfs_balance_item *item;
4310 struct btrfs_disk_balance_args disk_bargs;
4311 struct btrfs_path *path;
4312 struct extent_buffer *leaf;
4313 struct btrfs_key key;
4314 int ret;
4315
4316 path = btrfs_alloc_path();
4317 if (!path)
4318 return -ENOMEM;
4319
Ilya Dryomov68310a52012-06-22 12:24:12 -06004320 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01004321 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06004322 key.offset = 0;
4323
4324 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4325 if (ret < 0)
4326 goto out;
4327 if (ret > 0) { /* ret = -ENOENT; */
4328 ret = 0;
4329 goto out;
4330 }
4331
Ilya Dryomov59641012012-01-16 22:04:48 +02004332 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4333 if (!bctl) {
4334 ret = -ENOMEM;
4335 goto out;
4336 }
4337
Ilya Dryomov59641012012-01-16 22:04:48 +02004338 leaf = path->nodes[0];
4339 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4340
Ilya Dryomov68310a52012-06-22 12:24:12 -06004341 bctl->flags = btrfs_balance_flags(leaf, item);
4342 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004343
4344 btrfs_balance_data(leaf, item, &disk_bargs);
4345 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4346 btrfs_balance_meta(leaf, item, &disk_bargs);
4347 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4348 btrfs_balance_sys(leaf, item, &disk_bargs);
4349 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4350
David Sterbaeee95e32018-03-20 20:07:58 +01004351 /*
4352 * This should never happen, as the paused balance state is recovered
4353 * during mount without any chance of other exclusive ops to collide.
4354 *
4355 * This gives the exclusive op status to balance and keeps in paused
4356 * state until user intervention (cancel or umount). If the ownership
4357 * cannot be assigned, show a message but do not fail. The balance
4358 * is in a paused state and must have fs_info::balance_ctl properly
4359 * set up.
4360 */
4361 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
4362 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004363 "balance: cannot set exclusive op status, resume manually");
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004364
Ilya Dryomov68310a52012-06-22 12:24:12 -06004365 mutex_lock(&fs_info->balance_mutex);
David Sterba833aae12018-03-21 02:41:30 +01004366 BUG_ON(fs_info->balance_ctl);
4367 spin_lock(&fs_info->balance_lock);
4368 fs_info->balance_ctl = bctl;
4369 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov68310a52012-06-22 12:24:12 -06004370 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004371out:
4372 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004373 return ret;
4374}
4375
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004376int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4377{
4378 int ret = 0;
4379
4380 mutex_lock(&fs_info->balance_mutex);
4381 if (!fs_info->balance_ctl) {
4382 mutex_unlock(&fs_info->balance_mutex);
4383 return -ENOTCONN;
4384 }
4385
David Sterba3009a622018-03-21 01:31:04 +01004386 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004387 atomic_inc(&fs_info->balance_pause_req);
4388 mutex_unlock(&fs_info->balance_mutex);
4389
4390 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004391 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004392
4393 mutex_lock(&fs_info->balance_mutex);
4394 /* we are good with balance_ctl ripped off from under us */
David Sterba3009a622018-03-21 01:31:04 +01004395 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004396 atomic_dec(&fs_info->balance_pause_req);
4397 } else {
4398 ret = -ENOTCONN;
4399 }
4400
4401 mutex_unlock(&fs_info->balance_mutex);
4402 return ret;
4403}
4404
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004405int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4406{
4407 mutex_lock(&fs_info->balance_mutex);
4408 if (!fs_info->balance_ctl) {
4409 mutex_unlock(&fs_info->balance_mutex);
4410 return -ENOTCONN;
4411 }
4412
David Sterbacf7d20f2018-03-21 01:45:32 +01004413 /*
4414 * A paused balance with the item stored on disk can be resumed at
4415 * mount time if the mount is read-write. Otherwise it's still paused
4416 * and we must not allow cancelling as it deletes the item.
4417 */
4418 if (sb_rdonly(fs_info->sb)) {
4419 mutex_unlock(&fs_info->balance_mutex);
4420 return -EROFS;
4421 }
4422
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004423 atomic_inc(&fs_info->balance_cancel_req);
4424 /*
4425 * if we are running just wait and return, balance item is
4426 * deleted in btrfs_balance in this case
4427 */
David Sterba3009a622018-03-21 01:31:04 +01004428 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004429 mutex_unlock(&fs_info->balance_mutex);
4430 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004431 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004432 mutex_lock(&fs_info->balance_mutex);
4433 } else {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004434 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004435 /*
4436 * Lock released to allow other waiters to continue, we'll
4437 * reexamine the status again.
4438 */
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004439 mutex_lock(&fs_info->balance_mutex);
4440
David Sterbaa17c95d2018-03-20 17:28:05 +01004441 if (fs_info->balance_ctl) {
David Sterba149196a2018-03-20 20:23:09 +01004442 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004443 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain6dac13f2018-05-16 10:51:26 +08004444 btrfs_info(fs_info, "balance: canceled");
David Sterbaa17c95d2018-03-20 17:28:05 +01004445 }
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004446 }
4447
David Sterba3009a622018-03-21 01:31:04 +01004448 BUG_ON(fs_info->balance_ctl ||
4449 test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004450 atomic_dec(&fs_info->balance_cancel_req);
4451 mutex_unlock(&fs_info->balance_mutex);
4452 return 0;
4453}
4454
Stefan Behrens803b2f52013-08-15 17:11:21 +02004455static int btrfs_uuid_scan_kthread(void *data)
4456{
4457 struct btrfs_fs_info *fs_info = data;
4458 struct btrfs_root *root = fs_info->tree_root;
4459 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004460 struct btrfs_path *path = NULL;
4461 int ret = 0;
4462 struct extent_buffer *eb;
4463 int slot;
4464 struct btrfs_root_item root_item;
4465 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004466 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004467
4468 path = btrfs_alloc_path();
4469 if (!path) {
4470 ret = -ENOMEM;
4471 goto out;
4472 }
4473
4474 key.objectid = 0;
4475 key.type = BTRFS_ROOT_ITEM_KEY;
4476 key.offset = 0;
4477
Stefan Behrens803b2f52013-08-15 17:11:21 +02004478 while (1) {
Anand Jain7c829b72018-03-07 17:29:18 +08004479 ret = btrfs_search_forward(root, &key, path,
4480 BTRFS_OLDEST_GENERATION);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004481 if (ret) {
4482 if (ret > 0)
4483 ret = 0;
4484 break;
4485 }
4486
4487 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4488 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4489 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4490 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4491 goto skip;
4492
4493 eb = path->nodes[0];
4494 slot = path->slots[0];
4495 item_size = btrfs_item_size_nr(eb, slot);
4496 if (item_size < sizeof(root_item))
4497 goto skip;
4498
Stefan Behrens803b2f52013-08-15 17:11:21 +02004499 read_extent_buffer(eb, &root_item,
4500 btrfs_item_ptr_offset(eb, slot),
4501 (int)sizeof(root_item));
4502 if (btrfs_root_refs(&root_item) == 0)
4503 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004504
4505 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4506 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4507 if (trans)
4508 goto update_tree;
4509
4510 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004511 /*
4512 * 1 - subvol uuid item
4513 * 1 - received_subvol uuid item
4514 */
4515 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4516 if (IS_ERR(trans)) {
4517 ret = PTR_ERR(trans);
4518 break;
4519 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004520 continue;
4521 } else {
4522 goto skip;
4523 }
4524update_tree:
4525 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004526 ret = btrfs_uuid_tree_add(trans, root_item.uuid,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004527 BTRFS_UUID_KEY_SUBVOL,
4528 key.objectid);
4529 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004530 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004531 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004532 break;
4533 }
4534 }
4535
4536 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004537 ret = btrfs_uuid_tree_add(trans,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004538 root_item.received_uuid,
4539 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4540 key.objectid);
4541 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004542 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004543 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004544 break;
4545 }
4546 }
4547
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004548skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004549 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004550 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004551 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004552 if (ret)
4553 break;
4554 }
4555
Stefan Behrens803b2f52013-08-15 17:11:21 +02004556 btrfs_release_path(path);
4557 if (key.offset < (u64)-1) {
4558 key.offset++;
4559 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4560 key.offset = 0;
4561 key.type = BTRFS_ROOT_ITEM_KEY;
4562 } else if (key.objectid < (u64)-1) {
4563 key.offset = 0;
4564 key.type = BTRFS_ROOT_ITEM_KEY;
4565 key.objectid++;
4566 } else {
4567 break;
4568 }
4569 cond_resched();
4570 }
4571
4572out:
4573 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004574 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004575 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004576 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004577 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004578 else
Josef Bacikafcdd122016-09-02 15:40:02 -04004579 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004580 up(&fs_info->uuid_tree_rescan_sem);
4581 return 0;
4582}
4583
Stefan Behrens70f80172013-08-15 17:11:23 +02004584/*
4585 * Callback for btrfs_uuid_tree_iterate().
4586 * returns:
4587 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004588 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004589 * > 0 if the check failed, which means the caller shall remove the entry.
4590 */
4591static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4592 u8 *uuid, u8 type, u64 subid)
4593{
4594 struct btrfs_key key;
4595 int ret = 0;
4596 struct btrfs_root *subvol_root;
4597
4598 if (type != BTRFS_UUID_KEY_SUBVOL &&
4599 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4600 goto out;
4601
4602 key.objectid = subid;
4603 key.type = BTRFS_ROOT_ITEM_KEY;
4604 key.offset = (u64)-1;
4605 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4606 if (IS_ERR(subvol_root)) {
4607 ret = PTR_ERR(subvol_root);
4608 if (ret == -ENOENT)
4609 ret = 1;
4610 goto out;
4611 }
4612
4613 switch (type) {
4614 case BTRFS_UUID_KEY_SUBVOL:
4615 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4616 ret = 1;
4617 break;
4618 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4619 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4620 BTRFS_UUID_SIZE))
4621 ret = 1;
4622 break;
4623 }
4624
4625out:
4626 return ret;
4627}
4628
4629static int btrfs_uuid_rescan_kthread(void *data)
4630{
4631 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4632 int ret;
4633
4634 /*
4635 * 1st step is to iterate through the existing UUID tree and
4636 * to delete all entries that contain outdated data.
4637 * 2nd step is to add all missing entries to the UUID tree.
4638 */
4639 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4640 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004641 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004642 up(&fs_info->uuid_tree_rescan_sem);
4643 return ret;
4644 }
4645 return btrfs_uuid_scan_kthread(data);
4646}
4647
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004648int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4649{
4650 struct btrfs_trans_handle *trans;
4651 struct btrfs_root *tree_root = fs_info->tree_root;
4652 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004653 struct task_struct *task;
4654 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004655
4656 /*
4657 * 1 - root node
4658 * 1 - root item
4659 */
4660 trans = btrfs_start_transaction(tree_root, 2);
4661 if (IS_ERR(trans))
4662 return PTR_ERR(trans);
4663
4664 uuid_root = btrfs_create_tree(trans, fs_info,
4665 BTRFS_UUID_TREE_OBJECTID);
4666 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004667 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004668 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004669 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004670 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004671 }
4672
4673 fs_info->uuid_root = uuid_root;
4674
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004675 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004676 if (ret)
4677 return ret;
4678
4679 down(&fs_info->uuid_tree_rescan_sem);
4680 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4681 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004682 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004683 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004684 up(&fs_info->uuid_tree_rescan_sem);
4685 return PTR_ERR(task);
4686 }
4687
4688 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004689}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004690
Stefan Behrens70f80172013-08-15 17:11:23 +02004691int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4692{
4693 struct task_struct *task;
4694
4695 down(&fs_info->uuid_tree_rescan_sem);
4696 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4697 if (IS_ERR(task)) {
4698 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004699 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004700 up(&fs_info->uuid_tree_rescan_sem);
4701 return PTR_ERR(task);
4702 }
4703
4704 return 0;
4705}
4706
Chris Mason8f18cf12008-04-25 16:53:30 -04004707/*
4708 * shrinking a device means finding all of the device extents past
4709 * the new size, and then following the back refs to the chunks.
4710 * The chunk relocation code actually frees the device extent
4711 */
4712int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4713{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004714 struct btrfs_fs_info *fs_info = device->fs_info;
4715 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004716 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004717 struct btrfs_dev_extent *dev_extent = NULL;
4718 struct btrfs_path *path;
4719 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004720 u64 chunk_offset;
4721 int ret;
4722 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004723 int failed = 0;
4724 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004725 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004726 struct extent_buffer *l;
4727 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004728 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004729 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004730 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004731 u64 diff;
4732
4733 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004734 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004735
Anand Jain401e29c2017-12-04 12:54:55 +08004736 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Stefan Behrens63a212a2012-11-05 18:29:28 +01004737 return -EINVAL;
4738
Chris Mason8f18cf12008-04-25 16:53:30 -04004739 path = btrfs_alloc_path();
4740 if (!path)
4741 return -ENOMEM;
4742
Gu Jinxiang0338dff2018-04-27 16:22:07 +08004743 path->reada = READA_BACK;
Chris Mason8f18cf12008-04-25 16:53:30 -04004744
David Sterba34441362016-10-04 19:34:27 +02004745 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004746
Miao Xie7cc8e582014-09-03 21:35:38 +08004747 btrfs_device_set_total_bytes(device, new_size);
Anand Jainebbede42017-12-04 12:54:52 +08004748 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05004749 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004750 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004751 }
David Sterba34441362016-10-04 19:34:27 +02004752 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004753
Josef Bacikba1bf482009-09-11 16:11:19 -04004754again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004755 key.objectid = device->devid;
4756 key.offset = (u64)-1;
4757 key.type = BTRFS_DEV_EXTENT_KEY;
4758
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004759 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004760 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004761 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004762 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004763 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004764 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004765 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004766
4767 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004768 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004769 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004770 if (ret < 0)
4771 goto done;
4772 if (ret) {
4773 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004774 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004775 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004776 }
4777
4778 l = path->nodes[0];
4779 slot = path->slots[0];
4780 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4781
Josef Bacikba1bf482009-09-11 16:11:19 -04004782 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004783 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004784 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004785 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004786 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004787
4788 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4789 length = btrfs_dev_extent_length(l, dev_extent);
4790
Josef Bacikba1bf482009-09-11 16:11:19 -04004791 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004792 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004793 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004794 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004795 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004796
Chris Mason8f18cf12008-04-25 16:53:30 -04004797 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004798 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004799
Liu Boa6f93c72017-11-15 16:28:11 -07004800 /*
4801 * We may be relocating the only data chunk we have,
4802 * which could potentially end up with losing data's
4803 * raid profile, so lets allocate an empty one in
4804 * advance.
4805 */
4806 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4807 if (ret < 0) {
4808 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4809 goto done;
4810 }
4811
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004812 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4813 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004814 if (ret == -ENOSPC) {
Josef Bacikba1bf482009-09-11 16:11:19 -04004815 failed++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004816 } else if (ret) {
4817 if (ret == -ETXTBSY) {
4818 btrfs_warn(fs_info,
4819 "could not shrink block group %llu due to active swapfile",
4820 chunk_offset);
4821 }
4822 goto done;
4823 }
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004824 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004825
4826 if (failed && !retried) {
4827 failed = 0;
4828 retried = true;
4829 goto again;
4830 } else if (failed && retried) {
4831 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004832 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004833 }
4834
Chris Balld6397ba2009-04-27 07:29:03 -04004835 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004836 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004837 if (IS_ERR(trans)) {
4838 ret = PTR_ERR(trans);
4839 goto done;
4840 }
4841
David Sterba34441362016-10-04 19:34:27 +02004842 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004843
4844 /*
4845 * We checked in the above loop all device extents that were already in
4846 * the device tree. However before we have updated the device's
4847 * total_bytes to the new size, we might have had chunk allocations that
4848 * have not complete yet (new block groups attached to transaction
4849 * handles), and therefore their device extents were not yet in the
4850 * device tree and we missed them in the loop above. So if we have any
4851 * pending chunk using a device extent that overlaps the device range
4852 * that we can not use anymore, commit the current transaction and
4853 * repeat the search on the device tree - this way we guarantee we will
4854 * not have chunks using device extents that end beyond 'new_size'.
4855 */
4856 if (!checked_pending_chunks) {
4857 u64 start = new_size;
4858 u64 len = old_size - new_size;
4859
Jeff Mahoney499f3772015-06-15 09:41:17 -04004860 if (contains_pending_extent(trans->transaction, device,
4861 &start, len)) {
David Sterba34441362016-10-04 19:34:27 +02004862 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004863 checked_pending_chunks = true;
4864 failed = 0;
4865 retried = false;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004866 ret = btrfs_commit_transaction(trans);
Filipe Manana53e489b2015-06-02 14:43:21 +01004867 if (ret)
4868 goto done;
4869 goto again;
4870 }
4871 }
4872
Miao Xie7cc8e582014-09-03 21:35:38 +08004873 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004874 if (list_empty(&device->resized_list))
4875 list_add_tail(&device->resized_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004876 &fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004877
Chris Balld6397ba2009-04-27 07:29:03 -04004878 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004879 btrfs_set_super_total_bytes(super_copy,
4880 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004881 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004882
4883 /* Now btrfs_update_device() will change the on-disk size. */
4884 ret = btrfs_update_device(trans, device);
Anand Jain801660b2018-08-06 18:12:37 +08004885 if (ret < 0) {
4886 btrfs_abort_transaction(trans, ret);
4887 btrfs_end_transaction(trans);
4888 } else {
4889 ret = btrfs_commit_transaction(trans);
4890 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004891done:
4892 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004893 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004894 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004895 btrfs_device_set_total_bytes(device, old_size);
Anand Jainebbede42017-12-04 12:54:52 +08004896 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Filipe Manana53e489b2015-06-02 14:43:21 +01004897 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004898 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004899 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004900 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004901 return ret;
4902}
4903
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004904static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004905 struct btrfs_key *key,
4906 struct btrfs_chunk *chunk, int item_size)
4907{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004908 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004909 struct btrfs_disk_key disk_key;
4910 u32 array_size;
4911 u8 *ptr;
4912
David Sterba34441362016-10-04 19:34:27 +02004913 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004914 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004915 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004916 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004917 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004918 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004919 }
Chris Mason0b86a832008-03-24 15:01:56 -04004920
4921 ptr = super_copy->sys_chunk_array + array_size;
4922 btrfs_cpu_key_to_disk(&disk_key, key);
4923 memcpy(ptr, &disk_key, sizeof(disk_key));
4924 ptr += sizeof(disk_key);
4925 memcpy(ptr, chunk, item_size);
4926 item_size += sizeof(disk_key);
4927 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004928 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004929
Chris Mason0b86a832008-03-24 15:01:56 -04004930 return 0;
4931}
4932
Miao Xieb2117a32011-01-05 10:07:28 +00004933/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004934 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004935 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004936static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004937{
Arne Jansen73c5de02011-04-12 12:07:57 +02004938 const struct btrfs_device_info *di_a = a;
4939 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004940
Arne Jansen73c5de02011-04-12 12:07:57 +02004941 if (di_a->max_avail > di_b->max_avail)
4942 return -1;
4943 if (di_a->max_avail < di_b->max_avail)
4944 return 1;
4945 if (di_a->total_avail > di_b->total_avail)
4946 return -1;
4947 if (di_a->total_avail < di_b->total_avail)
4948 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004949 return 0;
4950}
4951
David Woodhouse53b381b2013-01-29 18:40:14 -05004952static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4953{
Zhao Leiffe2d202015-01-20 15:11:44 +08004954 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004955 return;
4956
Miao Xieceda0862013-04-11 10:30:16 +00004957 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004958}
4959
Qu Wenruo062d4d12018-01-30 18:20:46 +08004960#define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004961 - sizeof(struct btrfs_chunk)) \
4962 / sizeof(struct btrfs_stripe) + 1)
4963
4964#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4965 - 2 * sizeof(struct btrfs_disk_key) \
4966 - 2 * sizeof(struct btrfs_chunk)) \
4967 / sizeof(struct btrfs_stripe) + 1)
4968
Miao Xieb2117a32011-01-05 10:07:28 +00004969static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
David Sterba72b468c2017-02-10 19:46:27 +01004970 u64 start, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004971{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004972 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004973 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004974 struct btrfs_device *device;
Arne Jansen73c5de02011-04-12 12:07:57 +02004975 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004976 struct extent_map_tree *em_tree;
4977 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004978 struct btrfs_device_info *devices_info = NULL;
4979 u64 total_avail;
4980 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004981 int data_stripes; /* number of stripes that count for
4982 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004983 int sub_stripes; /* sub_stripes info for map */
4984 int dev_stripes; /* stripes per dev */
4985 int devs_max; /* max devs to use */
4986 int devs_min; /* min devs needed */
4987 int devs_increment; /* ndevs has to be a multiple of this */
4988 int ncopies; /* how many copies to data has */
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02004989 int nparity; /* number of stripes worth of bytes to
4990 store parity information */
Miao Xieb2117a32011-01-05 10:07:28 +00004991 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004992 u64 max_stripe_size;
4993 u64 max_chunk_size;
4994 u64 stripe_size;
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02004995 u64 chunk_size;
Arne Jansen73c5de02011-04-12 12:07:57 +02004996 int ndevs;
4997 int i;
4998 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004999 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00005000
Ilya Dryomov0c460c02012-03-27 17:09:17 +03005001 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02005002
Qu Wenruo4117f202018-01-22 13:50:54 +08005003 if (list_empty(&fs_devices->alloc_list)) {
5004 if (btrfs_test_opt(info, ENOSPC_DEBUG))
5005 btrfs_debug(info, "%s: no writable device", __func__);
Miao Xieb2117a32011-01-05 10:07:28 +00005006 return -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08005007 }
Miao Xieb2117a32011-01-05 10:07:28 +00005008
Qu Wenruo3e72ee82018-01-30 18:20:45 +08005009 index = btrfs_bg_flags_to_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02005010
Liu Bo31e50222012-11-21 14:18:10 +00005011 sub_stripes = btrfs_raid_array[index].sub_stripes;
5012 dev_stripes = btrfs_raid_array[index].dev_stripes;
5013 devs_max = btrfs_raid_array[index].devs_max;
5014 devs_min = btrfs_raid_array[index].devs_min;
5015 devs_increment = btrfs_raid_array[index].devs_increment;
5016 ncopies = btrfs_raid_array[index].ncopies;
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02005017 nparity = btrfs_raid_array[index].nparity;
Arne Jansen73c5de02011-04-12 12:07:57 +02005018
5019 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09005020 max_stripe_size = SZ_1G;
Qu Wenruofce466e2018-07-03 17:10:05 +08005021 max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08005022 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08005023 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02005024 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05005025 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09005026 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
5027 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05005028 else
Byongho Leeee221842015-12-15 01:42:10 +09005029 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02005030 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08005031 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08005032 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02005033 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09005034 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02005035 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08005036 if (!devs_max)
5037 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02005038 } else {
David Sterba351fd352014-05-15 16:48:20 +02005039 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02005040 type);
5041 BUG_ON(1);
5042 }
5043
Andrea Gelmini52042d82018-11-28 12:05:13 +01005044 /* We don't want a chunk larger than 10% of writable space */
Arne Jansen73c5de02011-04-12 12:07:57 +02005045 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
5046 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00005047
David Sterba31e818f2015-02-20 18:00:26 +01005048 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00005049 GFP_NOFS);
5050 if (!devices_info)
5051 return -ENOMEM;
5052
Arne Jansen73c5de02011-04-12 12:07:57 +02005053 /*
5054 * in the first pass through the devices list, we gather information
5055 * about the available holes on each device.
5056 */
5057 ndevs = 0;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03005058 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Arne Jansen73c5de02011-04-12 12:07:57 +02005059 u64 max_avail;
5060 u64 dev_offset;
5061
Anand Jainebbede42017-12-04 12:54:52 +08005062 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00005063 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05005064 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02005065 continue;
5066 }
5067
Anand Jaine12c9622017-12-04 12:54:53 +08005068 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
5069 &device->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08005070 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Arne Jansen73c5de02011-04-12 12:07:57 +02005071 continue;
5072
5073 if (device->total_bytes > device->bytes_used)
5074 total_avail = device->total_bytes - device->bytes_used;
5075 else
5076 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00005077
5078 /* If there is no space on this device, skip it. */
5079 if (total_avail == 0)
5080 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02005081
Josef Bacik6df9a952013-06-27 13:22:46 -04005082 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02005083 max_stripe_size * dev_stripes,
5084 &dev_offset, &max_avail);
5085 if (ret && ret != -ENOSPC)
5086 goto error;
5087
5088 if (ret == 0)
5089 max_avail = max_stripe_size * dev_stripes;
5090
Qu Wenruo4117f202018-01-22 13:50:54 +08005091 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) {
5092 if (btrfs_test_opt(info, ENOSPC_DEBUG))
5093 btrfs_debug(info,
5094 "%s: devid %llu has no free space, have=%llu want=%u",
5095 __func__, device->devid, max_avail,
5096 BTRFS_STRIPE_LEN * dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02005097 continue;
Qu Wenruo4117f202018-01-22 13:50:54 +08005098 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005099
Eric Sandeen063d0062013-01-31 00:55:01 +00005100 if (ndevs == fs_devices->rw_devices) {
5101 WARN(1, "%s: found more than %llu devices\n",
5102 __func__, fs_devices->rw_devices);
5103 break;
5104 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005105 devices_info[ndevs].dev_offset = dev_offset;
5106 devices_info[ndevs].max_avail = max_avail;
5107 devices_info[ndevs].total_avail = total_avail;
5108 devices_info[ndevs].dev = device;
5109 ++ndevs;
5110 }
5111
5112 /*
5113 * now sort the devices by hole size / available space
5114 */
5115 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
5116 btrfs_cmp_device_info, NULL);
5117
5118 /* round down to number of usable stripes */
Nikolay Borisove5600fd2017-06-27 10:02:25 +03005119 ndevs = round_down(ndevs, devs_increment);
Arne Jansen73c5de02011-04-12 12:07:57 +02005120
Qu Wenruoba89b802018-01-31 13:56:15 +08005121 if (ndevs < devs_min) {
Arne Jansen73c5de02011-04-12 12:07:57 +02005122 ret = -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08005123 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
5124 btrfs_debug(info,
5125 "%s: not enough devices with free space: have=%d minimum required=%d",
Qu Wenruoba89b802018-01-31 13:56:15 +08005126 __func__, ndevs, devs_min);
Qu Wenruo4117f202018-01-22 13:50:54 +08005127 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005128 goto error;
5129 }
5130
Nikolay Borisovf148ef42017-06-27 10:02:26 +03005131 ndevs = min(ndevs, devs_max);
5132
Arne Jansen73c5de02011-04-12 12:07:57 +02005133 /*
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01005134 * The primary goal is to maximize the number of stripes, so use as
5135 * many devices as possible, even if the stripes are not maximum sized.
5136 *
5137 * The DUP profile stores more than one stripe per device, the
5138 * max_avail is the total size so we have to adjust.
Arne Jansen73c5de02011-04-12 12:07:57 +02005139 */
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01005140 stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02005141 num_stripes = ndevs * dev_stripes;
5142
David Woodhouse53b381b2013-01-29 18:40:14 -05005143 /*
5144 * this will have to be fixed for RAID1 and RAID10 over
5145 * more drives
5146 */
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02005147 data_stripes = (num_stripes - nparity) / ncopies;
Chris Mason86db2572013-02-20 16:23:40 -05005148
5149 /*
5150 * Use the number of data stripes to figure out how big this chunk
5151 * is really going to be in terms of logical address space,
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02005152 * and compare that answer with the max chunk size. If it's higher,
5153 * we try to reduce stripe_size.
Chris Mason86db2572013-02-20 16:23:40 -05005154 */
5155 if (stripe_size * data_stripes > max_chunk_size) {
Qu Wenruo793ff2c2018-01-31 14:16:34 +08005156 /*
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02005157 * Reduce stripe_size, round it up to a 16MB boundary again and
5158 * then use it, unless it ends up being even bigger than the
5159 * previous value we had already.
Chris Mason86db2572013-02-20 16:23:40 -05005160 */
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02005161 stripe_size = min(round_up(div_u64(max_chunk_size,
5162 data_stripes), SZ_16M),
Qu Wenruo793ff2c2018-01-31 14:16:34 +08005163 stripe_size);
Chris Mason86db2572013-02-20 16:23:40 -05005164 }
5165
Ilya Dryomov37db63a2012-04-13 17:05:08 +03005166 /* align to BTRFS_STRIPE_LEN */
Nikolay Borisov500ceed2017-07-14 09:55:41 +03005167 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
Arne Jansen73c5de02011-04-12 12:07:57 +02005168
Miao Xieb2117a32011-01-05 10:07:28 +00005169 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
5170 if (!map) {
5171 ret = -ENOMEM;
5172 goto error;
5173 }
5174 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04005175
Arne Jansen73c5de02011-04-12 12:07:57 +02005176 for (i = 0; i < ndevs; ++i) {
5177 for (j = 0; j < dev_stripes; ++j) {
5178 int s = i * dev_stripes + j;
5179 map->stripes[s].dev = devices_info[i].dev;
5180 map->stripes[s].physical = devices_info[i].dev_offset +
5181 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04005182 }
Chris Mason6324fbf2008-03-24 15:01:59 -04005183 }
Nikolay Borisov500ceed2017-07-14 09:55:41 +03005184 map->stripe_len = BTRFS_STRIPE_LEN;
5185 map->io_align = BTRFS_STRIPE_LEN;
5186 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04005187 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04005188 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005189
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005190 chunk_size = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005191
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005192 trace_btrfs_chunk_alloc(info, map, start, chunk_size);
liubo1abe9b82011-03-24 11:18:59 +00005193
David Sterba172ddd62011-04-21 00:48:27 +02005194 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05005195 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08005196 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00005197 ret = -ENOMEM;
5198 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05005199 }
Wang Shilong298a8f92014-06-19 10:42:52 +08005200 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04005201 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05005202 em->start = start;
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005203 em->len = chunk_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005204 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005205 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04005206 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005207
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005208 em_tree = &info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04005209 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005210 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005211 if (ret) {
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03005212 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005213 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07005214 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005215 }
Yan Zheng2b820322008-11-17 21:11:30 -05005216
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03005217 list_add_tail(&em->list, &trans->transaction->pending_chunks);
5218 refcount_inc(&em->refs);
5219 write_unlock(&em_tree->lock);
5220
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005221 ret = btrfs_make_block_group(trans, 0, type, start, chunk_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005222 if (ret)
5223 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05005224
Hans van Kranenburg2f29df42018-10-04 23:24:38 +02005225 for (i = 0; i < map->num_stripes; i++)
5226 btrfs_device_set_bytes_used(map->stripes[i].dev,
5227 map->stripes[i].dev->bytes_used + stripe_size);
Miao Xie43530c42014-09-03 21:35:36 +08005228
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03005229 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
Miao Xie1c116182014-09-03 21:35:37 +08005230
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005231 free_extent_map(em);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005232 check_raid56_incompat_flag(info, type);
David Woodhouse53b381b2013-01-29 18:40:14 -05005233
Miao Xieb2117a32011-01-05 10:07:28 +00005234 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05005235 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00005236
Josef Bacik6df9a952013-06-27 13:22:46 -04005237error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005238 write_lock(&em_tree->lock);
5239 remove_extent_mapping(em_tree, em);
5240 write_unlock(&em_tree->lock);
5241
5242 /* One for our allocation */
5243 free_extent_map(em);
5244 /* One for the tree reference */
5245 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00005246 /* One for the pending_chunks list reference */
5247 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00005248error:
Miao Xieb2117a32011-01-05 10:07:28 +00005249 kfree(devices_info);
5250 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005251}
5252
Josef Bacik6df9a952013-06-27 13:22:46 -04005253int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Nikolay Borisov97aff912018-07-20 19:37:53 +03005254 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05005255{
Nikolay Borisov97aff912018-07-20 19:37:53 +03005256 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04005257 struct btrfs_root *extent_root = fs_info->extent_root;
5258 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05005259 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05005260 struct btrfs_device *device;
5261 struct btrfs_chunk *chunk;
5262 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005263 struct extent_map *em;
5264 struct map_lookup *map;
5265 size_t item_size;
5266 u64 dev_offset;
5267 u64 stripe_size;
5268 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08005269 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005270
Omar Sandoval60ca8422018-05-16 16:34:31 -07005271 em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
Liu Bo592d92e2017-03-14 13:33:55 -07005272 if (IS_ERR(em))
5273 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04005274
Jeff Mahoney95617d62015-06-03 10:55:48 -04005275 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04005276 item_size = btrfs_chunk_item_size(map->num_stripes);
5277 stripe_size = em->orig_block_len;
5278
5279 chunk = kzalloc(item_size, GFP_NOFS);
5280 if (!chunk) {
5281 ret = -ENOMEM;
5282 goto out;
5283 }
5284
Filipe Manana50460e32015-11-20 10:42:47 +00005285 /*
5286 * Take the device list mutex to prevent races with the final phase of
5287 * a device replace operation that replaces the device object associated
5288 * with the map's stripes, because the device object's id can change
5289 * at any time during that final phase of the device replace operation
5290 * (dev-replace.c:btrfs_dev_replace_finishing()).
5291 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005292 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04005293 for (i = 0; i < map->num_stripes; i++) {
5294 device = map->stripes[i].dev;
5295 dev_offset = map->stripes[i].physical;
5296
Yan Zheng2b820322008-11-17 21:11:30 -05005297 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07005298 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005299 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03005300 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
5301 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005302 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005303 break;
5304 }
5305 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005306 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00005307 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05005308 }
5309
Yan Zheng2b820322008-11-17 21:11:30 -05005310 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005311 for (i = 0; i < map->num_stripes; i++) {
5312 device = map->stripes[i].dev;
5313 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05005314
5315 btrfs_set_stack_stripe_devid(stripe, device->devid);
5316 btrfs_set_stack_stripe_offset(stripe, dev_offset);
5317 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
5318 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05005319 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005320 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05005321
5322 btrfs_set_stack_chunk_length(chunk, chunk_size);
5323 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
5324 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
5325 btrfs_set_stack_chunk_type(chunk, map->type);
5326 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
5327 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
5328 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005329 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05005330 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
5331
5332 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5333 key.type = BTRFS_CHUNK_ITEM_KEY;
5334 key.offset = chunk_offset;
5335
5336 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005337 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5338 /*
5339 * TODO: Cleanup of inserted chunk root in case of
5340 * failure.
5341 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005342 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05005343 }
liubo1abe9b82011-03-24 11:18:59 +00005344
Josef Bacik6df9a952013-06-27 13:22:46 -04005345out:
Yan Zheng2b820322008-11-17 21:11:30 -05005346 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04005347 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005348 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005349}
5350
5351/*
Andrea Gelmini52042d82018-11-28 12:05:13 +01005352 * Chunk allocation falls into two parts. The first part does work
5353 * that makes the new allocated chunk usable, but does not do any operation
5354 * that modifies the chunk tree. The second part does the work that
5355 * requires modifying the chunk tree. This division is important for the
Yan Zheng2b820322008-11-17 21:11:30 -05005356 * bootstrap process of adding storage to a seed btrfs.
5357 */
Nikolay Borisovc216b202018-06-20 15:49:06 +03005358int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type)
Yan Zheng2b820322008-11-17 21:11:30 -05005359{
5360 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005361
Nikolay Borisovc216b202018-06-20 15:49:06 +03005362 lockdep_assert_held(&trans->fs_info->chunk_mutex);
5363 chunk_offset = find_next_chunk(trans->fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005364 return __btrfs_alloc_chunk(trans, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05005365}
5366
Chris Masond3977122009-01-05 21:25:51 -05005367static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +01005368 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05005369{
5370 u64 chunk_offset;
5371 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005372 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05005373 int ret;
5374
Josef Bacik6df9a952013-06-27 13:22:46 -04005375 chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005376 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005377 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005378 if (ret)
5379 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005380
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005381 sys_chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005382 alloc_profile = btrfs_system_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005383 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005384 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005385}
5386
Miao Xied20983b2014-07-03 18:22:13 +08005387static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5388{
5389 int max_errors;
5390
5391 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5392 BTRFS_BLOCK_GROUP_RAID10 |
5393 BTRFS_BLOCK_GROUP_RAID5 |
5394 BTRFS_BLOCK_GROUP_DUP)) {
5395 max_errors = 1;
5396 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5397 max_errors = 2;
5398 } else {
5399 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005400 }
5401
Miao Xied20983b2014-07-03 18:22:13 +08005402 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05005403}
5404
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005405int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005406{
5407 struct extent_map *em;
5408 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005409 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005410 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005411 int i;
5412
Omar Sandoval60ca8422018-05-16 16:34:31 -07005413 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07005414 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005415 return 1;
5416
Jeff Mahoney95617d62015-06-03 10:55:48 -04005417 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005418 for (i = 0; i < map->num_stripes; i++) {
Anand Jaine6e674b2017-12-04 12:54:54 +08005419 if (test_bit(BTRFS_DEV_STATE_MISSING,
5420 &map->stripes[i].dev->dev_state)) {
Miao Xied20983b2014-07-03 18:22:13 +08005421 miss_ndevs++;
5422 continue;
5423 }
Anand Jainebbede42017-12-04 12:54:52 +08005424 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5425 &map->stripes[i].dev->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05005426 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005427 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005428 }
5429 }
Miao Xied20983b2014-07-03 18:22:13 +08005430
5431 /*
5432 * If the number of missing devices is larger than max errors,
5433 * we can not write the data into that chunk successfully, so
5434 * set it readonly.
5435 */
5436 if (miss_ndevs > btrfs_chunk_max_errors(map))
5437 readonly = 1;
5438end:
Yan Zheng2b820322008-11-17 21:11:30 -05005439 free_extent_map(em);
5440 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005441}
5442
5443void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
5444{
David Sterbaa8067e02011-04-21 00:34:43 +02005445 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04005446}
5447
5448void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
5449{
5450 struct extent_map *em;
5451
Chris Masond3977122009-01-05 21:25:51 -05005452 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04005453 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005454 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
5455 if (em)
5456 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005457 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005458 if (!em)
5459 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005460 /* once for us */
5461 free_extent_map(em);
5462 /* once for the tree */
5463 free_extent_map(em);
5464 }
5465}
5466
Stefan Behrens5d964052012-11-05 14:59:07 +01005467int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005468{
5469 struct extent_map *em;
5470 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005471 int ret;
5472
Omar Sandoval60ca8422018-05-16 16:34:31 -07005473 em = btrfs_get_chunk_map(fs_info, logical, len);
Liu Bo592d92e2017-03-14 13:33:55 -07005474 if (IS_ERR(em))
5475 /*
5476 * We could return errors for these cases, but that could get
5477 * ugly and we'd probably do the same thing which is just not do
5478 * anything else and exit, so return 1 so the callers don't try
5479 * to use other copies.
5480 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005481 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005482
Jeff Mahoney95617d62015-06-03 10:55:48 -04005483 map = em->map_lookup;
Chris Masonf1885912008-04-09 16:28:12 -04005484 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5485 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005486 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5487 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005488 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5489 ret = 2;
5490 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
Liu Bo8810f752018-01-02 13:36:41 -07005491 /*
5492 * There could be two corrupted data stripes, we need
5493 * to loop retry in order to rebuild the correct data.
Nikolay Borisove7e02092018-06-20 15:48:55 +03005494 *
Liu Bo8810f752018-01-02 13:36:41 -07005495 * Fail a stripe at a time on every retry except the
5496 * stripe under reconstruction.
5497 */
5498 ret = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04005499 else
5500 ret = 1;
5501 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005502
David Sterbacb5583d2018-09-07 16:11:23 +02005503 down_read(&fs_info->dev_replace.rwsem);
Liu Bo6fad8232017-03-14 13:33:59 -07005504 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5505 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005506 ret++;
David Sterbacb5583d2018-09-07 16:11:23 +02005507 up_read(&fs_info->dev_replace.rwsem);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005508
Chris Masonf1885912008-04-09 16:28:12 -04005509 return ret;
5510}
5511
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005512unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005513 u64 logical)
5514{
5515 struct extent_map *em;
5516 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005517 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005518
Omar Sandoval60ca8422018-05-16 16:34:31 -07005519 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005520
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005521 if (!WARN_ON(IS_ERR(em))) {
5522 map = em->map_lookup;
5523 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5524 len = map->stripe_len * nr_data_stripes(map);
5525 free_extent_map(em);
5526 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005527 return len;
5528}
5529
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005530int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005531{
5532 struct extent_map *em;
5533 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005534 int ret = 0;
5535
Omar Sandoval60ca8422018-05-16 16:34:31 -07005536 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005537
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005538 if(!WARN_ON(IS_ERR(em))) {
5539 map = em->map_lookup;
5540 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5541 ret = 1;
5542 free_extent_map(em);
5543 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005544 return ret;
5545}
5546
Stefan Behrens30d98612012-11-06 14:52:18 +01005547static int find_live_mirror(struct btrfs_fs_info *fs_info,
Anand Jain99f92a72018-03-14 16:29:12 +08005548 struct map_lookup *map, int first,
Anand Jain8ba0ae72018-03-14 16:29:13 +08005549 int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005550{
5551 int i;
Anand Jain99f92a72018-03-14 16:29:12 +08005552 int num_stripes;
Anand Jain8ba0ae72018-03-14 16:29:13 +08005553 int preferred_mirror;
Stefan Behrens30d98612012-11-06 14:52:18 +01005554 int tolerance;
5555 struct btrfs_device *srcdev;
5556
Anand Jain99f92a72018-03-14 16:29:12 +08005557 ASSERT((map->type &
5558 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)));
5559
5560 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5561 num_stripes = map->sub_stripes;
5562 else
5563 num_stripes = map->num_stripes;
5564
Anand Jain8ba0ae72018-03-14 16:29:13 +08005565 preferred_mirror = first + current->pid % num_stripes;
5566
Stefan Behrens30d98612012-11-06 14:52:18 +01005567 if (dev_replace_is_ongoing &&
5568 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5569 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5570 srcdev = fs_info->dev_replace.srcdev;
5571 else
5572 srcdev = NULL;
5573
5574 /*
5575 * try to avoid the drive that is the source drive for a
5576 * dev-replace procedure, only choose it if no other non-missing
5577 * mirror is available
5578 */
5579 for (tolerance = 0; tolerance < 2; tolerance++) {
Anand Jain8ba0ae72018-03-14 16:29:13 +08005580 if (map->stripes[preferred_mirror].dev->bdev &&
5581 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5582 return preferred_mirror;
Anand Jain99f92a72018-03-14 16:29:12 +08005583 for (i = first; i < first + num_stripes; i++) {
Stefan Behrens30d98612012-11-06 14:52:18 +01005584 if (map->stripes[i].dev->bdev &&
5585 (tolerance || map->stripes[i].dev != srcdev))
5586 return i;
5587 }
Chris Masondfe25022008-05-13 13:46:40 -04005588 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005589
Chris Masondfe25022008-05-13 13:46:40 -04005590 /* we couldn't find one that doesn't fail. Just return something
5591 * and the io error handling code will clean up eventually
5592 */
Anand Jain8ba0ae72018-03-14 16:29:13 +08005593 return preferred_mirror;
Chris Masondfe25022008-05-13 13:46:40 -04005594}
5595
David Woodhouse53b381b2013-01-29 18:40:14 -05005596static inline int parity_smaller(u64 a, u64 b)
5597{
5598 return a > b;
5599}
5600
5601/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005602static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005603{
5604 struct btrfs_bio_stripe s;
5605 int i;
5606 u64 l;
5607 int again = 1;
5608
5609 while (again) {
5610 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005611 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005612 if (parity_smaller(bbio->raid_map[i],
5613 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005614 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005615 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005616 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005617 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005618 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005619 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005620
David Woodhouse53b381b2013-01-29 18:40:14 -05005621 again = 1;
5622 }
5623 }
5624 }
5625}
5626
Zhao Lei6e9606d2015-01-20 15:11:34 +08005627static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5628{
5629 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005630 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005631 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005632 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005633 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005634 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005635 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005636 /*
5637 * plus the raid_map, which includes both the tgt dev
5638 * and the stripes
5639 */
5640 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005641 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005642
5643 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005644 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005645
5646 return bbio;
5647}
5648
5649void btrfs_get_bbio(struct btrfs_bio *bbio)
5650{
Elena Reshetova140475a2017-03-03 10:55:10 +02005651 WARN_ON(!refcount_read(&bbio->refs));
5652 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005653}
5654
5655void btrfs_put_bbio(struct btrfs_bio *bbio)
5656{
5657 if (!bbio)
5658 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005659 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005660 kfree(bbio);
5661}
5662
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005663/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5664/*
5665 * Please note that, discard won't be sent to target device of device
5666 * replace.
5667 */
5668static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5669 u64 logical, u64 length,
5670 struct btrfs_bio **bbio_ret)
5671{
5672 struct extent_map *em;
5673 struct map_lookup *map;
5674 struct btrfs_bio *bbio;
5675 u64 offset;
5676 u64 stripe_nr;
5677 u64 stripe_nr_end;
5678 u64 stripe_end_offset;
5679 u64 stripe_cnt;
5680 u64 stripe_len;
5681 u64 stripe_offset;
5682 u64 num_stripes;
5683 u32 stripe_index;
5684 u32 factor = 0;
5685 u32 sub_stripes = 0;
5686 u64 stripes_per_dev = 0;
5687 u32 remaining_stripes = 0;
5688 u32 last_stripe = 0;
5689 int ret = 0;
5690 int i;
5691
5692 /* discard always return a bbio */
5693 ASSERT(bbio_ret);
5694
Omar Sandoval60ca8422018-05-16 16:34:31 -07005695 em = btrfs_get_chunk_map(fs_info, logical, length);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005696 if (IS_ERR(em))
5697 return PTR_ERR(em);
5698
5699 map = em->map_lookup;
5700 /* we don't discard raid56 yet */
5701 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5702 ret = -EOPNOTSUPP;
5703 goto out;
5704 }
5705
5706 offset = logical - em->start;
5707 length = min_t(u64, em->len - offset, length);
5708
5709 stripe_len = map->stripe_len;
5710 /*
5711 * stripe_nr counts the total number of stripes we have to stride
5712 * to get to this block
5713 */
5714 stripe_nr = div64_u64(offset, stripe_len);
5715
5716 /* stripe_offset is the offset of this block in its stripe */
5717 stripe_offset = offset - stripe_nr * stripe_len;
5718
5719 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005720 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005721 stripe_cnt = stripe_nr_end - stripe_nr;
5722 stripe_end_offset = stripe_nr_end * map->stripe_len -
5723 (offset + length);
5724 /*
5725 * after this, stripe_nr is the number of stripes on this
5726 * device we have to walk to find the data, and stripe_index is
5727 * the number of our device in the stripe array
5728 */
5729 num_stripes = 1;
5730 stripe_index = 0;
5731 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5732 BTRFS_BLOCK_GROUP_RAID10)) {
5733 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5734 sub_stripes = 1;
5735 else
5736 sub_stripes = map->sub_stripes;
5737
5738 factor = map->num_stripes / sub_stripes;
5739 num_stripes = min_t(u64, map->num_stripes,
5740 sub_stripes * stripe_cnt);
5741 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5742 stripe_index *= sub_stripes;
5743 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5744 &remaining_stripes);
5745 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5746 last_stripe *= sub_stripes;
5747 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5748 BTRFS_BLOCK_GROUP_DUP)) {
5749 num_stripes = map->num_stripes;
5750 } else {
5751 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5752 &stripe_index);
5753 }
5754
5755 bbio = alloc_btrfs_bio(num_stripes, 0);
5756 if (!bbio) {
5757 ret = -ENOMEM;
5758 goto out;
5759 }
5760
5761 for (i = 0; i < num_stripes; i++) {
5762 bbio->stripes[i].physical =
5763 map->stripes[stripe_index].physical +
5764 stripe_offset + stripe_nr * map->stripe_len;
5765 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5766
5767 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5768 BTRFS_BLOCK_GROUP_RAID10)) {
5769 bbio->stripes[i].length = stripes_per_dev *
5770 map->stripe_len;
5771
5772 if (i / sub_stripes < remaining_stripes)
5773 bbio->stripes[i].length +=
5774 map->stripe_len;
5775
5776 /*
5777 * Special for the first stripe and
5778 * the last stripe:
5779 *
5780 * |-------|...|-------|
5781 * |----------|
5782 * off end_off
5783 */
5784 if (i < sub_stripes)
5785 bbio->stripes[i].length -=
5786 stripe_offset;
5787
5788 if (stripe_index >= last_stripe &&
5789 stripe_index <= (last_stripe +
5790 sub_stripes - 1))
5791 bbio->stripes[i].length -=
5792 stripe_end_offset;
5793
5794 if (i == sub_stripes - 1)
5795 stripe_offset = 0;
5796 } else {
5797 bbio->stripes[i].length = length;
5798 }
5799
5800 stripe_index++;
5801 if (stripe_index == map->num_stripes) {
5802 stripe_index = 0;
5803 stripe_nr++;
5804 }
5805 }
5806
5807 *bbio_ret = bbio;
5808 bbio->map_type = map->type;
5809 bbio->num_stripes = num_stripes;
5810out:
5811 free_extent_map(em);
5812 return ret;
5813}
5814
Liu Bo5ab56092017-03-14 13:33:57 -07005815/*
5816 * In dev-replace case, for repair case (that's the only case where the mirror
5817 * is selected explicitly when calling btrfs_map_block), blocks left of the
5818 * left cursor can also be read from the target drive.
5819 *
5820 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5821 * array of stripes.
5822 * For READ, it also needs to be supported using the same mirror number.
5823 *
5824 * If the requested block is not left of the left cursor, EIO is returned. This
5825 * can happen because btrfs_num_copies() returns one more in the dev-replace
5826 * case.
5827 */
5828static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5829 u64 logical, u64 length,
5830 u64 srcdev_devid, int *mirror_num,
5831 u64 *physical)
5832{
5833 struct btrfs_bio *bbio = NULL;
5834 int num_stripes;
5835 int index_srcdev = 0;
5836 int found = 0;
5837 u64 physical_of_found = 0;
5838 int i;
5839 int ret = 0;
5840
5841 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5842 logical, &length, &bbio, 0, 0);
5843 if (ret) {
5844 ASSERT(bbio == NULL);
5845 return ret;
5846 }
5847
5848 num_stripes = bbio->num_stripes;
5849 if (*mirror_num > num_stripes) {
5850 /*
5851 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5852 * that means that the requested area is not left of the left
5853 * cursor
5854 */
5855 btrfs_put_bbio(bbio);
5856 return -EIO;
5857 }
5858
5859 /*
5860 * process the rest of the function using the mirror_num of the source
5861 * drive. Therefore look it up first. At the end, patch the device
5862 * pointer to the one of the target drive.
5863 */
5864 for (i = 0; i < num_stripes; i++) {
5865 if (bbio->stripes[i].dev->devid != srcdev_devid)
5866 continue;
5867
5868 /*
5869 * In case of DUP, in order to keep it simple, only add the
5870 * mirror with the lowest physical address
5871 */
5872 if (found &&
5873 physical_of_found <= bbio->stripes[i].physical)
5874 continue;
5875
5876 index_srcdev = i;
5877 found = 1;
5878 physical_of_found = bbio->stripes[i].physical;
5879 }
5880
5881 btrfs_put_bbio(bbio);
5882
5883 ASSERT(found);
5884 if (!found)
5885 return -EIO;
5886
5887 *mirror_num = index_srcdev + 1;
5888 *physical = physical_of_found;
5889 return ret;
5890}
5891
Liu Bo73c0f222017-03-14 13:33:58 -07005892static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5893 struct btrfs_bio **bbio_ret,
5894 struct btrfs_dev_replace *dev_replace,
5895 int *num_stripes_ret, int *max_errors_ret)
5896{
5897 struct btrfs_bio *bbio = *bbio_ret;
5898 u64 srcdev_devid = dev_replace->srcdev->devid;
5899 int tgtdev_indexes = 0;
5900 int num_stripes = *num_stripes_ret;
5901 int max_errors = *max_errors_ret;
5902 int i;
5903
5904 if (op == BTRFS_MAP_WRITE) {
5905 int index_where_to_add;
5906
5907 /*
5908 * duplicate the write operations while the dev replace
5909 * procedure is running. Since the copying of the old disk to
5910 * the new disk takes place at run time while the filesystem is
5911 * mounted writable, the regular write operations to the old
5912 * disk have to be duplicated to go to the new disk as well.
5913 *
5914 * Note that device->missing is handled by the caller, and that
5915 * the write to the old disk is already set up in the stripes
5916 * array.
5917 */
5918 index_where_to_add = num_stripes;
5919 for (i = 0; i < num_stripes; i++) {
5920 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5921 /* write to new disk, too */
5922 struct btrfs_bio_stripe *new =
5923 bbio->stripes + index_where_to_add;
5924 struct btrfs_bio_stripe *old =
5925 bbio->stripes + i;
5926
5927 new->physical = old->physical;
5928 new->length = old->length;
5929 new->dev = dev_replace->tgtdev;
5930 bbio->tgtdev_map[i] = index_where_to_add;
5931 index_where_to_add++;
5932 max_errors++;
5933 tgtdev_indexes++;
5934 }
5935 }
5936 num_stripes = index_where_to_add;
5937 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5938 int index_srcdev = 0;
5939 int found = 0;
5940 u64 physical_of_found = 0;
5941
5942 /*
5943 * During the dev-replace procedure, the target drive can also
5944 * be used to read data in case it is needed to repair a corrupt
5945 * block elsewhere. This is possible if the requested area is
5946 * left of the left cursor. In this area, the target drive is a
5947 * full copy of the source drive.
5948 */
5949 for (i = 0; i < num_stripes; i++) {
5950 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5951 /*
5952 * In case of DUP, in order to keep it simple,
5953 * only add the mirror with the lowest physical
5954 * address
5955 */
5956 if (found &&
5957 physical_of_found <=
5958 bbio->stripes[i].physical)
5959 continue;
5960 index_srcdev = i;
5961 found = 1;
5962 physical_of_found = bbio->stripes[i].physical;
5963 }
5964 }
5965 if (found) {
5966 struct btrfs_bio_stripe *tgtdev_stripe =
5967 bbio->stripes + num_stripes;
5968
5969 tgtdev_stripe->physical = physical_of_found;
5970 tgtdev_stripe->length =
5971 bbio->stripes[index_srcdev].length;
5972 tgtdev_stripe->dev = dev_replace->tgtdev;
5973 bbio->tgtdev_map[index_srcdev] = num_stripes;
5974
5975 tgtdev_indexes++;
5976 num_stripes++;
5977 }
5978 }
5979
5980 *num_stripes_ret = num_stripes;
5981 *max_errors_ret = max_errors;
5982 bbio->num_tgtdevs = tgtdev_indexes;
5983 *bbio_ret = bbio;
5984}
5985
Liu Bo2b19a1f2017-03-14 13:34:00 -07005986static bool need_full_stripe(enum btrfs_map_op op)
5987{
5988 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5989}
5990
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005991static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5992 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005993 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005994 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005995 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005996{
5997 struct extent_map *em;
5998 struct map_lookup *map;
Chris Mason0b86a832008-03-24 15:01:56 -04005999 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04006000 u64 stripe_offset;
6001 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006002 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01006003 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04006004 int i;
Li Zefande11cc12011-12-01 12:55:47 +08006005 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04006006 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04006007 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08006008 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006009 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01006010 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
6011 int dev_replace_is_ongoing = 0;
6012 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006013 int patch_the_first_stripe_for_dev_replace = 0;
6014 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05006015 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04006016
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006017 if (op == BTRFS_MAP_DISCARD)
6018 return __btrfs_map_block_for_discard(fs_info, logical,
6019 *length, bbio_ret);
6020
Omar Sandoval60ca8422018-05-16 16:34:31 -07006021 em = btrfs_get_chunk_map(fs_info, logical, *length);
Liu Bo592d92e2017-03-14 13:33:55 -07006022 if (IS_ERR(em))
6023 return PTR_ERR(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04006024
Jeff Mahoney95617d62015-06-03 10:55:48 -04006025 map = em->map_lookup;
Chris Mason0b86a832008-03-24 15:01:56 -04006026 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04006027
David Woodhouse53b381b2013-01-29 18:40:14 -05006028 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04006029 stripe_nr = offset;
6030 /*
6031 * stripe_nr counts the total number of stripes we have to stride
6032 * to get to this block
6033 */
David Sterba47c57132015-02-20 18:43:47 +01006034 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04006035
David Woodhouse53b381b2013-01-29 18:40:14 -05006036 stripe_offset = stripe_nr * stripe_len;
Josef Bacike042d1e2016-04-12 12:54:40 -04006037 if (offset < stripe_offset) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006038 btrfs_crit(fs_info,
6039 "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
Josef Bacike042d1e2016-04-12 12:54:40 -04006040 stripe_offset, offset, em->start, logical,
6041 stripe_len);
6042 free_extent_map(em);
6043 return -EINVAL;
6044 }
Chris Mason593060d2008-03-25 16:50:33 -04006045
6046 /* stripe_offset is the offset of this block in its stripe*/
6047 stripe_offset = offset - stripe_offset;
6048
David Woodhouse53b381b2013-01-29 18:40:14 -05006049 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08006050 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006051 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
6052 raid56_full_stripe_start = offset;
6053
6054 /* allow a write of a full stripe, but make sure we don't
6055 * allow straddling of stripes
6056 */
David Sterba47c57132015-02-20 18:43:47 +01006057 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
6058 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05006059 raid56_full_stripe_start *= full_stripe_len;
6060 }
6061
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006062 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006063 u64 max_len;
6064 /* For writes to RAID[56], allow a full stripeset across all disks.
6065 For other RAID types and for RAID[56] reads, just allow a single
6066 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08006067 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006068 (op == BTRFS_MAP_WRITE)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006069 max_len = stripe_len * nr_data_stripes(map) -
6070 (offset - raid56_full_stripe_start);
6071 } else {
6072 /* we limit the length of each bio to what fits in a stripe */
6073 max_len = stripe_len - stripe_offset;
6074 }
6075 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04006076 } else {
6077 *length = em->len - offset;
6078 }
Chris Masonf2d8d742008-04-21 10:03:05 -04006079
Nikolay Borisovda12fe52018-11-27 20:57:58 +02006080 /*
6081 * This is for when we're called from btrfs_bio_fits_in_stripe and all
6082 * it cares about is the length
6083 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006084 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04006085 goto out;
6086
David Sterbacb5583d2018-09-07 16:11:23 +02006087 down_read(&dev_replace->rwsem);
Stefan Behrens472262f2012-11-06 14:43:46 +01006088 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
David Sterba53176dd2018-04-05 01:41:06 +02006089 /*
6090 * Hold the semaphore for read during the whole operation, write is
6091 * requested at commit time but must wait.
6092 */
Stefan Behrens472262f2012-11-06 14:43:46 +01006093 if (!dev_replace_is_ongoing)
David Sterbacb5583d2018-09-07 16:11:23 +02006094 up_read(&dev_replace->rwsem);
Stefan Behrens472262f2012-11-06 14:43:46 +01006095
Stefan Behrensad6d6202012-11-06 15:06:47 +01006096 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006097 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07006098 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
6099 dev_replace->srcdev->devid,
6100 &mirror_num,
6101 &physical_to_patch_in_first_stripe);
6102 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006103 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07006104 else
6105 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006106 } else if (mirror_num > map->num_stripes) {
6107 mirror_num = 0;
6108 }
6109
Chris Masonf2d8d742008-04-21 10:03:05 -04006110 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04006111 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00006112 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01006113 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6114 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006115 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08006116 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00006117 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Anand Jainde483732017-10-12 16:43:00 +08006118 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04006119 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04006120 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04006121 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04006122 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01006123 stripe_index = find_live_mirror(fs_info, map, 0,
Stefan Behrens30d98612012-11-06 14:52:18 +01006124 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006125 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006126 }
Chris Mason2fff7342008-04-29 14:12:09 -04006127
Chris Mason611f0e02008-04-03 16:29:03 -04006128 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
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;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006131 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04006132 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006133 } else {
6134 mirror_num = 1;
6135 }
Chris Mason2fff7342008-04-29 14:12:09 -04006136
Chris Mason321aecc2008-04-16 10:49:51 -04006137 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01006138 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04006139
David Sterba47c57132015-02-20 18:43:47 +01006140 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04006141 stripe_index *= map->sub_stripes;
6142
Anand Jainde483732017-10-12 16:43:00 +08006143 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04006144 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04006145 else if (mirror_num)
6146 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04006147 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04006148 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01006149 stripe_index = find_live_mirror(fs_info, map,
6150 stripe_index,
Stefan Behrens30d98612012-11-06 14:52:18 +01006151 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04006152 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006153 }
David Woodhouse53b381b2013-01-29 18:40:14 -05006154
Zhao Leiffe2d202015-01-20 15:11:44 +08006155 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08006156 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006157 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07006158 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbab8b93ad2015-01-16 17:26:13 +01006159 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05006160
6161 /* RAID[56] write or recovery. Return all stripes */
6162 num_stripes = map->num_stripes;
6163 max_errors = nr_parity_stripes(map);
6164
David Woodhouse53b381b2013-01-29 18:40:14 -05006165 *length = map->stripe_len;
6166 stripe_index = 0;
6167 stripe_offset = 0;
6168 } else {
6169 /*
6170 * Mirror #0 or #1 means the original data block.
6171 * Mirror #2 is RAID5 parity block.
6172 * Mirror #3 is RAID6 Q block.
6173 */
David Sterba47c57132015-02-20 18:43:47 +01006174 stripe_nr = div_u64_rem(stripe_nr,
6175 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05006176 if (mirror_num > 1)
6177 stripe_index = nr_data_stripes(map) +
6178 mirror_num - 2;
6179
6180 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01006181 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
6182 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006183 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08006184 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05006185 }
Chris Mason8790d502008-04-03 16:29:03 -04006186 } else {
6187 /*
David Sterba47c57132015-02-20 18:43:47 +01006188 * after this, stripe_nr is the number of stripes on this
6189 * device we have to walk to find the data, and stripe_index is
6190 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04006191 */
David Sterba47c57132015-02-20 18:43:47 +01006192 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6193 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006194 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04006195 }
Josef Bacike042d1e2016-04-12 12:54:40 -04006196 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006197 btrfs_crit(fs_info,
6198 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04006199 stripe_index, map->num_stripes);
6200 ret = -EINVAL;
6201 goto out;
6202 }
Chris Mason593060d2008-03-25 16:50:33 -04006203
Stefan Behrens472262f2012-11-06 14:43:46 +01006204 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07006205 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006206 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006207 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006208 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006209 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08006210 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006211 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08006212
Zhao Lei6e9606d2015-01-20 15:11:34 +08006213 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08006214 if (!bbio) {
6215 ret = -ENOMEM;
6216 goto out;
6217 }
Liu Bo6fad8232017-03-14 13:33:59 -07006218 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
Miao Xie2c8cdd62014-11-14 16:06:25 +08006219 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08006220
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006221 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07006222 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
6223 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006224 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01006225 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006226
6227 bbio->raid_map = (u64 *)((void *)bbio->stripes +
6228 sizeof(struct btrfs_bio_stripe) *
6229 num_alloc_stripes +
6230 sizeof(int) * tgtdev_indexes);
6231
6232 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01006233 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006234
6235 /* Fill in the logical address of each stripe */
6236 tmp = stripe_nr * nr_data_stripes(map);
6237 for (i = 0; i < nr_data_stripes(map); i++)
6238 bbio->raid_map[(i+rot) % num_stripes] =
6239 em->start + (tmp + i) * map->stripe_len;
6240
6241 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
6242 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
6243 bbio->raid_map[(i+rot+1) % num_stripes] =
6244 RAID6_Q_STRIPE;
6245 }
6246
Li Zefanec9ef7a2011-12-01 14:06:42 +08006247
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006248 for (i = 0; i < num_stripes; i++) {
6249 bbio->stripes[i].physical =
6250 map->stripes[stripe_index].physical +
6251 stripe_offset +
6252 stripe_nr * map->stripe_len;
6253 bbio->stripes[i].dev =
6254 map->stripes[stripe_index].dev;
6255 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04006256 }
Li Zefande11cc12011-12-01 12:55:47 +08006257
Liu Bo2b19a1f2017-03-14 13:34:00 -07006258 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08006259 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08006260
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006261 if (bbio->raid_map)
6262 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08006263
Liu Bo73c0f222017-03-14 13:33:58 -07006264 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006265 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07006266 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
6267 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01006268 }
6269
Li Zefande11cc12011-12-01 12:55:47 +08006270 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08006271 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08006272 bbio->num_stripes = num_stripes;
6273 bbio->max_errors = max_errors;
6274 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006275
6276 /*
6277 * this is the case that REQ_READ && dev_replace_is_ongoing &&
6278 * mirror_num == num_stripes + 1 && dev_replace target drive is
6279 * available as a mirror
6280 */
6281 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
6282 WARN_ON(num_stripes > 1);
6283 bbio->stripes[0].dev = dev_replace->tgtdev;
6284 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
6285 bbio->mirror_num = map->num_stripes + 1;
6286 }
Chris Masoncea9e442008-04-09 16:28:12 -04006287out:
Liu Bo73beece2015-07-17 16:49:19 +08006288 if (dev_replace_is_ongoing) {
David Sterba53176dd2018-04-05 01:41:06 +02006289 lockdep_assert_held(&dev_replace->rwsem);
6290 /* Unlock and let waiting writers proceed */
David Sterbacb5583d2018-09-07 16:11:23 +02006291 up_read(&dev_replace->rwsem);
Liu Bo73beece2015-07-17 16:49:19 +08006292 }
Chris Mason0b86a832008-03-24 15:01:56 -04006293 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08006294 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006295}
6296
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006297int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04006298 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02006299 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04006300{
Mike Christieb3d3fa52016-06-05 14:31:53 -05006301 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006302 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04006303}
6304
Miao Xieaf8e2d12014-10-23 14:42:50 +08006305/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006306int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08006307 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02006308 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08006309{
David Sterba825ad4c2017-03-28 14:45:22 +02006310 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08006311}
6312
Nikolay Borisov63a9c7b2018-05-04 10:53:05 +03006313int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
6314 u64 physical, u64 **logical, int *naddrs, int *stripe_len)
Yan Zhenga512bbf2008-12-08 16:46:26 -05006315{
Yan Zhenga512bbf2008-12-08 16:46:26 -05006316 struct extent_map *em;
6317 struct map_lookup *map;
6318 u64 *buf;
6319 u64 bytenr;
6320 u64 length;
6321 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006322 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006323 int i, j, nr = 0;
6324
Omar Sandoval60ca8422018-05-16 16:34:31 -07006325 em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07006326 if (IS_ERR(em))
Josef Bacik835d9742013-03-19 12:13:25 -04006327 return -EIO;
Josef Bacik835d9742013-03-19 12:13:25 -04006328
Jeff Mahoney95617d62015-06-03 10:55:48 -04006329 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006330 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05006331 rmap_len = map->stripe_len;
6332
Yan Zhenga512bbf2008-12-08 16:46:26 -05006333 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01006334 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006335 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01006336 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08006337 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01006338 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05006339 rmap_len = map->stripe_len * nr_data_stripes(map);
6340 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006341
David Sterba31e818f2015-02-20 18:00:26 +01006342 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006343 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05006344
6345 for (i = 0; i < map->num_stripes; i++) {
Yan Zhenga512bbf2008-12-08 16:46:26 -05006346 if (map->stripes[i].physical > physical ||
6347 map->stripes[i].physical + length <= physical)
6348 continue;
6349
6350 stripe_nr = physical - map->stripes[i].physical;
Liu Bo42c61ab2017-04-03 13:45:24 -07006351 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006352
6353 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
6354 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01006355 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006356 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
6357 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05006358 } /* else if RAID[56], multiply by nr_data_stripes().
6359 * Alternatively, just use rmap_len below instead of
6360 * map->stripe_len */
6361
6362 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05006363 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006364 for (j = 0; j < nr; j++) {
6365 if (buf[j] == bytenr)
6366 break;
6367 }
Chris Mason934d3752008-12-08 16:43:10 -05006368 if (j == nr) {
6369 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006370 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05006371 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006372 }
6373
Yan Zhenga512bbf2008-12-08 16:46:26 -05006374 *logical = buf;
6375 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006376 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006377
6378 free_extent_map(em);
6379 return 0;
6380}
6381
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006382static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08006383{
Mike Snitzer326e1db2015-05-22 09:14:03 -04006384 bio->bi_private = bbio->private;
6385 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006386 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04006387
Zhao Lei6e9606d2015-01-20 15:11:34 +08006388 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08006389}
6390
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006391static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04006392{
Chris Mason9be33952013-05-17 18:30:14 -04006393 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006394 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04006395
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006396 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02006397 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006398 if (bio->bi_status == BLK_STS_IOERR ||
6399 bio->bi_status == BLK_STS_TARGET) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02006400 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04006401 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08006402 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006403
6404 BUG_ON(stripe_index >= bbio->num_stripes);
6405 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02006406 if (dev->bdev) {
Mike Christie37226b22016-06-05 14:31:52 -05006407 if (bio_op(bio) == REQ_OP_WRITE)
Anand Jain1cb34c82017-10-21 01:45:33 +08006408 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006409 BTRFS_DEV_STAT_WRITE_ERRS);
David Sterba0cc068e2019-03-07 15:40:50 +01006410 else if (!(bio->bi_opf & REQ_RAHEAD))
Anand Jain1cb34c82017-10-21 01:45:33 +08006411 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006412 BTRFS_DEV_STAT_READ_ERRS);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06006413 if (bio->bi_opf & REQ_PREFLUSH)
Anand Jain1cb34c82017-10-21 01:45:33 +08006414 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006415 BTRFS_DEV_STAT_FLUSH_ERRS);
Stefan Behrens597a60f2012-06-14 16:42:31 +02006416 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02006417 }
6418 }
Chris Mason8790d502008-04-03 16:29:03 -04006419
Jan Schmidta1d3c472011-08-04 17:15:33 +02006420 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006421 is_orig_bio = 1;
6422
Miao Xiec404e0d2014-01-30 16:46:55 +08006423 btrfs_bio_counter_dec(bbio->fs_info);
6424
Jan Schmidta1d3c472011-08-04 17:15:33 +02006425 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006426 if (!is_orig_bio) {
6427 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006428 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006429 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006430
Chris Mason9be33952013-05-17 18:30:14 -04006431 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006432 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006433 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006434 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006435 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006436 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006437 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006438 /*
6439 * this bio is actually up to date, we didn't
6440 * go over the max number of errors
6441 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006442 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006443 }
Miao Xiec55f1392014-06-19 10:42:54 +08006444
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006445 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006446 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006447 bio_put(bio);
6448 }
Chris Mason8790d502008-04-03 16:29:03 -04006449}
6450
Chris Mason8b712842008-06-11 16:50:36 -04006451/*
6452 * see run_scheduled_bios for a description of why bios are collected for
6453 * async submit.
6454 *
6455 * This will add one bio to the pending list for a device and make sure
6456 * the work struct is scheduled.
6457 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006458static noinline void btrfs_schedule_bio(struct btrfs_device *device,
Mike Christie4e49ea42016-06-05 14:31:41 -05006459 struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04006460{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006461 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -04006462 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04006463 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006464
6465 /* don't bother with additional async steps for reads, right now */
Mike Christie37226b22016-06-05 14:31:52 -05006466 if (bio_op(bio) == REQ_OP_READ) {
Mike Christie4e49ea42016-06-05 14:31:41 -05006467 btrfsic_submit_bio(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01006468 return;
Chris Mason8b712842008-06-11 16:50:36 -04006469 }
6470
Chris Mason492bb6de2008-07-31 16:29:02 -04006471 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04006472 bio->bi_next = NULL;
Chris Mason8b712842008-06-11 16:50:36 -04006473
6474 spin_lock(&device->io_lock);
Christoph Hellwig67f055c2016-11-01 07:40:06 -06006475 if (op_is_sync(bio->bi_opf))
Chris Masonffbd5172009-04-20 15:50:09 -04006476 pending_bios = &device->pending_sync_bios;
6477 else
6478 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006479
Chris Masonffbd5172009-04-20 15:50:09 -04006480 if (pending_bios->tail)
6481 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006482
Chris Masonffbd5172009-04-20 15:50:09 -04006483 pending_bios->tail = bio;
6484 if (!pending_bios->head)
6485 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006486 if (device->running_pending)
6487 should_queue = 0;
6488
6489 spin_unlock(&device->io_lock);
6490
6491 if (should_queue)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006492 btrfs_queue_work(fs_info->submit_workers, &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04006493}
6494
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006495static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6496 u64 physical, int dev_nr, int async)
Josef Bacikde1ee922012-10-19 16:50:56 -04006497{
6498 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006499 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006500
6501 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006502 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006503 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006504 bio->bi_iter.bi_sector = physical >> 9;
Misono Tomohiro672d5992018-08-02 16:19:07 +09006505 btrfs_debug_in_rcu(fs_info,
6506 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6507 bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector,
6508 (u_long)dev->bdev->bd_dev, rcu_str_deref(dev->name), dev->devid,
6509 bio->bi_iter.bi_size);
Christoph Hellwig74d46992017-08-23 19:10:32 +02006510 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006511
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006512 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006513
Josef Bacikde1ee922012-10-19 16:50:56 -04006514 if (async)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006515 btrfs_schedule_bio(dev, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006516 else
Mike Christie4e49ea42016-06-05 14:31:41 -05006517 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006518}
6519
Josef Bacikde1ee922012-10-19 16:50:56 -04006520static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6521{
6522 atomic_inc(&bbio->error);
6523 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006524 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006525 WARN_ON(bio != bbio->orig_bio);
6526
Chris Mason9be33952013-05-17 18:30:14 -04006527 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006528 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006529 if (atomic_read(&bbio->error) > bbio->max_errors)
6530 bio->bi_status = BLK_STS_IOERR;
6531 else
6532 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006533 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006534 }
6535}
6536
Omar Sandoval58efbc92017-08-22 23:45:59 -07006537blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6538 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006539{
Chris Mason0b86a832008-03-24 15:01:56 -04006540 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006541 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006542 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006543 u64 length = 0;
6544 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006545 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006546 int dev_nr;
6547 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006548 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006549
Kent Overstreet4f024f32013-10-11 15:44:27 -07006550 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006551 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006552
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006553 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006554 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006555 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006556 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006557 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006558 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006559 }
Chris Masoncea9e442008-04-09 16:28:12 -04006560
Jan Schmidta1d3c472011-08-04 17:15:33 +02006561 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006562 bbio->orig_bio = first_bio;
6563 bbio->private = first_bio->bi_private;
6564 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006565 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006566 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6567
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006568 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006569 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006570 /* In this case, map_length has been set to the length of
6571 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006572 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006573 ret = raid56_parity_write(fs_info, bio, bbio,
6574 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006575 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006576 ret = raid56_parity_recover(fs_info, bio, bbio,
6577 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006578 }
Miao Xie42452152014-11-25 16:39:28 +08006579
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006580 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006581 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006582 }
6583
Chris Masoncea9e442008-04-09 16:28:12 -04006584 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006585 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006586 "mapping failed logical %llu bio len %llu len %llu",
6587 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006588 BUG();
6589 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006590
Zhao Lei08da7572015-02-12 15:42:16 +08006591 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006592 dev = bbio->stripes[dev_nr].dev;
Nikolay Borisovfc8a1682018-11-08 16:16:38 +02006593 if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING,
6594 &dev->dev_state) ||
Anand Jainebbede42017-12-04 12:54:52 +08006595 (bio_op(first_bio) == REQ_OP_WRITE &&
6596 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006597 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006598 continue;
6599 }
6600
David Sterba3aa8e072017-06-02 17:38:30 +02006601 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006602 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006603 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006604 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006605
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006606 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6607 dev_nr, async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006608 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006609 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006610 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006611}
6612
Anand Jain09ba3bc2019-01-19 14:48:55 +08006613/*
6614 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
6615 * return NULL.
6616 *
6617 * If devid and uuid are both specified, the match must be exact, otherwise
6618 * only devid is used.
6619 *
6620 * If @seed is true, traverse through the seed devices.
6621 */
Anand Jaine4319cd2019-01-17 23:32:31 +08006622struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006623 u64 devid, u8 *uuid, u8 *fsid,
6624 bool seed)
Chris Mason0b86a832008-03-24 15:01:56 -04006625{
Yan Zheng2b820322008-11-17 21:11:30 -05006626 struct btrfs_device *device;
Chris Mason0b86a832008-03-24 15:01:56 -04006627
Anand Jaine4319cd2019-01-17 23:32:31 +08006628 while (fs_devices) {
Yan Zheng2b820322008-11-17 21:11:30 -05006629 if (!fsid ||
Anand Jaine4319cd2019-01-17 23:32:31 +08006630 !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
Anand Jain09ba3bc2019-01-19 14:48:55 +08006631 list_for_each_entry(device, &fs_devices->devices,
6632 dev_list) {
6633 if (device->devid == devid &&
6634 (!uuid || memcmp(device->uuid, uuid,
6635 BTRFS_UUID_SIZE) == 0))
6636 return device;
6637 }
Yan Zheng2b820322008-11-17 21:11:30 -05006638 }
Anand Jain09ba3bc2019-01-19 14:48:55 +08006639 if (seed)
6640 fs_devices = fs_devices->seed;
6641 else
6642 return NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05006643 }
6644 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006645}
6646
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006647static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006648 u64 devid, u8 *dev_uuid)
6649{
6650 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006651
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006652 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6653 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006654 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006655
6656 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006657 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006658 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006659
Anand Jaine6e674b2017-12-04 12:54:54 +08006660 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05006661 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006662
Chris Masondfe25022008-05-13 13:46:40 -04006663 return device;
6664}
6665
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006666/**
6667 * btrfs_alloc_device - allocate struct btrfs_device
6668 * @fs_info: used only for generating a new devid, can be NULL if
6669 * devid is provided (i.e. @devid != NULL).
6670 * @devid: a pointer to devid for this device. If NULL a new devid
6671 * is generated.
6672 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6673 * is generated.
6674 *
6675 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006676 * on error. Returned struct is not linked onto any lists and must be
David Sterbaa425f9d2018-03-20 15:47:33 +01006677 * destroyed with btrfs_free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006678 */
6679struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6680 const u64 *devid,
6681 const u8 *uuid)
6682{
6683 struct btrfs_device *dev;
6684 u64 tmp;
6685
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306686 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006687 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006688
6689 dev = __alloc_device();
6690 if (IS_ERR(dev))
6691 return dev;
6692
6693 if (devid)
6694 tmp = *devid;
6695 else {
6696 int ret;
6697
6698 ret = find_next_devid(fs_info, &tmp);
6699 if (ret) {
David Sterbaa425f9d2018-03-20 15:47:33 +01006700 btrfs_free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006701 return ERR_PTR(ret);
6702 }
6703 }
6704 dev->devid = tmp;
6705
6706 if (uuid)
6707 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6708 else
6709 generate_random_uuid(dev->uuid);
6710
Liu Bo9e0af232014-08-15 23:36:53 +08006711 btrfs_init_work(&dev->work, btrfs_submit_helper,
6712 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006713
6714 return dev;
6715}
6716
Liu Boe06cd3d2016-06-03 12:05:15 -07006717/* Return -EIO if any error, otherwise return 0. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006718static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006719 struct extent_buffer *leaf,
6720 struct btrfs_chunk *chunk, u64 logical)
6721{
6722 u64 length;
6723 u64 stripe_len;
6724 u16 num_stripes;
6725 u16 sub_stripes;
6726 u64 type;
Gu Jinxiang315409b2018-07-04 18:16:39 +08006727 u64 features;
6728 bool mixed = false;
Liu Boe06cd3d2016-06-03 12:05:15 -07006729
6730 length = btrfs_chunk_length(leaf, chunk);
6731 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6732 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6733 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6734 type = btrfs_chunk_type(leaf, chunk);
6735
6736 if (!num_stripes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006737 btrfs_err(fs_info, "invalid chunk num_stripes: %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006738 num_stripes);
6739 return -EIO;
6740 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006741 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
6742 btrfs_err(fs_info, "invalid chunk logical %llu", logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006743 return -EIO;
6744 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006745 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
6746 btrfs_err(fs_info, "invalid chunk sectorsize %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006747 btrfs_chunk_sector_size(leaf, chunk));
6748 return -EIO;
6749 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006750 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
6751 btrfs_err(fs_info, "invalid chunk length %llu", length);
Liu Boe06cd3d2016-06-03 12:05:15 -07006752 return -EIO;
6753 }
6754 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006755 btrfs_err(fs_info, "invalid chunk stripe length: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006756 stripe_len);
6757 return -EIO;
6758 }
6759 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6760 type) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006761 btrfs_err(fs_info, "unrecognized chunk type: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006762 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6763 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6764 btrfs_chunk_type(leaf, chunk));
6765 return -EIO;
6766 }
Gu Jinxiang315409b2018-07-04 18:16:39 +08006767
6768 if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0) {
6769 btrfs_err(fs_info, "missing chunk type flag: 0x%llx", type);
6770 return -EIO;
6771 }
6772
6773 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) &&
6774 (type & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA))) {
6775 btrfs_err(fs_info,
6776 "system chunk with data or metadata type: 0x%llx", type);
6777 return -EIO;
6778 }
6779
6780 features = btrfs_super_incompat_flags(fs_info->super_copy);
6781 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
6782 mixed = true;
6783
6784 if (!mixed) {
6785 if ((type & BTRFS_BLOCK_GROUP_METADATA) &&
6786 (type & BTRFS_BLOCK_GROUP_DATA)) {
6787 btrfs_err(fs_info,
6788 "mixed chunk type in non-mixed mode: 0x%llx", type);
6789 return -EIO;
6790 }
6791 }
6792
Liu Boe06cd3d2016-06-03 12:05:15 -07006793 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
Johannes Thumshirn349ae632019-02-18 11:28:37 +01006794 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes != 2) ||
Liu Boe06cd3d2016-06-03 12:05:15 -07006795 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
6796 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
Johannes Thumshirn349ae632019-02-18 11:28:37 +01006797 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes != 2) ||
Liu Boe06cd3d2016-06-03 12:05:15 -07006798 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
6799 num_stripes != 1)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006800 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006801 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
6802 num_stripes, sub_stripes,
6803 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
6804 return -EIO;
6805 }
6806
6807 return 0;
6808}
6809
Anand Jain5a2b8e62017-10-09 11:07:45 +08006810static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006811 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006812{
Anand Jain2b902df2017-10-09 11:07:46 +08006813 if (error)
6814 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6815 devid, uuid);
6816 else
6817 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6818 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006819}
6820
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006821static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
Chris Mason0b86a832008-03-24 15:01:56 -04006822 struct extent_buffer *leaf,
6823 struct btrfs_chunk *chunk)
6824{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006825 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006826 struct map_lookup *map;
6827 struct extent_map *em;
6828 u64 logical;
6829 u64 length;
6830 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006831 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006832 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006833 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006834 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006835
Chris Masone17cade2008-04-15 15:41:47 -04006836 logical = key->offset;
6837 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006838 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006839
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006840 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006841 if (ret)
6842 return ret;
Chris Masona061fc82008-05-07 11:43:44 -04006843
Chris Mason890871b2009-09-02 16:24:52 -04006844 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006845 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006846 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006847
6848 /* already mapped? */
6849 if (em && em->start <= logical && em->start + em->len > logical) {
6850 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006851 return 0;
6852 } else if (em) {
6853 free_extent_map(em);
6854 }
Chris Mason0b86a832008-03-24 15:01:56 -04006855
David Sterba172ddd62011-04-21 00:48:27 +02006856 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006857 if (!em)
6858 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006859 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006860 if (!map) {
6861 free_extent_map(em);
6862 return -ENOMEM;
6863 }
6864
Wang Shilong298a8f92014-06-19 10:42:52 +08006865 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006866 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006867 em->start = logical;
6868 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006869 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006870 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006871 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006872
Chris Mason593060d2008-03-25 16:50:33 -04006873 map->num_stripes = num_stripes;
6874 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6875 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006876 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6877 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006878 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Qu Wenruocf90d882018-08-01 10:37:19 +08006879 map->verified_stripes = 0;
Chris Mason593060d2008-03-25 16:50:33 -04006880 for (i = 0; i < num_stripes; i++) {
6881 map->stripes[i].physical =
6882 btrfs_stripe_offset_nr(leaf, chunk, i);
6883 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006884 read_extent_buffer(leaf, uuid, (unsigned long)
6885 btrfs_stripe_dev_uuid_nr(chunk, i),
6886 BTRFS_UUID_SIZE);
Anand Jaine4319cd2019-01-17 23:32:31 +08006887 map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006888 devid, uuid, NULL, true);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006889 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006890 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006891 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006892 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006893 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006894 }
Chris Masondfe25022008-05-13 13:46:40 -04006895 if (!map->stripes[i].dev) {
6896 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006897 add_missing_dev(fs_info->fs_devices, devid,
6898 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006899 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006900 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006901 btrfs_err(fs_info,
6902 "failed to init missing dev %llu: %ld",
6903 devid, PTR_ERR(map->stripes[i].dev));
6904 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006905 }
Anand Jain2b902df2017-10-09 11:07:46 +08006906 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006907 }
Anand Jaine12c9622017-12-04 12:54:53 +08006908 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6909 &(map->stripes[i].dev->dev_state));
6910
Chris Mason0b86a832008-03-24 15:01:56 -04006911 }
6912
Chris Mason890871b2009-09-02 16:24:52 -04006913 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006914 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006915 write_unlock(&map_tree->map_tree.lock);
Qu Wenruo64f64f42018-08-01 10:37:20 +08006916 if (ret < 0) {
6917 btrfs_err(fs_info,
6918 "failed to add chunk map, start=%llu len=%llu: %d",
6919 em->start, em->len, ret);
6920 }
Chris Mason0b86a832008-03-24 15:01:56 -04006921 free_extent_map(em);
6922
Qu Wenruo64f64f42018-08-01 10:37:20 +08006923 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006924}
6925
Jeff Mahoney143bede2012-03-01 14:56:26 +01006926static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006927 struct btrfs_dev_item *dev_item,
6928 struct btrfs_device *device)
6929{
6930 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006931
6932 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006933 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6934 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006935 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006936 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006937 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006938 device->type = btrfs_device_type(leaf, dev_item);
6939 device->io_align = btrfs_device_io_align(leaf, dev_item);
6940 device->io_width = btrfs_device_io_width(leaf, dev_item);
6941 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006942 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Anand Jain401e29c2017-12-04 12:54:55 +08006943 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Chris Mason0b86a832008-03-24 15:01:56 -04006944
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006945 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006946 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006947}
6948
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006949static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006950 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006951{
6952 struct btrfs_fs_devices *fs_devices;
6953 int ret;
6954
David Sterbaa32bf9a2018-03-16 02:21:22 +01006955 lockdep_assert_held(&uuid_mutex);
David Sterba2dfeca92017-06-14 02:48:07 +02006956 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006957
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006958 fs_devices = fs_info->fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006959 while (fs_devices) {
Anand Jain44880fd2017-07-29 17:50:09 +08006960 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006961 return fs_devices;
6962
Yan Zheng2b820322008-11-17 21:11:30 -05006963 fs_devices = fs_devices->seed;
6964 }
6965
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006966 fs_devices = find_fsid(fsid, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05006967 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006968 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006969 return ERR_PTR(-ENOENT);
6970
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006971 fs_devices = alloc_fs_devices(fsid, NULL);
Miao Xie5f375832014-09-03 21:35:46 +08006972 if (IS_ERR(fs_devices))
6973 return fs_devices;
6974
6975 fs_devices->seeding = 1;
6976 fs_devices->opened = 1;
6977 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006978 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006979
6980 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006981 if (IS_ERR(fs_devices))
6982 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006983
Anand Jain897fb572018-04-12 10:29:28 +08006984 ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006985 if (ret) {
6986 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006987 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006988 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006989 }
Yan Zheng2b820322008-11-17 21:11:30 -05006990
6991 if (!fs_devices->seeding) {
Anand Jain0226e0e2018-04-12 10:29:27 +08006992 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006993 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006994 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006995 goto out;
6996 }
6997
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006998 fs_devices->seed = fs_info->fs_devices->seed;
6999 fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05007000out:
Miao Xie5f375832014-09-03 21:35:46 +08007001 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05007002}
7003
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007004static int read_one_dev(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04007005 struct extent_buffer *leaf,
7006 struct btrfs_dev_item *dev_item)
7007{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007008 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04007009 struct btrfs_device *device;
7010 u64 devid;
7011 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08007012 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04007013 u8 dev_uuid[BTRFS_UUID_SIZE];
7014
Chris Mason0b86a832008-03-24 15:01:56 -04007015 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02007016 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04007017 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02007018 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08007019 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05007020
Nikolay Borisovde37aa52018-10-30 16:43:24 +02007021 if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007022 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08007023 if (IS_ERR(fs_devices))
7024 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05007025 }
7026
Anand Jaine4319cd2019-01-17 23:32:31 +08007027 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08007028 fs_uuid, true);
Miao Xie5f375832014-09-03 21:35:46 +08007029 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08007030 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08007031 btrfs_report_missing_device(fs_info, devid,
7032 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08007033 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08007034 }
Yan Zheng2b820322008-11-17 21:11:30 -05007035
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007036 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08007037 if (IS_ERR(device)) {
7038 btrfs_err(fs_info,
7039 "failed to add missing dev %llu: %ld",
7040 devid, PTR_ERR(device));
7041 return PTR_ERR(device);
7042 }
Anand Jain2b902df2017-10-09 11:07:46 +08007043 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08007044 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08007045 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08007046 if (!btrfs_test_opt(fs_info, DEGRADED)) {
7047 btrfs_report_missing_device(fs_info,
7048 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08007049 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08007050 }
7051 btrfs_report_missing_device(fs_info, devid,
7052 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08007053 }
Miao Xie5f375832014-09-03 21:35:46 +08007054
Anand Jaine6e674b2017-12-04 12:54:54 +08007055 if (!device->bdev &&
7056 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05007057 /*
7058 * this happens when a device that was properly setup
7059 * in the device info lists suddenly goes bad.
7060 * device->bdev is NULL, and so we have to set
7061 * device->missing to one here
7062 */
Miao Xie5f375832014-09-03 21:35:46 +08007063 device->fs_devices->missing_devices++;
Anand Jaine6e674b2017-12-04 12:54:54 +08007064 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05007065 }
Miao Xie5f375832014-09-03 21:35:46 +08007066
7067 /* Move the device to its own fs_devices */
7068 if (device->fs_devices != fs_devices) {
Anand Jaine6e674b2017-12-04 12:54:54 +08007069 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
7070 &device->dev_state));
Miao Xie5f375832014-09-03 21:35:46 +08007071
7072 list_move(&device->dev_list, &fs_devices->devices);
7073 device->fs_devices->num_devices--;
7074 fs_devices->num_devices++;
7075
7076 device->fs_devices->missing_devices--;
7077 fs_devices->missing_devices++;
7078
7079 device->fs_devices = fs_devices;
7080 }
Yan Zheng2b820322008-11-17 21:11:30 -05007081 }
7082
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007083 if (device->fs_devices != fs_info->fs_devices) {
Anand Jainebbede42017-12-04 12:54:52 +08007084 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
Yan Zheng2b820322008-11-17 21:11:30 -05007085 if (device->generation !=
7086 btrfs_device_generation(leaf, dev_item))
7087 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04007088 }
Chris Mason0b86a832008-03-24 15:01:56 -04007089
7090 fill_device_from_item(leaf, dev_item, device);
Anand Jaine12c9622017-12-04 12:54:53 +08007091 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jainebbede42017-12-04 12:54:52 +08007092 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jain401e29c2017-12-04 12:54:55 +08007093 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05007094 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03007095 atomic64_add(device->total_bytes - device->bytes_used,
7096 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04007097 }
Chris Mason0b86a832008-03-24 15:01:56 -04007098 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007099 return ret;
7100}
7101
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007102int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04007103{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007104 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007105 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04007106 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04007107 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04007108 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01007109 u8 *array_ptr;
7110 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04007111 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007112 u32 num_stripes;
7113 u32 array_size;
7114 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01007115 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07007116 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04007117 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04007118
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007119 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02007120 /*
7121 * This will create extent buffer of nodesize, superblock size is
7122 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
7123 * overallocate but we can keep it as-is, only the first page is used.
7124 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007125 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07007126 if (IS_ERR(sb))
7127 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01007128 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04007129 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02007130 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04007131 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01007132 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02007133 * pages up-to-date when the page is larger: extent does not cover the
7134 * whole page and consequently check_page_uptodate does not find all
7135 * the page's extents up-to-date (the hole beyond sb),
7136 * write_extent_buffer then triggers a WARN_ON.
7137 *
7138 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
7139 * but sb spans only this function. Add an explicit SetPageUptodate call
7140 * to silence the warning eg. on PowerPC 64.
7141 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007142 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04007143 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05007144
Chris Masona061fc82008-05-07 11:43:44 -04007145 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04007146 array_size = btrfs_super_sys_array_size(super_copy);
7147
David Sterba1ffb22c2014-10-31 19:02:42 +01007148 array_ptr = super_copy->sys_chunk_array;
7149 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
7150 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007151
David Sterba1ffb22c2014-10-31 19:02:42 +01007152 while (cur_offset < array_size) {
7153 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01007154 len = sizeof(*disk_key);
7155 if (cur_offset + len > array_size)
7156 goto out_short_read;
7157
Chris Mason0b86a832008-03-24 15:01:56 -04007158 btrfs_disk_key_to_cpu(&key, disk_key);
7159
David Sterba1ffb22c2014-10-31 19:02:42 +01007160 array_ptr += len;
7161 sb_array_offset += len;
7162 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04007163
Chris Mason0d81ba52008-03-24 15:02:07 -04007164 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01007165 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01007166 /*
7167 * At least one btrfs_chunk with one stripe must be
7168 * present, exact stripe count check comes afterwards
7169 */
7170 len = btrfs_chunk_item_size(1);
7171 if (cur_offset + len > array_size)
7172 goto out_short_read;
7173
7174 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01007175 if (!num_stripes) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007176 btrfs_err(fs_info,
7177 "invalid number of stripes %u in sys_array at offset %u",
David Sterbaf5cdedd2015-11-30 17:27:06 +01007178 num_stripes, cur_offset);
7179 ret = -EIO;
7180 break;
7181 }
7182
Liu Boe06cd3d2016-06-03 12:05:15 -07007183 type = btrfs_chunk_type(sb, chunk);
7184 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007185 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07007186 "invalid chunk type %llu in sys_array at offset %u",
7187 type, cur_offset);
7188 ret = -EIO;
7189 break;
7190 }
7191
David Sterbae3540ea2014-11-05 15:24:51 +01007192 len = btrfs_chunk_item_size(num_stripes);
7193 if (cur_offset + len > array_size)
7194 goto out_short_read;
7195
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007196 ret = read_one_chunk(fs_info, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04007197 if (ret)
7198 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007199 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007200 btrfs_err(fs_info,
7201 "unexpected item type %u in sys_array at offset %u",
7202 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04007203 ret = -EIO;
7204 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007205 }
David Sterba1ffb22c2014-10-31 19:02:42 +01007206 array_ptr += len;
7207 sb_array_offset += len;
7208 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04007209 }
Liu Bod8651772016-06-03 17:41:42 -07007210 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07007211 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04007212 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01007213
7214out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007215 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01007216 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07007217 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07007218 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01007219 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04007220}
7221
Qu Wenruo21634a12017-03-09 09:34:36 +08007222/*
7223 * Check if all chunks in the fs are OK for read-write degraded mount
7224 *
Anand Jain6528b992017-12-18 17:08:59 +08007225 * If the @failing_dev is specified, it's accounted as missing.
7226 *
Qu Wenruo21634a12017-03-09 09:34:36 +08007227 * Return true if all chunks meet the minimal RW mount requirements.
7228 * Return false if any chunk doesn't meet the minimal RW mount requirements.
7229 */
Anand Jain6528b992017-12-18 17:08:59 +08007230bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
7231 struct btrfs_device *failing_dev)
Qu Wenruo21634a12017-03-09 09:34:36 +08007232{
7233 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
7234 struct extent_map *em;
7235 u64 next_start = 0;
7236 bool ret = true;
7237
7238 read_lock(&map_tree->map_tree.lock);
7239 em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1);
7240 read_unlock(&map_tree->map_tree.lock);
7241 /* No chunk at all? Return false anyway */
7242 if (!em) {
7243 ret = false;
7244 goto out;
7245 }
7246 while (em) {
7247 struct map_lookup *map;
7248 int missing = 0;
7249 int max_tolerated;
7250 int i;
7251
7252 map = em->map_lookup;
7253 max_tolerated =
7254 btrfs_get_num_tolerated_disk_barrier_failures(
7255 map->type);
7256 for (i = 0; i < map->num_stripes; i++) {
7257 struct btrfs_device *dev = map->stripes[i].dev;
7258
Anand Jaine6e674b2017-12-04 12:54:54 +08007259 if (!dev || !dev->bdev ||
7260 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
Qu Wenruo21634a12017-03-09 09:34:36 +08007261 dev->last_flush_error)
7262 missing++;
Anand Jain6528b992017-12-18 17:08:59 +08007263 else if (failing_dev && failing_dev == dev)
7264 missing++;
Qu Wenruo21634a12017-03-09 09:34:36 +08007265 }
7266 if (missing > max_tolerated) {
Anand Jain6528b992017-12-18 17:08:59 +08007267 if (!failing_dev)
7268 btrfs_warn(fs_info,
Andrea Gelmini52042d82018-11-28 12:05:13 +01007269 "chunk %llu missing %d devices, max tolerance is %d for writable mount",
Qu Wenruo21634a12017-03-09 09:34:36 +08007270 em->start, missing, max_tolerated);
7271 free_extent_map(em);
7272 ret = false;
7273 goto out;
7274 }
7275 next_start = extent_map_end(em);
7276 free_extent_map(em);
7277
7278 read_lock(&map_tree->map_tree.lock);
7279 em = lookup_extent_mapping(&map_tree->map_tree, next_start,
7280 (u64)(-1) - next_start);
7281 read_unlock(&map_tree->map_tree.lock);
7282 }
7283out:
7284 return ret;
7285}
7286
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007287int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04007288{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007289 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04007290 struct btrfs_path *path;
7291 struct extent_buffer *leaf;
7292 struct btrfs_key key;
7293 struct btrfs_key found_key;
7294 int ret;
7295 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007296 u64 total_dev = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007297
Chris Mason0b86a832008-03-24 15:01:56 -04007298 path = btrfs_alloc_path();
7299 if (!path)
7300 return -ENOMEM;
7301
Anand Jain3dd0f7a2018-04-12 10:29:32 +08007302 /*
7303 * uuid_mutex is needed only if we are mounting a sprout FS
7304 * otherwise we don't need it.
7305 */
Li Zefanb367e472011-12-07 11:38:24 +08007306 mutex_lock(&uuid_mutex);
David Sterba34441362016-10-04 19:34:27 +02007307 mutex_lock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007308
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007309 /*
7310 * Read all device items, and then all the chunk items. All
7311 * device items are found before any chunk item (their object id
7312 * is smaller than the lowest possible object id for a chunk
7313 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04007314 */
7315 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
7316 key.offset = 0;
7317 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007318 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00007319 if (ret < 0)
7320 goto error;
Chris Masond3977122009-01-05 21:25:51 -05007321 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007322 leaf = path->nodes[0];
7323 slot = path->slots[0];
7324 if (slot >= btrfs_header_nritems(leaf)) {
7325 ret = btrfs_next_leaf(root, path);
7326 if (ret == 0)
7327 continue;
7328 if (ret < 0)
7329 goto error;
7330 break;
7331 }
7332 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007333 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
7334 struct btrfs_dev_item *dev_item;
7335 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04007336 struct btrfs_dev_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007337 ret = read_one_dev(fs_info, leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007338 if (ret)
7339 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007340 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04007341 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
7342 struct btrfs_chunk *chunk;
7343 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007344 ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05007345 if (ret)
7346 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04007347 }
7348 path->slots[0]++;
7349 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07007350
7351 /*
7352 * After loading chunk tree, we've got all device information,
7353 * do another round of validation checks.
7354 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007355 if (total_dev != fs_info->fs_devices->total_devices) {
7356 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007357 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007358 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07007359 total_dev);
7360 ret = -EINVAL;
7361 goto error;
7362 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007363 if (btrfs_super_total_bytes(fs_info->super_copy) <
7364 fs_info->fs_devices->total_rw_bytes) {
7365 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007366 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007367 btrfs_super_total_bytes(fs_info->super_copy),
7368 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07007369 ret = -EINVAL;
7370 goto error;
7371 }
Chris Mason0b86a832008-03-24 15:01:56 -04007372 ret = 0;
7373error:
David Sterba34441362016-10-04 19:34:27 +02007374 mutex_unlock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007375 mutex_unlock(&uuid_mutex);
7376
Yan Zheng2b820322008-11-17 21:11:30 -05007377 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007378 return ret;
7379}
Stefan Behrens442a4f62012-05-25 16:06:08 +02007380
Miao Xiecb517ea2013-05-15 07:48:19 +00007381void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7382{
7383 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7384 struct btrfs_device *device;
7385
Liu Bo29cc83f2014-05-11 23:14:59 +08007386 while (fs_devices) {
7387 mutex_lock(&fs_devices->device_list_mutex);
7388 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007389 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007390 mutex_unlock(&fs_devices->device_list_mutex);
7391
7392 fs_devices = fs_devices->seed;
7393 }
Miao Xiecb517ea2013-05-15 07:48:19 +00007394}
7395
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007396static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
7397{
7398 int i;
7399
7400 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7401 btrfs_dev_stat_reset(dev, i);
7402}
7403
7404int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7405{
7406 struct btrfs_key key;
7407 struct btrfs_key found_key;
7408 struct btrfs_root *dev_root = fs_info->dev_root;
7409 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7410 struct extent_buffer *eb;
7411 int slot;
7412 int ret = 0;
7413 struct btrfs_device *device;
7414 struct btrfs_path *path = NULL;
7415 int i;
7416
7417 path = btrfs_alloc_path();
7418 if (!path) {
7419 ret = -ENOMEM;
7420 goto out;
7421 }
7422
7423 mutex_lock(&fs_devices->device_list_mutex);
7424 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7425 int item_size;
7426 struct btrfs_dev_stats_item *ptr;
7427
David Sterba242e2952016-01-25 17:51:31 +01007428 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7429 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007430 key.offset = device->devid;
7431 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7432 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007433 __btrfs_reset_dev_stats(device);
7434 device->dev_stats_valid = 1;
7435 btrfs_release_path(path);
7436 continue;
7437 }
7438 slot = path->slots[0];
7439 eb = path->nodes[0];
7440 btrfs_item_key_to_cpu(eb, &found_key, slot);
7441 item_size = btrfs_item_size_nr(eb, slot);
7442
7443 ptr = btrfs_item_ptr(eb, slot,
7444 struct btrfs_dev_stats_item);
7445
7446 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7447 if (item_size >= (1 + i) * sizeof(__le64))
7448 btrfs_dev_stat_set(device, i,
7449 btrfs_dev_stats_value(eb, ptr, i));
7450 else
7451 btrfs_dev_stat_reset(device, i);
7452 }
7453
7454 device->dev_stats_valid = 1;
7455 btrfs_dev_stat_print_on_load(device);
7456 btrfs_release_path(path);
7457 }
7458 mutex_unlock(&fs_devices->device_list_mutex);
7459
7460out:
7461 btrfs_free_path(path);
7462 return ret < 0 ? ret : 0;
7463}
7464
7465static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007466 struct btrfs_device *device)
7467{
Nikolay Borisov5495f192018-07-20 19:37:49 +03007468 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007469 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007470 struct btrfs_path *path;
7471 struct btrfs_key key;
7472 struct extent_buffer *eb;
7473 struct btrfs_dev_stats_item *ptr;
7474 int ret;
7475 int i;
7476
David Sterba242e2952016-01-25 17:51:31 +01007477 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7478 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007479 key.offset = device->devid;
7480
7481 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007482 if (!path)
7483 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007484 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7485 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007486 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007487 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007488 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007489 goto out;
7490 }
7491
7492 if (ret == 0 &&
7493 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7494 /* need to delete old one and insert a new one */
7495 ret = btrfs_del_item(trans, dev_root, path);
7496 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007497 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007498 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007499 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007500 goto out;
7501 }
7502 ret = 1;
7503 }
7504
7505 if (ret == 1) {
7506 /* need to insert a new item */
7507 btrfs_release_path(path);
7508 ret = btrfs_insert_empty_item(trans, dev_root, path,
7509 &key, sizeof(*ptr));
7510 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007511 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007512 "insert dev_stats item for device %s failed %d",
7513 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007514 goto out;
7515 }
7516 }
7517
7518 eb = path->nodes[0];
7519 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7520 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7521 btrfs_set_dev_stats_value(eb, ptr, i,
7522 btrfs_dev_stat_read(device, i));
7523 btrfs_mark_buffer_dirty(eb);
7524
7525out:
7526 btrfs_free_path(path);
7527 return ret;
7528}
7529
7530/*
7531 * called from commit_transaction. Writes all changed device stats to disk.
7532 */
7533int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
7534 struct btrfs_fs_info *fs_info)
7535{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007536 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7537 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007538 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007539 int ret = 0;
7540
7541 mutex_lock(&fs_devices->device_list_mutex);
7542 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007543 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7544 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007545 continue;
7546
Nikolay Borisov9deae962017-10-24 13:47:37 +03007547
7548 /*
7549 * There is a LOAD-LOAD control dependency between the value of
7550 * dev_stats_ccnt and updating the on-disk values which requires
7551 * reading the in-memory counters. Such control dependencies
7552 * require explicit read memory barriers.
7553 *
7554 * This memory barriers pairs with smp_mb__before_atomic in
7555 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7556 * barrier implied by atomic_xchg in
7557 * btrfs_dev_stats_read_and_reset
7558 */
7559 smp_rmb();
7560
Nikolay Borisov5495f192018-07-20 19:37:49 +03007561 ret = update_dev_stat_item(trans, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007562 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007563 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007564 }
7565 mutex_unlock(&fs_devices->device_list_mutex);
7566
7567 return ret;
7568}
7569
Stefan Behrens442a4f62012-05-25 16:06:08 +02007570void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7571{
7572 btrfs_dev_stat_inc(dev, index);
7573 btrfs_dev_stat_print_on_error(dev);
7574}
7575
Eric Sandeen48a3b632013-04-25 20:41:01 +00007576static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007577{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007578 if (!dev->dev_stats_valid)
7579 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007580 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007581 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007582 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007583 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7584 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7585 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007586 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7587 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007588}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007589
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007590static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7591{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007592 int i;
7593
7594 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7595 if (btrfs_dev_stat_read(dev, i) != 0)
7596 break;
7597 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7598 return; /* all values == 0, suppress message */
7599
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007600 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007601 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007602 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007603 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7604 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7605 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7606 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7607 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7608}
7609
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007610int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007611 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007612{
7613 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007614 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007615 int i;
7616
7617 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain09ba3bc2019-01-19 14:48:55 +08007618 dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL,
7619 true);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007620 mutex_unlock(&fs_devices->device_list_mutex);
7621
7622 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007623 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007624 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007625 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007626 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007627 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007628 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007629 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7630 if (stats->nr_items > i)
7631 stats->values[i] =
7632 btrfs_dev_stat_read_and_reset(dev, i);
7633 else
7634 btrfs_dev_stat_reset(dev, i);
7635 }
7636 } else {
7637 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7638 if (stats->nr_items > i)
7639 stats->values[i] = btrfs_dev_stat_read(dev, i);
7640 }
7641 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7642 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7643 return 0;
7644}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007645
David Sterbada353f62017-02-14 17:55:53 +01007646void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007647{
7648 struct buffer_head *bh;
7649 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08007650 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007651
Anand Jain12b1c262015-08-14 18:32:59 +08007652 if (!bdev)
7653 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007654
Anand Jain12b1c262015-08-14 18:32:59 +08007655 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7656 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007657
Anand Jain12b1c262015-08-14 18:32:59 +08007658 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7659 continue;
7660
7661 disk_super = (struct btrfs_super_block *)bh->b_data;
7662
7663 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7664 set_buffer_dirty(bh);
7665 sync_dirty_buffer(bh);
7666 brelse(bh);
7667 }
7668
7669 /* Notify udev that device has changed */
7670 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7671
7672 /* Update ctime/mtime for device path for libblkid */
7673 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007674}
Miao Xie935e5cc2014-09-03 21:35:33 +08007675
7676/*
7677 * Update the size of all devices, which is used for writing out the
7678 * super blocks.
7679 */
7680void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
7681{
7682 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7683 struct btrfs_device *curr, *next;
7684
7685 if (list_empty(&fs_devices->resized_devices))
7686 return;
7687
7688 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02007689 mutex_lock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007690 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
7691 resized_list) {
7692 list_del_init(&curr->resized_list);
7693 curr->commit_total_bytes = curr->disk_total_bytes;
7694 }
David Sterba34441362016-10-04 19:34:27 +02007695 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007696 mutex_unlock(&fs_devices->device_list_mutex);
7697}
Miao Xiece7213c2014-09-03 21:35:34 +08007698
7699/* Must be invoked during the transaction commit */
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007700void btrfs_update_commit_device_bytes_used(struct btrfs_transaction *trans)
Miao Xiece7213c2014-09-03 21:35:34 +08007701{
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007702 struct btrfs_fs_info *fs_info = trans->fs_info;
Miao Xiece7213c2014-09-03 21:35:34 +08007703 struct extent_map *em;
7704 struct map_lookup *map;
7705 struct btrfs_device *dev;
7706 int i;
7707
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007708 if (list_empty(&trans->pending_chunks))
Miao Xiece7213c2014-09-03 21:35:34 +08007709 return;
7710
7711 /* In order to kick the device replace finish process */
David Sterba34441362016-10-04 19:34:27 +02007712 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007713 list_for_each_entry(em, &trans->pending_chunks, list) {
Jeff Mahoney95617d62015-06-03 10:55:48 -04007714 map = em->map_lookup;
Miao Xiece7213c2014-09-03 21:35:34 +08007715
7716 for (i = 0; i < map->num_stripes; i++) {
7717 dev = map->stripes[i].dev;
7718 dev->commit_bytes_used = dev->bytes_used;
7719 }
7720 }
David Sterba34441362016-10-04 19:34:27 +02007721 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007722}
Anand Jain5a13f432015-03-10 06:38:31 +08007723
7724void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7725{
7726 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7727 while (fs_devices) {
7728 fs_devices->fs_info = fs_info;
7729 fs_devices = fs_devices->seed;
7730 }
7731}
7732
7733void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7734{
7735 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7736 while (fs_devices) {
7737 fs_devices->fs_info = NULL;
7738 fs_devices = fs_devices->seed;
7739 }
7740}
David Sterba46df06b2018-07-13 20:46:30 +02007741
7742/*
7743 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7744 */
7745int btrfs_bg_type_to_factor(u64 flags)
7746{
7747 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
7748 BTRFS_BLOCK_GROUP_RAID10))
7749 return 2;
7750 return 1;
7751}
Qu Wenruocf90d882018-08-01 10:37:19 +08007752
7753
7754static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes)
7755{
7756 int index = btrfs_bg_flags_to_raid_index(type);
7757 int ncopies = btrfs_raid_array[index].ncopies;
7758 int data_stripes;
7759
7760 switch (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
7761 case BTRFS_BLOCK_GROUP_RAID5:
7762 data_stripes = num_stripes - 1;
7763 break;
7764 case BTRFS_BLOCK_GROUP_RAID6:
7765 data_stripes = num_stripes - 2;
7766 break;
7767 default:
7768 data_stripes = num_stripes / ncopies;
7769 break;
7770 }
7771 return div_u64(chunk_len, data_stripes);
7772}
7773
7774static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
7775 u64 chunk_offset, u64 devid,
7776 u64 physical_offset, u64 physical_len)
7777{
7778 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
7779 struct extent_map *em;
7780 struct map_lookup *map;
Qu Wenruo05a37c42018-10-05 17:45:55 +08007781 struct btrfs_device *dev;
Qu Wenruocf90d882018-08-01 10:37:19 +08007782 u64 stripe_len;
7783 bool found = false;
7784 int ret = 0;
7785 int i;
7786
7787 read_lock(&em_tree->lock);
7788 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
7789 read_unlock(&em_tree->lock);
7790
7791 if (!em) {
7792 btrfs_err(fs_info,
7793"dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7794 physical_offset, devid);
7795 ret = -EUCLEAN;
7796 goto out;
7797 }
7798
7799 map = em->map_lookup;
7800 stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes);
7801 if (physical_len != stripe_len) {
7802 btrfs_err(fs_info,
7803"dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7804 physical_offset, devid, em->start, physical_len,
7805 stripe_len);
7806 ret = -EUCLEAN;
7807 goto out;
7808 }
7809
7810 for (i = 0; i < map->num_stripes; i++) {
7811 if (map->stripes[i].dev->devid == devid &&
7812 map->stripes[i].physical == physical_offset) {
7813 found = true;
7814 if (map->verified_stripes >= map->num_stripes) {
7815 btrfs_err(fs_info,
7816 "too many dev extents for chunk %llu found",
7817 em->start);
7818 ret = -EUCLEAN;
7819 goto out;
7820 }
7821 map->verified_stripes++;
7822 break;
7823 }
7824 }
7825 if (!found) {
7826 btrfs_err(fs_info,
7827 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7828 physical_offset, devid);
7829 ret = -EUCLEAN;
7830 }
Qu Wenruo05a37c42018-10-05 17:45:55 +08007831
7832 /* Make sure no dev extent is beyond device bondary */
Anand Jain09ba3bc2019-01-19 14:48:55 +08007833 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
Qu Wenruo05a37c42018-10-05 17:45:55 +08007834 if (!dev) {
7835 btrfs_err(fs_info, "failed to find devid %llu", devid);
7836 ret = -EUCLEAN;
7837 goto out;
7838 }
Qu Wenruo1b3922a2019-01-08 14:08:18 +08007839
7840 /* It's possible this device is a dummy for seed device */
7841 if (dev->disk_total_bytes == 0) {
Anand Jain09ba3bc2019-01-19 14:48:55 +08007842 dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL,
7843 NULL, false);
Qu Wenruo1b3922a2019-01-08 14:08:18 +08007844 if (!dev) {
7845 btrfs_err(fs_info, "failed to find seed devid %llu",
7846 devid);
7847 ret = -EUCLEAN;
7848 goto out;
7849 }
7850 }
7851
Qu Wenruo05a37c42018-10-05 17:45:55 +08007852 if (physical_offset + physical_len > dev->disk_total_bytes) {
7853 btrfs_err(fs_info,
7854"dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7855 devid, physical_offset, physical_len,
7856 dev->disk_total_bytes);
7857 ret = -EUCLEAN;
7858 goto out;
7859 }
Qu Wenruocf90d882018-08-01 10:37:19 +08007860out:
7861 free_extent_map(em);
7862 return ret;
7863}
7864
7865static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)
7866{
7867 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
7868 struct extent_map *em;
7869 struct rb_node *node;
7870 int ret = 0;
7871
7872 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08007873 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
Qu Wenruocf90d882018-08-01 10:37:19 +08007874 em = rb_entry(node, struct extent_map, rb_node);
7875 if (em->map_lookup->num_stripes !=
7876 em->map_lookup->verified_stripes) {
7877 btrfs_err(fs_info,
7878 "chunk %llu has missing dev extent, have %d expect %d",
7879 em->start, em->map_lookup->verified_stripes,
7880 em->map_lookup->num_stripes);
7881 ret = -EUCLEAN;
7882 goto out;
7883 }
7884 }
7885out:
7886 read_unlock(&em_tree->lock);
7887 return ret;
7888}
7889
7890/*
7891 * Ensure that all dev extents are mapped to correct chunk, otherwise
7892 * later chunk allocation/free would cause unexpected behavior.
7893 *
7894 * NOTE: This will iterate through the whole device tree, which should be of
7895 * the same size level as the chunk tree. This slightly increases mount time.
7896 */
7897int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info)
7898{
7899 struct btrfs_path *path;
7900 struct btrfs_root *root = fs_info->dev_root;
7901 struct btrfs_key key;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007902 u64 prev_devid = 0;
7903 u64 prev_dev_ext_end = 0;
Qu Wenruocf90d882018-08-01 10:37:19 +08007904 int ret = 0;
7905
7906 key.objectid = 1;
7907 key.type = BTRFS_DEV_EXTENT_KEY;
7908 key.offset = 0;
7909
7910 path = btrfs_alloc_path();
7911 if (!path)
7912 return -ENOMEM;
7913
7914 path->reada = READA_FORWARD;
7915 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7916 if (ret < 0)
7917 goto out;
7918
7919 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7920 ret = btrfs_next_item(root, path);
7921 if (ret < 0)
7922 goto out;
7923 /* No dev extents at all? Not good */
7924 if (ret > 0) {
7925 ret = -EUCLEAN;
7926 goto out;
7927 }
7928 }
7929 while (1) {
7930 struct extent_buffer *leaf = path->nodes[0];
7931 struct btrfs_dev_extent *dext;
7932 int slot = path->slots[0];
7933 u64 chunk_offset;
7934 u64 physical_offset;
7935 u64 physical_len;
7936 u64 devid;
7937
7938 btrfs_item_key_to_cpu(leaf, &key, slot);
7939 if (key.type != BTRFS_DEV_EXTENT_KEY)
7940 break;
7941 devid = key.objectid;
7942 physical_offset = key.offset;
7943
7944 dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
7945 chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext);
7946 physical_len = btrfs_dev_extent_length(leaf, dext);
7947
Qu Wenruo5eb19382018-10-05 17:45:54 +08007948 /* Check if this dev extent overlaps with the previous one */
7949 if (devid == prev_devid && physical_offset < prev_dev_ext_end) {
7950 btrfs_err(fs_info,
7951"dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
7952 devid, physical_offset, prev_dev_ext_end);
7953 ret = -EUCLEAN;
7954 goto out;
7955 }
7956
Qu Wenruocf90d882018-08-01 10:37:19 +08007957 ret = verify_one_dev_extent(fs_info, chunk_offset, devid,
7958 physical_offset, physical_len);
7959 if (ret < 0)
7960 goto out;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007961 prev_devid = devid;
7962 prev_dev_ext_end = physical_offset + physical_len;
7963
Qu Wenruocf90d882018-08-01 10:37:19 +08007964 ret = btrfs_next_item(root, path);
7965 if (ret < 0)
7966 goto out;
7967 if (ret > 0) {
7968 ret = 0;
7969 break;
7970 }
7971 }
7972
7973 /* Ensure all chunks have corresponding dev extents */
7974 ret = verify_chunk_dev_extent_mapping(fs_info);
7975out:
7976 btrfs_free_path(path);
7977 return ret;
7978}
Omar Sandovaleede2bf2016-11-03 10:28:12 -07007979
7980/*
7981 * Check whether the given block group or device is pinned by any inode being
7982 * used as a swapfile.
7983 */
7984bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr)
7985{
7986 struct btrfs_swapfile_pin *sp;
7987 struct rb_node *node;
7988
7989 spin_lock(&fs_info->swapfile_pins_lock);
7990 node = fs_info->swapfile_pins.rb_node;
7991 while (node) {
7992 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
7993 if (ptr < sp->ptr)
7994 node = node->rb_left;
7995 else if (ptr > sp->ptr)
7996 node = node->rb_right;
7997 else
7998 break;
7999 }
8000 spin_unlock(&fs_info->swapfile_pins_lock);
8001 return node != NULL;
8002}